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.
- package/bin/index.js +95 -91
- 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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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 =
|
|
49
|
-
templateGit = templateGit || answers.customTemplateGit;
|
|
50
|
-
const targetDir = path.resolve(process.cwd(), projectName);
|
|
57
|
+
const projectName = path.basename(targetDir);
|
|
51
58
|
|
|
52
59
|
// -----------------------------
|
|
53
|
-
//
|
|
60
|
+
// 当前目录检测
|
|
54
61
|
// -----------------------------
|
|
55
62
|
if (fs.existsSync(targetDir)) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
75
|
+
if (!isCurrentDir && opts.force) {
|
|
76
|
+
await fs.remove(targetDir);
|
|
77
|
+
}
|
|
71
78
|
}
|
|
72
79
|
|
|
73
80
|
// -----------------------------
|
|
74
|
-
//
|
|
81
|
+
// 模板下载(当前目录使用临时目录)
|
|
75
82
|
// -----------------------------
|
|
76
|
-
console.log(chalk.blue('\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',
|
|
91
|
+
['clone', '--depth', '1', opts.template, tmpDir],
|
|
81
92
|
{ stdio: 'inherit' }
|
|
82
93
|
);
|
|
83
94
|
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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
|
-
|
|
118
|
+
const pm = opts.pm || detectPackageManager();
|
|
115
119
|
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// -----------------------------
|
|
129
|
+
// 启动项目
|
|
130
|
+
// -----------------------------
|
|
131
|
+
if (opts.start) {
|
|
132
|
+
console.log(chalk.green('\n🚀 启动项目...\n'));
|
|
122
133
|
try {
|
|
123
|
-
execSync(`${
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
|
137
|
-
try {
|
|
138
|
-
|
|
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
|
|