fix templates

Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
Signed-off-by: Aramis Sennyey <159921952+aramissennyeydd@users.noreply.github.com>
Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
aramissennyeydd
2024-03-03 11:10:36 -05:00
parent ddf9e55c3a
commit be1db622eb
11 changed files with 753 additions and 254 deletions
@@ -14,51 +14,85 @@
* limitations under the License.
*/
import fs from 'fs-extra';
import YAML from 'js-yaml';
import chalk from 'chalk';
import { resolve } from 'path';
import {
OPENAPI_IGNORE_FILES,
OUTPUT_PATH,
} from '../../../../../lib/openapi/constants';
import { paths as cliPaths } from '../../../../../lib/paths';
import { TS_SCHEMA_PATH } from '../../../../../lib/openapi/constants';
import { promisify } from 'util';
import { exec as execCb } from 'child_process';
import { getPathToCurrentOpenApiSpec } from '../../../../../lib/openapi/helpers';
const exec = promisify(execCb);
import { mkdirpSync } from 'fs-extra';
import fs from 'fs-extra';
import { exec } from '../../../../../lib/exec';
import { resolvePackagePath } from '@backstage/backend-common';
import {
getPathToCurrentOpenApiSpec,
getRelativePathToFile,
} from '../../../../../lib/openapi/helpers';
async function generate(abortSignal?: AbortController) {
const openapiPath = await getPathToCurrentOpenApiSpec();
const yaml = YAML.load(await fs.readFile(openapiPath, 'utf8'));
const resolvedOpenapiPath = await getPathToCurrentOpenApiSpec();
const resolvedOutputDirectory = await getRelativePathToFile(OUTPUT_PATH);
mkdirpSync(resolvedOutputDirectory);
const tsPath = cliPaths.resolveTarget(TS_SCHEMA_PATH);
await fs.mkdirp(resolvedOutputDirectory);
// The first set of comment slashes allow for the eslint notice plugin to run
// with onNonMatchingHeader: 'replace', as is the case in the open source
// Backstage repo. Otherwise the auto-generated comment will be removed by the
// lint call below.
await fs.writeFile(
tsPath,
`//
// ******************************************************************
// * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. *
// ******************************************************************
import {createValidatedOpenApiRouter} from '@backstage/backend-openapi-utils';
export const spec = ${JSON.stringify(yaml, null, 2)} as const;
export const createOpenApiRouter = async (
options?: Parameters<typeof createValidatedOpenApiRouter>['1'],
) => createValidatedOpenApiRouter<typeof spec>(spec, options);
`,
resolve(resolvedOutputDirectory, '.openapi-generator-ignore'),
OPENAPI_IGNORE_FILES.join('\n'),
);
await exec(`yarn backstage-cli package lint --fix ${tsPath}`, {
signal: abortSignal?.signal,
});
if (await cliPaths.resolveTargetRoot('node_modules/.bin/prettier')) {
await exec(`yarn prettier --write ${tsPath}`, {
cwd: cliPaths.targetRoot,
await exec(
'node',
[
resolvePackagePath('@openapitools/openapi-generator-cli', 'main.js'),
'generate',
'-i',
resolvedOpenapiPath,
'-o',
resolvedOutputDirectory,
'-g',
'typescript',
'-c',
resolvePackagePath(
'@backstage/repo-tools',
'templates/typescript-backstage.server.yaml',
),
`--additional-properties=clientPackageName=@backstage/catalog-client`,
'--generator-key',
'v3.0',
],
{
maxBuffer: Number.MAX_VALUE,
cwd: resolvePackagePath('@backstage/repo-tools'),
env: {
...process.env,
},
signal: abortSignal?.signal,
},
);
await exec(
`yarn backstage-cli package lint --fix ${resolvedOutputDirectory}`,
[],
{
signal: abortSignal?.signal,
},
);
const prettier = cliPaths.resolveTargetRoot('node_modules/.bin/prettier');
if (prettier) {
await exec(`${prettier} --write ${resolvedOutputDirectory}`, [], {
signal: abortSignal?.signal,
});
}
fs.removeSync(resolve(resolvedOutputDirectory, '.openapi-generator-ignore'));
fs.rmSync(resolve(resolvedOutputDirectory, '.openapi-generator'), {
recursive: true,
force: true,
});
}
export async function command({
@@ -45,6 +45,7 @@ export const OPENAPI_IGNORE_FILES = [
// Override the created version.
'apis/*.ts',
'!apis/*.client.ts',
'!apis/*.server.ts',
'models/*.ts',
'!models/*.model.ts',
@@ -8,25 +8,27 @@ files:
client/model.mustache:
templateType: Model
destinationFilename: .model.ts
client/models/modelGeneric.mustache:
modelGeneric.mustache:
templateType: SupportingFiles
client/models/modelOneOf.mustache:
modelOneOf.mustache:
templateType: SupportingFiles
client/models/modelGenericAdditionalProperties.mustache:
modelGenericAdditionalProperties.mustache:
templateType: SupportingFiles
client/models/ modelGenericEnums.mustache:
modelGenericEnums.mustache:
templateType: SupportingFiles
client/models/modelAlias.mustache:
modelAlias.mustache:
templateType: SupportingFiles
client/models/modelEnum.mustache:
modelEnum.mustache:
templateType: SupportingFiles
client/models/modelTaggedUnion.mustache:
modelTaggedUnion.mustache:
templateType: SupportingFiles
client/models/models_all.mustache:
templateType: SupportingFiles
destinationFilename: models/index.ts
client/types/fetch.ts: {}
client/types/discovery.ts: {}
client/types/fetch.ts:
destinationFilename: types/fetch.ts
client/types/discovery.ts:
destinationFilename: types/discovery.ts
client/apis/index.mustache:
templateType: SupportingFiles
destinationFilename: apis/index.ts
@@ -4,7 +4,7 @@ files:
server/api.mustache:
templateType: API
# For some reason, they check for destinationFilename differences. We have to change the ending to override the file.
destinationFilename: .client.ts
destinationFilename: .server.ts
client/apis/index.mustache:
templateType: SupportingFiles
destinationFilename: apis/index.ts
@@ -22,7 +22,8 @@ type ExtendsString<Path extends string> = Path;
type InputOutput = {
{{#operation}}
'#{{httpMethod}}|{{path}}': {
'{{path}}': {
'{{httpMethod}}': {
path: {
{{#pathParams}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}},
@@ -37,6 +38,7 @@ type ExtendsString<Path extends string> = Path;
body: {{{dataType}}},
{{/bodyParam}}
response: {{{returnType}}}{{^returnType}}void{{/returnType}},
}
},
{{/operation}}