axhub-make 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/bin/index.js +95 -91
  2. package/package.json +1 -1
package/bin/index.js CHANGED
@@ -2,90 +2,108 @@
2
2
 
3
3
  const fs = require('fs-extra');
4
4
  const path = require('path');
5
+ const os = require('os');
5
6
  const { execFileSync, execSync } = require('child_process');
6
- const inquirer = require('inquirer');
7
7
  const chalk = require('chalk');
8
8
 
9
- async function run() {
10
- console.log(chalk.magenta.bold('\n🚀 Axhub Make - 项目初始化\n'));
11
-
12
- // -----------------------------
13
- // 1. 参数解析
14
- // -----------------------------
15
- const args = process.argv.slice(2);
16
- let templateGit = '';
9
+ // -----------------------------
10
+ // 参数解析(无依赖,稳定)
11
+ // -----------------------------
12
+ function parseArgs(argv) {
13
+ const args = argv.slice(2);
14
+ const opts = {
15
+ dir: '.',
16
+ template: 'https://github.com/lintendo/Axhub-Make.git',
17
+ install: true,
18
+ start: true,
19
+ force: false,
20
+ pm: null
21
+ };
17
22
 
18
23
  for (let i = 0; i < args.length; i++) {
19
- if (args[i] === '-t' || args[i] === '--template') {
20
- templateGit = args[i + 1];
21
- break;
24
+ const a = args[i];
25
+
26
+ if (!a.startsWith('-') && opts.dir === '.') {
27
+ opts.dir = a;
28
+ continue;
22
29
  }
23
- }
24
30
 
25
- // -----------------------------
26
- // 2. 交互
27
- // -----------------------------
28
- const questions = [
29
- {
30
- type: 'input',
31
- name: 'projectName',
32
- message: '请输入项目名称:',
33
- default: 'my-app'
31
+ if (a === '-t' || a === '--template') {
32
+ opts.template = args[++i];
33
+ continue;
34
34
  }
35
- ];
36
-
37
- if (!templateGit) {
38
- questions.push({
39
- type: 'input',
40
- name: 'customTemplateGit',
41
- message: '请输入 Git 仓库地址:',
42
- default: 'https://github.com/lintendo/Axhub-Make.git'
43
- });
35
+
36
+ if (a === '--no-install') opts.install = false;
37
+ if (a === '--no-start') opts.start = false;
38
+ if (a === '--force') opts.force = true;
39
+ if (a === '--pm') opts.pm = args[++i];
44
40
  }
45
41
 
46
- const answers = await inquirer.prompt(questions);
42
+ return opts;
43
+ }
44
+
45
+ async function run() {
46
+ console.log(chalk.magenta.bold('\n🚀 Axhub Make\n'));
47
+
48
+ const opts = parseArgs(process.argv);
49
+
50
+ const isCurrentDir =
51
+ opts.dir === '.' || opts.dir === './' || opts.dir === '';
52
+
53
+ const targetDir = isCurrentDir
54
+ ? process.cwd()
55
+ : path.resolve(process.cwd(), opts.dir);
47
56
 
48
- const projectName = answers.projectName.trim();
49
- templateGit = templateGit || answers.customTemplateGit;
50
- const targetDir = path.resolve(process.cwd(), projectName);
57
+ const projectName = path.basename(targetDir);
51
58
 
52
59
  // -----------------------------
53
- // 3. 目录检查
60
+ // 当前目录检测
54
61
  // -----------------------------
55
62
  if (fs.existsSync(targetDir)) {
56
- const { overwrite } = await inquirer.prompt([
57
- {
58
- type: 'confirm',
59
- name: 'overwrite',
60
- message: `目录 ${projectName} 已存在,是否覆盖?`,
61
- default: false
62
- }
63
- ]);
64
-
65
- if (!overwrite) {
66
- console.log(chalk.yellow('已取消'));
67
- return;
63
+ const files = await fs.readdir(targetDir);
64
+
65
+ if (files.length > 0 && !opts.force) {
66
+ console.log(
67
+ chalk.red(
68
+ '❌ 当前目录非空。\n' +
69
+ '如确认覆盖,请使用 --force'
70
+ )
71
+ );
72
+ process.exit(1);
68
73
  }
69
74
 
70
- await fs.remove(targetDir);
75
+ if (!isCurrentDir && opts.force) {
76
+ await fs.remove(targetDir);
77
+ }
71
78
  }
72
79
 
73
80
  // -----------------------------
74
- // 4. git clone(安全版,支持空格/中文路径)
81
+ // 模板下载(当前目录使用临时目录)
75
82
  // -----------------------------
76
- console.log(chalk.blue('\n📡 正在下载模板...\n'));
83
+ console.log(chalk.blue('\n📡 下载模板...\n'));
84
+
85
+ const tmpDir = isCurrentDir
86
+ ? path.join(os.tmpdir(), `axhub-make-${Date.now()}`)
87
+ : targetDir;
77
88
 
78
89
  execFileSync(
79
90
  'git',
80
- ['clone', '--depth', '1', templateGit, targetDir],
91
+ ['clone', '--depth', '1', opts.template, tmpDir],
81
92
  { stdio: 'inherit' }
82
93
  );
83
94
 
84
- // 移除 .git
85
- await fs.remove(path.join(targetDir, '.git'));
95
+ await fs.remove(path.join(tmpDir, '.git'));
96
+
97
+ if (isCurrentDir) {
98
+ await fs.copy(tmpDir, targetDir, {
99
+ overwrite: true,
100
+ errorOnExist: false
101
+ });
102
+ await fs.remove(tmpDir);
103
+ }
86
104
 
87
105
  // -----------------------------
88
- // 5. 自动修改 package.json
106
+ // package.json 处理
89
107
  // -----------------------------
90
108
  const pkgPath = path.join(targetDir, 'package.json');
91
109
  if (fs.existsSync(pkgPath)) {
@@ -95,55 +113,41 @@ async function run() {
95
113
  }
96
114
 
97
115
  // -----------------------------
98
- // 6. 安装依赖
99
- // -----------------------------
100
- const pkgManager = getPackageManager();
101
-
102
- console.log(
103
- chalk.blue(`\n📦 正在使用 ${pkgManager} 安装依赖(请稍等)...\n`)
104
- );
105
-
106
- execSync(`${pkgManager} install`, {
107
- cwd: targetDir,
108
- stdio: 'inherit'
109
- });
110
-
111
- // -----------------------------
112
- // 7. 直接启动(dev 优先,fallback start)
116
+ // 安装依赖
113
117
  // -----------------------------
114
- console.log(chalk.green('\n🚀 依赖安装完成,正在启动项目...\n'));
118
+ const pm = opts.pm || detectPackageManager();
115
119
 
116
- try {
117
- execSync(`${pkgManager} run dev`, {
120
+ if (opts.install) {
121
+ console.log(chalk.blue(`\n📦 安装依赖(${pm})...\n`));
122
+ execSync(`${pm} install`, {
118
123
  cwd: targetDir,
119
124
  stdio: 'inherit'
120
125
  });
121
- } catch (e) {
126
+ }
127
+
128
+ // -----------------------------
129
+ // 启动项目
130
+ // -----------------------------
131
+ if (opts.start) {
132
+ console.log(chalk.green('\n🚀 启动项目...\n'));
122
133
  try {
123
- execSync(`${pkgManager} run start`, {
124
- cwd: targetDir,
125
- stdio: 'inherit'
126
- });
127
- } catch (_) {
128
- // Ctrl+C 中断属于正常行为
134
+ execSync(`${pm} run dev`, { cwd: targetDir, stdio: 'inherit' });
135
+ } catch {
136
+ try {
137
+ execSync(`${pm} run start`, { cwd: targetDir, stdio: 'inherit' });
138
+ } catch {}
129
139
  }
130
140
  }
141
+
142
+ console.log(chalk.green('\n✅ 完成'));
131
143
  }
132
144
 
133
145
  // -----------------------------
134
146
  // 包管理器检测
135
147
  // -----------------------------
136
- function getPackageManager() {
137
- try {
138
- execSync('pnpm -v', { stdio: 'ignore' });
139
- return 'pnpm';
140
- } catch {}
141
-
142
- try {
143
- execSync('yarn -v', { stdio: 'ignore' });
144
- return 'yarn';
145
- } catch {}
146
-
148
+ function detectPackageManager() {
149
+ try { execSync('pnpm -v', { stdio: 'ignore' }); return 'pnpm'; } catch {}
150
+ try { execSync('yarn -v', { stdio: 'ignore' }); return 'yarn'; } catch {}
147
151
  return 'npm';
148
152
  }
149
153
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axhub-make",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Axhub Make scaffolding tool",
5
5
  "bin": {
6
6
  "axhub-make": "./bin/index.js"