-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·323 lines (272 loc) · 10.5 KB
/
cli.js
File metadata and controls
executable file
·323 lines (272 loc) · 10.5 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env node
const path = require('node:path');
const fs = require('fs-extra');
const yaml = require('js-yaml');
const prompts = require('@clack/prompts');
const { isCancel, cancel, select, text, intro, outro, confirm } = prompts;
const color = require('picocolors');
let radixComponentsPath = '../../contrib/radix/components';
const activeThemePath = process.cwd();
async function listComponents() {
try {
intro(color.magenta(`Available ${color.bold('Radix')} Components:`));
const components = await fs.readdirSync(radixComponentsPath);
for (const componentDir of components) {
const ymlPath = `${radixComponentsPath}/${componentDir}/${componentDir}.component.yml`;
if (fs.existsSync(ymlPath)) {
const fileContents = await fs.readFile(ymlPath, 'utf8');
const doc = yaml.load(fileContents);
if (doc?.name) {
const description = doc.description || 'No description available';
console.log(`- ${color.magenta(doc.name)}: ${description}`);
}
}
}
outro(
color.magenta(
`That's all. To add a component to your theme, run ${color.inverse('drupal-radix-cli add')} or to generate a new one ${color.inverse('drupal-radix-cli generate')}.`,
),
);
} catch (error) {
console.error('Error listing components:', error);
}
}
async function updateTemplateReferences(componentName, themeName) {
const updatedTemplates = [];
const templatesPath = path.join(activeThemePath, 'templates');
if (!fs.existsSync(templatesPath)) {
return updatedTemplates;
}
try {
const templateFiles = await findTwigFiles(templatesPath);
for (const templateFile of templateFiles) {
const fileContent = await fs.readFile(templateFile, 'utf8');
const radixPattern = new RegExp(`radix:${componentName}`, 'g');
if (radixPattern.test(fileContent)) {
const updatedContent = fileContent.replace(radixPattern, `${themeName}:${componentName}`);
await fs.writeFile(templateFile, updatedContent, 'utf8');
updatedTemplates.push(path.relative(activeThemePath, templateFile));
}
}
} catch (error) {
console.error('Error updating template references:', error);
}
return updatedTemplates;
}
async function findTwigFiles(dir) {
const twigFiles = [];
async function scanDirectory(currentDir) {
const items = await fs.readdir(currentDir);
for (const item of items) {
const fullPath = path.join(currentDir, item);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
await scanDirectory(fullPath);
} else if (item.endsWith('.html.twig')) {
twigFiles.push(fullPath);
}
}
}
await scanDirectory(dir);
return twigFiles;
}
async function addComponent(skipTemplateUpdate = false) {
try {
const components = fs.readdirSync(radixComponentsPath);
const options = components
.map((component) => {
const ymlPath = `${radixComponentsPath}/${component}/${component}.component.yml`;
if (fs.existsSync(ymlPath)) {
const fileContents = fs.readFileSync(ymlPath, 'utf8');
const doc = yaml.load(fileContents);
if (doc?.name) {
return { value: component, label: doc.name };
}
}
return null;
})
.filter(Boolean);
if (options.length === 0) {
outro(color.yellow('No components available to add.'));
return;
}
const maxItems = 8;
const componentName = await select({
message: 'Pick a Radix component to add to your theme.',
options: options,
maxItems: maxItems,
onCancel: () => {
cancel('Operation cancelled.');
process.exit(0);
},
});
if (isCancel(componentName)) {
cancel('Operation cancelled.');
process.exit(0);
}
const sourcePath = `${radixComponentsPath}/${componentName}`;
const targetPath = `${activeThemePath}/components/${componentName}`;
if (fs.existsSync(targetPath)) {
// Ask user if they want to overwrite the existing component
const overwrite = await confirm({
message: `${componentName} already exists. Do you want to overwrite it?`,
initial: false,
});
if (isCancel(overwrite) || !overwrite) {
outro(color.yellow(`Operation cancelled. ${componentName} was not overwritten.`));
process.exit(0);
}
fs.removeSync(targetPath);
}
fs.copySync(sourcePath, targetPath);
let outputMessage = color.magenta(`Component ${componentName} has been added at: ${color.bold(targetPath)}`);
if (!skipTemplateUpdate) {
// Get theme name from the current working directory
const themeName = path.basename(activeThemePath);
// Check and update templates
const updatedTemplates = await updateTemplateReferences(componentName, themeName);
if (updatedTemplates.length > 0) {
outputMessage += `\n\n${color.green('Updated template references:')}`;
for (const templatePath of updatedTemplates) {
outputMessage += `\n${color.cyan(`- ${templatePath}`)}`;
}
}
} else {
outputMessage += `\n${color.yellow('Template update skipped (--no-template flag used), update your twig templates manually.')}`;
}
outro(outputMessage);
} catch (error) {
console.error('Error during the add component process:', error);
}
}
async function generateComponent() {
const response = await text({
message: 'What is the name of your component?',
placeholder: 'eg. card',
onCancel: () => {
cancel(outro(color.yellow(`"Operation cancelled."`)));
process.exit(0);
},
});
if (isCancel(response)) {
cancel(outro(color.yellow(`"Operation cancelled."`)));
process.exit(0);
}
const componentName = response;
const componentsDirPath = path.join(activeThemePath, 'components');
const componentDirPath = path.join(componentsDirPath, componentName);
try {
if (await fs.pathExists(componentDirPath)) {
outro(color.yellow(`The ${color.italic(componentName)} component already exists! maybe try another name?`));
process.exit(0);
}
await fs.ensureDir(componentsDirPath);
await fs.ensureDir(componentDirPath);
const filesToCreate = [
`${componentName}.twig`,
`${componentName}.scss`,
`_${componentName}.js`,
'README.mdx',
`${componentName}.component.yml`,
];
for (const file of filesToCreate) {
const filePath = path.join(componentDirPath, file);
await fs.ensureFile(filePath);
}
// Initial content for the SCSS file
const scssFilePath = path.join(componentDirPath, `${componentName}.scss`);
await fs.appendFile(scssFilePath, '@import "../../src/scss/init";\n');
// Initial content for the component YML file
const componentYmlPath = path.join(componentDirPath, `${componentName}.component.yml`);
const componentYmlContent = `$schema: https://git.drupalcode.org/project/drupal/-/raw/10.1.x/core/modules/sdc/src/metadata.schema.json
name: ${componentName}
status: experimental
description: 'The ${componentName} component auto-generated by drupal-radix-cli'
`;
await fs.writeFile(componentYmlPath, componentYmlContent);
// Initial content for the component README file
const readmeMdPath = path.join(componentDirPath, 'README.md');
const readmeMdContent = `# ${componentName} Component
This component was generated by the \`drupal-radix-cli\` tool. Feel free to update this README to provide more information about your component and how to use it.`;
await fs.writeFile(readmeMdPath, readmeMdContent);
// Initial content for the twig file
const twigFilePath = path.join(componentDirPath, `${componentName}.twig`);
const twigCommentBlock = `{#
/**
* @file
* Template for ${componentName} component.
*/
#}`;
await fs.writeFile(twigFilePath, twigCommentBlock);
console.log(
`Component ${componentName} generated successfully. Make sure to remove anything that you don't need and update your ${componentName}.component.yml file.`,
);
} catch (error) {
console.error('Error generating component:', error);
process.exit(1);
}
}
function showHelp() {
intro(`
${color.magenta('Usage: drupal-radix-cli [command] [--radix-path <path>] [--no-template]')}
Commands:
${color.green('list')} - Displays a list of available Radix components.
${color.green('add')} - Adds a new component to your theme, replacing the existing one if it's already there.
${color.green('generate')} - Generates a new component structure within the 'components' directory.
${color.green('help')} - Shows this help message.
Flags:
${color.green('--radix-path')} - Optional. Specify the path to the Radix components directory. If not provided, defaults to "../../../contrib/radix/components".
${color.green('--no-template')} - Optional. Skip updating template references when adding components.
`);
outro(color.magenta('You can also use the --help flag to show this help message.'));
}
(async () => {
const args = process.argv.slice(2);
const radixPathFlagIndex = args.findIndex((arg) => arg === '--radix-path');
const noTemplateFlagIndex = args.findIndex((arg) => arg === '--no-template');
if (radixPathFlagIndex > -1) {
let providedPath = args[radixPathFlagIndex + 1];
if (!providedPath) {
try {
providedPath = await text({
message: 'Enter the path to the Radix components directory:',
placeholder: '../../../contrib/radix/components',
onCancel: () => {
cancel(outro(color.yellow(`"Operation cancelled."`)));
process.exit(0);
},
});
radixComponentsPath = path.resolve(providedPath);
} catch (error) {
if (isCancel(error)) {
cancel(outro(color.yellow(`"Operation cancelled."`)));
process.exit(1);
} else {
console.error('An unexpected error occurred:', error);
process.exit(1);
}
}
} else {
radixComponentsPath = path.resolve(providedPath);
}
}
const command = args[0] || 'help';
const skipTemplateUpdate = noTemplateFlagIndex > -1;
try {
if (command === 'list') {
await listComponents();
} else if (command === 'add') {
await addComponent(skipTemplateUpdate);
} else if (command === 'generate') {
await generateComponent();
} else if (command === 'help') {
showHelp();
} else {
console.log(`Unknown command: ${command}`);
showHelp();
}
} catch (error) {
console.error('An error occurred:', error);
process.exit(1);
}
})();