create-swdg-frontend 0.1.0 → 0.1.1
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 +3 -13
- package/package.json +1 -1
- package/template/scripts/gen-api.cjs +93 -41
- package/template/swdg/bin/index.js +0 -23
- package/template/swdg/package.json +0 -27
package/README.md
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
|
-
# create-swdg
|
|
1
|
+
# create-swdg-frontend
|
|
2
2
|
|
|
3
3
|
Scaffold a new project from the SWDG frontend template.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
pnpm create swdg my-app
|
|
8
|
+
pnpm create swdg-frontend my-app
|
|
9
9
|
# or
|
|
10
|
-
pnpm dlx create-swdg my-app
|
|
10
|
+
pnpm dlx create-swdg-frontend my-app
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Options:
|
|
14
|
-
|
|
15
|
-
- `--install` to install dependencies after creation
|
|
16
|
-
- `--no-install` to skip installing dependencies
|
|
17
|
-
- `--pm pnpm|npm|yarn` to choose the package manager (default: pnpm)
|
|
18
|
-
- `--git` to run `git init`
|
|
19
|
-
- `--no-git` to skip `git init`
|
|
20
|
-
- `--gis` to include Cesium GIS assets
|
|
21
|
-
- `--no-gis` to exclude Cesium GIS assets
|
|
22
|
-
|
|
23
13
|
## Sync template
|
|
24
14
|
|
|
25
15
|
```bash
|
package/package.json
CHANGED
|
@@ -4,9 +4,6 @@ const axios = require('axios');
|
|
|
4
4
|
const { execFileSync } = require('child_process');
|
|
5
5
|
const fse = require('fs-extra');
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* 修复旧式参数结构
|
|
9
|
-
*/
|
|
10
7
|
function fixTypeName(name) {
|
|
11
8
|
return name.replace(/\s+/g, '');
|
|
12
9
|
}
|
|
@@ -71,26 +68,99 @@ function fixSwagger(doc) {
|
|
|
71
68
|
return doc;
|
|
72
69
|
}
|
|
73
70
|
|
|
74
|
-
/**
|
|
75
|
-
* 下载并保存 API 文档
|
|
76
|
-
*/
|
|
77
71
|
async function fetchAndFixSwagger(url, outputPath) {
|
|
78
72
|
const { data } = await axios.get(url);
|
|
79
73
|
const fixed = fixSwagger(data);
|
|
80
74
|
fs.writeFileSync(outputPath, JSON.stringify(fixed, null, 2), 'utf-8');
|
|
81
75
|
}
|
|
82
76
|
|
|
77
|
+
function quoteForCmd(arg) {
|
|
78
|
+
if (arg === '') return '""';
|
|
79
|
+
const needsQuote = /[\s&()^\[\]{}=;!'+,`~|<>"]/g.test(arg);
|
|
80
|
+
if (!needsQuote) return arg;
|
|
81
|
+
return `"${String(arg).replace(/"/g, '\\"')}"`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function execOnWindowsViaCmd(commandLine) {
|
|
85
|
+
execFileSync('cmd.exe', ['/d', '/s', '/c', commandLine], { stdio: 'inherit' });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function runAndReport(cmd, args, options = {}) {
|
|
89
|
+
try {
|
|
90
|
+
execFileSync(cmd, args, options);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
const stdout = e && e.stdout ? e.stdout.toString() : '';
|
|
93
|
+
const stderr = e && e.stderr ? e.stderr.toString() : '';
|
|
94
|
+
if (stdout.trim()) console.error(`\n[openapi-generator stdout]\n${stdout}\n`);
|
|
95
|
+
if (stderr.trim()) console.error(`\n[openapi-generator stderr]\n${stderr}\n`);
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
83
100
|
/**
|
|
84
|
-
*
|
|
101
|
+
* 如果你不想改系统环境变量:
|
|
102
|
+
* 1) 安装 JDK 后,改这里的路径为你的 JDK 安装目录
|
|
103
|
+
* 2) 脚本会临时把 JAVA_HOME/bin 注入 PATH,只影响本次运行
|
|
85
104
|
*/
|
|
105
|
+
function withJavaEnv(baseEnv) {
|
|
106
|
+
const env = { ...baseEnv };
|
|
107
|
+
|
|
108
|
+
// ✅ 如果你已全局装好 Java(java -version 可用),可以把这块注释掉
|
|
109
|
+
// TODO: 把这里改成你的 JDK 路径
|
|
110
|
+
const JDK_HOME = env.JDK_HOME || env.JAVA_HOME || 'C:\\Program Files\\Java\\jdk-17';
|
|
111
|
+
|
|
112
|
+
if (process.platform === 'win32') {
|
|
113
|
+
const javaBin = path.join(JDK_HOME, 'bin');
|
|
114
|
+
env.JAVA_HOME = JDK_HOME;
|
|
115
|
+
env.PATH = `${javaBin};${env.PATH || ''}`;
|
|
116
|
+
} else {
|
|
117
|
+
// Unix: 一般装好后 java 已在 PATH;如需也可注入
|
|
118
|
+
// env.JAVA_HOME = JDK_HOME;
|
|
119
|
+
// env.PATH = `${path.join(JDK_HOME, 'bin')}:${env.PATH || ''}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return env;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function runOpenapiGenerator(args) {
|
|
126
|
+
const localCliMain = path.resolve(
|
|
127
|
+
process.cwd(),
|
|
128
|
+
'node_modules',
|
|
129
|
+
'@openapitools',
|
|
130
|
+
'openapi-generator-cli',
|
|
131
|
+
'main.js'
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const env = withJavaEnv(process.env);
|
|
135
|
+
|
|
136
|
+
if (fs.existsSync(localCliMain)) {
|
|
137
|
+
runAndReport(process.execPath, [localCliMain, ...args], {
|
|
138
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
139
|
+
env,
|
|
140
|
+
});
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (process.platform === 'win32') {
|
|
145
|
+
const cmdline = ['npx', 'openapi-generator-cli', ...args]
|
|
146
|
+
.map(quoteForCmd)
|
|
147
|
+
.join(' ');
|
|
148
|
+
execFileSync('cmd.exe', ['/d', '/s', '/c', cmdline], { stdio: 'inherit', env });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
runAndReport('npx', ['openapi-generator-cli', ...args], {
|
|
153
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
154
|
+
env,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
86
158
|
function generateClient(inputPath, outputDir) {
|
|
87
|
-
// Avoid space issues by passing relative paths to the CLI
|
|
88
159
|
const relativeInputPath = path.relative(process.cwd(), inputPath);
|
|
89
160
|
const relativeOutputDir = path.relative(process.cwd(), outputDir);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
[
|
|
93
|
-
'openapi-generator-cli',
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
runOpenapiGenerator([
|
|
94
164
|
'generate',
|
|
95
165
|
'--skip-validate-spec',
|
|
96
166
|
'-i',
|
|
@@ -99,19 +169,18 @@ function generateClient(inputPath, outputDir) {
|
|
|
99
169
|
'typescript-axios',
|
|
100
170
|
'-o',
|
|
101
171
|
relativeOutputDir,
|
|
102
|
-
]
|
|
103
|
-
|
|
104
|
-
|
|
172
|
+
]);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
const msg = e && e.message ? e.message : String(e);
|
|
175
|
+
console.error(`❌ openapi-generator 执行失败: ${msg}`);
|
|
176
|
+
throw e;
|
|
177
|
+
}
|
|
105
178
|
}
|
|
106
179
|
|
|
107
|
-
/**
|
|
108
|
-
* 清理和移动生成结果
|
|
109
|
-
*/
|
|
110
180
|
function moveToSrcApi(serviceName, tempDir, targetBase) {
|
|
111
181
|
const destDir = path.join(targetBase, serviceName);
|
|
112
182
|
if (fs.existsSync(destDir)) fse.removeSync(destDir);
|
|
113
183
|
|
|
114
|
-
// 移除无用文件
|
|
115
184
|
const filesToRemove = [
|
|
116
185
|
'.swagger-codegen',
|
|
117
186
|
'.gitignore',
|
|
@@ -128,14 +197,9 @@ function moveToSrcApi(serviceName, tempDir, targetBase) {
|
|
|
128
197
|
}
|
|
129
198
|
|
|
130
199
|
fse.moveSync(tempDir, destDir, { overwrite: true });
|
|
131
|
-
console.log(
|
|
132
|
-
`✅ ${serviceName} 接口生成完毕,已移动到 src/api/${serviceName}`
|
|
133
|
-
);
|
|
200
|
+
console.log(`✅ ${serviceName} 接口生成完毕,已移动到 src/api/${serviceName}`);
|
|
134
201
|
}
|
|
135
202
|
|
|
136
|
-
/**
|
|
137
|
-
* 入口函数
|
|
138
|
-
*/
|
|
139
203
|
async function generateService(serviceName, apiUrl) {
|
|
140
204
|
const tempPath = path.join(__dirname, '.tmp', serviceName);
|
|
141
205
|
const fixedJsonPath = path.join(tempPath, 'swagger-fixed.json');
|
|
@@ -146,31 +210,19 @@ async function generateService(serviceName, apiUrl) {
|
|
|
146
210
|
moveToSrcApi(serviceName, tempPath, path.resolve(__dirname, '../src/api'));
|
|
147
211
|
}
|
|
148
212
|
|
|
149
|
-
/**
|
|
150
|
-
* 多服务入口
|
|
151
|
-
*/
|
|
152
213
|
async function main() {
|
|
153
214
|
const base = 'http://10.20.124.50:30030';
|
|
154
215
|
const services = [
|
|
155
|
-
{
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
name: 'storageservice',
|
|
161
|
-
url: `${base}/storageservice/v2/v3/api-docs`,
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
name: 'underwatersys',
|
|
165
|
-
url: `${base}/underwatersys/v3/api-docs`,
|
|
166
|
-
},
|
|
216
|
+
{ name: 'userservice', url: `${base}/userservice/v2/v3/api-docs` },
|
|
217
|
+
{ name: 'storageservice', url: `${base}/storageservice/v2/v3/api-docs` },
|
|
218
|
+
{ name: 'underwatersys', url: `${base}/underwatersys/v3/api-docs` },
|
|
167
219
|
];
|
|
168
220
|
|
|
169
221
|
for (const svc of services) {
|
|
170
222
|
try {
|
|
171
223
|
await generateService(svc.name, svc.url);
|
|
172
224
|
} catch (err) {
|
|
173
|
-
console.error(`❌ 生成失败: ${svc.name}`, err.message);
|
|
225
|
+
console.error(`❌ 生成失败: ${svc.name}`, err && err.message ? err.message : err);
|
|
174
226
|
}
|
|
175
227
|
}
|
|
176
228
|
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { spawn } from 'node:child_process';
|
|
3
|
-
import { createRequire } from 'node:module';
|
|
4
|
-
|
|
5
|
-
const require = createRequire(import.meta.url);
|
|
6
|
-
let cliPath;
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
cliPath = require.resolve('create-swdg/bin/index.js');
|
|
10
|
-
} catch (error) {
|
|
11
|
-
console.error(
|
|
12
|
-
'Failed to resolve create-swdg. Please run: npm i -g create-swdg'
|
|
13
|
-
);
|
|
14
|
-
process.exit(1);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const child = spawn(process.execPath, [cliPath, ...process.argv.slice(2)], {
|
|
18
|
-
stdio: 'inherit',
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
child.on('exit', (code) => {
|
|
22
|
-
process.exit(code ?? 1);
|
|
23
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "swdg",
|
|
3
|
-
"version": "0.1.1",
|
|
4
|
-
"description": "SWDG frontend scaffolding CLI wrapper",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"swdg": "bin/index.js"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"bin"
|
|
11
|
-
],
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"create-swdg": "^0.1.3"
|
|
14
|
-
},
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=18"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"swdg",
|
|
20
|
-
"create",
|
|
21
|
-
"scaffold",
|
|
22
|
-
"vite",
|
|
23
|
-
"vue"
|
|
24
|
-
],
|
|
25
|
-
"author": "inupedia",
|
|
26
|
-
"license": "MIT"
|
|
27
|
-
}
|