-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·47 lines (40 loc) · 1.18 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
#!/usr/bin/env node
const yargs = require('yargs');
const { argv } = yargs;
const fs = require('fs');
const chalk = require('chalk');
yargs
.usage('Usage: $0 <command> [options]')
.example(
'$0 --template="./templatest/sample.tsx" --output="components/Form/"',
'Write sample.tsx with strings replaced to output components directory'
)
.nargs('template', 1)
.nargs('output', 1)
.describe('template', 'Load a template file')
.describe('output', 'Ouput file location')
.demandOption(['template', 'output'])
.help('h')
.alias('h', 'help')
.epilog('Powered by Devzstudio').argv;
const startProcess = async () => {
await fs.readFile(argv.template, 'utf8', async function (err, data) {
if (err) throw err;
const ignoreCommandsList = ['template', 'output'];
let fileContents = await data;
Object.keys(argv).map((key) => {
if (!ignoreCommandsList.includes(key)) {
const slug = `{{${key}}}`;
const regEx = new RegExp(slug, 'g');
fileContents = fileContents.replace(regEx, argv[key]);
}
});
await fs.writeFile(argv.output, fileContents, function (err) {
if (err) {
return console.log(err);
}
chalk.blue('The file was saved!');
});
});
};
startProcess();