This repository has been archived by the owner on Jan 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
57 lines (50 loc) · 1.47 KB
/
component.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
const argv = require('minimist')(process.argv.slice(2), {
boolean: [ 'section' ]
});
const mkdirp = require('mkdirp');
const path = require('path');
const fs = require('fs');
const maxstache = require('maxstache');
const chalk = require('chalk');
const type = argv.section ? 'section' : 'component';
let name = argv._[0];
if (!name) {
console.error(chalk.red(`Error: Must give a ${type} name to create.`));
process.exit(0);
}
name = name.charAt(0).toUpperCase() + name.slice(1);
const cwd = process.cwd();
const dir = path.resolve(__dirname, `../src/${type}s/` + name);
fs.stat(dir, (err, stat) => {
if (err) {
write();
} else {
console.log(chalk.red(`Path at ${path.relative(cwd, dir)} already exists!`));
}
});
function write () {
mkdirp(dir, err => {
if (err) throw err;
Promise.all([
template(path.resolve(__dirname, 'template/Component.js'), path.resolve(dir, `${name}.js`)),
template(path.resolve(__dirname, 'template/Component.less'), path.resolve(dir, `${name}.less`))
]).then(() => {
console.log(`Created new ${name} ${type} at ${dir}`);
}).catch(err => console.error(err));
});
}
function template (input, output) {
const data = {
name: name
};
return new Promise((resolve, reject) => {
fs.readFile(input, 'utf8', (err, str) => {
if (err) return reject(err);
str = maxstache(str, data);
fs.writeFile(output, str, err => {
if (err) return reject(err);
resolve();
});
});
});
}