Skip to content

Commit

Permalink
chore: changeset Version Packages
Browse files Browse the repository at this point in the history
  • Loading branch information
liyatang committed Jan 1, 2025
1 parent 0c0dac1 commit f86532a
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 28 deletions.
2 changes: 2 additions & 0 deletions common/commit-lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# @fe-free/commit-lint

## 1.2.2

## 1.2.1

## 1.2.0
Expand Down
2 changes: 1 addition & 1 deletion common/commit-lint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fe-free/commit-lint",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"main": "index.js",
"publishConfig": {
Expand Down
2 changes: 2 additions & 0 deletions common/eslint-config-base/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# @fe-free/eslint-config-base

## 1.2.2

## 1.2.1

## 1.2.0
Expand Down
2 changes: 1 addition & 1 deletion common/eslint-config-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fe-free/eslint-config-base",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"main": "index.js",
"publishConfig": {
Expand Down
6 changes: 6 additions & 0 deletions common/free-scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @fe-free/free-scripts

## 1.2.2

### Patch Changes

- feat: free-scripts add jsonUrl

## 1.2.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion common/free-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fe-free/free-scripts",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"main": "dist/index.js",
"bin": {
Expand Down
85 changes: 62 additions & 23 deletions common/free-scripts/src/build_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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 {
Expand All @@ -101,6 +125,7 @@ interface Options {
async function buildApi(options: Options) {
console.log('buildApi', options);

// 检查输入和输出目录
if (
!options.output ||
!options.input ||
Expand All @@ -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 };
2 changes: 2 additions & 0 deletions common/style-lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# @fe-free/style-lint

## 1.2.2

## 1.2.1

## 1.2.0
Expand Down
2 changes: 1 addition & 1 deletion common/style-lint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fe-free/style-lint",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"main": "index.js",
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packageManager": "[email protected]",
"engines": {
"node": ">=16.19.1",
"pnpm": "^8.7.6",
"pnpm": "^9.15.1",
"changeset": "^2.26.2"
},
"scripts": {
Expand Down

0 comments on commit f86532a

Please sign in to comment.