文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
packages/cli
调整 package.json
{
"name": "@oweqian/cli",
"version": "0.0.0",
"description": "oweqian cli",
// ...
"main": "lib/index.js",
"bin": {
"cli-oweqian": "bin/cli.js"
},
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib",
"bin"
],
"scripts": {
"test": "jest"
},
"dependencies": {
"@oweqian/commit": "^0.0.0",
"@oweqian/init": "^0.0.0",
"@oweqian/install": "^0.0.0",
"@oweqian/lint": "^0.0.0",
"@oweqian/utils": "^0.0.0",
"chalk": "^5.1.0",
"commander": "^9.4.1",
"dirname-filename-esm": "^1.1.1",
"fs-extra": "^10.1.0",
"import-local": "^3.1.0",
"semver": "^7.3.8"
},
"devDependencies": {
"@babel/core": "^7.19.3",
"@babel/preset-env": "^7.19.4",
"execa": "^6.1.0",
"jest": "^29.1.2"
},
"type": "module"
}
可执行文件
#!/usr/bin/env node
import importLocal from "import-local";
import { filename } from "dirname-filename-esm";
import { log } from "@oweqian/utils";
import entry from "../lib/index.js";
const __filename = filename(import.meta);
// 优先加载本地脚手架版本
if (importLocal(__filename)) {
log.info("cli", "使用本次 cli-oweqian 版本");
} else {
entry(process.argv.slice(2));
}
入口文件
import createInitCommand from "@oweqian/init";
import createInstallCommand from "@oweqian/install";
import createLintCommand from "@oweqian/lint";
import createCommitCommand from "@oweqian/commit";
import createCLI from "./createCLI.js";
import "./exception.js";
export default function (args) {
const program = createCLI();
// 项目创建工具
createInitCommand(program);
// 源码下载工具
createInstallCommand(program);
// 代码规范检查工具
createLintCommand(program);
// 代码提交工具
createCommitCommand(program);
program.parse(process.argv);
}
捕获异常日志
import { printErrorLog } from "@oweqian/utils";
// 处理未捕获的异常
process.on("uncaughtException", (e) => printErrorLog(e, "error"));
// 处理未处理的 Promise Reject
process.on("unhandledRejection", (e) => printErrorLog(e, "promise"));
命令行基础结构
import path from "node:path";
import { program } from "commander";
import { dirname } from "dirname-filename-esm";
import semver from "semver";
import chalk from "chalk";
import fse from "fs-extra";
import { log } from "@oweqian/utils";
// 获取当前模块的目录路径
const __dirname = dirname(import.meta);
// 获取 package.json 文件路径
const pkgPath = path.resolve(__dirname, "../package.json");
// 读取 package.json 文件
const pkg = fse.readJSONSync(pkgPath);
// 定义最低 Node.js 版本
const LOWEST_NODE_VERSION = "14.0.0";
// 检查 Node.js 版本
function checkNodeVersion() {
// 使用 semver 检查当前运行的 Node.js 版本是否满足最低要求
if (!semver.gte(process.version, LOWEST_NODE_VERSION)) {
throw new Error(
chalk.red(
`cli-oweqian 需要安装 ${LOWEST_NODE_VERSION} 以上版本的 Node.js`
)
);
}
}
function preAction() {
// 检查 Node 版本
checkNodeVersion();
}
function createCLI() {
log.info("version", pkg.version);
program
.name(Object.keys(pkg.bin)[0])
.usage("<command> [options]")
.version(pkg.version)
.option("-d, --debug", "是否开启调试模式", false)
.hook("preAction", preAction);
// 处理 debug 选项
program.on("option:debug", function () {
if (program.opts().debug) {
log.verbose("debug", "launch debug mode!");
}
});
// 处理未知命令
program.on("command:*", function (obj) {
log.error("未知的命令:" + obj[0]);
});
return program;
}
export default createCLI;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论