-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·89 lines (64 loc) · 2.04 KB
/
index.js
File metadata and controls
executable file
·89 lines (64 loc) · 2.04 KB
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
#! /usr/bin/env node
const yargs = require('yargs');
const fs = require('fs');
let argv = yargs
.option('name', {
type: 'string',
describe: '组件名',
alias: 'n'
})
.option('mname', {
type: 'string',
describe: '模块名',
alias: 'm'
})
.example('onion --name test-abc --mname test1', '创建一个组件')
.help('h')
.alias('h', 'help')
.epilog('designed by onion @2017')
.argv;
// 参数分为 componentName 就够了
let basepath = process.cwd();
// component模板
let componentName = argv.name;
let moduleName = argv.mname;
if([componentName, moduleName].includes(undefined)) {
console.log('name、mname必填');
process.exit(-1);
}
let originalName = componentName;
if (componentName.includes('-')) {
componentName = componentName.replace(/-([a-z])/g, (str, match) => match.toLocaleUpperCase())
}
fs.mkdirSync(`${basepath}/${originalName}`);
let entryJsTmpl = `import ${componentName}Controller from './${originalName}.component.js';
let ${componentName}Module = angular.module('${moduleName}', []);
${componentName}Module.extend(${componentName}Controller);
export default ${componentName}Module.name;
`;
let entryJspath = `${basepath}/${originalName}/${originalName}.js`;
let htmlpath = `${basepath}/${originalName}/${originalName}.html`;
let csspath = `${basepath}/${originalName}/${originalName}.css`;
let componentTmpl = `import { Inject, Component } from 'angular-onion';
import ${componentName}Html from './${originalName}.html';
import './${originalName}.css';
@Component({
selector: '${originalName}',
props: {
},
template: ${componentName}Html
})
@Inject()
class ${componentName}Controller {
constructor () {
}
$onInit () {
}
}
export default ${componentName}Controller;
`;
let componentPath = `${basepath}/${originalName}/${originalName}.component.js`;
fs.writeFileSync(htmlpath, '<div></div>');
fs.writeFileSync(csspath, '');
fs.writeFileSync(entryJspath, entryJsTmpl);
fs.writeFileSync(componentPath, componentTmpl);