|
3 | 3 | */ |
4 | 4 | import { readFileSync, writeFileSync } from 'node:fs'; |
5 | 5 | import fastGlob from 'fast-glob'; |
6 | | -import _ from 'lodash'; |
7 | | -import pc from 'picocolors'; |
8 | 6 | import deepMerge from 'deepmerge'; |
| 7 | +import { pathToFileURL } from 'node:url'; |
9 | 8 |
|
10 | 9 | /** |
11 | 10 | * Internal dependencies |
12 | 11 | */ |
13 | | -import { getConfig, getPlugins, importFresh } from '../utils/index.mjs'; |
| 12 | +import { getConfig, getPlugins, Config } from '../utils/config.mjs'; |
| 13 | +import { logInfo, logError, logSuccess } from '../utils/logger.mjs'; |
| 14 | +import { |
| 15 | + camelCase, |
| 16 | + setNestedValue, |
| 17 | + isEmpty, |
| 18 | +} from '../utils/object-helpers.mjs'; |
14 | 19 |
|
15 | 20 | async function build() { |
16 | | - const configs = await getConfig(); |
| 21 | + try { |
| 22 | + const configs: Array<Config> = await getConfig(); |
17 | 23 |
|
18 | | - for (const config of configs) { |
19 | | - const { src, version } = config; |
| 24 | + // Assign default names if not provided |
| 25 | + configs.forEach((config, index) => { |
| 26 | + config.name = config.name || `config-${index}`; |
| 27 | + }); |
| 28 | + |
| 29 | + for (const config of configs) { |
| 30 | + logInfo(`Building theme.json for config: ${config.name}`); |
| 31 | + const themeJson = await generateThemeJson({ |
| 32 | + ...config, |
| 33 | + }); |
| 34 | + writeFileSync( |
| 35 | + config.dest, |
| 36 | + JSON.stringify(themeJson, null, config.pretty ? '\t' : ''), |
| 37 | + ); |
| 38 | + logSuccess(`theme.json created at ${config.dest}`); |
| 39 | + } |
| 40 | + } catch (error) { |
| 41 | + logError( |
| 42 | + 'Error during build process', |
| 43 | + error instanceof Error ? error : undefined, |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function generateThemeJson(config: Config) { |
| 49 | + try { |
| 50 | + const { src, version, addSchema, wpVersion } = config; |
20 | 51 | const plugins = await getPlugins(config); |
21 | 52 |
|
22 | | - let initialThemeJson: any = { |
23 | | - version, |
24 | | - }; |
| 53 | + let themeJson: { version: number; $schema?: string } = { version }; |
25 | 54 |
|
26 | | - if (config.addSchema) { |
| 55 | + if (addSchema) { |
27 | 56 | const schemaVersion = |
28 | | - config.wpVersion === 'trunk' |
29 | | - ? 'trunk' |
30 | | - : `wp/${config.wpVersion}`; |
31 | | - initialThemeJson.$schema = `https://schemas.wp.org/${schemaVersion}/theme.json`; |
| 57 | + wpVersion === 'trunk' ? 'trunk' : `wp/${wpVersion}`; |
| 58 | + themeJson.$schema = `https://schemas.wp.org/${schemaVersion}/theme.json`; |
32 | 59 | } |
33 | 60 |
|
34 | | - if (!!plugins.before.length) { |
| 61 | + if (plugins.before.length) { |
35 | 62 | for (const plugin of plugins.before) { |
36 | | - initialThemeJson = await plugin(initialThemeJson, config); |
| 63 | + themeJson = await plugin(themeJson, config); |
37 | 64 | } |
38 | 65 | } |
39 | 66 |
|
40 | | - const files = fastGlob.sync(src + '**/*', { |
41 | | - ignore: [src + '**/*.temp-fresh-import.{mjs,cjs}'], |
| 67 | + const files = fastGlob.sync(`${src}**/*`, { |
| 68 | + ignore: [`${src}**/*.temp-fresh-import.{mjs,cjs}`], |
42 | 69 | }); |
43 | 70 |
|
44 | | - let themeJson = await files.reduce(async (previousValue, file) => { |
45 | | - let nextValue = await previousValue; |
46 | | - |
47 | | - try { |
48 | | - let fileConfig; |
49 | | - |
50 | | - if (file.endsWith('.cjs') || file.endsWith('.mjs')) { |
51 | | - const importedFile = await importFresh(file); |
52 | | - |
53 | | - fileConfig = importedFile.default; |
54 | | - |
55 | | - if (typeof fileConfig === 'function') { |
56 | | - fileConfig = fileConfig(); |
57 | | - } |
58 | | - } else if (file.endsWith('.json')) { |
59 | | - const content = readFileSync(file, { |
60 | | - encoding: 'utf-8', |
61 | | - }); |
62 | | - |
63 | | - fileConfig = JSON.parse(content); |
64 | | - } else { |
65 | | - console.log( |
66 | | - pc.red( |
67 | | - `${file.replace( |
68 | | - src, |
69 | | - '', |
70 | | - )}: File not supported. Supported extensions are: json, cjs and mjs.\n`, |
71 | | - ), |
72 | | - ); |
73 | | - process.exit(1); |
74 | | - } |
75 | | - |
76 | | - if (!_.isEmpty(fileConfig)) { |
77 | | - const destination = file |
78 | | - .replace(src, '') |
79 | | - .replace(/\.[^/.]+$/, ''); |
80 | | - |
81 | | - const splittedDestination = destination.split('/blocks/'); |
82 | | - |
83 | | - if (splittedDestination[0]) { |
84 | | - let dest = splittedDestination[0].split('/'); |
85 | | - dest = dest.map(_.camelCase); |
86 | | - |
87 | | - if (splittedDestination[1]) { |
88 | | - const [blockNamespace, blockName, ...blockDest] = |
89 | | - splittedDestination[1].split('/'); |
90 | | - dest = [ |
91 | | - ...dest, |
92 | | - 'blocks', |
93 | | - `${blockNamespace}/${blockName}`, |
94 | | - ...blockDest, |
95 | | - ]; |
96 | | - } |
97 | | - |
98 | | - const newNextValue = _.set({}, dest, fileConfig); |
99 | | - nextValue = deepMerge(nextValue, newNextValue); |
100 | | - } |
101 | | - } |
102 | | - } catch (err) { |
103 | | - console.log(file, err); |
| 71 | + for (const file of files) { |
| 72 | + logInfo(`Processing file: ${file}`); |
| 73 | + const fileConfig = await processFile(file, src); |
| 74 | + if (fileConfig) { |
| 75 | + themeJson = { |
| 76 | + ...deepMerge(themeJson, fileConfig), |
| 77 | + version: themeJson.version, |
| 78 | + }; |
104 | 79 | } |
| 80 | + } |
105 | 81 |
|
106 | | - return nextValue; |
107 | | - }, Promise.resolve(initialThemeJson)); |
108 | | - |
109 | | - if (!!plugins.after.length) { |
| 82 | + if (plugins.after.length) { |
110 | 83 | for (const plugin of plugins.after) { |
111 | 84 | themeJson = await plugin(themeJson, config); |
112 | 85 | } |
113 | 86 | } |
114 | 87 |
|
115 | | - writeFileSync( |
116 | | - config.dest, |
117 | | - JSON.stringify(themeJson, null, config.pretty ? '\t' : ''), |
| 88 | + return themeJson; |
| 89 | + } catch (error) { |
| 90 | + logError( |
| 91 | + 'Error generating theme.json', |
| 92 | + error instanceof Error ? error : undefined, |
118 | 93 | ); |
| 94 | + |
| 95 | + throw error; |
119 | 96 | } |
| 97 | +} |
120 | 98 |
|
121 | | - console.log(pc.green('🎉 theme.json created')); |
| 99 | +interface FileConfig { |
| 100 | + [key: string]: any; |
| 101 | +} |
| 102 | + |
| 103 | +async function processFile( |
| 104 | + file: string, |
| 105 | + src: string, |
| 106 | +): Promise<FileConfig | undefined> { |
| 107 | + try { |
| 108 | + let fileConfig: FileConfig | undefined; |
| 109 | + |
| 110 | + if (file.endsWith('.cjs') || file.endsWith('.mjs')) { |
| 111 | + // Convert the file path to a file:// URL |
| 112 | + const fileUrl = pathToFileURL(file); |
| 113 | + const importedFile = await import(fileUrl.href); // Use the file:// URL for dynamic import |
| 114 | + fileConfig = importedFile.default; |
| 115 | + |
| 116 | + if (typeof fileConfig === 'function') { |
| 117 | + fileConfig = fileConfig(); |
| 118 | + } |
| 119 | + } else if (file.endsWith('.json')) { |
| 120 | + const content = readFileSync(file, { encoding: 'utf-8' }); |
| 121 | + fileConfig = JSON.parse(content) as FileConfig; |
| 122 | + } else { |
| 123 | + throw new Error(`Unsupported file type: ${file}`); |
| 124 | + } |
| 125 | + |
| 126 | + if (fileConfig && !isEmpty(fileConfig)) { |
| 127 | + const destination = file.replace(src, '').replace(/\.[^/.]+$/, ''); |
| 128 | + const splittedDestination = destination.split('/blocks/'); |
| 129 | + |
| 130 | + let dest: string[] = (splittedDestination[0] ?? '') |
| 131 | + .split('/') |
| 132 | + .map(camelCase); |
| 133 | + |
| 134 | + if (splittedDestination[1]) { |
| 135 | + const [blockNamespace, blockName, ...blockDest] = |
| 136 | + splittedDestination[1].split('/'); |
| 137 | + dest = [ |
| 138 | + ...dest, |
| 139 | + 'blocks', |
| 140 | + `${blockNamespace}/${blockName}`, |
| 141 | + ...blockDest, |
| 142 | + ]; |
| 143 | + } |
| 144 | + |
| 145 | + return setNestedValue({}, dest, fileConfig); |
| 146 | + } |
| 147 | + |
| 148 | + return undefined; // Explicitly return undefined if no fileConfig is found |
| 149 | + } catch (error) { |
| 150 | + logError( |
| 151 | + `Error processing file: ${file}`, |
| 152 | + error instanceof Error ? error : undefined, |
| 153 | + ); |
| 154 | + |
| 155 | + throw error; |
| 156 | + } |
122 | 157 | } |
123 | 158 |
|
124 | 159 | export default build; |
0 commit comments