From 65d0577525ba31ef9873bcbcd241dc402954ed03 Mon Sep 17 00:00:00 2001 From: shulan Date: Wed, 22 May 2024 10:37:20 +0000 Subject: [PATCH] chore: short option format & format debug info (#12) --- .changeset/modern-cameras-drive.md | 6 ++++++ src/executer.ts | 28 +++++++------------------- src/index.ts | 19 ++++++++---------- src/plugins/auto-execute.ts | 32 ++++++++++++++++++------------ src/util/file.ts | 2 +- src/util/log.ts | 7 +++++++ 6 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 .changeset/modern-cameras-drive.md create mode 100644 src/util/log.ts diff --git a/.changeset/modern-cameras-drive.md b/.changeset/modern-cameras-drive.md new file mode 100644 index 0000000..ce36edd --- /dev/null +++ b/.changeset/modern-cameras-drive.md @@ -0,0 +1,6 @@ +--- +'farmup': patch +--- + +1. short option format +2. format debug info diff --git a/src/executer.ts b/src/executer.ts index 3d44fd0..dd1510e 100644 --- a/src/executer.ts +++ b/src/executer.ts @@ -2,6 +2,7 @@ import { type ExecaChildProcess, execaCommand } from 'execa'; import { ExecuteMode, type ExecuteOption, type ResolvedCommonOptions } from './types/options'; import type { Logger } from '@farmfe/core'; import { delay } from './util/async'; +import { trimEndLF } from './util/log'; export class Executer { child?: ExecaChildProcess; @@ -39,33 +40,18 @@ export class Executer { stdio: 'pipe', }); - child.stdout?.on('data', (data) => { - logger.debug(data.toString()); - }); + child.stdout?.on('data', (data) => logger.debug(trimEndLF(data.toString()))); - child.stderr?.on('data', (err) => { - logger.error(err); - }); + child.stderr?.on('data', (err) => logger.error(err)); this.child = child; - process.on('beforeExit', () => { - this.closeChild(); - }); - process.on('exit', () => { - this.closeChild(); - }); + process.on('beforeExit', this.closeChild); + process.on('exit', this.closeChild); child.on('exit', (code) => { - if (child) { - const message = `"${name}" PID ${child.pid}`; - if (code === 0) { - this.logger.info(`${message} done`); - } else { - this.logger.info(`${message} killed`); - } - this.child = undefined; - } + this.logger.info(`"${name}" PID ${child.pid} ${code === 0 ? 'done' : `exit ${code}`}`); + this.child = undefined; }); } diff --git a/src/index.ts b/src/index.ts index 0a21453..a08d75d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,7 @@ import autoExecute, { NormalizeOption } from './plugins/auto-execute'; import { ExecuteMode, type CommonOptions } from './types/options'; import autoExternal from './plugins/auto-external'; import path from 'node:path'; -import { isString } from 'lodash-es'; +import { isBoolean, isString } from 'lodash-es'; const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)).toString()); @@ -77,7 +77,7 @@ const cli = cac('farmup'); cli.option( '--target [target]', - "target for output, default is node, support 'browser' | 'node' | 'node16' | 'node-legacy' | 'node-next' | 'browser-legacy' | 'browser-es2015' | 'browser-es2017' | 'browser-esnext'", + "target for output, default is node, support 'browser'、'node'、'node16'、'node-legacy'、'node-next'、'browser-legacy'、'browser-es2015'、'browser-es2017'、'browser-esnext'", ) .option('--mode [mode]', 'mode for build, default is development, choose one from "development" or "production"') .option('--minify', 'minify for output') @@ -85,11 +85,9 @@ cli.option( .option('--no-config', 'if farm.config.[ext] exists, it will be ignore') .option('--format [format]', 'choose one from "cjs" or "esm"') .option('--external [...external]', 'external') - .option('--watch [...files]', 'watch files', { default: false }) - .option('-w [...file]', 'watch files', { default: false }) + .option('-w, --watch [...files]', 'watch files', { default: false }) .option('--no-auto-external', 'if not found module, auto as external', { default: true }) - .option('--exec [file]', 'custom execute command') - .option('-e [file]', 'custom execute command'); + .option('-e, --exec [file]', 'custom execute command'); // biome-ignore lint/suspicious/noExplicitAny: async function commonOptionsFromArgs(args: Record): Promise> { @@ -102,8 +100,7 @@ async function commonOptionsFromArgs(args: Record): Promise): Promise (item === true ? undefined : item)) .filter(Boolean), diff --git a/src/plugins/auto-execute.ts b/src/plugins/auto-execute.ts index 28cea68..d2b13ed 100644 --- a/src/plugins/auto-execute.ts +++ b/src/plugins/auto-execute.ts @@ -2,7 +2,7 @@ import { Logger, type Resource, type JsPlugin } from '@farmfe/core'; import path from 'node:path'; import { type CommonOptions, ExecuteMode, type ResolvedCommonOptions } from '../types/options'; import { NormalizeOption } from '../config/normalize'; -import { CLI_NAME, logger } from '../config/constant'; +import { CLI_NAME, logger as defaultLogger } from '../config/constant'; import { Executer } from '../executer'; export { NormalizeOption, Executer }; @@ -24,21 +24,25 @@ function findOutputEntry(resource: Resource[], options: ResolvedCommonOptions) { } } -export default function autoExecute(options: CommonOptions = {}): JsPlugin { +export default function autoExecute(options: CommonOptions = {}, logger = defaultLogger): JsPlugin { let outputDir: string | undefined = undefined; let executer: Executer | null = null; - const normalize_option = new NormalizeOption(options, logger); + const normalizeOption = new NormalizeOption(options, logger); return { name: `${CLI_NAME}:execute`, priority: Number.NEGATIVE_INFINITY, async config(config) { - const normalizedOption = await normalize_option.config(config); - return normalizedOption; + return await normalizeOption.config(config); }, + configResolved(config) { outputDir = config.compilation?.output?.path; + const format = config.compilation?.output?.format || normalizeOption.options.format; + const targetEnv = config.compilation?.output?.targetEnv || normalizeOption.options.target; + const entry = Object.values(config.compilation?.input || normalizeOption.options.entry)[0]; + logger.debug(`[entry: ${entry}] [format: ${format}] [target: ${targetEnv}]`); }, configureCompiler(compiler) { @@ -57,13 +61,13 @@ export default function autoExecute(options: CommonOptions = {}): JsPlugin { } for (const entry of entries) { - compiler.addExtraWatchFile(entry, normalize_option.options.watchFiles); + compiler.addExtraWatchFile(entry, normalizeOption.options.watchFiles); } }, writeResources: { async executor(param) { - if (normalize_option.options.noExecute) { + if (normalizeOption.options.noExecute) { return; } @@ -72,7 +76,7 @@ export default function autoExecute(options: CommonOptions = {}): JsPlugin { return; } - const resourceEntry = findOutputEntry(Object.values(param.resourcesMap), normalize_option.options); + const resourceEntry = findOutputEntry(Object.values(param.resourcesMap), normalizeOption.options); if (!resourceEntry) { logger.error('not found output entry'); @@ -80,17 +84,19 @@ export default function autoExecute(options: CommonOptions = {}): JsPlugin { } // TODO: multiple entry - const execute_path = path.join(outputDir, resourceEntry!.name); + const executePath = path.join(outputDir, resourceEntry!.name); if (!executer) { - executer = new Executer(normalize_option.options.execute, logger, normalize_option.options); + executer = new Executer(normalizeOption.options.execute, logger, normalizeOption.options); } + const nameWithoutExt = path.parse(resourceEntry.name).name; + executer.execute( - execute_path, - resourceEntry.name, + executePath, + nameWithoutExt, new Logger({ - name: `${CLI_NAME}:${resourceEntry.name}`, + name: `${CLI_NAME}:${nameWithoutExt}`, }), ); }, diff --git a/src/util/file.ts b/src/util/file.ts index 31d9d00..606536c 100644 --- a/src/util/file.ts +++ b/src/util/file.ts @@ -1,4 +1,4 @@ -import { stat } from "fs-extra"; +import { stat } from 'fs-extra'; export const isExists = async (filename: string) => { try { diff --git a/src/util/log.ts b/src/util/log.ts new file mode 100644 index 0000000..93fb8a1 --- /dev/null +++ b/src/util/log.ts @@ -0,0 +1,7 @@ +export const trimEndLF = (str: string) => { + if (str.endsWith('\n')) { + return str.slice(0, -1); + } + + return str; +};