Skip to content

Commit e1fab63

Browse files
committed
fix(#163): code generation for configurations without glob pattern
1 parent 0570896 commit e1fab63

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

apps/generator-cli/src/app/services/generator.service.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ describe('GeneratorService', () => {
8585
someBool: false,
8686
},
8787
},
88+
['no-glob.json']: {
89+
noGlob: {
90+
inputSpec: 'http://example.local/openapi.json',
91+
output: 'no-glob/#{name}',
92+
name: '#{name}',
93+
nameUcFirst: '#{Name}',
94+
cwd: '#{cwd}',
95+
base: '#{base}',
96+
dir: '#{dir}',
97+
path: '#{path}',
98+
relDir: '#{relDir}',
99+
relPath: '#{relPath}',
100+
ext: '#{ext}'
101+
}
102+
}
88103
}
89104

90105
const specFiles = {
@@ -170,6 +185,21 @@ describe('GeneratorService', () => {
170185
]],
171186
['none.json', []],
172187
['also-none.json', []],
188+
['no-glob.json', [
189+
cmd('[noGlob] http://example.local/openapi.json', [
190+
`--input-spec="http://example.local/openapi.json"`,
191+
`--output="no-glob/openapi"`,
192+
`--name="openapi"`,
193+
`--name-uc-first="Openapi"`,
194+
`--cwd="${cwd}"`,
195+
`--base="openapi.json"`,
196+
`--dir="#{dir}"`,
197+
`--path="http://example.local/openapi.json"`,
198+
`--rel-dir="#{relDir}"`,
199+
`--rel-path="#{relPath}"`,
200+
`--ext="json"`,
201+
]),
202+
]],
173203
])('%s', (filePath, expectedCommands) => {
174204

175205
let returnValue: boolean

apps/generator-cli/src/app/services/generator.service.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export class GeneratorService {
4040
const commands = flatten(enabledGenerators.map(([name, config]) => {
4141
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4242
const {glob: globPattern, disabled, ...params} = config
43+
44+
if (!globPattern) {
45+
return [{
46+
name: `[${name}] ${params.inputSpec}`,
47+
command: this.buildCommand(cwd, params)
48+
}]
49+
}
50+
4351
const specFiles = glob.sync(globPattern, {cwd})
4452

4553
if (specFiles.length < 1) {
@@ -48,7 +56,7 @@ export class GeneratorService {
4856

4957
return glob.sync(globPattern, {cwd}).map(spec => ({
5058
name: `[${name}] ${spec}`,
51-
command: this.buildCommand(cwd, spec, params),
59+
command: this.buildCommand(cwd, params, spec)
5260
}))
5361
}))
5462

@@ -77,11 +85,11 @@ export class GeneratorService {
7785
}).join('\n'))
7886
}
7987

80-
private buildCommand(cwd: string, specFile: string, params: Record<string, unknown>) {
81-
const absoluteSpecPath = path.resolve(cwd, specFile)
88+
private buildCommand(cwd: string, params: Record<string, unknown>, specFile?: string) {
89+
const absoluteSpecPath = specFile ? path.resolve(cwd, specFile) : String(params.inputSpec)
8290

8391
const command = Object.entries({
84-
['input-spec']: absoluteSpecPath,
92+
inputSpec: absoluteSpecPath,
8593
...params,
8694
}).map(([k, v]) => {
8795

@@ -113,17 +121,19 @@ export class GeneratorService {
113121
cwd,
114122

115123
base: path.basename(absoluteSpecPath),
116-
dir: path.dirname(absoluteSpecPath),
124+
dir: specFile && path.dirname(absoluteSpecPath),
117125
path: absoluteSpecPath,
118126

119-
relDir: path.dirname(specFile),
127+
relDir: specFile && path.dirname(specFile),
120128
relPath: specFile,
121-
ext: ext.split('.').slice(-1).pop(),
129+
ext: ext.split('.').slice(-1).pop()
122130
}
123131

124-
return this.cmd(Object.entries(placeholders).reduce((cmd, [search, replacement]) => {
125-
return cmd.split(`#{${search}}`).join(replacement)
126-
}, command))
132+
return this.cmd(Object.entries(placeholders)
133+
.filter(([, replacement]) => !!replacement)
134+
.reduce((cmd, [search, replacement]) => {
135+
return cmd.split(`#{${search}}`).join(replacement)
136+
}, command))
127137
}
128138

129139
private cmd = (appendix: string) => [

0 commit comments

Comments
 (0)