From 0efcbf707a82dce61a63ce723e028ed39e8c1a77 Mon Sep 17 00:00:00 2001 From: Northword Date: Fri, 7 Mar 2025 22:02:59 +0800 Subject: [PATCH] refactor(scaffold)!: replace chalk with node-style-text ref: https://github.com/es-tooling/module-replacements/pull/198 BREAKING CHANGE: drop node18, node20 support --- packages/scaffold/build.config.ts | 8 ++++++++ packages/scaffold/package.json | 12 ++++++------ packages/scaffold/src/core/builder.ts | 18 +++++++++--------- packages/scaffold/src/types/env.d.ts | 4 +++- packages/scaffold/src/utils/logger.ts | 22 +++++++++++----------- packages/scaffold/tsconfig.json | 2 +- pnpm-lock.yaml | 11 ++++++++--- 7 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 packages/scaffold/build.config.ts diff --git a/packages/scaffold/build.config.ts b/packages/scaffold/build.config.ts new file mode 100644 index 0000000..1bf5572 --- /dev/null +++ b/packages/scaffold/build.config.ts @@ -0,0 +1,8 @@ +import { defineBuildConfig } from "unbuild"; + +export default defineBuildConfig({ + declaration: "node16", + rollup: { + inlineDependencies: ["node-style-text"], + }, +}); diff --git a/packages/scaffold/package.json b/packages/scaffold/package.json index 92feb15..ca2a0fa 100644 --- a/packages/scaffold/package.json +++ b/packages/scaffold/package.json @@ -24,20 +24,20 @@ ], "exports": { ".": { - "types": "./dist/index.d.ts", + "types": "./dist/index.d.mts", "import": "./dist/index.mjs" }, "./cli": { "import": "./dist/cli.mjs" }, "./vendor": { - "types": "./dist/vendor/index.d.ts", + "types": "./dist/vendor/index.d.mts", "import": "./dist/vendor/index.mjs" } }, "main": "./dist/index.mjs", "module": "./dist/index.mjs", - "types": "./dist/index.d.ts", + "types": "./dist/index.d.mts", "bin": { "zotero-plugin": "./bin/zotero-plugin.mjs" }, @@ -46,7 +46,7 @@ "dist" ], "engines": { - "node": ">=18.20.7" + "node": ">=22.8.0" }, "scripts": { "dev": "unbuild --stub", @@ -73,7 +73,6 @@ "adm-zip": "^0.5.16", "bumpp": "^10.0.3", "c12": "^3.0.2", - "chalk": "^5.4.1", "chokidar": "^4.0.3", "commander": "^13.1.0", "es-toolkit": "^1.32.0", @@ -89,6 +88,7 @@ "devDependencies": { "@commander-js/extra-typings": "^13.1.0", "@types/adm-zip": "^0.5.7", - "@types/fs-extra": "^11.0.4" + "@types/fs-extra": "^11.0.4", + "node-style-text": "^0.0.7" } } diff --git a/packages/scaffold/src/core/builder.ts b/packages/scaffold/src/core/builder.ts index 6e58d67..8b84085 100644 --- a/packages/scaffold/src/core/builder.ts +++ b/packages/scaffold/src/core/builder.ts @@ -6,10 +6,10 @@ import { readFile, writeFile } from "node:fs/promises"; import { basename, dirname, join } from "node:path"; import process from "node:process"; import AdmZip from "adm-zip"; -import chalk from "chalk"; import { escapeRegExp, toMerged } from "es-toolkit"; import { build as buildAsync } from "esbuild"; import { copy, emptyDir, move, outputFile, outputJSON, readJSON, writeJson } from "fs-extra/esm"; +import styleText from "node-style-text"; import { glob } from "tinyglobby"; import { generateHash } from "../utils/crypto.js"; import { PrefsManager, renderPluginPrefsDts } from "../utils/prefs-manager.js"; @@ -35,7 +35,7 @@ export default class Build extends Base { const t = new Date(); this.buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); this.logger.info( - `Building version ${chalk.blue(version)} to ${chalk.blue(dist)} at ${chalk.blue(this.buildTime)} in ${chalk.blue(process.env.NODE_ENV)} mode.`, + `Building version ${styleText.blue(version)} to ${styleText.blue(dist)} at ${styleText.blue(this.buildTime)} in ${styleText.blue(process.env.NODE_ENV)} mode.`, ); await this.ctx.hooks.callHook("build:init", this.ctx); @@ -212,14 +212,14 @@ export default class Build extends Base { } if (!allMessages.has(attrVal)) { - this.logger.warn(`HTML data-i10n-id '${chalk.blue(attrVal)}' in ${chalk.gray(htmlPath)} do not exist in any FTL message, skip to namespace it.`); + this.logger.warn(`HTML data-i10n-id '${styleText.blue(attrVal)}' in ${styleText.gray(htmlPath)} do not exist in any FTL message, skip to namespace it.`); continue; } messagesInHTML.add(attrVal); const namespacedAttr = `${namespace}-${attrVal}`; htmlContent = htmlContent.replace(matched, `${attrKey}="${namespacedAttr}"`); - this.logger.debug(`HTML data-i10n-id '${chalk.blue(attrVal)}' in ${chalk.gray(htmlPath)} is namespaced to ${chalk.blue(namespacedAttr)}.`); + this.logger.debug(`HTML data-i10n-id '${styleText.blue(attrVal)}' in ${styleText.gray(htmlPath)} is namespaced to ${styleText.blue(namespacedAttr)}.`); } if (build.fluent.prefixFluentMessages) @@ -244,7 +244,7 @@ export default class Build extends Base { .map(([locale]) => locale); if (missingLocales.length > 0) { - this.logger.warn(`HTML data-l10n-id '${chalk.blue(messageInHTML)}' is missing in locales: ${missingLocales.join(", ")}.`); + this.logger.warn(`HTML data-l10n-id '${styleText.blue(messageInHTML)}' is missing in locales: ${missingLocales.join(", ")}.`); } }); } @@ -291,20 +291,20 @@ export default class Build extends Base { for (const match of matchs) { const [matched, key] = match; if (key.startsWith(prefix)) { - this.logger.debug(`Pref key '${chalk.blue(key)}' is already starts with '${prefix}', skip prefixing it.`); + this.logger.debug(`Pref key '${styleText.blue(key)}' is already starts with '${prefix}', skip prefixing it.`); continue; } else if (key.startsWith("extensions.")) { - this.logger.warn(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} starts with 'extensions.' but not '${chalk.blue(prefix)}', skip prefixing it.`); + this.logger.warn(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} starts with 'extensions.' but not '${styleText.blue(prefix)}', skip prefixing it.`); continue; } else if (!(key in prefsWithPrefix) && !(key in prefsWithoutPrefix)) { - this.logger.warn(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} is not found in prefs.js, skip prefixing it.`); + this.logger.warn(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} is not found in prefs.js, skip prefixing it.`); continue; } else { const prefixed = `${prefix}.${key}`; - this.logger.debug(`Pref key '${chalk.blue(key)}' in ${chalk.gray(path)} is prefixed to ${chalk.blue(prefixed)}.`); + this.logger.debug(`Pref key '${styleText.blue(key)}' in ${styleText.gray(path)} is prefixed to ${styleText.blue(prefixed)}.`); content = content.replace(matched, `preference="${prefixed}"`); } } diff --git a/packages/scaffold/src/types/env.d.ts b/packages/scaffold/src/types/env.d.ts index e86313c..417028c 100644 --- a/packages/scaffold/src/types/env.d.ts +++ b/packages/scaffold/src/types/env.d.ts @@ -1,7 +1,7 @@ declare global { namespace NodeJS { interface ProcessEnv { - NODE_ENV: "development" | "peoduction"; + NODE_ENV: "development" | "production" | "test"; GITHUB_TOKEN?: string; ZOTERO_PLUGIN_ZOTERO_BIN_PATH?: string; ZOTERO_PLUGIN_PROFILE_PATH?: string; @@ -10,3 +10,5 @@ declare global { } } } + +export {}; diff --git a/packages/scaffold/src/utils/logger.ts b/packages/scaffold/src/utils/logger.ts index 602488e..6981f65 100644 --- a/packages/scaffold/src/utils/logger.ts +++ b/packages/scaffold/src/utils/logger.ts @@ -1,7 +1,7 @@ import process from "node:process"; import readline from "node:readline"; -import chalk from "chalk"; import { isPlainObject } from "es-toolkit"; +import styleText from "node-style-text"; import { isDebug } from "std-env"; /** @@ -19,13 +19,13 @@ export type LogLevelType = keyof typeof LOG_LEVEL; // Configuration constants const SYMBOLS = { - SUCCESS: chalk.green("✔"), - INFO: chalk.blue("ℹ"), - FAIL: chalk.red("✖"), - TIP: chalk.blue("→"), - ERROR: chalk.bgRed(" ERROR "), - WARN: chalk.bgYellow(" WARN "), - DEBUG: chalk.grey("⚙"), + SUCCESS: styleText.green("✔"), + INFO: styleText.blue("ℹ"), + FAIL: styleText.red("✖"), + TIP: styleText.blue("→"), + ERROR: styleText.bgRed(" ERROR "), + WARN: styleText.bgYellow(" WARN "), + DEBUG: styleText.grey("⚙"), NONE: "", }; @@ -136,7 +136,7 @@ export class Logger { } private formatError(error: Error): string { - return `${chalk.red(error.name)}: ${chalk.red(error.message)}\n${error.stack}`; + return `${styleText.red(error.name)}: ${styleText.red(error.message)}\n${error.stack}`; } /** @@ -195,8 +195,8 @@ export class Logger { this.logInternal(content, LOG_METHODS_CONFIG.fail, options); } - public ready(content: unknown): void { - this.logInternal(chalk.green(content), LOG_METHODS_CONFIG.success); + public ready(content: string): void { + this.logInternal(styleText.green(content), LOG_METHODS_CONFIG.success); } public clear(): void { diff --git a/packages/scaffold/tsconfig.json b/packages/scaffold/tsconfig.json index e7b6a58..345ab08 100644 --- a/packages/scaffold/tsconfig.json +++ b/packages/scaffold/tsconfig.json @@ -11,5 +11,5 @@ "esModuleInterop": true, "skipLibCheck": true }, - "include": ["src", "test"] + "include": ["src", "test", "src/types"] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b1b4289..4a01fa3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -104,9 +104,6 @@ importers: c12: specifier: ^3.0.2 version: 3.0.2 - chalk: - specifier: ^5.4.1 - version: 5.4.1 chokidar: specifier: ^4.0.3 version: 4.0.3 @@ -153,6 +150,9 @@ importers: '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 + node-style-text: + specifier: ^0.0.7 + version: 0.0.7 packages/scaffold/test/e2e/fixtures: {} @@ -3015,6 +3015,9 @@ packages: node-releases@2.0.19: resolution: {integrity: sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==} + node-style-text@0.0.7: + resolution: {integrity: sha512-Nl9GeP2qEG8NM7rgjSi6nhuBdiciYRL5eHKn25+UlpclBWYRzMVfSyhB5vR87uOcSvWGSwpjucLl8PGiGjs7XQ==} + normalize-package-data@6.0.2: resolution: {integrity: sha512-V6gygoYb/5EmNI+MEGrWkC+e6+Rr7mTmfHrxDbLzxQogBkgzo76rkok0Am6thgSF7Mv2nLOajAJj5vDJZEFn7g==} engines: {node: ^16.14.0 || >=18.0.0} @@ -7117,6 +7120,8 @@ snapshots: node-releases@2.0.19: {} + node-style-text@0.0.7: {} + normalize-package-data@6.0.2: dependencies: hosted-git-info: 7.0.2