-
Notifications
You must be signed in to change notification settings - Fork 66
/
post-process.ts
109 lines (94 loc) · 3.65 KB
/
post-process.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as fs from "fs";
import * as path from "path";
import kebabCase from "lodash/kebabCase";
import { parse } from "ts-command-line-args";
// Used to split OpenAPI generated files into multiple files in order to work around
// the restrictions around file naming and splitting in OpenAPI generator
const TSAPI_SPLIT_FILE_HEADER = "###TSAPI_SPLIT_FILE###";
const TSAPI_WRITE_FILE_START = "###TSAPI_WRITE_FILE###";
const TSAPI_WRITE_FILE_END = "###/TSAPI_WRITE_FILE###";
interface Arguments {
/**
* Path to the directory containing output files
*/
readonly outputPath: string;
/**
* Path to the source directory relative to the output directory
*/
readonly srcDir: string;
}
interface WriteFileConfig {
readonly dir: string;
readonly name: string;
readonly ext: string;
readonly overwrite?: boolean;
readonly kebabCaseFileName?: boolean;
}
void (async () => {
const args = parse<Arguments>({
outputPath: { type: String },
srcDir: { type: String },
});
// OpenAPI generator writes a manifest called FILES which lists the files it generated.
const openApiGeneratedFilesManifestPath = path.join(
args.outputPath,
".openapi-generator",
"FILES"
);
// Read the file paths from the manifest
const generatedFiles = fs
.readFileSync(openApiGeneratedFilesManifestPath, { encoding: "utf-8" })
.split("\n")
.filter((x) => x);
const additionalGeneratedFiles: string[] = [];
// Loop over generated files
generatedFiles.forEach((generatedFile) => {
const filePath = path.join(args.outputPath, generatedFile);
if (fs.existsSync(filePath)) {
const contents = fs.readFileSync(filePath, "utf-8");
if (contents.startsWith(TSAPI_SPLIT_FILE_HEADER)) {
// Split by the start template
contents
.split(TSAPI_WRITE_FILE_START)
.filter((t) => t.includes(TSAPI_WRITE_FILE_END))
.forEach((destinationFileTemplate) => {
// Split by the end template to receive the file path, and contents
const [configString, newFileContents] =
destinationFileTemplate.split(TSAPI_WRITE_FILE_END);
const config = JSON.parse(configString) as WriteFileConfig;
const newFileName = `${
config.kebabCaseFileName ? kebabCase(config.name) : config.name
}${config.ext}`;
const relativeNewFileDir = path.join(args.srcDir, config.dir);
const relativeNewFilePath = path.join(
relativeNewFileDir,
newFileName
);
const newFilePath = path.join(args.outputPath, relativeNewFilePath);
// Write to the instructed file path (relative to the src dir)
if (!fs.existsSync(newFilePath) || config.overwrite) {
// Create it's containing directory if needed
fs.mkdirSync(path.join(args.outputPath, relativeNewFileDir), {
recursive: true,
});
fs.writeFileSync(newFilePath, newFileContents);
// Overwritten files are added to the manifest so that they can be cleaned up
// by clean-openapi-generated-code
if (config.overwrite) {
additionalGeneratedFiles.push(relativeNewFilePath);
}
}
});
// Delete the original file
fs.rmSync(filePath);
}
}
});
// Update the manifest with any overwritten files
fs.writeFileSync(
openApiGeneratedFilesManifestPath,
[...generatedFiles, ...additionalGeneratedFiles].join("\n")
);
})();