Skip to content

Commit

Permalink
Merge pull request #14 from long-woo/dev
Browse files Browse the repository at this point in the history
fix: 修复生成的WebClientBase文件generateURL方法正则问题
  • Loading branch information
long-woo committed Jul 18, 2023
2 parents ced0192 + 4f5959d commit 98ed9a4
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ STC(Swagger Transform Code) 是一个 Swagger 文档转换成代码文件的工

## 快速开始

按系统[下载](https://github.com/long-woo/stc/releases/tag/1.0.1)
按系统[下载](https://github.com/long-woo/stc/releases)

- stc:Intel 系列的 Mac
- stc-m:M 系列的 Mac
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": "1.1.3",
"tasks": {
"dev": "deno run -A --watch=src src/main.ts --url 'http://127.0.0.1:4523/export/openapi/7?version=3.1' --tag=2 --outDir=out",
"version": "echo '1.1.2' > release/version",
"version": "echo '1.1.3' > release/version",
"build:npm": "deno run -A src/npm/index.ts",
"build:mac": "deno compile -A src/main.ts --output release/stc --target x86_64-apple-darwin",
"build:mac-m": "deno compile -A src/main.ts --output release/stc-m --target aarch64-apple-darwin",
Expand Down
3 changes: 0 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,3 @@
* @module
*/
export { start } from "./src/cli.ts";

const __VERSION__ = "1.0.0";
Deno.env.set("VERSION", __VERSION__);
97 changes: 90 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,76 @@ import { IPluginContext } from "./plugins/typeDeclaration.ts";
import { getDefinition } from "./definition.ts";
import { getApiPath } from "./path.ts";
import { ISwaggerOptions, ISwaggerResult } from "./swagger.ts";
import { createFile, emptyDirectory, readFile } from "./util.ts";
import { createAppFile, createFile, emptyDirectory, readFile } from "./util.ts";
import denoJson from "/deno.json" assert { type: "json" };

const checkUpdate = async () => {
Logs.info("检查更新...");
const version = Number(denoJson.version?.replace(/\./g, "") ?? 0);

const res = await fetch(
"https://api.github.com/repos/long-woo/stc/releases/latest",
);

if (res.ok) {
const data = await res.json();
const latestVersion = data.tag_name;
const _lastVersion = Number(latestVersion.replace(/\./g, "") ?? 0);

if (version < _lastVersion) {
Logs.info("发现新版本,正在更新中...");
const dir = Deno.cwd();
const systemInfo = Deno.build;
const appNameMap: Record<string, string> = {
"x86_64-apple-darwin": "stc",
"aarch64-apple-darwin": "stc-m",
"x86_64-pc-windows-msvc": "stc-win",
"x86_64-unknown-linux-gnu": "stc-linux",
};
const downloadUrl =
`https://github.com/long-woo/stc/releases/download/${latestVersion}/${
appNameMap[systemInfo.target]
}`;
const downloadApp = await fetch(downloadUrl);

if (downloadApp.ok) {
const content = await downloadApp.arrayBuffer();

await createAppFile(
`${dir}/stc`,
content,
);

Logs.info("更新完成,请重新运行");
return;
}

Logs.error(downloadApp.statusText);

// const command = new Deno.Command("deno", {
// args: [
// "compile",
// "-A",
// `https://deno.land/x/stc@${latestVersion}/mod.ts`,
// "--output",
// `${dir}/stc`,
// ],
// });

// const { code, stderr } = await command.output();

// if (code === 0) {
// Logs.info("更新完成,请重新运行");
// return;
// }

// Logs.error(new TextDecoder().decode(stderr));
return;
}

Logs.info("已经是最新版本");
}
};

/**
* 创建上下文
Expand Down Expand Up @@ -121,7 +190,7 @@ const printHelp = () => {
选项:
-h, --help 显示帮助信息
--url 远程地址或本地文件路径
-o, --outDir 输出目录,默认为 Deno 当前执行的目录下 swagger2code_out
-o, --outDir 输出目录,默认为 Deno 当前执行的目录下 stc_out
-p, --platform 平台,可选值:axios、wechat, [default: "axios"]
-l, --lang 语言,用于输出文件的后缀名, [default: "ts"]
--include 包含解析接口
Expand All @@ -130,25 +199,24 @@ const printHelp = () => {
-v, --version 显示版本信息
示例:
stc -o ./out --url http://petstore.swagger.io/v2/swagger.json
stc -o ./out -p wechat -l ts --url http://petstore.swagger.io/v2/swagger.json
stc -o ./stc_out --url http://petstore.swagger.io/v2/swagger.json
stc -o ./stc_out -p axios -l ts --url http://petstore.swagger.io/v2/swagger.json
`);
Deno.exit(0);
};

/**
* 主入口
*/
export const main = (): ISwaggerOptions => {
export const main = async (): Promise<ISwaggerOptions> => {
// 定义命令行参数和选项的配置
const argsConfig: ParseOptions = {
boolean: ["help"],
boolean: ["help", "version"],
string: [
"url",
"outDir",
"platform",
"lang",
"version",
"tag",
],
alias: {
Expand All @@ -173,6 +241,21 @@ export const main = (): ISwaggerOptions => {
// 解析命令行参数和选项
const args: Args = parse(Deno.args, argsConfig);

// 检查更新
await checkUpdate();

// 帮助
if (args.help) {
printHelp();
Deno.exit(0);
}

// 版本
if (args.version) {
console.log(`stc v${denoJson.version}`);
Deno.exit(0);
}

// 检查 url
if (!args.url) {
Logs.error("必须提供选项 url");
Expand Down
5 changes: 1 addition & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { main, start } from "./cli.ts";

const __VERSION__ = "1.0.0";
Deno.env.set("VERSION", __VERSION__);

if (import.meta.main) {
const options = main();
const options = await main();

// 启动
start(options);
Expand Down
2 changes: 1 addition & 1 deletion src/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@loongwoo/stc",
"version": "1.1.2",
"version": "1.1.3",
"description": "Swagger 文档转换为接口文件",
"main": "script/cli.js",
"module": "esm/cli.js",
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/typescript/defintion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Logs from "/src/console.ts";
import { IDefinitionVirtualProperty } from "/src/swagger.ts";
import { convertType, propCommit, upperCase } from "/src/util.ts";
import Logs from "../../console.ts";
import { IDefinitionVirtualProperty } from "../../swagger.ts";
import { convertType, propCommit, upperCase } from "../../util.ts";

/**
* 解析枚举
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createFile } from "/src/util.ts";
import { createFile } from "../../util.ts";
import { ISwaggerOptions } from "../../swagger.ts";
import { IPlugin } from "../typeDeclaration.ts";
import { parserDefinition } from "./defintion.ts";
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/typescript/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
IPathVirtualParameter,
IPathVirtualProperty,
IPathVirtualPropertyResponse,
} from "/src/swagger.ts";
import { convertType, propCommit, upperCase } from "/src/util.ts";
import Logs from "/src/console.ts";
} from "../../swagger.ts";
import { convertType, propCommit, upperCase } from "../../util.ts";
import Logs from "../../console.ts";

interface IApiRealParams {
path?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/typescript/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class WebClientBase {
static generateURL(url: string, path?: IDefaultObject) {
// 替换路由参数
const newURL = url.replace(
/[\{|:](\w+)[\}]?/gi,
/[\\{|:](\\w+)[\\}]?/gi,
(_key: string, _value: string): string => {
return path ? path[_value] as string : "";
},
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ ${content}`),
);
};

/**
* 将给定的内容写入指定路径的文件中
*
* @param {string} filePath - 要创建或覆盖的文件的路径
* @param {ArrayBuffer} content - 要写入文件的内容
* @return {Promise<void>} - 在文件成功写入时解析,或在出现错误时拒绝
*/

export const createAppFile = (filePath: string, content: ArrayBuffer) =>
Deno.writeFile(filePath, new Uint8Array(content));

/**
* 覆盖复制文件
* @param from - 复制位置
Expand Down
31 changes: 31 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assertEquals } from "std/testing/asserts.ts";

Deno.test("测试-输出版本信息", async () => {
const command = new Deno.Command("deno", {
args: [
"run",
"-A",
"src/main.ts",
"--version",
],
});
const { code } = await command.output();

// console.log(new TextDecoder().decode(stdout));
assertEquals(0, code);
});

Deno.test("测试-输出帮助信息", async () => {
const command = new Deno.Command("deno", {
args: [
"run",
"-A",
"src/main.ts",
"--help",
],
});
const { code } = await command.output();

// console.log(new TextDecoder().decode(stdout));
assertEquals(0, code);
});

0 comments on commit 98ed9a4

Please sign in to comment.