Skip to content

Commit

Permalink
refactor: reduce the installation size (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: uiolee <22849383+uiolee@users.noreply.github.com>
  • Loading branch information
SukkaW and uiolee authored Apr 14, 2024
1 parent e5d779c commit 7dc1913
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 74 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@
"tsbw": "tsc -b -v -w"
},
"dependencies": {
"chalk": "^5.3.0",
"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",
Expand Down
54 changes: 4 additions & 50 deletions pnpm-lock.yaml

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

16 changes: 9 additions & 7 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Console, ConsoleConstructorOptions } from "node:console";
import { default as chalk } from "chalk";
import { default as picocolors } from "picocolors";
const { bold, bgWhite, gray, bgCyan, bgYellow, bgRed, bgBlue, underline } =
picocolors;

class Logger extends Console {
constructor(
Expand All @@ -11,23 +13,23 @@ class Logger extends Console {
super(consoleOptions);
}
debug(...args: any[]) {

Check warning on line 15 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.debug(chalk.bgWhite.bold("DEBUG"), ...args);
super.debug(bgWhite(bold("DEBUG")), ...args);
}
log(...args: any[]) {

Check warning on line 18 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.log(chalk.gray.bold("LOG "), ...args);
super.log(gray(bold("LOG ")), ...args);
}
info(...args: any[]) {

Check warning on line 21 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.info(chalk.bgCyan.bold("INFO "), ...args);
super.info(bgCyan(bold("INFO ")), ...args);
}
warn(...args: any[]) {

Check warning on line 24 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.warn(chalk.bgYellow.bold("WARN "), ...args);
super.warn(bgYellow(bold("WARN ")), ...args);
}
error(...args: any[]) {

Check warning on line 27 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.error(chalk.bgRed.bold("ERROR"), ...args);
super.error(bgRed(bold("ERROR")), ...args);
}
group(...args: any[]): void {

Check warning on line 30 in src/log.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
super.log();
super.group(chalk.bgBlue.bold.underline(...args));
super.group(...args.map((arg) => bgBlue(bold(underline(arg)))));
}
t = super.trace;
d = super.debug;
Expand Down
45 changes: 31 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -47,12 +47,16 @@ const main = async () => {

logger.group(`Copying \`${STARTER}\``);
const [voidd, pm] = await Promise.all([

Check warning on line 49 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint

'voidd' is assigned a value but never used
copy(STARTER_DIR, initOptions.blogPath)
cp(STARTER_DIR, initOptions.blogPath, {
force: initOptions.force,
recursive: true,
})
.then(() => {
logger.log(`Copied \`${STARTER}\` to "${initOptions.blogPath}"`);
})
.catch((err) => {
logger.error("Copy failed: ", err);
process.exit(1);
})
.finally(() => {
logger.groupEnd();
Expand Down Expand Up @@ -189,34 +193,39 @@ const checkPackageManager = (): Promise<PM> => {
};

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 = () => {
const ls: any[] = [];

Check warning on line 221 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

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!`);
})
Expand All @@ -227,8 +236,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!`);
})
Expand All @@ -241,7 +258,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");
Expand Down

0 comments on commit 7dc1913

Please sign in to comment.