-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (67 loc) · 1.73 KB
/
index.js
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
#!/usr/bin/env node
import prompts from 'prompts';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
const { componentName, template } = await prompts([
{
type: 'text',
name: 'componentName',
message: 'Component name',
initial: 'oc-component',
},
{
type: 'select',
name: 'template',
message: 'Select a template',
choices: [
{ title: 'Vanilla', value: 'es6' },
{ title: 'React', value: 'react' },
{ title: 'Solid', value: 'solid' },
{ title: 'Preact', value: 'preact' },
{ title: 'Vue', value: 'vue' },
{ title: 'Elm', value: 'elm' },
],
},
]);
try {
fs.mkdirSync(`./${componentName}`);
} catch (err) {
if (err.code === 'EEXIST') {
console.log('Folder already exists');
process.exit(1);
}
throw err;
}
const templateDir = path.resolve(
fileURLToPath(import.meta.url),
`../templates/${template}`
);
console.log();
console.log('Creating the template');
fs.cpSync(templateDir, `./${componentName}`, {
recursive: true,
});
fs.writeFileSync(
`./${componentName}/.gitignore`,
['node_modules', '_package'].join('\n'),
'utf-8'
);
replaceJson(`./${componentName}/package.json`, (pkg) => ({
...pkg,
name: componentName,
scripts: {
...(pkg.scripts || {}),
start: `oc dev .. --components ${componentName}`,
},
}));
console.log('Finished. To start your oc for the first time:');
console.log();
console.log(` cd ${componentName}`);
console.log(' npm install');
console.log(' npm start');
function replaceJson(file, transformer) {
const json = JSON.parse(fs.readFileSync(file, 'utf-8'));
const transformed = transformer(json);
fs.writeFileSync(file, JSON.stringify(transformed, null, 2), 'utf-8');
}