-
Notifications
You must be signed in to change notification settings - Fork 229
/
loadDevkits.ts
56 lines (52 loc) 路 2 KB
/
loadDevkits.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
import path from 'path';
import Config from './config';
import getOptionFromCommand from "./commandOptions";
const registerDevkitCommand = (command: any, commandConfig: any, directoryPath: any, ctx: any) => {
const builder = commandConfig.builder;
const [ packageName ] = builder.split(':', 2);
const config = new Config(ctx);
const pkgPath = path.join(directoryPath, 'node_modules', packageName);
try {
const devkitConfig = config.loadDevkitConfig(pkgPath);
const { implementation, description, optionsDescription = {} } = devkitConfig.builders[command];
const options = getOptionFromCommand(optionsDescription);
if (Array.isArray(implementation)) {
ctx.commander.register(command, description, async () => {
for (let i = 0; i < implementation.length; i ++) {
const action = path.join(pkgPath, implementation[i]);
await require(action)(ctx);
}
}, options);
} else {
const action = path.join(pkgPath, implementation);
ctx.commander.register(command, description, () => {
require(action)(ctx);
}, options);
}
} catch (e) {
ctx.logger.debug(`${ pkgPath } not found!`);
}
};
export default function loadDevkits(ctx: any): Promise<void> {
const config = new Config(ctx);
const configData = config.loadProjectConfig();
const directoryPath = config.getProjectDirectory();
return new Promise<any>((resolve, reject) => {
if (configData) {
ctx.projectPath = directoryPath;
ctx.projectConfig = configData;
if (configData.devkit && configData.devkit.commands) {
const commandsConfig = configData.devkit.commands;
for (const command in commandsConfig) {
const commandConfig = commandsConfig[command];
registerDevkitCommand(command, commandConfig, directoryPath, ctx);
}
} else {
ctx.logger.error('Your project config is not correct.');
}
} else {
ctx.logger.debug('Run commands not in a feflow project.');
}
resolve();
});
}