-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
107 lines (85 loc) · 2.41 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
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
#!/usr/bin/env node
'use strict';
const fs = require("fs");
const path = require("path");
const changeCase = require("change-case");
const program = require("commander");
const BASR_PATH = path.resolve(__dirname, './Component');
const common = require(path.resolve(__dirname, './common'));
const {
clearLine,
createClassName,
createModuleName,
createComponentName,
} = common;
function wirte(path, code) {
fs.writeSync(fs.openSync(path, "w"), clearLine(code));
};
function getOpts(name = '', parentName = '', cmd = {}) {
return Object.assign({
name,
parentName,
}, cmd);
}
function createComponent(opts = {}) {
const fileNames = fs.readdirSync(BASR_PATH);
const name = createComponentName(opts);
const {
hooks: isHooks,
single: isSingle,
function: isFunction,
} = opts;
const dir = isSingle ? path.resolve() : path.resolve(name);
if (isSingle) {
const fn = require(path.resolve(BASR_PATH, 'component.template.js'));
const code = fn(opts);
const filePath = path.resolve(dir, `${name}.jsx`);
wirte(filePath, code);
return;
}
fs.mkdirSync(`./${name}`);
fileNames.forEach((fileName) => {
const fn = require(path.resolve(BASR_PATH, fileName));
switch (fileName) {
case 'component.template.js': {
const code = fn(opts);
const filePath = path.resolve(dir, `${name}.jsx`);
wirte(filePath, code);
break;
}
case 'style.template.js': {
const code = fn(opts);
const filePath = path.resolve(dir, `${name}.scss`);
wirte(filePath, code);
break;
}
case 'hooks.js': {
if (isHooks && isFunction) {
const code = fn(opts);
const filePath = path.resolve(dir, fileName);
wirte(filePath, code);
}
break;
}
default: {
const code = fn(opts);
const filePath = path.resolve(dir, fileName);
wirte(filePath, code);
break;
}
}
});
}
function run(name = '', parentName = '', cmd = {}) {
const opts = getOpts(name, parentName, cmd);
createComponent(opts);
};
program
.name('create-react-file')
.arguments('<name> [parentName]')
.option('-f, --function', 'create function component')
.option('-p, --page', 'create component as page')
.option('-s, --single', 'create single file Component.jsx')
.option('-h, --hooks', 'create file hooks.js')
.action(run)
.parse(process.argv);