feat(cli): 优化 logger,优化 ast 解析

This commit is contained in:
kevinyzheng 2022-11-04 16:03:35 +08:00 committed by roymondchen
parent a03ac7c78b
commit 958bcd3ec5
3 changed files with 31 additions and 17 deletions

View File

@ -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<void>): 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;

View File

@ -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));
};

View File

@ -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<string, string>, 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}`);