forked from Azure/autorest.typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescriptGenerator.ts
executable file
·146 lines (130 loc) · 5.21 KB
/
typescriptGenerator.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as prettier from "prettier";
import * as fsextra from "fs-extra";
import * as path from "path";
import { CodeModel } from "@autorest/codemodel";
import { Project, IndentationText } from "ts-morph";
import { AutorestExtensionHost } from "@autorest/extension-base";
import { transformCodeModel } from "./transforms/transforms";
import { transformSamples } from './transforms/samplesTransforms';
import { generateClient } from "./generators/clientFileGenerator";
import { generateModels } from "./generators/modelsGenerator";
import { generateMappers } from "./generators/mappersGenerator";
import { generateIndexFile } from "./generators/indexGenerator";
import { generatePackageJson } from "./generators/static/packageFileGenerator";
import { generateApiExtractorConfig } from "./generators/static/apiExtractorConfig";
import { generateLicenseFile } from "./generators/static/licenseFileGenerator";
import { generateReadmeFile } from "./generators/static/readmeFileGenerator";
import { generateSampleTestFile } from "./generators/test/sampleTestGenerator";
import { generateTsConfig } from "./generators/static/tsConfigFileGenerator";
import { generateRollupConfig } from "./generators/static/rollupConfigFileGenerator";
import { generateOperations } from "./generators/operationGenerator";
import { generateOperationsInterfaces } from "./generators/operationInterfaceGenerator";
import { generateSampleEnv } from './generators/samples/sampleEnvGenerator';
import { generateSamples } from './generators/samples/sampleGenerator';
import { generateParameters } from "./generators/parametersGenerator";
import { generateLroFiles } from "./generators/LROGenerator";
import { generateTracingFile } from "./generators/tracingFileGenerator";
import { getAutorestOptions } from "./autorestSession";
import { conflictResolver } from "./conflictResolver";
const prettierTypeScriptOptions: prettier.Options = {
parser: "typescript",
arrowParens: "always",
bracketSpacing: true,
endOfLine: "lf",
printWidth: 80,
semi: true,
singleQuote: false,
tabWidth: 2
};
const prettierJSONOptions: prettier.Options = {
parser: "json",
tabWidth: 2,
semi: false,
singleQuote: false
};
export async function generateTypeScriptLibrary(
codeModel: CodeModel,
host: AutorestExtensionHost
): Promise<void> {
const project = new Project({
useInMemoryFileSystem: true,
manipulationSettings: {
indentationText: IndentationText.TwoSpaces
}
});
const {
packageDetails,
licenseHeader: shouldGenerateLicense,
generateTest,
generateSample,
outputPath,
srcPath
} = getAutorestOptions();
const clientDetails = await transformCodeModel(codeModel);
conflictResolver(clientDetails);
if (generateSample) {
clientDetails.samples = await transformSamples(codeModel, clientDetails);
}
// Skip metadata generation if `generate-metadata` is explicitly false
generatePackageJson(project, clientDetails);
generateLicenseFile(project);
generateReadmeFile(clientDetails, project);
if (generateTest) {
generateSampleTestFile(project);
}
generateTsConfig(project);
generateRollupConfig(project);
generateApiExtractorConfig(project);
generateClient(clientDetails, project);
generateModels(clientDetails, project);
generateMappers(clientDetails, project);
generateOperations(clientDetails, project);
generateOperationsInterfaces(clientDetails, project);
if (generateSample && clientDetails?.samples?.length && clientDetails?.samples?.length > 0) {
generateSamples(clientDetails, project);
generateSampleEnv(project);
}
generateParameters(clientDetails, project);
generateIndexFile(project, clientDetails);
await generateLroFiles(clientDetails, project);
generateTracingFile(project);
const licenseHeader = `
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/
`;
// Save the source files to the virtual filesystem
project.saveSync();
const fs = project.getFileSystem();
const pathToClear = outputPath ? path.join(outputPath, srcPath) : srcPath;
fsextra.emptyDirSync(`${pathToClear}`);
// Loop over the files
for (const file of project.getSourceFiles()) {
const filePath = file.getFilePath();
const isJson = /\.json$/gi.test(filePath);
const isSourceCode = /\.(ts|js)$/gi.test(filePath);
let fileContents = fs.readFileSync(filePath);
// Add the license header to source code files
if (shouldGenerateLicense && isSourceCode) {
fileContents = `${licenseHeader.trimLeft()}\n${fileContents}`;
}
// Format the contents if necessary
if (isJson || isSourceCode) {
fileContents = prettier.format(
fileContents,
isJson ? prettierJSONOptions : prettierTypeScriptOptions
);
}
// Write the file to the AutoRest host
host.writeFile({
filename: filePath.substr(1), // Get rid of the leading slash '/'
content: fileContents
});
}
}