-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.ts
More file actions
94 lines (81 loc) · 2.82 KB
/
setup.ts
File metadata and controls
94 lines (81 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { jsonc } from 'jsonc'
import path from 'node:path'
import { copyFileIfChanged, writeFileIfChanged } from './files.js'
import logger from './logger.js'
import { getCollectionNodes } from './nodes.js'
import { DeepObject, deepMerge } from './objects.js'
import { LiquidNode } from './types.js'
export async function copySetupComponentFiles(
collectionDir: string,
destination: string,
componentSelector: string
): Promise<void> {
const collectionNodes = await getCollectionNodes(collectionDir)
const setupFiles = collectionNodes
.filter(node => componentSelector === '*' || componentSelector.includes(path.basename(node.file, '.liquid')))
.flatMap(node => node.setup)
const settingsSchema: object[] = []
const settingsData: DeepObject = {}
// Process all files in parallel
await Promise.all(setupFiles.map(async (setupFile) => {
const node = collectionNodes.find(n => n.file === setupFile)
if (!node) return
if (node.name === 'settings_schema.json') {
const schemaItems = await processSettingsSchema(setupFile, node)
settingsSchema.push(...schemaItems)
} else if (node.name === 'settings_data.json') {
const dataItems = await processSettingsData(setupFile, node)
deepMerge(settingsData, dataItems)
} else {
copyFileIfChanged(node.file, path.join(destination, node.themeFolder, node.name))
}
}))
// Write combined settings schema
writeFileIfChanged(
JSON.stringify(settingsSchema),
path.join(destination, 'config', 'settings_schema.json')
)
// Write combined settings data
writeFileIfChanged(
JSON.stringify(settingsData),
path.join(destination, 'config', 'settings_data.json')
)
}
export async function processSettingsSchema(
setupFile: string,
node: LiquidNode
): Promise<object[]> {
if (node?.name !== 'settings_schema.json') {
return []
}
try {
const schema = jsonc.parse(node.body, { stripComments: true })
if (!Array.isArray(schema)) {
logger.warn(`Invalid schema format in ${setupFile}: Expected an array`)
return []
}
return schema
} catch (error) {
logger.warn(`Failed to parse settings schema from ${setupFile}: ${error instanceof Error ? error.message : 'Unknown error'}`)
return []
}
}
export async function processSettingsData(
setupFile: string,
node: LiquidNode
): Promise<DeepObject> {
if (node?.name !== 'settings_data.json') {
return {}
}
try {
const data = jsonc.parse(node.body, { stripComments: true })
if (typeof data !== 'object' || data === null) {
logger.warn(`Invalid settings data format in ${setupFile}: Expected an object`)
return {}
}
return data as DeepObject
} catch (error) {
logger.warn(`Failed to parse settings data from ${setupFile}: ${error instanceof Error ? error.message : 'Unknown error'}`)
return {}
}
}