接上一个博客源码共读-UMI-3,接下来,我们来继续执行 umi test
从第一个博客中我们知道,执行umi test会执行lib/scripts/test.js
lib/scripts/test.js
import test from '../test';
const args = process.argv.slice(2);
const watch = args.indexOf('-w') > -1 || args.indexOf('--watch') > -1;
const coverage = args.indexOf('--coverage') > -1;
test({
watch,
coverage,
});这里简要的看下代码,从umi-test中引入test模块,然后执行。(help也是一样的,从其他文件导入方法,help的略)
packages\umi\src\test.md
export default from 'umi-test';这里使用独立的模块处理了,所以后面我们查看umi-test的时候,再做详细介绍。
接下来我们来执行umi help
lib/scripts/help.js(略)
packages\umi\src\help.md
import chalk from 'chalk';把现有的支持的命令都罗列出来,名字,简介,别名
const commands = {
build: {
name: 'build',
description: 'Create a production build',
},
dev: {
name: 'dev',
description: 'Start a development server',
},
test: {
name: 'test',
description: 'Start a development server',
},
help: {
name: 'help',
description: 'show help',
aliases: ['h'],
},
version: {
name: 'version',
description: 'Outputs Umi version.',
aliases: ['v'],
},
};统一调用的打印方法,其实这个应该放别的地方,做util工具使用
class Logger {
info = message => {
console.log(`${message}`);
};
error = message => {
console.error(chalk.red(message));
};
success = message => {
console.error(chalk.green(message));
};
}对现有支持的数组做一个遍历,打印出所有的使用方法
export default function(opts = {}) {
const { type } = opts;
const logger = new Logger();
logger.info(`\nUsage: umi <command>\n`);
if (!commands[type]) {
logger.error(`Unknown script : ${chalk.cyan(type)}.`);
}
logger.info(`Available Commands:`);
for (const key in commands) {
if (commands.hasOwnProperty(key)) {
const cmd = commands[key];
logger.info(` ${chalk.cyan(cmd.name)} ${cmd.description}`);
}
}
// 这里应该是留做后续接口扩展说明的,如使用umi dev --help,应该要打印出dev的说明,还有所支持的环境变量等等。
// Support for subsequent extensions
// logger.info(`\nFor more detailed help run "umi [command name] --help"`);
}文中注释可扩展的地方是因为现在umi版本还不是那么稳定,这一块的功能我没想好怎么处理。应该要支持多语言。
这一块是因为有人提了iss说help文档过于简陋,所以我从angular-cli里面搬过来的。
本节课就到此结束了,喜欢我的博客的朋友可以关注我。后续会有实战演练哦。
Github地址
https://github.com/xiaohuoni/source-code-co-reading-umi
感谢您的阅读。
我是莽夫,希望你开心。
如果你觉得本文对你有帮助,请扫描文末二维码,支持博主原创。
希望大家关注我的个人公众号ionic_
应邀加入快问码,大家有任何疑问可以向我提问。
版权声明:本文为onil_chen原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。