-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathindex.ts
More file actions
364 lines (346 loc) · 15.9 KB
/
index.ts
File metadata and controls
364 lines (346 loc) · 15.9 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { npmCommand } from "./npm.js";
import { createTempDirectory, removeDirectory, readTspLocation, getEmitterFromRepoConfig } from "./fs.js";
import { Logger, printBanner, enableDebug, printVersion } from "./log.js";
import { TspLocation, compileTsp, discoverMainFile, resolveTspConfigUrl } from "./typespec.js";
import { getOptions } from "./options.js";
import { mkdir, writeFile, cp, readFile, access, stat } from "node:fs/promises";
import { addSpecFiles, checkoutCommit, cloneRepo, getRepoRoot, sparseCheckout } from "./git.js";
import { doesFileExist } from "./network.js";
import { parse as parseYaml } from "yaml";
import { joinPaths, normalizePath, resolvePath } from "@typespec/compiler";
import { formatAdditionalDirectories, getAdditionalDirectoryName } from "./utils.js";
import { resolve } from "node:path";
import { spawn } from "node:child_process";
import { config as dotenvConfig } from "dotenv";
async function sdkInit(
{
config,
outputDir,
emitter,
commit,
repo,
isUrl,
}: {
config: string;
outputDir: string;
emitter: string;
commit: string | undefined;
repo: string | undefined;
isUrl: boolean;
}): Promise<string> {
if (isUrl) {
// URL scenario
const repoRoot = await getRepoRoot(outputDir);
const resolvedConfigUrl = resolveTspConfigUrl(config);
const cloneDir = joinPaths(repoRoot, "..", "sparse-spec");
await mkdir(cloneDir, { recursive: true });
Logger.debug(`Created temporary sparse-checkout directory ${cloneDir}`);
Logger.debug(`Cloning repo to ${cloneDir}`);
await cloneRepo(outputDir, cloneDir, `https://github.com/${resolvedConfigUrl.repo}.git`);
await sparseCheckout(cloneDir);
const tspConfigPath = joinPaths(resolvedConfigUrl.path, "tspconfig.yaml");
await addSpecFiles(cloneDir, tspConfigPath)
await checkoutCommit(cloneDir, resolvedConfigUrl.commit);
let data;
try {
data = await readFile(joinPaths(cloneDir, tspConfigPath), "utf8");
} catch (err) {
throw new Error(`Could not read tspconfig.yaml at ${tspConfigPath}. Error: ${err}`);
}
if (!data) {
throw new Error(`tspconfig.yaml is empty at ${tspConfigPath}`);
}
const configYaml = parseYaml(data);
const serviceDir = configYaml?.parameters?.["service-dir"]?.default;
if (!serviceDir) {
throw new Error(`Parameter service-dir is not defined correctly in tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`)
}
Logger.debug(`Service directory: ${serviceDir}`)
const packageDir: string | undefined = configYaml?.options?.[emitter]?.["package-dir"];
if (!packageDir) {
throw new Error(`Missing package-dir in ${emitter} options of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`);
}
const newPackageDir = joinPaths(outputDir, serviceDir, packageDir!)
await mkdir(newPackageDir, { recursive: true });
const additionalDirOutput = formatAdditionalDirectories(configYaml?.parameters?.dependencies?.additionalDirectories);
await writeFile(
joinPaths(newPackageDir, "tsp-location.yaml"),
`directory: ${resolvedConfigUrl.path}\ncommit: ${resolvedConfigUrl.commit}\nrepo: ${resolvedConfigUrl.repo}\nadditionalDirectories:${additionalDirOutput}\n`);
Logger.debug(`Removing sparse-checkout directory ${cloneDir}`);
await removeDirectory(cloneDir);
return newPackageDir;
} else {
// Local directory scenario
if (!config.endsWith("tspconfig.yaml")) {
config = joinPaths(config, "tspconfig.yaml");
}
let data;
try {
data = await readFile(config, "utf8");
} catch (err) {
throw new Error(`Could not read tspconfig.yaml at ${config}`);
}
if (!data) {
throw new Error(`tspconfig.yaml is empty at ${config}`);
}
const configYaml = parseYaml(data);
const serviceDir = configYaml?.parameters?.["service-dir"]?.default;
if (!serviceDir) {
throw new Error(`Parameter service-dir is not defined correctly in tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`)
}
Logger.debug(`Service directory: ${serviceDir}`)
const additionalDirOutput = formatAdditionalDirectories(configYaml?.parameters?.dependencies?.additionalDirectories);
const packageDir = configYaml?.options?.[emitter]?.["package-dir"];
if (!packageDir) {
throw new Error(`Missing package-dir in ${emitter} options of tspconfig.yaml. Please refer to https://github.com/Azure/azure-rest-api-specs/blob/main/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml for the right schema.`);
}
const newPackageDir = joinPaths(outputDir, serviceDir, packageDir)
await mkdir(newPackageDir, { recursive: true });
config = config.replaceAll("\\", "/");
const matchRes = config.match('.*/(?<path>specification/.*)/tspconfig.yaml$')
var directory = "";
if (matchRes) {
if (matchRes.groups) {
directory = matchRes.groups!["path"]!;
}
}
writeFile(joinPaths(newPackageDir, "tsp-location.yaml"),
`directory: ${directory}\ncommit: ${commit}\nrepo: ${repo}\nadditionalDirectories:${additionalDirOutput}\n`);
return newPackageDir;
}
}
async function syncTspFiles(outputDir: string, localSpecRepo?: string) {
const tempRoot = await createTempDirectory(outputDir);
const repoRoot = await getRepoRoot(outputDir);
Logger.debug(`Repo root is ${repoRoot}`);
if (!repoRoot) {
throw new Error("Could not find repo root");
}
const tspLocation: TspLocation = await readTspLocation(outputDir);
const dirSplit = tspLocation.directory.split("/");
let projectName = dirSplit[dirSplit.length - 1];
Logger.debug(`Using project name: ${projectName}`)
if (!projectName) {
projectName = "src";
}
const srcDir = joinPaths(tempRoot, projectName);
await mkdir(srcDir, { recursive: true });
if (localSpecRepo) {
Logger.debug(`Using local spec directory: ${localSpecRepo}`);
function filter(src: string): boolean {
if (src.includes("node_modules")) {
return false;
}
if (src.includes("tsp-output")) {
return false;
}
return true;
}
await cp(localSpecRepo, srcDir, { recursive: true, filter: filter });
const localSpecRepoRoot = await getRepoRoot(localSpecRepo);
Logger.info(`Local spec repo root is ${localSpecRepoRoot}`)
if (!localSpecRepoRoot) {
throw new Error("Could not find local spec repo root, please make sure the path is correct");
}
for (const dir of tspLocation.additionalDirectories!) {
Logger.info(`Syncing additional directory: ${dir}`);;
await cp(joinPaths(localSpecRepoRoot, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true, filter: filter });
}
} else {
const cloneDir = joinPaths(repoRoot, "..", "sparse-spec");
await mkdir(cloneDir, { recursive: true });
Logger.debug(`Created temporary sparse-checkout directory ${cloneDir}`);
Logger.debug(`Cloning repo to ${cloneDir}`);
await cloneRepo(tempRoot, cloneDir, `https://github.com/${tspLocation.repo}.git`);
await sparseCheckout(cloneDir);
await addSpecFiles(cloneDir, tspLocation.directory)
for (const dir of tspLocation.additionalDirectories ?? []) {
Logger.info(`Processing additional directory: ${dir}`);
await addSpecFiles(cloneDir, dir);
}
await checkoutCommit(cloneDir, tspLocation.commit);
await cp(joinPaths(cloneDir, tspLocation.directory), srcDir, { recursive: true });
for (const dir of tspLocation.additionalDirectories!) {
Logger.info(`Syncing additional directory: ${dir}`);
await cp(joinPaths(cloneDir, dir), joinPaths(tempRoot, getAdditionalDirectoryName(dir)), { recursive: true });
}
Logger.debug(`Removing sparse-checkout directory ${cloneDir}`);
await removeDirectory(cloneDir);
}
try {
const emitterLockPath = joinPaths(repoRoot, "eng", "emitter-package-lock.json");
await cp(emitterLockPath, joinPaths(srcDir, "package-lock.json"), { recursive: true });
} catch (err) {
Logger.debug(`Ran into the following error when looking for emitter-package-lock.json: ${err}`);
Logger.debug("Will attempt look for emitter-package.json...");
}
try {
const emitterPath = joinPaths(repoRoot, "eng", "emitter-package.json");
await cp(emitterPath, joinPaths(srcDir, "package.json"), { recursive: true });
} catch (err) {
throw new Error(`Ran into the following error: ${err}\nTo continue using tsp-client, please provide a valid emitter-package.json file in the eng/ directory of the repository.`);
}
}
async function generate({
rootUrl,
noCleanup,
additionalEmitterOptions,
}: {
rootUrl: string;
noCleanup: boolean;
additionalEmitterOptions?: string;
}) {
const tempRoot = joinPaths(rootUrl, "TempTypeSpecFiles");
const tspLocation = await readTspLocation(rootUrl);
const dirSplit = tspLocation.directory.split("/");
let projectName = dirSplit[dirSplit.length - 1];
if (!projectName) {
throw new Error("cannot find project name");
}
const srcDir = joinPaths(tempRoot, projectName);
const emitter = await getEmitterFromRepoConfig(joinPaths(await getRepoRoot(rootUrl), "eng", "emitter-package.json"));
if (!emitter) {
throw new Error("emitter is undefined");
}
const mainFilePath = await discoverMainFile(srcDir);
const resolvedMainFilePath = joinPaths(srcDir, mainFilePath);
Logger.info("Installing dependencies from npm...");
const args: string[] = [];
try {
// Check if package-lock.json exists, if it does, we'll install dependencies through `npm ci`
await stat(joinPaths(srcDir, "package-lock.json"));
args.push("ci");
} catch (err) {
// If package-lock.json doesn't exist, we'll attempt to install dependencies through `npm install`
args.push("install");
}
// NOTE: This environment variable should be used for developer testing only. A force
// install may ignore any conflicting dependencies and result in unexpected behavior.
dotenvConfig({path: resolve(await getRepoRoot(rootUrl), ".env")});
if (process.env['TSPCLIENT_FORCE_INSTALL']?.toLowerCase() === "true") {
args.push("--force");
}
await npmCommand(srcDir, args);
await compileTsp({ emitterPackage: emitter, outputPath: rootUrl, resolvedMainFilePath, saveInputs: noCleanup, additionalEmitterOptions });
if (noCleanup) {
Logger.debug(`Skipping cleanup of temp directory: ${tempRoot}`);
} else {
Logger.debug("Cleaning up temp directory");
await removeDirectory(tempRoot);
}
}
async function convert(readme: string, outputDir: string): Promise<void> {
return new Promise((resolve, reject) => {
const autorest = spawn("npx", ["autorest", readme, "--openapi-to-typespec", "--use=@autorest/openapi-to-typespec", `--output-folder=${outputDir}`], {
cwd: outputDir,
stdio: "inherit",
shell: true,
});
autorest.once("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`openapi to typespec conversion failed exited with code ${code}`));
}
});
autorest.once("error", (err) => {
reject(new Error(`openapi to typespec conversion failed with error: ${err}`));
});
});
}
async function generateLockFile(rootUrl: string, repoRoot: string) {
Logger.info("Generating lock file...");
const args: string[] = ["install"];
if (process.env['TSPCLIENT_FORCE_INSTALL']?.toLowerCase() === "true") {
args.push("--force");
}
const tempRoot = await createTempDirectory(rootUrl);
await cp(joinPaths(repoRoot, "eng", "emitter-package.json"), joinPaths(tempRoot, "package.json"));
await npmCommand(tempRoot, args);
const lockFile = await stat(joinPaths(tempRoot, "package-lock.json"));
if (lockFile.isFile()) {
await cp(joinPaths(tempRoot, "package-lock.json"), joinPaths(repoRoot, "eng", "emitter-package-lock.json"));
}
await removeDirectory(tempRoot);
Logger.info(`Lock file generated in ${joinPaths(rootUrl, "emitter-package-lock.json")}`);
}
async function main() {
const options = await getOptions();
if (options.debug) {
enableDebug();
}
printBanner();
await printVersion();
let rootUrl = resolvePath(".");
if (options.outputDir) {
rootUrl = resolvePath(options.outputDir);
}
const repoRoot = await getRepoRoot(rootUrl);
try {
// FIXME: this is a workaround meanwhile we fix the issue with failing to delete the sparse-spec directory
// Tracking issue: https://github.com/Azure/azure-sdk-tools/issues/7636
access(joinPaths(repoRoot, "..", "sparse-spec")).then(() => {
Logger.debug("Deleting existing sparse-spec directory");
removeDirectory(joinPaths(repoRoot, "..", "sparse-spec"));
}).catch(() => {});
} catch (err) {
Logger.debug(`Error occurred while attempting to remove sparse-spec directory: ${err}`);
}
if (options.generateLockFile) {
await generateLockFile(rootUrl, repoRoot);
return;
}
switch (options.command) {
case "init":
const emitter = await getEmitterFromRepoConfig(joinPaths(repoRoot, "eng", "emitter-package.json"));
if (!emitter) {
throw new Error("Couldn't find emitter-package.json in the repo");
}
const outputDir = await sdkInit({config: options.tspConfig!, outputDir: rootUrl, emitter, commit: options.commit, repo: options.repo, isUrl: options.isUrl});
Logger.info(`SDK initialized in ${outputDir}`);
if (!options.skipSyncAndGenerate) {
await syncTspFiles(outputDir);
await generate({ rootUrl: outputDir, noCleanup: options.noCleanup, additionalEmitterOptions: options.emitterOptions});
}
break;
case "sync":
await syncTspFiles(rootUrl, options.localSpecRepo);
break;
case "generate":
await generate({ rootUrl, noCleanup: options.noCleanup, additionalEmitterOptions: options.emitterOptions});
break;
case "update":
if (options.repo && !options.commit) {
throw new Error("Commit SHA is required when specifying `--repo`, please specify a commit using `--commit`");
}
if (options.commit) {
const tspLocation: TspLocation = await readTspLocation(rootUrl);
tspLocation.commit = options.commit ?? tspLocation.commit;
tspLocation.repo = options.repo ?? tspLocation.repo;
await writeFile(joinPaths(rootUrl, "tsp-location.yaml"), `directory: ${tspLocation.directory}\ncommit: ${tspLocation.commit}\nrepo: ${tspLocation.repo}\nadditionalDirectories: ${tspLocation.additionalDirectories}`);
} else if (options.tspConfig) {
const tspLocation: TspLocation = await readTspLocation(rootUrl);
const tspConfig = resolveTspConfigUrl(options.tspConfig);
tspLocation.commit = tspConfig.commit ?? tspLocation.commit;
tspLocation.repo = tspConfig.repo ?? tspLocation.repo;
await writeFile(joinPaths(rootUrl, "tsp-location.yaml"), `directory: ${tspLocation.directory}\ncommit: ${tspLocation.commit}\nrepo: ${tspLocation.repo}\nadditionalDirectories: ${tspLocation.additionalDirectories}`);
}
await syncTspFiles(rootUrl, options.localSpecRepo);
await generate({ rootUrl, noCleanup: options.noCleanup, additionalEmitterOptions: options.emitterOptions});
break;
case "convert":
Logger.info("Converting swagger to typespec...");
let readme = options.swaggerReadme!;
if (await doesFileExist(readme)) {
readme = normalizePath(resolve(readme));
}
await convert(readme, rootUrl);
break;
default:
throw new Error(`Unknown command: ${options.command}`);
}
}
main().catch((err) => {
Logger.error(err);
process.exit(1);
});