-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.ys.command.js
58 lines (53 loc) · 1.87 KB
/
.ys.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
55
56
57
58
const fs = require('fs-extra');
const path = require('path');
const Commander = require('./command');
module.exports = class CommanderModule {
constructor(thread, installer) {
this.installer = installer;
this.thread = thread;
}
static ['command:framework'](app, installer) {
app.command('router <name>')
.describe('创建一个新的路由')
.action(installer.task(Commander));
}
['options:plugin']() {
return {
enable: true,
package: 'ys-pg-koa-router',
agent: ['agent'],
dependencies: []
};
}
async ['life:created']({ cwd }) {
const routerDir = path.resolve(cwd, 'app', 'router');
const indexFilePath = path.resolve(routerDir, 'index.js');
if (!fs.existsSync(routerDir)) {
fs.ensureDirSync(routerDir);
this.thread.on('beforeRollback', async () => {
this.installer.spinner.debug('-', path.relative(process.cwd(), routerDir));
fs.removeSync(routerDir);
await this.installer.delay(50);
});
}
if (fs.existsSync(indexFilePath)) {
throw new Error(`file '${indexFilePath}' is already exists.`);
}
const data = `module.exports = (app, router) => {
router.get('/', app.controller.index.common);
}`;
fs.writeFileSync(indexFilePath, data, 'utf8');
this.thread.on('beforeRollback', async () => {
this.installer.spinner.debug('-', path.relative(process.cwd(), indexFilePath));
fs.unlinkSync(indexFilePath);
await this.installer.delay(50);
});
this.installer.spinner.success('+', path.relative(process.cwd(), indexFilePath));
}
async ['life:destroyed']({ cwd }) {
this.installer.spinner.warn('正在删除项目中的路由文件 ...');
await this.installer.execScript(cwd, 'rm', '-rf', 'app/router');
this.installer.spinner.warn('项目中的路由文件删除成功!');
await this.installer.delay(50);
}
}