Skip to content

Commit

Permalink
fix: 修复默认模板以及模板位置
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangwenyang committed Aug 10, 2021
1 parent 57987b1 commit d3639e0
Show file tree
Hide file tree
Showing 11 changed files with 2,107 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ web_modules/
# Next.js build output
.next
out
!out/_templates/

# Nuxt.js build / generate output
.nuxt
Expand Down Expand Up @@ -116,3 +117,4 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
* 修复默认API名称

### 0.0.31 (2021-08-04)
* 修复默认模板
* 修复默认模板

### 0.0.32 (2021-08-10)
* 修复默认模板判重问题
* 修复当没有选中默认文件时,出现重复根目录选择问题

### 0.0.33 (2021-08-10)
* 修复模板位置
8 changes: 8 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { src, dest } = require('gulp');

function copyTemplates(cb) {
src('./src/_templates/**/*').pipe(dest('./out/_templates'));
cb();
}

exports.default = copyTemplates;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "jiangwenyang",
"icon": "images/logo.png",
"description": "从swagger快速生成API文件",
"version": "0.0.31",
"version": "0.0.33",
"engines": {
"vscode": "^1.58.0"
},
Expand Down Expand Up @@ -57,7 +57,8 @@
]
},
"scripts": {
"vscode:prepublish": "yarn run compile",
"copy": "gulp",
"vscode:prepublish": "yarn run copy && yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
Expand All @@ -75,6 +76,7 @@
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.27.0",
"glob": "^7.1.7",
"gulp": "^4.0.2",
"mocha": "^8.4.0",
"typescript": "^4.3.2",
"vscode-test": "^1.5.2"
Expand Down
13 changes: 13 additions & 0 deletions src/_templates/api/new/index.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
to: src/api<%= basePath %>.js
inject: true
append: true
skip_if: <%= method + h.changeCase.pascal(path) %>
---

// <%= summary %>
<% requstBodyMethodList=["post",'put'] -%>
<% methodMap={delete:"del"} -%>
export const <%= method + h.changeCase.pascal(path) %> = <%= requstBodyMethodList.includes(method) ? "data" : "params" %> => {
return <%= methodMap[method] || method %>(genPath(basePath, '<%= path %>'), <%= requstBodyMethodList.includes(method) ? "data" : "{ params }" %>)
}
25 changes: 22 additions & 3 deletions src/extension/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type Result = {
result: any;
};

/**
* 执行hygen生成
* @param {PathInfo} api API信息
* @param {string} rootPath 根目录
* @returns
*/
const hygen = (api: PathInfo, rootPath: string) => {
const argv = Object.entries(api).flatMap(([key, value]) => {
try {
Expand All @@ -38,10 +44,18 @@ const hygen = (api: PathInfo, rootPath: string) => {
});
};

/**
* 显示API生成结果
* @param {Result[]} successList 成功列表
* @param {Result[]} failureList 失败列表
*/
const showResult = (successList: Result[], failureList: Result[]) => {
const outputChanel = vscode.window.createOutputChannel('swagger-to-api');

successList.length && outputChanel.appendLine('--------生成成功API--------');
successList.length &&
outputChanel.appendLine(
'--------生成成功API(注:跳过也视为生成成功)--------'
);
successList.forEach(({ api }) =>
outputChanel.appendLine(formatGenResult(api))
);
Expand All @@ -55,11 +69,16 @@ const showResult = (successList: Result[], failureList: Result[]) => {
vscode.window.showInformationMessage('生成API成功!');
} else {
vscode.window.showErrorMessage('API生成失败,请检查模板配置!');
outputChanel.show();
}
outputChanel.show();
outputChanel.dispose();
};

/**
* 生成API
* @export
* @returns
*/
export default async function gen() {
const rootPath = await getRootPath();

Expand All @@ -74,7 +93,7 @@ export default async function gen() {
return;
}

const apis = await prepareApi();
const apis = await prepareApi(rootPath);

if (!(apis && apis.length)) {
return;
Expand Down
6 changes: 4 additions & 2 deletions src/extension/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { Config } from './types';
* @export
* @returns
*/
export default async function getConfig(): Promise<Config | undefined> {
const rootPath = await getRootPath();
export default async function getConfig(
rootPath?: string
): Promise<Config | undefined> {
rootPath = rootPath || (await getRootPath());
if (!rootPath) {
return;
}
Expand Down
9 changes: 7 additions & 2 deletions src/extension/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as vscode from 'vscode';
import { getRootPath, isTemplatesExits, copyTemplates } from './helpers';

export default async () => {
/**
* 初始化生成项目模板
* @export
* @returns
*/
export default async function init() {
const rootPath = await getRootPath();
if (!rootPath) {
return;
Expand All @@ -24,4 +29,4 @@ export default async () => {
} catch (error) {
console.log(error);
}
};
}
14 changes: 10 additions & 4 deletions src/extension/prepareApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Docs, PathMethod, PathInfo } from '../core/types';
import { getPathsGroupByTag } from '../core/utils';

// 选择并获取文档
const prepareDocs = async () => {
const { services } = (await getConfig()) || {};
const prepareDocs = async (rootPath?: string) => {
const { services } = (await getConfig(rootPath)) || {};

if (!(services && services.length)) {
vscode.window.showErrorMessage('请先配置服务地址!');
Expand Down Expand Up @@ -92,8 +92,14 @@ const selectAPI = async (docs: Docs) => {
})) as PathInfo[];
};

export default async function prepareAPI() {
const docs = await prepareDocs();
/**
* API生成获取选择
* @export
* @param {string} [rootPath]
* @returns
*/
export default async function prepareAPI(rootPath?: string) {
const docs = await prepareDocs(rootPath);

if (!docs) {
vscode.window.showErrorMessage('获取接口文档失败,请检查配置!');
Expand Down
Binary file not shown.
Loading

0 comments on commit d3639e0

Please sign in to comment.