Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce the installation size #7

Merged
merged 9 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -10,24 +12,24 @@
) {
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 { 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 All @@ -25,7 +25,7 @@
force: boolean;
}

let packageJson: any;

Check warning on line 28 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
let starterVersion: string;
let initOptions: InitOptions = {
blogName: "hexo-site",
Expand All @@ -46,13 +46,17 @@
: await checkPath(initOptions.blogPath);

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 @@ -158,7 +162,7 @@
logger.info(`Your hexo blog will be initialized in "${path}"`);
}
})
.catch((err) => {

Check warning on line 165 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint

'err' is defined but never used
logger.info(`Your hexo blog will be initialized in "${path}"`);
});
};
Expand Down Expand Up @@ -189,34 +193,39 @@
};

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 @@
});

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 @@
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