Skip to content

Commit

Permalink
fix(#163): code generation for configurations without glob pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kay-schecker committed Dec 22, 2020
1 parent 0570896 commit e1fab63
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
30 changes: 30 additions & 0 deletions apps/generator-cli/src/app/services/generator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ describe('GeneratorService', () => {
someBool: false,
},
},
['no-glob.json']: {
noGlob: {
inputSpec: 'http://example.local/openapi.json',
output: 'no-glob/#{name}',
name: '#{name}',
nameUcFirst: '#{Name}',
cwd: '#{cwd}',
base: '#{base}',
dir: '#{dir}',
path: '#{path}',
relDir: '#{relDir}',
relPath: '#{relPath}',
ext: '#{ext}'
}
}
}

const specFiles = {
Expand Down Expand Up @@ -170,6 +185,21 @@ describe('GeneratorService', () => {
]],
['none.json', []],
['also-none.json', []],
['no-glob.json', [
cmd('[noGlob] http://example.local/openapi.json', [
`--input-spec="http://example.local/openapi.json"`,
`--output="no-glob/openapi"`,
`--name="openapi"`,
`--name-uc-first="Openapi"`,
`--cwd="${cwd}"`,
`--base="openapi.json"`,
`--dir="#{dir}"`,
`--path="http://example.local/openapi.json"`,
`--rel-dir="#{relDir}"`,
`--rel-path="#{relPath}"`,
`--ext="json"`,
]),
]],
])('%s', (filePath, expectedCommands) => {

let returnValue: boolean
Expand Down
30 changes: 20 additions & 10 deletions apps/generator-cli/src/app/services/generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export class GeneratorService {
const commands = flatten(enabledGenerators.map(([name, config]) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {glob: globPattern, disabled, ...params} = config

if (!globPattern) {
return [{
name: `[${name}] ${params.inputSpec}`,
command: this.buildCommand(cwd, params)
}]
}

const specFiles = glob.sync(globPattern, {cwd})

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

return glob.sync(globPattern, {cwd}).map(spec => ({
name: `[${name}] ${spec}`,
command: this.buildCommand(cwd, spec, params),
command: this.buildCommand(cwd, params, spec)
}))
}))

Expand Down Expand Up @@ -77,11 +85,11 @@ export class GeneratorService {
}).join('\n'))
}

private buildCommand(cwd: string, specFile: string, params: Record<string, unknown>) {
const absoluteSpecPath = path.resolve(cwd, specFile)
private buildCommand(cwd: string, params: Record<string, unknown>, specFile?: string) {
const absoluteSpecPath = specFile ? path.resolve(cwd, specFile) : String(params.inputSpec)

const command = Object.entries({
['input-spec']: absoluteSpecPath,
inputSpec: absoluteSpecPath,
...params,
}).map(([k, v]) => {

Expand Down Expand Up @@ -113,17 +121,19 @@ export class GeneratorService {
cwd,

base: path.basename(absoluteSpecPath),
dir: path.dirname(absoluteSpecPath),
dir: specFile && path.dirname(absoluteSpecPath),
path: absoluteSpecPath,

relDir: path.dirname(specFile),
relDir: specFile && path.dirname(specFile),
relPath: specFile,
ext: ext.split('.').slice(-1).pop(),
ext: ext.split('.').slice(-1).pop()
}

return this.cmd(Object.entries(placeholders).reduce((cmd, [search, replacement]) => {
return cmd.split(`#{${search}}`).join(replacement)
}, command))
return this.cmd(Object.entries(placeholders)
.filter(([, replacement]) => !!replacement)
.reduce((cmd, [search, replacement]) => {
return cmd.split(`#{${search}}`).join(replacement)
}, command))
}

private cmd = (appendix: string) => [
Expand Down

0 comments on commit e1fab63

Please sign in to comment.