@aztt-cli/core 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `@aztt-cli/core`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const core = require('@aztt-cli/core');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/bin/index.mjs ADDED
@@ -0,0 +1,11 @@
1
+ #! /usr/bin/env node
2
+ import importLocal from "import-local";
3
+ import core from "../build/lib/index.js";
4
+ import { argv } from "process";
5
+ import log from "@aztt-cli/log";
6
+
7
+ if (importLocal(import.meta.url)) {
8
+ log.info("aztt-cli本地模式");
9
+ } else {
10
+ core(argv.slice(2));
11
+ }
package/lib/index.ts ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ import { homedir } from "os";
3
+ import path from "path";
4
+ import pkg from "../package.json" assert { type: "json" };
5
+ import log, { LogLevel } from "@aztt-cli/log";
6
+ import rootCheck from "root-check";
7
+ import { pathExistsSync } from "path-exists";
8
+ import dotenv from "dotenv";
9
+ import colors from "colors";
10
+ import { getNpmSemverVersion } from "@aztt-cli/get-npm-info";
11
+ import semver from "semver";
12
+ import { Command } from "commander";
13
+ import exec from "@aztt-cli/exec";
14
+
15
+ async function core() {
16
+ try {
17
+ await prepare();
18
+ registerCommand();
19
+ } catch (e) {
20
+ log.error(e);
21
+ if (program.opts()?.debug) {
22
+ console.log(e);
23
+ }
24
+ }
25
+ }
26
+
27
+ // 注册命令
28
+ const program = new Command();
29
+ function registerCommand() {
30
+ program
31
+ .name(Object.keys(pkg.bin)[0])
32
+ .version(pkg.version)
33
+ .usage("<command> [options]")
34
+ .option("-d --debug", "是否开启调试模式", false)
35
+ .option("-t --targetPath <targetPath>", "是否指定本地调试文件路径", "");
36
+
37
+ // init命令
38
+ program
39
+ .command("init [projectName]")
40
+ .description("初始化项目")
41
+ .option("-f --force", "是否强制初始化", false)
42
+ .action(exec);
43
+
44
+ // 监听debug选项,开启debug模式
45
+ program.on("option:debug", () => {
46
+ if (program.opts().debug) {
47
+ process.env.LOG_LEVEL = "debug";
48
+ } else {
49
+ process.env.LOG_LEVEL = "info";
50
+ }
51
+ log.setLevel(process.env.LOG_LEVEL as LogLevel);
52
+ });
53
+
54
+ // 监听targetPath选项
55
+ program.on("option:targetPath", () => {
56
+ process.env.CLI_TARGET_PATH = program.opts().targetPath; // 设置目标路径
57
+ });
58
+ // 监听未知命令行参数
59
+ program.on("command:*", (obj) => {
60
+ const availableCommands = program.commands.map((cmd) => cmd.name());
61
+ console.log(colors.red("未知的命令:" + obj[0]));
62
+ if (availableCommands.length > 0) {
63
+ console.log(colors.red("可用命令:" + availableCommands.join(",")));
64
+ }
65
+ });
66
+ // 解析命令行参数
67
+ program.parse(process.argv);
68
+
69
+ if (program.args && program.args.length < 1) {
70
+ program.outputHelp();
71
+ console.log();
72
+ }
73
+ }
74
+
75
+ async function prepare() {
76
+ checkPkgVersion(); // 确认当前脚手架的版本,防止使用了过时的版本
77
+ checkRoot(); // 检查是否是 root 用户,防止以 root 用户运行可能引起的权限问题
78
+ checkEnv(); // 检查 .env 文件并加载环境变量,确保脚手架的配置是正确的
79
+ checkUserHome(); // 检查用户主目录是否存在,确保可以访问到用户目录,避免路径问题
80
+ await checkGlobalVersion(); // 检测是否有可用的新版脚手架,提醒用户更新
81
+ }
82
+ // 检查cli最新版本
83
+ async function checkGlobalVersion() {
84
+ const currentVersion = pkg.version;
85
+ const npmName = pkg.name;
86
+ // 获取最新版本
87
+ const lastVersion = await getNpmSemverVersion(currentVersion, npmName);
88
+ if (lastVersion && semver.gt(lastVersion, currentVersion)) {
89
+ log.warn(
90
+ colors.yellow(`请手动更新 ${npmName},当前版本:${currentVersion},最新版本:${lastVersion}
91
+ 更新命令: npm install -g ${npmName}`)
92
+ );
93
+ }
94
+ }
95
+
96
+ function checkPkgVersion() {
97
+ log.info(`aztt-cli ${pkg.version}`);
98
+ }
99
+
100
+ function checkRoot() {
101
+ rootCheck();
102
+ }
103
+
104
+ function checkUserHome() {
105
+ if (!homedir() || !pathExistsSync(homedir())) {
106
+ throw new Error(colors.red("用户主目录不存在!"));
107
+ }
108
+ }
109
+
110
+ function checkEnv() {
111
+ const envPath = path.resolve(homedir(), ".env");
112
+ if (pathExistsSync(envPath)) {
113
+ dotenv.config({ path: envPath });
114
+ }
115
+ createDefaultConfig(); // 创建配置文件
116
+ }
117
+
118
+ function createDefaultConfig() {
119
+ const cliConfig = {
120
+ home: homedir(),
121
+ };
122
+ if (process.env.CLI_HOME) {
123
+ cliConfig["cliHome"] = path.join(homedir(), process.env.CLI_HOME);
124
+ } else {
125
+ cliConfig["cliHome"] = path.join(homedir(), ".aztt-cli");
126
+ }
127
+ process.env.CLI_HOME_PATH = cliConfig["cliHome"];
128
+ }
129
+
130
+ export default core;
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@aztt-cli/core",
3
+ "version": "0.0.0",
4
+ "description": "> TODO: description",
5
+ "type": "module",
6
+ "author": "za990326 <956267437@qq.com>",
7
+ "homepage": "",
8
+ "license": "ISC",
9
+ "main": "build/index.js",
10
+ "bin": {
11
+ "aztt-cli": "bin/index.mjs"
12
+ },
13
+ "directories": {
14
+ "lib": "lib",
15
+ "test": "__tests__"
16
+ },
17
+ "files": [
18
+ "lib"
19
+ ],
20
+ "publishConfig": {
21
+ "registry": "https://registry.npmjs.org/",
22
+ "access": "public"
23
+ },
24
+ "scripts": {
25
+ "test": "node ./__tests__/@aztt-cli/core.test.js",
26
+ "build": "tsc --watch"
27
+ },
28
+ "dependencies": {
29
+ "@aztt-cli/core": "file:",
30
+ "@aztt-cli/exec": "file:../../core/exec",
31
+ "@aztt-cli/get-npm-info": "file:../../utils/get-npm-info",
32
+ "@aztt-cli/log": "file:../../utils/log",
33
+ "colors": "^1.4.0",
34
+ "commander": "^13.1.0",
35
+ "dotenv": "^16.4.7",
36
+ "import-local": "^3.2.0",
37
+ "path-exists": "^5.0.0",
38
+ "root-check": "^2.0.0",
39
+ "semver": "^7.7.1"
40
+ },
41
+ "devDependencies": {
42
+ "@types/node": "^22.13.1",
43
+ "@types/root-check": "^1.0.2",
44
+ "@types/semver": "^7.5.8",
45
+ "typescript": "^5.7.3"
46
+ }
47
+ }