-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.js
54 lines (50 loc) · 1.68 KB
/
command.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
const fs = require('fs-extra');
const path = require('path');
const template = require('./template');
const jsbeautifier = require('js-beautify');
module.exports = class CommanderModule {
constructor(thread, installer) {
this.installer = installer;
this.thread = thread;
}
beautiful(str) {
return jsbeautifier.js_beautify(str, {
indent_size: 2
})
}
addFile(cwd, name) {
name = name.replace(/\.js$/i, '');
if (!/^[a-z][a-z0-9_/]*$/.test(name)) {
throw new Error('模块命名不规范');
}
const filePath = path.resolve(cwd, name + '.js');
const dir = path.dirname(filePath);
const filename = path.basename(filePath, '.js');
fs.ensureDirSync(dir);
if (fs.existsSync(filePath)) {
throw new Error(`file '${filePath}' is already exists.`);
}
const data = this.beautiful(template(filename));
fs.writeFileSync(filePath, data, 'utf8');
this.thread.on('beforeRollback', async () => {
this.installer.spinner.debug('-', path.relative(process.cwd(), filePath));
fs.unlinkSync(filePath);
await this.installer.delay(50);
});
this.installer.spinner.success('+', path.relative(process.cwd(), filePath));
}
async render(name) {
const root = this.installer.root;
const type = this.installer.type;
if (!root || type !== 'framework') {
throw new Error('非项目目录无法使用此命令');
}
const relativePath = path.relative(root, process.cwd());
const cachePath = path.resolve(root, 'app/cache');
const isInCacheDir = relativePath.indexOf('app/cache') === 0;
if (isInCacheDir) {
return this.addFile(process.cwd(), name);
}
this.addFile(cachePath, name);
}
}