脚手架架构设计图
脚手架准备过程
const pkg = require('../package.json');
function checkNodeVersion() {
const currentVersion = process.version;
const lowestVersion = constant.LOWEST_NODE_VERSION;
if (!semver.gte(currentVersion, lowestVersion)) {
throw new Error(colors.red(`filway-cli 需要安装 v${lowestVersion} 以上版本的 Node.js`));
}
}
function checkRoot() {
const rootCheck = require('root-check');
rootCheck();
}
const userHome = require('user-home');
function checkUserHome() {
if (!userHome || !pathExists(userHome)) {
throw new Error(colors.red('当前登录用户主目录不存在!'));
}
}
function checkInputArgs() {
const minimist = require('minimist');
args = minimist(process.argv.slice(2));
checkArgs();
}
function checkArgs() {
if (args.debug) {
process.env.LOG_LEVEL = 'verbose';
} else {
process.env.LOG_LEVEL = 'info';
}
log.level = process.env.LOG_LEVEL;
}
function checkEnv() {
const dotenv = require('dotenv');
const dotenvPath = path.resolve(userHome, '.env');
if (pathExists(dotenvPath)) {
dotenv.config({
path: dotenvPath
});
}
createDefaultConfig();
}
function createDefaultConfig() {
const cliConfig = {
home: userHome,
};
if (process.env.CLI_HOME) {
cliConfig['cliHome'] = path.join(userHome, process.env.CLI_HOME);
} else {
cliConfig['cliHome'] = path.join(userHome, constant.DEFAULT_CLI_HOME);
}
process.env.CLI_HOME_PATCH = cliConfig.cliHome;
}
async function checkGlobalUpdate() {
const currentVersion = pkg.version;
const npmName = pkg.name;
const { getNpmSemverVersion } = require('@filway-cli/get-npm-info')
const lastVersion = await getNpmSemverVersion(currentVersion, npmName)
if (lastVersion && semver.gt(lastVersion, currentVersion)) {
log.warn('更新提示',colors.yellow(`请手动更新 ${npmName}, 当前版本: ${currentVersion}, 最新版本: ${lastVersion}
更新命令: npm install -g ${npmName}`));
}
}
commander框架
const { Command } = require('commander');
const pkg = require('../package.json');
const program = new Command();
program
.name(Object.keys(pkg.bin)[0])
.usage('<command> [options]')
.version(pkg.version)
.option('-d, --debug', '是否开启调试模式', false)
.option('-e, --envName <envName>', '获取环境变量名称');
const clone = program.command('clone <source> [destination]');
clone
.description('clone a repository into a newly created directory')
.option('-f, --force', '是否强制克隆')
.action((source, destination, cmdObj) => {
console.log('clone command called', source, destination, cmdObj.force);
});
const service = new Command('service');
program.addCommand(service);
service
.command('start [port]')
.description('start service at some port')
.action((port) => {
console.log('do service start '+port)
});
service
.command('stop')
.description('stop service')
.action(() => {
console.log('stop service')
});
program
.command('install [name]', 'install package', {
executableFile: 'filway-cli',
hidden: true
})
.alias('i')
program.on('option:debug', function () {
if (program.opts().debug) {
process.env.LOG_LEVEL = 'verbose'
}
console.log(process.env.LOG_LEVEL)
})
program.on('command:*', function (obj) {
console.log(obj);
console.error('未知的命令: ' + obj[0]);
const availableCommands = program.commands.map( cmd => cmd.name());
console.log('可用命令: '+availableCommands.join(','));
})
program
.parse(process.argv);
如何让Node支持ES Module
- 通过webpack + babel 编译
- node js原生支持, 需要.mjs后缀