Skip to content

Commit

Permalink
refactor(scaffold)!: replace chalk with node-style-text
Browse files Browse the repository at this point in the history
ref: es-tooling/module-replacements#198

BREAKING CHANGE: drop node18, node20 support
  • Loading branch information
northword committed Mar 7, 2025
1 parent 733524d commit 0efcbf7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
8 changes: 8 additions & 0 deletions packages/scaffold/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: "node16",
rollup: {
inlineDependencies: ["node-style-text"],
},
});
12 changes: 6 additions & 6 deletions packages/scaffold/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -46,7 +46,7 @@
"dist"
],
"engines": {
"node": ">=18.20.7"
"node": ">=22.8.0"
},
"scripts": {
"dev": "unbuild --stub",
Expand All @@ -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",
Expand All @@ -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"
}
}
18 changes: 9 additions & 9 deletions packages/scaffold/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand All @@ -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(", ")}.`);
}
});
}
Expand Down Expand Up @@ -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}"`);
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/scaffold/src/types/env.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,3 +10,5 @@ declare global {
}
}
}

export {};
22 changes: 11 additions & 11 deletions packages/scaffold/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -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: "",
};

Expand Down Expand Up @@ -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}`;
}

/**
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/scaffold/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src", "test"]
"include": ["src", "test", "src/types"]
}
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0efcbf7

Please sign in to comment.