From 108ccc71a547d2940d1cfa4845176bdb30692a74 Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 21 Jul 2023 15:50:33 +0800 Subject: [PATCH] feat(cli:sta): bump swagger-typescript-api to 3.x --- package.json | 2 +- schematics/sta/index.ts | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 56b252f9b..d195ef434 100644 --- a/package.json +++ b/package.json @@ -181,7 +181,7 @@ "eslint-plugin-deprecation": "~1.4.1", "lint-staged": "^13.2.3", "raw-loader": "^4.0.2", - "swagger-typescript-api": "^12.0.4", + "swagger-typescript-api": "^13.0.0", "sitemap": "^7.1.1" }, "lint-staged": { diff --git a/schematics/sta/index.ts b/schematics/sta/index.ts index b1ed810a4..856f18cf2 100644 --- a/schematics/sta/index.ts +++ b/schematics/sta/index.ts @@ -4,7 +4,7 @@ import { normalize } from '@angular-devkit/core'; import { ProjectDefinition } from '@angular-devkit/core/src/workspace'; import { Rule, SchematicsException, Tree, chain, SchematicContext } from '@angular-devkit/schematics'; import * as colors from 'ansi-colors'; -import { rmdirSync, mkdirSync, existsSync, readFileSync } from 'fs'; +import { rmSync, mkdirSync, existsSync, readFileSync } from 'fs'; import { parse } from 'jsonc-parser'; import { resolve, join } from 'path'; import { generateApi, GenerateApiOutput, GenerateApiParams } from 'swagger-typescript-api'; @@ -42,7 +42,7 @@ function addPathInTsConfig(name: string): Rule { function cleanOutput(p: string): void { try { - rmdirSync(p, { recursive: true }); + rmSync(p, { recursive: true }); mkdirSync(p); } catch (e) {} } @@ -61,7 +61,13 @@ function tagsMapping(res: GenerateApiOutput, config: STAConfig): void { }); } -function fix(output: string, res: GenerateApiOutput, tree: Tree, context: SchematicContext, config: STAConfig): void { +async function fix( + output: string, + res: GenerateApiOutput, + tree: Tree, + context: SchematicContext, + config: STAConfig +): Promise { tagsMapping(res, config); const indexList = [`models`, `_base.service`]; const basePath = normalize(join(project.root, output.replace(process.cwd(), ''))); @@ -69,20 +75,20 @@ function fix(output: string, res: GenerateApiOutput, tree: Tree, context: Schema // definitions const dataTpl = res.getTemplate({ name: 'dataContracts', fileName: 'data-contracts.eta' }); const dataContent = res.renderTemplate(dataTpl, { ...res.configuration }); - tree.create(`${basePath}/models.ts`, filePrefix + res.formatTSContent(dataContent)); + tree.create(`${basePath}/models.ts`, filePrefix + (await res.formatTSContent(dataContent))); // Base Service const baseServiceTpl = res.getTemplate({ name: 'baseService', fileName: 'base.service.eta' }); const baseServiceContent = res.renderTemplate(baseServiceTpl, { ...res.configuration }); - tree.create(`${basePath}/_base.service.ts`, filePrefix + res.formatTSContent(baseServiceContent)); + tree.create(`${basePath}/_base.service.ts`, filePrefix + (await res.formatTSContent(baseServiceContent))); // Tag Service const dtoTypeTpl = res.getTemplate({ name: 'dto-type', fileName: 'dto-type.eta' }); const serviceTpl = res.getTemplate({ name: 'service', fileName: 'service.eta' }); - res.configuration.routes.combined.forEach(route => { + res.configuration.routes.combined.forEach(async route => { const routeIndex: string[] = []; // dto - const dtoContent = res.formatTSContent( + const dtoContent = await res.formatTSContent( res.renderTemplate(dtoTypeTpl, { ...res.configuration, route @@ -98,7 +104,10 @@ function fix(output: string, res: GenerateApiOutput, tree: Tree, context: Schema ...res.configuration, route }); - tree.create(`${basePath}/${route.moduleName}/service.ts`, filePrefix + res.formatTSContent(serviceContent)); + tree.create( + `${basePath}/${route.moduleName}/service.ts`, + filePrefix + (await res.formatTSContent(serviceContent)) + ); routeIndex.push(`service`); // index.ts @@ -200,8 +209,7 @@ function genProxy(config: STAConfig): Rule { generateApi(options) .then((res: GenerateApiOutput) => { cleanOutput(output); - fix(output, res, tree, context, config); - resolve(); + return fix(output, res, tree, context, config); }) .catch(ex => { throw new SchematicsException(`Generate error: ${ex}`);