-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (70 loc) · 1.95 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
#!/usr/bin/env node
const package = require('./package.json');
const program = require('commander');
const chalk = require('chalk');
const path = require('path');
const generateComponent = require('./lib/generate-component');
const generateOnlyComponent = require('./lib/generate-only-component');
// 获取真实路径
function getRealPath(_path) {
let pa = path.resolve('.'); // 当前路径
if (_path) {
// 传入path
pa = path.resolve(pa, _path);
}
return pa;
}
program
.command('create <name>')
.description('create a vue component with directory')
.alias('c')
.option('-p, --path [path]', 'real path; Please use relative paths')
.option('-o,--only', 'create only component without directory')
.option('-s,--scoped', 'Scoped CSS')
.option('-l, --lang [lang]', 'CSS Modules')
.option(
"-t, --typescript [format]",
"TypeScript support two formats (In multi folder only): vue template and html template"
)
.action((name, cmd) => {
try {
cmd.only
? generateOnlyComponent(
name,
getRealPath(cmd.path),
cmd.lang,
cmd.scoped,
cmd.typescript,
)
: generateComponent(
name,
getRealPath(cmd.path),
cmd.lang,
cmd.scoped,
cmd.typescript,
);
} catch (e) {
console.log(e);
}
});
// 其他未知命令
program.arguments('<command>').action(cmd => {
program.outputHelp();
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`));
console.log();
});
// 创建其他有用的帮助信息
program.on('--help', () => {
console.log();
console.log(
` Run ${chalk.cyan(
`vs <command> --help`,
)} for detailed usage of given command.`,
);
console.log();
});
program.commands.forEach(c => c.on('--help', () => console.log()));
program.version(package.version).parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}