Skip to content

Commit

Permalink
Merge pull request #82 from long-woo/dev
Browse files Browse the repository at this point in the history
release: 2.4.0
  • Loading branch information
long-woo authored Oct 8, 2024
2 parents 6ad69e7 + 1017f17 commit 3494b91
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ Create a `myPlugin.ts` file:

```ts
// 引用模块
// import { start } from 'https://deno.land/x/stc@2.3.0/mod.ts'
import { start } from 'jsr:@loongwoo/stc@^2.3.0'
// import { start } from 'https://deno.land/x/stc@2.4.0/mod.ts'
import { start } from 'jsr:@loongwoo/stc@^2.4.0'

// Defining plugins
const myPlugin: IPlugin = {
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@loongwoo/stc",
"version": "2.3.0",
"version": "2.4.0",
"exports": "./mod.ts",
"tasks": {
"pack": "deno run -A src/pack.ts",
"dev": "deno task pack && deno run -A --watch=src src/main.ts --url='https://petstore3.swagger.io/api/v3/openapi.json' --lang=js",
"serve": "deno run -A --watch=src src/service.ts",
"version": "echo '2.3.0' > release/version",
"version": "echo '2.4.0' > release/version",
"build:npm": "deno run -A src/npm/build.ts",
"build:mac": "deno compile -A --target x86_64-apple-darwin --output release/stc src/main.ts",
"build:mac-m": "deno compile -A --target aarch64-apple-darwin --output release/stc-m src/main.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/npm/pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@loongwoo/stc",
"version": "2.3.0",
"version": "2.4.0",
"description": "A tool for converting OpenApi/Swagger/Apifox into code.",
"type": "module",
"module": "esm/mod.js",
Expand Down
49 changes: 14 additions & 35 deletions src/plugins/javascript/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as esbuild from "npm:esbuild@^0.23.1";

import type { ISwaggerOptions } from "../../swagger.ts";
import type {
IPlugin,
Expand All @@ -10,30 +8,13 @@ import { createFile } from "../../common.ts";
import shared from "../typescript/shared/index.ts";
import { TypeScriptPlugin } from "../typescript/index.ts";
import { renderEtaString } from "../common.ts";
import { generateDeclarationFile } from "./oxc.ts";
import { generateDeclarationFile, oxcTransform } from "./oxc.ts";
// TODO: Deno compile 不支持 storage
// import { generateDeclarationFile } from "./declarationGenerator.ts";

let pluginOptions: ISwaggerOptions;
const actionDeclareData = new Map<string, string>();

/**
* Transforms the given code using esbuild.
*
* @param {string} code - The code to transform.
* @return {Promise<string>} The transformed code.
*/
const esTransform = async (code: string) => {
const result = await esbuild.transform(code, {
loader: "ts",
target: "esnext",
legalComments: "inline",
format: "esm",
});

return result.code;
};

export const JavaScriptPlugin: IPlugin = {
name: "stc:JavaScriptPlugin",
lang: "js",
Expand All @@ -54,17 +35,15 @@ export const JavaScriptPlugin: IPlugin = {
_tsTransform.definition.content,
);

_definition.content = await esTransform(_tsTransform.definition.content);
actionDeclareData.set("_types", _typeDeclaration);
}

if (_tsTransform?.action) {
for (const [key, content] of _tsTransform.action) {
const _tsCodeDeclaration = generateDeclarationFile(content);
const _jsCode = await esTransform(content);
const { code, declaration } = oxcTransform(content);

actionDeclareData.set(key, _tsCodeDeclaration);
_actionMapData.set(key.replace(/\.ts/, `.${this.lang}`), _jsCode);
actionDeclareData.set(key, declaration ?? "");
_actionMapData.set(key.replace(/\.ts/, `.${this.lang}`), code);
}
}

Expand All @@ -73,29 +52,29 @@ export const JavaScriptPlugin: IPlugin = {
action: _actionMapData,
};
},
async onEnd() {
onEnd() {
// 创建运行时需要的文件
const _baseFileContent = await esTransform(shared.apiClientBase);
const _baseFile = oxcTransform(shared.apiClientBase);
const _parserFetchRuntime = renderEtaString(
shared.fetchRuntime,
pluginOptions as unknown as Record<string, unknown>,
);
const _fetchRuntimeFileContent = await esTransform(_parserFetchRuntime);
const _httpClientContent = await esTransform(shared[pluginOptions.client!]);
const _fetchRuntimeFile = oxcTransform(_parserFetchRuntime);
const _httpClient = oxcTransform(shared[pluginOptions.client!]);

createFile(
`${pluginOptions.outDir}/shared/${pluginOptions.client}/index.${this.lang}`,
_httpClientContent,
_httpClient.code,
);

createFile(
`${pluginOptions.outDir}/shared/apiClientBase.${this.lang}`,
_baseFileContent,
_baseFile.code,
);

createFile(
`${pluginOptions.outDir}/shared/fetchRuntime.${this.lang}`,
_fetchRuntimeFileContent,
_fetchRuntimeFile.code,
);

// 创建接口声明文件
Expand All @@ -107,15 +86,15 @@ export const JavaScriptPlugin: IPlugin = {
});
createFile(
`${pluginOptions.outDir}/shared/apiClientBase.d.ts`,
generateDeclarationFile(shared.apiClientBase),
_baseFile.declaration ?? "",
);
createFile(
`${pluginOptions.outDir}/shared/fetchRuntime.d.ts`,
generateDeclarationFile(_parserFetchRuntime),
_fetchRuntimeFile.declaration ?? "",
);
createFile(
`${pluginOptions.outDir}/shared/${pluginOptions.client}/index.d.ts`,
generateDeclarationFile(shared[pluginOptions.client!]),
_httpClient.declaration ?? "",
);
},
};
49 changes: 42 additions & 7 deletions src/plugins/javascript/oxc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import oxc from "npm:oxc-transform@^0.30.1";
import oxc from "npm:oxc-transform@^0.30.5";

import Logs from "../../console.ts";

Expand All @@ -8,12 +8,14 @@ import Logs from "../../console.ts";
* @param {string} sourceCode - The source code to generate the declaration file from.
* @return {string} The generated declaration file content.
*/
export const generateDeclarationFile = (sourceCode: string) => {
const filename = `temp_${+new Date()}.ts`;

const { errors, code } = oxc.isolatedDeclaration(filename, sourceCode, {
sourcemap: false,
});
export const generateDeclarationFile = (sourceCode: string): string => {
const { errors, code } = oxc.isolatedDeclaration(
`temp_${+new Date()}.ts`,
sourceCode,
{
sourcemap: false,
},
);

if (errors.length > 0) {
Logs.error(errors.join("\n"));
Expand All @@ -22,3 +24,36 @@ export const generateDeclarationFile = (sourceCode: string) => {

return code;
};

/**
* Transforms the given source code into TypeScript, returning an object with the transformed code and declaration.
*
* @param {string} source - The source code to transform.
* @return {{ code: string, declaration: string }} An object with the transformed code and declaration.
*/
export const oxcTransform = (
source: string,
): { code: string; declaration?: string } => {
const { code, declaration, errors } = oxc.transform(
`temp_${+new Date()}.ts`,
source,
{
typescript: {
allowDeclareFields: false,
declaration: {
sourcemap: false,
},
},
},
);

if (errors.length > 0) {
Logs.error(errors.join("\n"));
return { code: "", declaration: "" };
}

return {
code,
declaration,
};
};
2 changes: 1 addition & 1 deletion src/plugins/typescript/shared/apiClientBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type IDefaultObject<T = unknown> = {
export interface IDefaultObject<T = unknown> {
[key: string]: T;
};

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/typescript/shared/fetchRuntime.eta
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
IDefaultObject,
ApiClientConfig,
ApiClientMethod,
ApiClientParams,
IDefaultObject
} from "./apiClientBase";
import { generateURL } from "./apiClientBase";
import { <% if (it.client === 'axios') { %> createAxios, <% } %>request } from "./<%= it.client %>";
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/typescript/shared/index.ts

Large diffs are not rendered by default.

0 comments on commit 3494b91

Please sign in to comment.