diff --git a/common/commit-lint/CHANGELOG.md b/common/commit-lint/CHANGELOG.md index 9fed624..17de36b 100644 --- a/common/commit-lint/CHANGELOG.md +++ b/common/commit-lint/CHANGELOG.md @@ -1,5 +1,7 @@ # @fe-free/commit-lint +## 1.2.2 + ## 1.2.1 ## 1.2.0 diff --git a/common/commit-lint/package.json b/common/commit-lint/package.json index e562563..2e35e66 100644 --- a/common/commit-lint/package.json +++ b/common/commit-lint/package.json @@ -1,6 +1,6 @@ { "name": "@fe-free/commit-lint", - "version": "1.2.1", + "version": "1.2.2", "description": "", "main": "index.js", "publishConfig": { diff --git a/common/eslint-config-base/CHANGELOG.md b/common/eslint-config-base/CHANGELOG.md index 545ba0d..0ef254d 100644 --- a/common/eslint-config-base/CHANGELOG.md +++ b/common/eslint-config-base/CHANGELOG.md @@ -1,5 +1,7 @@ # @fe-free/eslint-config-base +## 1.2.2 + ## 1.2.1 ## 1.2.0 diff --git a/common/eslint-config-base/package.json b/common/eslint-config-base/package.json index 8eba8cc..e85f880 100644 --- a/common/eslint-config-base/package.json +++ b/common/eslint-config-base/package.json @@ -1,6 +1,6 @@ { "name": "@fe-free/eslint-config-base", - "version": "1.2.1", + "version": "1.2.2", "description": "", "main": "index.js", "publishConfig": { diff --git a/common/free-scripts/CHANGELOG.md b/common/free-scripts/CHANGELOG.md index 2ee036a..21773ab 100644 --- a/common/free-scripts/CHANGELOG.md +++ b/common/free-scripts/CHANGELOG.md @@ -1,5 +1,11 @@ # @fe-free/free-scripts +## 1.2.2 + +### Patch Changes + +- feat: free-scripts add jsonUrl + ## 1.2.1 ### Patch Changes diff --git a/common/free-scripts/package.json b/common/free-scripts/package.json index 3839d30..35575bb 100644 --- a/common/free-scripts/package.json +++ b/common/free-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@fe-free/free-scripts", - "version": "1.2.1", + "version": "1.2.2", "description": "", "main": "dist/index.js", "bin": { diff --git a/common/free-scripts/src/build_api/index.ts b/common/free-scripts/src/build_api/index.ts index 07e5672..d3da2f5 100644 --- a/common/free-scripts/src/build_api/index.ts +++ b/common/free-scripts/src/build_api/index.ts @@ -29,12 +29,19 @@ async function code({ name, options }) { } // /api/base/base-dictionary/add 得到 baseDictionaryAdd +// /auth/login 得到 authLogin function toCamelCase(url) { - const parts = url.replace(/-/g, '/').replace(/_/g, '/').split('/'); + let parts = url.replace('/api/', '/').replace(/-/g, '/').replace(/_/g, '/').split('/'); + + // 历史原因,先这样实现 + if (parts[1] === parts[2]) { + parts = parts.slice(1); + } + const camelCase = - parts[3] + + parts[1] + parts - .slice(4) + .slice(2) .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join(''); return camelCase; @@ -57,7 +64,7 @@ function merge(mergeJSON, json) { }); } -async function build({ swaggerDoc: { docName, docUrl }, options }) { +async function swaggerJsonByDocUrl({ docUrl }) { const mergeJSON: any = {}; const resources = await fetch(`${docUrl}/swagger-resources`).then((response) => response.json()); @@ -73,24 +80,41 @@ async function build({ swaggerDoc: { docName, docUrl }, options }) { // fix,否则跑不通 generateApi mergeJSON.info.termsOfService = docUrl; + return mergeJSON; +} + +async function swaggerJsonByJsonUrl({ jsonUrl }) { + const json = await fetch(jsonUrl).then((response) => response.json()); + return json; +} + +function processJson({ json }) { // operationId 不采用原先值(后端没维护好),于是采用 url - for (const apiPath in mergeJSON.paths) { + for (const apiPath in json.paths) { const funName = toCamelCase(apiPath); // 只有一个方法,get post ... - for (const method in mergeJSON.paths[apiPath]) { - mergeJSON.paths[apiPath][method].operationId = funName; + for (const method in json.paths[apiPath]) { + json.paths[apiPath][method].operationId = funName; } } - const outputDir = path.resolve(options.output); + return json; +} + +function writeSwaggerJson({ output, docName, json }) { + const outputDir = path.resolve(output); fs.writeFileSync( path.resolve(outputDir, `./swagger/${docName}.json`), - JSON.stringify(mergeJSON, null, 2) + JSON.stringify(json, null, 2) ); +} - await code({ name: docName, options }); - - console.log('build_api success'); +function prepare({ options }) { + const outputDir = path.resolve(options.output); + fs.rmSync(path.resolve(outputDir, './api'), { recursive: true, force: true }); + fs.rmSync(path.resolve(outputDir, './swagger'), { recursive: true, force: true }); + fs.mkdirSync(path.resolve(outputDir, './api')); + fs.mkdirSync(path.resolve(outputDir, './swagger')); } interface Options { @@ -101,6 +125,7 @@ interface Options { async function buildApi(options: Options) { console.log('buildApi', options); + // 检查输入和输出目录 if ( !options.output || !options.input || @@ -110,25 +135,39 @@ async function buildApi(options: Options) { throw new Error('请指定有效输入目录和输出目录'); } - const outputDir = path.resolve(options.output); - - console.log('新建 api 目录 和 swagger 目录'); - fs.rmSync(path.resolve(outputDir, './api'), { recursive: true, force: true }); - fs.rmSync(path.resolve(outputDir, './swagger'), { recursive: true, force: true }); - fs.mkdirSync(path.resolve(outputDir, './api')); - fs.mkdirSync(path.resolve(outputDir, './swagger')); - + // 读取 package.json 配置 const packageJSON = fs.readJSONSync(path.resolve(options.input, './package.json')); - if (!packageJSON.swaggerDocs) { throw new Error('package.json swaggerDocs 未配置'); } + // 准备目录 + prepare({ options }); + + // 生成 api for (const item of packageJSON.swaggerDocs) { - await build({ swaggerDoc: item, options }); + (async function () { + let json; + // 获得 json + if (item.docUrl) { + json = await swaggerJsonByDocUrl({ docUrl: item.docUrl }); + } else if (item.jsonUrl) { + json = await swaggerJsonByJsonUrl({ jsonUrl: item.jsonUrl }); + } + + // 处理 json + json = processJson({ json }); + + // 写入 json + writeSwaggerJson({ output: options.output, docName: item.docName, json }); + // 生成 ts 文件 + await code({ name: item.docName, options }); + + console.log(`build-api success ${item.docName}`); + })(); } - console.log('buildApi success'); + console.log('build-api all success'); } export { buildApi }; diff --git a/common/style-lint/CHANGELOG.md b/common/style-lint/CHANGELOG.md index 4eb4605..44dc554 100644 --- a/common/style-lint/CHANGELOG.md +++ b/common/style-lint/CHANGELOG.md @@ -1,5 +1,7 @@ # @fe-free/style-lint +## 1.2.2 + ## 1.2.1 ## 1.2.0 diff --git a/common/style-lint/package.json b/common/style-lint/package.json index 6c4940e..eb003e5 100644 --- a/common/style-lint/package.json +++ b/common/style-lint/package.json @@ -1,6 +1,6 @@ { "name": "@fe-free/style-lint", - "version": "1.2.1", + "version": "1.2.2", "description": "", "main": "index.js", "publishConfig": { diff --git a/package.json b/package.json index a4f96d8..dae5742 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "packageManager": "pnpm@8.7.6", "engines": { "node": ">=16.19.1", - "pnpm": "^8.7.6", + "pnpm": "^9.15.1", "changeset": "^2.26.2" }, "scripts": {