UNPKG

@sun-fe-cli/sun

Version:

core of the sun-fe-cli

157 lines (132 loc) 5.05 kB
'use strict'; const path = require('path'); const semver = require('semver'); const colors = require('colors/safe'); const userHome = require('user-home'); const pathExists = require('path-exists').sync; const ora = require('ora'); const { Command } = require('commander'); const dedent = require("dedent"); const log = require('@sun-fe-cli/log'); const { DEFAULT_CLI_HOME } = require('@sun-fe-cli/constant'); const pkg = require('../package.json'); const exec = require('@sun-fe-cli/exec'); const { getLatestVersion } = require('@sun-fe-cli/utils'); const program = new Command(); module.exports = main; async function main() { try { await prepare(); // 准备阶段:做相应的检查工作 registerCommander(); // 注册commander } catch (err) { log.error('cli', colors.red(err.message)); log.verbose('cli', err); } } async function prepare() { checkPkgVersion(); // 检查本命令的版本。 // checkNodeVersion(); // 检查最新node版本信息。 checkRoot(); // 检查是否是root用户,如果是自动降级,因为有些文件由root操作后,其他用户无权限操作。 checkUserHome(); // 检查用户的主目录是否存在。 checkEnv(); // 检查环境变量 // await checkGlobalUpdate(); // 检查版本更新; } function checkPkgVersion() { log.notice('cli', `欢迎使用sun-fe-cli,当前版本:v${pkg.version}`); } // function checkNodeVersion() { // // 获取当前Node版本号 // const currentNodeVersion = process.version; // const lowestNodeVersion = LOWEST_NODE_VERSION; // if (!semver.gte(currentNodeVersion, lowestNodeVersion)) { // throw new Error(`sun-fe-cli 需要安装 v${lowestNodeVersion} 以上版本的 Node.js`); // } // } function checkRoot() { const rootCheck = require('root-check'); rootCheck(); } function checkUserHome() { if (!userHome || !pathExists(userHome)) { throw new Error(`当前登录用户主目录不存在`); } } function checkEnv() { const dotenv = require('dotenv'); const dotenvPath = path.resolve(userHome, '.sun-cli-fe.env'); if (pathExists(dotenvPath)) { // 如果文件已经存在,用dotenv解析文件。 // .env中需要配置 CLI_HOME 指定cli本地缓存的主目录。 dotenv.config({ path: dotenvPath }); } createDefaultConfig(); // 根据process.env.CLI_HOME配置 CLI_HOME_PATH // log.info('[环境变量]:', 'CLI_HOME_PATH =', process.env.CLI_HOME_PATH); } function createDefaultConfig() { const cliConfig = { home: userHome } if (process.env.CLI_HOME) { cliConfig['cliHomePath'] = path.join(userHome, process.env.CLI_HOME); } else { cliConfig['cliHomePath'] = path.join(userHome, DEFAULT_CLI_HOME); } process.env.CLI_HOME_PATH = cliConfig.cliHomePath; return cliConfig; } async function checkGlobalUpdate() { // TODO: 可以用 update-notifier 更新检查库,限制一天内只能查询一下。 // spinner.start('检查远程版本信息'); const spinner = ora('检查远程版本信息').start(); const lstVersion = await getLatestVersion(pkg.name); const currentVersion = pkg.version; if (!lstVersion) { // 远程信息没有获取到 spinner.warn('没有获取到远程版本信息\n') return; } if (semver.gt(lstVersion, currentVersion)) { // 如果远程版本比较高,提示更新。 spinner.warn(`版本过低,请更新!\n当前版本: v${currentVersion}\n最新版本: v${lstVersion}\n[更新命令]: npm -g ${pkg.name}\n`); } else { spinner.succeed(`已经是最新版本: v${lstVersion}\n`); } } function registerCommander() { const cmdBin = Object.keys(pkg.bin)[0]; program .name(cmdBin) .usage('<command> [options]') .description('一站式前端研发工作脚手架') .version(pkg.version) .option('-d, --debug', '是否开启调试模式', false) .option('-tp, --targetPath <targetPath>', '是否指定本地调试文件路径', ''); program .addHelpText('after', dedent(`Examples: $ ${cmdBin} --help` )); program .command('init [projectName]') .option('-f, --force', '是否强制初始化项目') .action(exec) const options = program.opts(); program.on('option:debug', function () { if (options.debug) { // 如果开启debug模式,调整log level process.env.CLI_LOG_LEVEL = 'verbose'; log.level = process.env.CLI_LOG_LEVEL; log.verbose('cli', '开启调试模式'); } }); program.on('option:targetPath', function () { process.env.CLI_TARGET_PATH = options.targetPath; }); program.parse(process.argv); // try { // program.parse(process.argv); // } catch (err) { // console.log('err') // // custom processing... // } }