Skip to content

Commit

Permalink
Merge pull request #21 from long-woo/dev
Browse files Browse the repository at this point in the history
feat: 新增 app 文件,修复 build:npm 错误
  • Loading branch information
long-woo committed Jul 28, 2023
2 parents ae45fb9 + 8bf58cb commit d9de465
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 338 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
deno-version: v1.x

# 使用 dtn 打包
# 使用 dnt(https​://deno.land/x/dnt​@0.38.0/mod.ts) 打包
- name: 构建成 npm 包
run: deno task build:npm

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ release
node_modules
npm_dist

examples/**/pnpm-lock.yaml
examples/**/yarn.lock
examples/**/package-lock.json

.DS_Store
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

STC(Swagger Transform Code) 是一个 Swagger 文档转换成代码文件的工具。

![Publish](https://github.com/long-woo/stc/actions/workflows/deno-build.yml/badge.svg)
![Publish to release](https://github.com/long-woo/stc/actions/workflows/deno-build.yml/badge.svg)
[![Publish Package to npmjs](https://github.com/long-woo/stc/actions/workflows/npm.yml/badge.svg)](https://github.com/long-woo/stc/actions/workflows/npm.yml)

特性:

Expand Down Expand Up @@ -62,7 +63,7 @@ stc --url=https://petstore3.swagger.io/api/v3/openapi.json --outDir=out

```ts
// 引用模块
import { start } from 'https://deno.land/x/stc@1.1.5/mod.ts'
import { start } from 'https://deno.land/x/stc@1.1.6/mod.ts'

// 定义插件
const myPlugin: IPlugin = {
Expand Down
6 changes: 3 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": "1.1.5",
"version": "1.1.6",
"tasks": {
"dev": "deno run -A --watch=src src/main.ts --url 'https://petstore3.swagger.io/api/v3/openapi.json' --outDir=out",
"version": "echo '1.1.5' > release/version",
"version": "echo '1.1.6' > 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 All @@ -12,7 +12,7 @@
"imports": {
"/": "./",
"./": "./",
"std/": "https://deno.land/std@0.192.0/",
"std/": "https://deno.land/std@0.195.0/",
"x/": "https://deno.land/x/"
},
"lint": {
Expand Down
252 changes: 46 additions & 206 deletions deno.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/my-plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { start } from '@loongwoo/stc'
const { start} = require('@loongwoo/stc')

const { start } = require('@loongwoo/stc')
console.log(start)
const myPlugin = {
name: 'stc:myPlugin',
setup(options) {
Expand Down
2 changes: 1 addition & 1 deletion examples/my-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"dev": "node index.js"
},
"devDependencies": {
"@loongwoo/stc": "^1.0.0"
"@loongwoo/stc": "file:../../npm_dist"
}
}
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
*
* @module
*/
export { start } from "./src/cli.ts";
export { start } from "./src/app.ts";
109 changes: 109 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Logs from "./console.ts";
import { PluginManager } from "./plugins/index.ts";
import { typeScriptPlugin } from "./plugins/typescript/index.ts";
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";

/**
* 创建上下文
* @param options
*/
const createContext = (context: IPluginContext) => context;

/**
* 初始化插件管理器
*/
const initPluginManager = (context: IPluginContext) => {
const pluginManager = new PluginManager();

// 注册插件
pluginManager.register([
typeScriptPlugin,
...(context.options.plugins ?? []),
]);
// 启动所有插件
pluginManager.setupAll(context);
};

/**
* 获取 Swagger 数据
* @param urlOrPath - 远程地址或本地
* @returns
*/
const getData = async (urlOrPath: string): Promise<ISwaggerResult> => {
if (!/^http(s?):\/\//.test(urlOrPath)) {
const content = await readFile(urlOrPath);

try {
return JSON.parse(content) as unknown as ISwaggerResult;
} catch (error) {
throw new Error(`api 文件解析失败。原因:${error}`);
}
}

// 从远程地址获取 Swagger 数据
const res = await fetch(urlOrPath);
const data = await res.json();

return data;
};

/**
* 启动
* @param options - 配置
*/
export const start = async (options: ISwaggerOptions) => {
// 创建上下文
const context = createContext({ options });

// 清空控制台信息
Logs.clear();

// 初始化插件管理器
initPluginManager(context);

const data = await getData(options.url);
// 触发插件 onload 事件
context.onLoad?.(data);

// 处理类型定义。v2 版本中,通过 `definitions` 属性获取。而 v3 版本,则通过 `components.schemas` 属性获取。
const defData = getDefinition(data.definitions || data.components?.schemas);
// 触发插件 onDefinition 事件
context.onDefinition?.(defData);

const actionData = getApiPath(data.paths, options);
// 触发插件 onAction 事件
context.onAction?.(actionData);

// 清空输出目录
await emptyDirectory(options.outDir);

// 触发插件 onTransform 事件
const transformData = context.onTransform?.(defData, actionData);

// 写入类型定义文件
if (transformData?.definition) {
createFile(
`${options.outDir}/types.${options.lang}`,
transformData.definition,
);
}

// 写入 API 文件
if (transformData?.action) {
transformData.action.forEach((content, filename) => {
createFile(
`${options.outDir}/${filename}.${options.lang}`,
content,
);
});
}

console.log("\n");
Logs.success(`API 文件生成完成:\n\t${options.outDir}\n`);
// 触发插件 onEnd 事件
context.onEnd?.();
};
119 changes: 10 additions & 109 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Args, parse, type ParseOptions } from "std/flags/mod.ts";
import ProgressBar from "x/progress@v1.3.8/mod.ts";

import Logs from "./console.ts";
import { PluginManager } from "./plugins/index.ts";
import { typeScriptPlugin } from "./plugins/typescript/index.ts";
import { IPluginContext } from "./plugins/typeDeclaration.ts";
import { getDefinition } from "./definition.ts";
import { getApiPath } from "./path.ts";
import { ISwaggerOptions, ISwaggerResult } from "./swagger.ts";
import { createAppFile, createFile, emptyDirectory, readFile } from "./util.ts";
import { ISwaggerOptions } from "./swagger.ts";
import { createAppFile } from "./util.ts";
import denoJson from "/deno.json" assert { type: "json" };

/**
* 检查更新并处理更新过程(如果有新版本可用)。
*
* @return {Promise<string>} 如果进行了更新,则返回最新版本,如果未找到更新,则返回当前版本。
*/
const checkUpdate = async (): Promise<string> => {
Logs.info("检查更新...");
const version = Number(denoJson.version?.replace(/\./g, "") ?? 0);
Expand All @@ -25,7 +25,9 @@ const checkUpdate = async (): Promise<string> => {
const _lastVersion = Number(latestVersion.replace(/\./g, "") ?? 0);

if (version < _lastVersion) {
Logs.info("发现新版本,正在更新中...");
Logs.info(
`发现新版本:${denoJson.version}${latestVersion},正在更新中...`,
);
const dir = Deno.cwd();
const systemInfo = Deno.build;

Expand Down Expand Up @@ -115,107 +117,6 @@ const checkUpdate = async (): Promise<string> => {
return denoJson.version;
};

/**
* 创建上下文
* @param options
*/
const createContext = (context: IPluginContext) => context;

/**
* 初始化插件管理器
*/
const initPluginManager = (context: IPluginContext) => {
const pluginManager = new PluginManager();

// 注册插件
pluginManager.register([
typeScriptPlugin,
...(context.options.plugins ?? []),
]);
// 启动所有插件
pluginManager.setupAll(context);
};

/**
* 获取 Swagger 数据
* @param urlOrPath - 远程地址或本地
* @returns
*/
const getData = async (urlOrPath: string): Promise<ISwaggerResult> => {
if (!/^http(s?):\/\//.test(urlOrPath)) {
const content = await readFile(urlOrPath);

try {
return JSON.parse(content) as unknown as ISwaggerResult;
} catch (error) {
throw new Error(`api 文件解析失败。原因:${error}`);
}
}

// 从远程地址获取 Swagger 数据
const res = await fetch(urlOrPath);
const data = await res.json();

return data;
};

/**
* 启动
* @param options - 配置
*/
export const start = async (options: ISwaggerOptions) => {
// 创建上下文
const context = createContext({ options });

// 清空控制台信息
Logs.clear();

// 初始化插件管理器
initPluginManager(context);

const data = await getData(options.url);
// 触发插件 onload 事件
context.onLoad?.(data);

// 处理类型定义。v2 版本中,通过 `definitions` 属性获取。而 v3 版本,则通过 `components.schemas` 属性获取。
const defData = getDefinition(data.definitions || data.components?.schemas);
// 触发插件 onDefinition 事件
context.onDefinition?.(defData);

const actionData = getApiPath(data.paths, options);
// 触发插件 onAction 事件
context.onAction?.(actionData);

// 清空输出目录
await emptyDirectory(options.outDir);

// 触发插件 onTransform 事件
const transformData = context.onTransform?.(defData, actionData);

// 写入类型定义文件
if (transformData?.definition) {
createFile(
`${options.outDir}/types.${options.lang}`,
transformData.definition,
);
}

// 写入 API 文件
if (transformData?.action) {
transformData.action.forEach((content, filename) => {
createFile(
`${options.outDir}/${filename}.${options.lang}`,
content,
);
});
}

console.log("\n");
Logs.success(`API 文件生成完成:\n\t${options.outDir}\n`);
// 触发插件 onEnd 事件
context.onEnd?.();
};

/**
* 打印帮助信息
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { main, start } from "./cli.ts";
import { main } from "./cli.ts";
import { start } from "./app.ts";

if (import.meta.main) {
const options = await main();
Expand Down
4 changes: 2 additions & 2 deletions src/npm/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dnt from "x/dnt@0.37.0/mod.ts";
import * as dnt from "x/dnt@0.38.0/mod.ts";
// import * as esbuild from "x/esbuild@v0.18.6/mod.js";
import pkg from "./package.json" assert { type: "json" };

Expand All @@ -20,7 +20,7 @@ import pkg from "./package.json" assert { type: "json" };
// });
// console.log(res);
await dnt.build({
entryPoints: ["./src/cli.ts"],
entryPoints: ["./mod.ts"],
outDir: "./npm_dist",
shims: {
deno: true,
Expand Down
13 changes: 3 additions & 10 deletions src/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@loongwoo/stc",
"version": "1.1.5",
"version": "1.1.6",
"description": "Swagger 文档转换为接口文件",
"main": "script/cli.js",
"module": "esm/cli.js",
"main": "script/mod.js",
"module": "esm/mod.js",
"repository": {
"type": "git",
"url": "git+https://github.com/long-woo/stc.git"
Expand All @@ -30,12 +30,5 @@
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"exports": {
".": {
"require": "./index.ts",
"import": "./index.ts"
},
"./*": "./index.ts"
}
}

0 comments on commit d9de465

Please sign in to comment.