Skip to content

Commit 1f530c1

Browse files
authored
feat: generate openapitools.json via task (#732)
Create a task that produces the openapitools.json file. This allows consumers to add their own steps to edit the file with compile-time values prior to being consumed. This is important for cases such as inserting a signed repository download URL.
1 parent ab8e648 commit 1f530c1

32 files changed

+4977
-2111
lines changed

packages/type-safe-api/src/project/codegen/components/open-api-tools-json-file.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
22
SPDX-License-Identifier: Apache-2.0 */
3+
import { ok as assert } from "node:assert";
4+
import { posix as path } from "path";
35
import { ProjectUtils } from "@aws/monorepo";
4-
import { JsonFile, Project } from "projen";
6+
import { JsonFile, Project, Task } from "projen";
57
import { OpenApiGeneratorCliConfig } from "../../types";
68

79
/**
@@ -41,14 +43,18 @@ export class OpenApiToolsJsonFile extends JsonFile {
4143
storageDir: "~/.open-api-generator-cli",
4244
};
4345

46+
public readonly createTask: Task;
47+
4448
constructor(project: Project) {
4549
if (OpenApiToolsJsonFile.of(project)) {
4650
throw new Error(
4751
`Project ${project.name} already has associated OpenApiToolsJsonFile component.`
4852
);
4953
}
5054

51-
super(project, "openapitools.json", {
55+
const localFilePath = "openapitools.json";
56+
const dynamicFilePath = path.join(".pdk/dynamic-files", localFilePath);
57+
super(project, dynamicFilePath, {
5258
obj: {
5359
// Schema is located in node_modules when generator cli is installed in tmp dir
5460
$schema:
@@ -57,6 +63,21 @@ export class OpenApiToolsJsonFile extends JsonFile {
5763
"generator-cli": () => this.config,
5864
},
5965
});
66+
67+
// Ignore the location that gets copied to
68+
project.addGitIgnore(`/${localFilePath}`);
69+
70+
// Create the task, but don't attach it anywhere yet. This is because of wanting
71+
// to do the generation as part of the "generate" tasks, which may not yet exist.
72+
this.createTask = project.addTask(`create-openapitools.json`, {
73+
steps: [{ exec: `cp -f ${dynamicFilePath} ${localFilePath}` }],
74+
});
75+
}
76+
77+
preSynthesize(): void {
78+
const generateTask = this.project.tasks.tryFind("generate");
79+
assert(generateTask);
80+
generateTask.prependSpawn(this.createTask);
6081
}
6182

6283
/**

0 commit comments

Comments
 (0)