@laomojituan/cli 1.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/cli.js +33 -0
- package/package.json +26 -0
- package/src/doctor.js +29 -0
- package/src/hello.js +7 -0
- package/src/install.js +44 -0
package/cli.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { Command } = require('commander');
|
|
4
|
+
const pkg = require('./package.json');
|
|
5
|
+
const install = require('./src/install');
|
|
6
|
+
const hello = require('./src/hello');
|
|
7
|
+
const doctor = require('./src/doctor');
|
|
8
|
+
|
|
9
|
+
const program = new Command();
|
|
10
|
+
|
|
11
|
+
program
|
|
12
|
+
.name('darkdemo')
|
|
13
|
+
.description('DarkDemo CLI —— 一个可被 npx 直接运行的最小 Node CLI 骨架')
|
|
14
|
+
.version(pkg.version);
|
|
15
|
+
|
|
16
|
+
program
|
|
17
|
+
.command('install')
|
|
18
|
+
.description('安装/初始化本工具:写配置文件、给出别名与补全提示')
|
|
19
|
+
.option('-f, --force', '覆盖已有配置,重新安装')
|
|
20
|
+
.action((options) => install(options, pkg.version));
|
|
21
|
+
|
|
22
|
+
program
|
|
23
|
+
.command('hello [name]')
|
|
24
|
+
.description('演示命令:打个招呼')
|
|
25
|
+
.option('-y, --yell', '大写输出')
|
|
26
|
+
.action(hello);
|
|
27
|
+
|
|
28
|
+
program
|
|
29
|
+
.command('doctor')
|
|
30
|
+
.description('检查运行环境与安装状态')
|
|
31
|
+
.action(doctor);
|
|
32
|
+
|
|
33
|
+
program.parse(process.argv);
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@laomojituan/cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Demo: a minimal npx-runnable Node CLI with an install subcommand",
|
|
5
|
+
"bin": {
|
|
6
|
+
"darkdemo": "cli.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"cli.js",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"start": "node cli.js",
|
|
14
|
+
"test": "node cli.js --version && node cli.js doctor"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"commander": "^12.1.0"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT"
|
|
26
|
+
}
|
package/src/doctor.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
function doctor() {
|
|
6
|
+
const configDir = path.join(os.homedir(), '.darkdemo');
|
|
7
|
+
const configFile = path.join(configDir, 'config.json');
|
|
8
|
+
|
|
9
|
+
const nodeMajor = Number(process.versions.node.split('.')[0]);
|
|
10
|
+
const checks = [
|
|
11
|
+
{ label: 'Node 版本 >= 18', pass: nodeMajor >= 18, detail: process.versions.node },
|
|
12
|
+
{ label: '配置目录存在', pass: fs.existsSync(configDir), detail: configDir },
|
|
13
|
+
{ label: '配置文件存在', pass: fs.existsSync(configFile), detail: configFile },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
let failed = 0;
|
|
17
|
+
for (const c of checks) {
|
|
18
|
+
console.log(`${c.pass ? '✔' : '✖'} ${c.label} (${c.detail})`);
|
|
19
|
+
if (!c.pass) failed += 1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!fs.existsSync(configFile)) {
|
|
23
|
+
console.log('\n提示:尚未安装,运行 npx @laomojituan/cli@latest install');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = doctor;
|
package/src/hello.js
ADDED
package/src/install.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const CONFIG_DIR = path.join(os.homedir(), '.darkdemo');
|
|
6
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
7
|
+
|
|
8
|
+
function install(options, version) {
|
|
9
|
+
const force = options.force === true;
|
|
10
|
+
|
|
11
|
+
const nodeMajor = Number(process.versions.node.split('.')[0]);
|
|
12
|
+
if (nodeMajor < 18) {
|
|
13
|
+
console.error(`✖ Node 版本过低:当前 ${process.versions.node},需要 >= 18`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (fs.existsSync(CONFIG_FILE) && !force) {
|
|
18
|
+
console.error(`✖ 已检测到配置文件:${CONFIG_FILE}`);
|
|
19
|
+
console.error(' 如需重新安装,请使用 --force');
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
24
|
+
|
|
25
|
+
const config = {
|
|
26
|
+
version,
|
|
27
|
+
installedAt: new Date().toISOString(),
|
|
28
|
+
node: process.versions.node,
|
|
29
|
+
platform: `${process.platform}/${process.arch}`,
|
|
30
|
+
};
|
|
31
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2) + '\n');
|
|
32
|
+
|
|
33
|
+
console.log('✔ 安装完成');
|
|
34
|
+
console.log(` 配置文件:${CONFIG_FILE}`);
|
|
35
|
+
console.log('');
|
|
36
|
+
console.log('可选:把下面这行加到 ~/.zshrc 或 ~/.bashrc,即可使用短命令:');
|
|
37
|
+
console.log(' alias darkdemo="npx @laomojituan/cli@latest"');
|
|
38
|
+
console.log('');
|
|
39
|
+
console.log('试试这些命令:');
|
|
40
|
+
console.log(' npx @laomojituan/cli@latest hello 世界');
|
|
41
|
+
console.log(' npx @laomojituan/cli@latest doctor');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = install;
|