UNPKG

2.92 kBPlain TextView Raw
1import path from 'path';
2import Config from './config';
3import getCommandLine from './commandOptions';
4import { FEFLOW_ROOT } from '../../shared/constant';
5import logger from "../logger";
6
7const registerDevkitCommand = (
8 command: any,
9 commandConfig: any,
10 directoryPath: any,
11 ctx: any
12) => {
13 const builder = commandConfig.builder;
14 const [packageName] = builder.split(':', 2);
15 const config = new Config(ctx);
16 const pkgPath = path.join(directoryPath, 'node_modules', packageName);
17 try {
18 const devkitConfig = config.loadDevkitConfig(pkgPath);
19 const {
20 implementation,
21 description,
22 optionsDescription,
23 usage = {}
24 } = devkitConfig.builders[command];
25
26 const options = getCommandLine(
27 optionsDescription || usage,
28 description,
29 command
30 );
31 const devkitLogger = logger({
32 debug: Boolean(ctx.args.debug),
33 silent: Boolean(ctx.args.silent),
34 name: packageName,
35 });
36 if (Array.isArray(implementation)) {
37 ctx.commander.register(
38 command,
39 description,
40 async () => {
41 for (let i = 0; i < implementation.length; i++) {
42 const action = path.join(pkgPath, implementation[i]);
43 await require(action)(Object.assign({}, ctx, {logger: devkitLogger}));
44 }
45 },
46 options,
47 packageName
48 );
49 } else {
50 const action = path.join(pkgPath, implementation);
51 ctx.commander.register(
52 command,
53 description,
54 () => {
55 require(action)(Object.assign({}, ctx, {logger: devkitLogger}));
56 },
57 options,
58 packageName
59 );
60 }
61 } catch (e) {
62 ctx.logger.debug(`${pkgPath} not found!`);
63 }
64};
65
66export default function loadDevkits(ctx: any): Promise<void> {
67 const config = new Config(ctx);
68 const configData = config.loadProjectConfig();
69 const directoryPath = config.getProjectDirectory();
70
71 return new Promise<any>((resolve, reject) => {
72 if (configData) {
73 ctx.projectPath = directoryPath;
74 ctx.projectConfig = configData;
75 if (configData.devkit && configData.devkit.commands) {
76 const commandsConfig = configData.devkit.commands;
77 for (const command in commandsConfig) {
78 const commandConfig = commandsConfig[command];
79 registerDevkitCommand(command, commandConfig, directoryPath, ctx);
80 }
81 } else {
82 if (path.basename(directoryPath) === FEFLOW_ROOT) {
83 ctx.logger.debug('Run commands in .fef root will not work.');
84 } else {
85 ctx.logger.error(
86 `A config file .feflowrc(.js|.yaml|.yml|.json) was detected in ${directoryPath}, but lost required property 'commands' in field 'devkit'. Please check your config file or just delete it.`
87 );
88 }
89 }
90 } else {
91 ctx.logger.debug('Run commands not in a feflow project.');
92 }
93 resolve();
94 });
95}
96
\No newline at end of file