Skip to content
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
2 changes: 2 additions & 0 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 picocolors } from "picocolors";

const { bold, bgWhite, gray, bgCyan, bgYellow, bgRed, bgBlue, underline } =
picocolors;

Expand All @@ -12,22 +14,22 @@
) {
super(consoleOptions);
}
debug(...args: any[]) {

Check warning on line 17 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

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

Check warning on line 20 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

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

Check warning on line 23 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

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

Check warning on line 26 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

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

Check warning on line 29 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

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

Check warning on line 32 in src/log.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
super.log();
super.group(...args.map((arg) => bgBlue(bold(underline(arg)))));
}
Expand Down
102 changes: 54 additions & 48 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { dirname, sep, resolve as pathResolve } from "node:path";
import { dirname, resolve as pathResolve, sep } from "node:path";
import { fileURLToPath } from "node:url";

import { Command, Option } from "commander";
import { spawn } from "node:child_process";
import { existsSync } from "node:fs";
import { readdir, readFile, cp, rm, mkdir, writeFile } from "node:fs/promises";
import { cp, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";

import { Command, Option } from "commander";

import { Logger } from "./log.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const logger = new Logger();
logger.time("create-hexo");

const STARTER = "hexo-starter";
const STARTER_DIR = pathResolve(__dirname, `../${STARTER}/`);
const RM_FILES = [".git", ".github"];
Expand All @@ -20,50 +23,34 @@
type PM = "pnpm" | "npm" | "yarn" | "bun";

interface InitOptions {
blogName: string;
blogPath: string;
siteName: string;
sitePath: string;
packageManager: PM | "auto";
force: boolean;
}

let packageJson: any;

Check warning on line 32 in src/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
let starterVersion: string;
let initOptions: InitOptions = {
blogName: "hexo-site",
blogPath: "./",
siteName: "hexo-site",
sitePath: "./",
packageManager: "npm",
force: false,
};

const main = async () => {
[packageJson, starterVersion] = await pre();

init();
parseArgs();

checkInfo();
printInfo();

initOptions.force
? logger.warn("Running in force mode. It's dangerous!")
: await checkPath(initOptions.blogPath);
: await checkPath(initOptions.sitePath);

logger.group(`Copying \`${STARTER}\``);
const [voidd, pm] = await Promise.all([
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();
}),
checkPackageManager(),
]);
const [_, pm] = await Promise.all([copyStarter(), checkPackageManager()]);

Check warning on line 53 in src/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

'_' is assigned a value but never used
logger.groupEnd();

logger.group(`Installing packages via \`${pm}\``);
Expand Down Expand Up @@ -96,16 +83,17 @@
logger.groupEnd();
return Promise.all([packageJson, starterVersion]);
};
const init = () => {

const parseArgs = () => {
const program = new Command(packageJson.name)
.argument("[blog_directory]", "the folder that you want to load Hexo")
.usage(`[blog_directory]`)
.action((blog_directory: string) => {
const path = blog_directory
? (initOptions.blogPath = pathResolve(blog_directory))
: pathResolve(initOptions.blogPath);
initOptions.blogPath = path;
initOptions.blogName = path.split(sep).reverse()[0];
.argument("[site_directory]", "the folder that you want to load Hexo")
.usage(`[site_directory]`)
.action((site_directory: string) => {
const path = site_directory
? (initOptions.sitePath = pathResolve(site_directory))
: pathResolve(initOptions.sitePath);
initOptions.sitePath = path;
initOptions.siteName = path.split(sep).reverse()[0];
})
.addOption(
new Option(
Expand All @@ -132,14 +120,14 @@

const printUsage = () => {
logger.group("Usage: ");
logger.l(" npm init hexo [blog_directory]", "\n");
logger.l(` pnpm create hexo [blog_directory]`, "\n");
logger.l(" yarn create hexo [blog_directory]", "\n");
logger.l(" bun create hexo [blog_directory]", "\n");
logger.l(" npm init hexo [site_directory]", "\n");
logger.l(` pnpm create hexo [site_directory]`, "\n");
logger.l(" yarn create hexo [site_directory]", "\n");
logger.l(" bun create hexo [site_directory]", "\n");
logger.groupEnd();
};

const checkInfo = () => {
const printInfo = () => {
logger.group("Env Info");
logger.log("runtime path: ", process.argv[0]);
logger.log("runtime version: ", process.versions.node);
Expand All @@ -151,6 +139,23 @@
logger.groupEnd();
};

const copyStarter = () => {
return cp(STARTER_DIR, initOptions.sitePath, {
force: initOptions.force,
recursive: true,
})
.then(() => {
logger.log(`Copied \`${STARTER}\` to "${initOptions.sitePath}"`);
})
.catch((err) => {
logger.error("Copy failed: ", err);
process.exit(1);
})
.finally(() => {
logger.groupEnd();
});
};

const checkPath = (path: string) => {
return readdir(path)
.then((files) => {
Expand All @@ -160,11 +165,11 @@
);
process.exit(1);
} else {
logger.info(`Your hexo blog will be initialized in "${path}"`);
logger.info(`Your hexo site will be initialized in "${path}"`);
}
})
.catch((err) => {

Check warning on line 171 in src/main.ts

View workflow job for this annotation

GitHub Actions / ESLint

'err' is defined but never used
logger.info(`Your hexo blog will be initialized in "${path}"`);
logger.info(`Your hexo site will be initialized in "${path}"`);
});
};

Expand Down Expand Up @@ -196,7 +201,7 @@
const installPackage = (pm: string) => {
return new Promise((resolve, reject) => {
const child = spawn(pm, ["install"], {
cwd: initOptions.blogPath,
cwd: initOptions.sitePath,
shell: true,
});
child.stdout?.setEncoding("utf8");
Expand Down Expand Up @@ -224,11 +229,11 @@
};

const post = () => {
const ls: any[] = [];
const ls: Array<Promise<unknown>> = [];

RM_FILES.forEach((item) => {
ls.push(
rm(pathResolve(initOptions.blogPath, item), {
rm(pathResolve(initOptions.sitePath, item), {
force: true,
recursive: true,
})
Expand All @@ -242,7 +247,7 @@
});

ADD_FILES.forEach((item) => {
const file = pathResolve(initOptions.blogPath, item);
const file = pathResolve(initOptions.sitePath, item);
const dir = dirname(file);

ls.push(
Expand All @@ -263,8 +268,9 @@

return Promise.all(ls);
};

const end = async () => {
logger.group("Finshed!");
logger.group("Finished!");
logger.info("Enjoy yourself!", "\n");
logger.groupEnd();
logger.timeEnd("create-hexo");
Expand Down