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