diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 6c3f2c9f..4f07b4da 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,7 +1,7 @@ import { cac } from 'cac'; -import chalk from 'chalk'; import { allowTs } from './utils/allowTs'; +import { error } from './utils/logger'; import { scripts } from './commands'; import { UserConfig } from './types'; @@ -11,7 +11,7 @@ import { UserConfig } from './types'; const wrapCommand = (cmd: (...args: any[]) => Promise): typeof cmd => { const wrappedCommand: typeof cmd = (...args) => cmd(...args).catch((err) => { - console.error(chalk.red(err.stack)); + error(err.stack); process.exit(1); }); return wrappedCommand; diff --git a/packages/cli/src/utils/logger.ts b/packages/cli/src/utils/logger.ts new file mode 100644 index 00000000..aba5fd16 --- /dev/null +++ b/packages/cli/src/utils/logger.ts @@ -0,0 +1,17 @@ +import chalk from 'chalk'; + +export const info = (msg: string) => { + console.log(chalk.white(msg)); +}; + +export const error = (msg: string) => { + console.log(chalk.red(msg)); +}; + +export const success = (msg: string) => { + console.log(chalk.green(msg)); +}; + +export const execInfo = (msg: string) => { + console.log(chalk.blue(msg)); +}; diff --git a/packages/cli/src/utils/resolveAppPackages.ts b/packages/cli/src/utils/resolveAppPackages.ts index c5996e3f..e1d54c25 100644 --- a/packages/cli/src/utils/resolveAppPackages.ts +++ b/packages/cli/src/utils/resolveAppPackages.ts @@ -2,13 +2,13 @@ import { execSync } from 'child_process'; import path from 'path'; import { exit } from 'process'; -import chalk from 'chalk'; import fs from 'fs-extra'; import * as recast from 'recast'; import type App from '../Core'; import { Entry, EntryType, ModuleMainFilePath, NpmConfig, PackageType } from '../types'; +import { error, execInfo, info } from './logger'; interface TypeAssertion { type: string; imports: any[]; @@ -102,7 +102,7 @@ export const resolveAppPackages = (app: App): ModuleMainFilePath => { try { npmInstall(dependencies, app.options.source, app.options.npmConfig); } catch (e) { - console.error(e); + error(e as string); } if (fs.existsSync(packageBakFile)) { @@ -144,8 +144,8 @@ const npmInstall = function (dependencies: Record, cwd: string, const command = `${client} ${install} ${packages} --registry ${registry}`; - console.log(chalk.blue(cwd)); - console.log(chalk.blue(command)); + execInfo(cwd); + execInfo(command); execSync(command, { stdio: 'inherit', @@ -330,8 +330,8 @@ const getIndexPath = function (entry: string) { const parseEntry = function ({ ast, package: module, indexPath }: ParseEntryOption) { if (!ast.program) { - console.log(`${module} 入口文件不合法`); - return exit(1); + error(`${module} 入口文件不合法`); + exit(1); } const tokens = getASTTokenByTraverse({ ast, indexPath }); @@ -339,16 +339,13 @@ const parseEntry = function ({ ast, package: module, indexPath }: ParseEntryOpti const { importComponentSource, importComponentToken, exportDefaultToken } = tokens; if (!config) { - console.log(`${module} ${EntryType.CONFIG} 文件声明不合法`); - return exit(1); + info(`${module} ${EntryType.CONFIG} 文件声明不合法`); } if (!value) { - console.log(`${module} ${EntryType.VALUE} 文件声明不合法`); - return exit(1); + info(`${module} ${EntryType.VALUE} 文件声明不合法`); } if (!event) { - // event 非必须,不需要 exit - console.log(`${module} ${EntryType.EVENT} 文件声明缺失`); + info(`${module} ${EntryType.EVENT} 文件声明缺失`); } const findIndex = importComponentToken.indexOf(exportDefaultToken); @@ -358,8 +355,8 @@ const parseEntry = function ({ ast, package: module, indexPath }: ParseEntryOpti } if (!component) { - console.log(`${module} ${EntryType.COMPONENT} 文件声明不合法`); - return exit(1); + info(`${module} ${EntryType.COMPONENT} 文件声明不合法`); + exit(1); } const reg = /^.*[/\\]node_modules[/\\](.*)/; @@ -397,7 +394,7 @@ const getASTTokenByTraverse = ({ ast, indexPath }: { ast: any; indexPath: string visitExportNamedDeclaration(p) { const { node } = p; const { specifiers, source } = node; - const name = specifiers?.[0].exported.name.toLowerCase(); + const name = specifiers?.[0]?.exported.name.toLowerCase(); if (name === EntryType.VALUE) { value = path.resolve(path.dirname(indexPath), `${source?.value}`);