diff --git a/packages/pont-engine/src/generators/generate.ts b/packages/pont-engine/src/generators/generate.ts index b6eb7050..3d723c6e 100644 --- a/packages/pont-engine/src/generators/generate.ts +++ b/packages/pont-engine/src/generators/generate.ts @@ -515,6 +515,11 @@ export class CodeGenerator { return; } } + + codeSnippet(inter: Interface): string { + const mod = inter.getContext().mod as Mod; + return `API.${mod.name}.${inter.name}`; + } } export class FilesManager { diff --git a/packages/pont-engine/src/manage.ts b/packages/pont-engine/src/manage.ts index 7d1ba653..ac93643c 100644 --- a/packages/pont-engine/src/manage.ts +++ b/packages/pont-engine/src/manage.ts @@ -1,4 +1,4 @@ -import { Interface, Mod, StandardDataSource } from './standard'; +import { StandardDataSource } from './standard'; import { Config, getTemplate, DataSourceConfig, hasChinese, diffDses, getRelatedBos } from './utils'; import * as fs from 'fs-extra'; import * as path from 'path'; @@ -23,8 +23,6 @@ export class Manager { fileManager: FilesManager; - codeSnippet: (inter: Interface) => string; - diffs = { modDiffs: [] as Model[], boDiffs: [] as Model[] @@ -47,8 +45,6 @@ export class Manager { async selectDataSource(name: string) { this.currConfig = this.allConfigs.find((conf) => conf.name === name); - this.setCodeSnippet(); - await this.readLocalDataSource(); await this.readRemoteDataSource(); @@ -150,7 +146,6 @@ export class Manager { constructor(private projectRoot: string, config: Config, configDir = process.cwd()) { this.allConfigs = config.getDataSourcesConfig(configDir); this.currConfig = this.allConfigs[0]; - this.setCodeSnippet(); } pollingId = null; @@ -479,7 +474,11 @@ export class Manager { DsManager.openReport(currProj); } - setCodeSnippet(config = this.currConfig) { - this.codeSnippet = Config.getCodeSnippetConfig(config) || function () {}; + getCodeSnippet() { + const generator = this.fileManager.fileStructures.generators.find((g) => { + return g.dataSource.name === this.currLocalDataSource.name; + }); + + return generator.codeSnippet; } } diff --git a/packages/pont-engine/src/utils.ts b/packages/pont-engine/src/utils.ts index 40ca86e7..996663d6 100644 --- a/packages/pont-engine/src/utils.ts +++ b/packages/pont-engine/src/utils.ts @@ -5,7 +5,7 @@ import * as prettier from 'prettier'; import * as ts from 'typescript'; import { ResolveConfigOptions } from 'prettier'; import { error } from './debugLog'; -import { Interface, Mod, StandardDataSource, StandardDataType } from './standard'; +import { Mod, StandardDataSource, StandardDataType } from './standard'; import { Manager } from './manage'; import { OriginType } from './scripts'; import { diff } from './diff'; @@ -38,14 +38,6 @@ export default function (url: string): string { } `; -const defaultCodeSnippetCode = ` -import { Mod, Interface } from "pont-engine"; - -export default function(mod: Mod,inter: Interface): StandardDataSource { - return ['API',mod.name,inter.name].join("."); -} -`; - export class Mocks { enable = false; port = 8080; @@ -81,7 +73,6 @@ export class DataSourceConfig { outDir = 'src/service'; scannedRange = []; transformPath = ''; - codeSnippetPath = ''; fetchMethodPath = ''; prettierConfig: ResolveConfigOptions = {}; /** 单位为秒,默认 20 分钟 */ @@ -109,7 +100,6 @@ export class Config extends DataSourceConfig { name: string; usingOperationId: boolean; transformPath?: string; - codeSnippetPath?: string; fetchMethodPath?: string; outDir?: string; }>; @@ -119,21 +109,6 @@ export class Config extends DataSourceConfig { this.origins = config.origins || []; } - static getCodeSnippetConfig(config: Config | DataSourceConfig) { - if (config.codeSnippetPath) { - const moduleResult = getTemplate(config.codeSnippetPath, '', defaultCodeSnippetCode) as any; - - if (moduleResult) { - return moduleResult.default; - } - } - - return (inter: Interface) => { - const context = inter.getContext(); - return `API${config.usingMultipleOrigins ? `.${context.dataSource.name}` : ''}.${context.mod.name}.${inter.name}`; - }; - } - static getTransformFromConfig(config: Config | DataSourceConfig) { if (config.transformPath) { const moduleResult = getTemplate(config.transformPath, '', defaultTransformCode) as any; @@ -200,7 +175,6 @@ export class Config extends DataSourceConfig { scannedRange: Array.isArray(this.scannedRange) ? this.scannedRange.map((dir) => path.join(configDir, dir)) : [], templatePath: this.templatePath ? path.join(configDir, this.templatePath) : undefined, transformPath: this.transformPath ? path.join(configDir, this.transformPath) : undefined, - codeSnippetPath: this.codeSnippetPath ? path.join(configDir, this.codeSnippetPath) : undefined, fetchMethodPath: this.fetchMethodPath ? path.join(configDir, this.fetchMethodPath) : undefined }; diff --git a/packages/vscode-pont/src/UI.ts b/packages/vscode-pont/src/UI.ts index 612423d9..fbcc7cba 100644 --- a/packages/vscode-pont/src/UI.ts +++ b/packages/vscode-pont/src/UI.ts @@ -183,7 +183,7 @@ export class Control { } findInterface(ignoreEdit = false) { - const codeTemplate = this.manager.codeSnippet; + const codeTemplate = this.manager.getCodeSnippet(); const items = this.manager.currLocalDataSource.mods .map((mod) => { @@ -211,7 +211,7 @@ export class Control { return; } - const code = codeTemplate(item.inter); + const code = codeTemplate?.(item.inter) || ''; const editor = vscode.window.activeTextEditor; @@ -305,7 +305,7 @@ export class Control { p.report({ message: '更新成功!' }); vscode.window.showInformationMessage(modName + '更新成功!'); - resolve(); + resolve(true); } catch (e) { reject(e); } @@ -355,7 +355,7 @@ export class Control { p.report({ message: '更新成功!' }); vscode.window.showInformationMessage(boName + '更新成功!'); - resolve(); + resolve(true); } catch (e) { reject(e); } @@ -394,7 +394,7 @@ export class Control { p.report({ message: '更新成功!' }); vscode.window.showInformationMessage('更新成功!'); - resolve(); + resolve(true); } catch (e) { reject(e); }