From 1267166570a8ee6091c49273339536aa731f285e Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 9 Apr 2024 17:17:29 +0800 Subject: [PATCH 1/9] refactor: replace `chalk` with `picocolors` --- package.json | 4 ++-- pnpm-lock.yaml | 12 +++--------- src/log.ts | 14 +++++++------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 1690db9..4de8a66 100644 --- a/package.json +++ b/package.json @@ -46,10 +46,10 @@ "tsbw": "tsc -b -v -w" }, "dependencies": { - "chalk": "^5.3.0", "commander": "^12.0.0", "execa": "^8.0.1", - "fs-extra": "^11.2.0" + "fs-extra": "^11.2.0", + "picocolors": "^1.0.0" }, "devDependencies": { "@jest/globals": "^29.7.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 59f9ece..a373d8a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,9 +5,6 @@ settings: excludeLinksFromLockfile: false dependencies: - chalk: - specifier: ^5.3.0 - version: 5.3.0 commander: specifier: ^12.0.0 version: 12.0.0 @@ -17,6 +14,9 @@ dependencies: fs-extra: specifier: ^11.2.0 version: 11.2.0 + picocolors: + specifier: ^1.0.0 + version: 1.0.0 devDependencies: '@jest/globals': @@ -1624,11 +1624,6 @@ packages: supports-color: 7.2.0 dev: true - /chalk@5.3.0: - resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==} - engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} - dev: false - /char-regex@1.0.2: resolution: {integrity: sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==} engines: {node: '>=10'} @@ -3665,7 +3660,6 @@ packages: /picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} - dev: true /picomatch@2.3.1: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} diff --git a/src/log.ts b/src/log.ts index dffe983..ca54a02 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,5 +1,5 @@ import { Console, ConsoleConstructorOptions } from "node:console"; -import { default as chalk } from "chalk"; +import { bold, bgWhite, gray, bgCyan, bgYellow, bgRed, bgBlue, underline } from "picocolors"; class Logger extends Console { constructor( @@ -11,23 +11,23 @@ class Logger extends Console { super(consoleOptions); } debug(...args: any[]) { - super.debug(chalk.bgWhite.bold("DEBUG"), ...args); + super.debug(bgWhite(bold("DEBUG")), ...args); } log(...args: any[]) { - super.log(chalk.gray.bold("LOG "), ...args); + super.log(gray(bold("LOG ")), ...args); } info(...args: any[]) { - super.info(chalk.bgCyan.bold("INFO "), ...args); + super.info(bgCyan(bold("INFO ")), ...args); } warn(...args: any[]) { - super.warn(chalk.bgYellow.bold("WARN "), ...args); + super.warn(bgYellow(bold("WARN ")), ...args); } error(...args: any[]) { - super.error(chalk.bgRed.bold("ERROR"), ...args); + super.error(bgRed(bold("ERROR")), ...args); } group(...args: any[]): void { super.log(); - super.group(chalk.bgBlue.bold.underline(...args)); + super.group(args.map(arg => bgBlue(bold(underline(arg))))); } t = super.trace; d = super.debug; From 831d214fbc5d843f8877595ea3dc19ad2119e2a1 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 9 Apr 2024 17:26:25 +0800 Subject: [PATCH 2/9] refactor: replace `fs-extra` --- package.json | 2 -- pnpm-lock.yaml | 42 +----------------------------------------- src/main.ts | 18 +++++++++++++----- 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 4de8a66..1cdd03d 100644 --- a/package.json +++ b/package.json @@ -48,13 +48,11 @@ "dependencies": { "commander": "^12.0.0", "execa": "^8.0.1", - "fs-extra": "^11.2.0", "picocolors": "^1.0.0" }, "devDependencies": { "@jest/globals": "^29.7.0", "@jest/types": "^29.6.3", - "@types/fs-extra": "^11.0.4", "@types/node": "^20.11.17", "@typescript-eslint/eslint-plugin": "^6.21.0", "@typescript-eslint/parser": "^6.21.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a373d8a..4c43784 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,9 +11,6 @@ dependencies: execa: specifier: ^8.0.1 version: 8.0.1 - fs-extra: - specifier: ^11.2.0 - version: 11.2.0 picocolors: specifier: ^1.0.0 version: 1.0.0 @@ -25,9 +22,6 @@ devDependencies: '@jest/types': specifier: ^29.6.3 version: 29.6.3 - '@types/fs-extra': - specifier: ^11.0.4 - version: 11.0.4 '@types/node': specifier: ^20.11.17 version: 20.11.17 @@ -1056,13 +1050,6 @@ packages: '@babel/types': 7.23.9 dev: true - /@types/fs-extra@11.0.4: - resolution: {integrity: sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==} - dependencies: - '@types/jsonfile': 6.1.4 - '@types/node': 20.11.17 - dev: true - /@types/graceful-fs@4.1.9: resolution: {integrity: sha512-olP3sd1qOEe5dXTSaFvQG+02VdRXcdytWLAZsAq1PecU8uqQAhkrnbli7DagjtXKW/Bl7YJbUsa8MPcuc8LHEQ==} dependencies: @@ -1089,12 +1076,6 @@ packages: resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} dev: true - /@types/jsonfile@6.1.4: - resolution: {integrity: sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ==} - dependencies: - '@types/node': 20.11.17 - dev: true - /@types/node@20.11.17: resolution: {integrity: sha512-QmgQZGWu1Yw9TDyAP9ZzpFJKynYNeOvwMJmaxABfieQoVoiVOS6MN1WSpqpRcbeA5+RW82kraAVxCCJg+780Qw==} dependencies: @@ -2308,15 +2289,6 @@ packages: map-cache: 0.2.2 dev: true - /fs-extra@11.2.0: - resolution: {integrity: sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==} - engines: {node: '>=14.14'} - dependencies: - graceful-fs: 4.2.11 - jsonfile: 6.1.0 - universalify: 2.0.1 - dev: false - /fs.realpath@1.0.0: resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==} dev: true @@ -2421,6 +2393,7 @@ packages: /graceful-fs@4.2.11: resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==} + dev: true /graphemer@1.4.0: resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==} @@ -3251,14 +3224,6 @@ packages: hasBin: true dev: true - /jsonfile@6.1.0: - resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} - dependencies: - universalify: 2.0.1 - optionalDependencies: - graceful-fs: 4.2.11 - dev: false - /keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} dependencies: @@ -4215,11 +4180,6 @@ packages: set-value: 2.0.1 dev: true - /universalify@2.0.1: - resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==} - engines: {node: '>= 10.0.0'} - dev: false - /unset-value@1.0.0: resolution: {integrity: sha512-PcA2tsuGSF9cnySLHTLSh2qrQiJ70mn+r+Glzxv2TWZblxsxCC52BDlZoPCsz7STd9pN7EZetkWZBAvk4cgZdQ==} engines: {node: '>=0.10.0'} diff --git a/src/main.ts b/src/main.ts index 1c67788..6ee33c2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,8 +3,8 @@ import { fileURLToPath } from "node:url"; import { Command, Option } from "commander"; import { execa } from "execa"; -import { copy, ensureFile, remove } from "fs-extra"; -import { readdir, readFile } from "fs/promises"; +import { existsSync } from "node:fs"; +import { readdir, readFile, cp, rm, mkdir, writeFile } from "node:fs/promises"; import { Logger } from "./log.js"; const __filename = fileURLToPath(import.meta.url); @@ -47,7 +47,7 @@ const main = async () => { logger.group(`Copying \`${STARTER}\``); const [voidd, pm] = await Promise.all([ - copy(STARTER_DIR, initOptions.blogPath) + cp(STARTER_DIR, initOptions.blogPath) .then(() => { logger.log(`Copied \`${STARTER}\` to "${initOptions.blogPath}"`); }) @@ -216,7 +216,7 @@ const post = () => { RM_FILES.forEach((item) => { ls.push( - remove(pathResolve(initOptions.blogPath, item)) + rm(pathResolve(initOptions.blogPath, item), { force: true, recursive: true }) .then(() => { logger.log(`remove "${item}" success!`); }) @@ -227,8 +227,16 @@ const post = () => { }); ADD_FILES.forEach((item) => { + const file = pathResolve(initOptions.blogPath, item); + const dir = dirname(file); + ls.push( - ensureFile(pathResolve(initOptions.blogPath, item)) + mkdir(dir, { recursive: true }) + .then(() => { + if (!existsSync(file)) { + return writeFile(file, ""); + } + }) .then(() => { logger.log(`add "${item}" success!`); }) From 884d3e19330f1e990c08dd559d28d2f40f579bf6 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:18:31 +0800 Subject: [PATCH 3/9] fix picocolors --- src/log.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/log.ts b/src/log.ts index ca54a02..3cc25ac 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,5 +1,14 @@ import { Console, ConsoleConstructorOptions } from "node:console"; -import { bold, bgWhite, gray, bgCyan, bgYellow, bgRed, bgBlue, underline } from "picocolors"; +import { default as pc } from "picocolors"; + +const bgWhite = pc.bgWhite; +const gray = pc.gray; +const bgCyan = pc.bgCyan; +const bgYellow = pc.bgYellow; +const bgRed = pc.bgRed; +const bgBlue = pc.bgBlue; +const bold = pc.bold; +const underline = pc.underline; class Logger extends Console { constructor( @@ -27,7 +36,7 @@ class Logger extends Console { } group(...args: any[]): void { super.log(); - super.group(args.map(arg => bgBlue(bold(underline(arg))))); + super.group(...args.map((arg) => bgBlue(bold(underline(arg))))); } t = super.trace; d = super.debug; From a487c5bf41f0846bb380e454bb5bf9d722be7839 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:27:55 +0800 Subject: [PATCH 4/9] fix cp directory --- src/main.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 6ee33c2..f22abd9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -47,7 +47,10 @@ const main = async () => { logger.group(`Copying \`${STARTER}\``); const [voidd, pm] = await Promise.all([ - cp(STARTER_DIR, initOptions.blogPath) + cp(STARTER_DIR, initOptions.blogPath, { + force: initOptions.force, + recursive: true, + }) .then(() => { logger.log(`Copied \`${STARTER}\` to "${initOptions.blogPath}"`); }) From 02a6d26ba70a27de27e7c43469fc47c006dd4b55 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:28:37 +0800 Subject: [PATCH 5/9] exit on cp fail --- src/main.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.ts b/src/main.ts index f22abd9..e29c34d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,6 +56,7 @@ const main = async () => { }) .catch((err) => { logger.error("Copy failed: ", err); + process.exit(1); }) .finally(() => { logger.groupEnd(); From 58d8608bc84c9186646b3b96bdfdd9b257d6a35b Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:30:46 +0800 Subject: [PATCH 6/9] apply eslint --- src/main.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index e29c34d..3487601 100644 --- a/src/main.ts +++ b/src/main.ts @@ -220,7 +220,10 @@ const post = () => { RM_FILES.forEach((item) => { ls.push( - rm(pathResolve(initOptions.blogPath, item), { force: true, recursive: true }) + rm(pathResolve(initOptions.blogPath, item), { + force: true, + recursive: true, + }) .then(() => { logger.log(`remove "${item}" success!`); }) From 8e9b86c9b7d699d2c810c258855f9bd085c0c0e9 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:31:02 +0800 Subject: [PATCH 7/9] improve log output --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 3487601..637cce5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -256,7 +256,7 @@ const post = () => { return Promise.all(ls); }; const end = async () => { - logger.group("Finshed!", "\n"); + logger.group("Finshed!"); logger.info("Enjoy yourself!", "\n"); logger.groupEnd(); logger.timeEnd("create-hexo"); From 4d55902730a310b6b1ac35371a7607debdf17275 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:31:17 +0800 Subject: [PATCH 8/9] rename childprocess --- src/main.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index 637cce5..f05be9f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -193,26 +193,28 @@ const checkPackageManager = (): Promise => { }; const installPackage = (pm: string) => { - const cp = execa(pm, ["install"], { cwd: initOptions.blogPath }); - cp.stdout?.setEncoding("utf8"); - cp.stdout?.on("data", (data) => { + const child = execa(pm, ["install"], { + cwd: initOptions.blogPath, + }); + child.stdout?.setEncoding("utf8"); + child.stdout?.on("data", (data) => { logger.log(pm, data); }); - cp.stderr?.setEncoding("utf8"); - cp.stderr?.on("data", function (data) { + child.stderr?.setEncoding("utf8"); + child.stderr?.on("data", function (data) { logger.warn(pm, data); }); - cp.on("error", (err) => { + child.on("error", (err) => { logger.error("Install error: ", err); }); - cp.on("close", (code) => { + child.on("close", (code) => { if (code !== 0) { logger.error("Install error: ", code); } else { logger.info("Install package finshed"); } }); - return cp; + return child; }; const post = () => { From d6aedcc885e847a9e01ccaca05d167331cade0bc Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Apr 2024 16:05:26 +0800 Subject: [PATCH 9/9] fix import picocolors --- src/log.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/log.ts b/src/log.ts index 3cc25ac..d1dfa62 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,14 +1,7 @@ import { Console, ConsoleConstructorOptions } from "node:console"; -import { default as pc } from "picocolors"; - -const bgWhite = pc.bgWhite; -const gray = pc.gray; -const bgCyan = pc.bgCyan; -const bgYellow = pc.bgYellow; -const bgRed = pc.bgRed; -const bgBlue = pc.bgBlue; -const bold = pc.bold; -const underline = pc.underline; +import { default as picocolors } from "picocolors"; +const { bold, bgWhite, gray, bgCyan, bgYellow, bgRed, bgBlue, underline } = + picocolors; class Logger extends Console { constructor(