-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
64 lines (49 loc) · 1.07 KB
/
cli.ts
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
import * as fs from 'fs';
import * as program from 'commander';
export const getFunction = (name) => `
export function ${name}() {
}
`;
export const getClass = (name) => `
export class ${name} {
}
`;
export const getSpec = (name) =>
`import { ${name} } from './${name}';
test('${name}', () => {
expect()
});
`;
export const getMD = (name) =>
`**${name}**
description
\`\`\`
code
\`\`\`
`;
program
.arguments('<command> [type] [name]')
.action((command, type, name) => {
switch (command) {
case 'g':
case 'generate': {
const dir = `./${name}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(`${dir}/${name}.ts`, getGenerator(type)(name));
fs.writeFileSync(`${dir}/${name}.spec.ts`, getSpec(name));
fs.writeFileSync(`${dir}/README.md`, getMD(name));
break;
}
}
})
.parse(process.argv);
function getGenerator(type) {
if ([ 'c', 'class' ].includes(type)) {
return getClass;
}
if ([ 'f', 'function' ].includes(type)) {
return getFunction;
}
}