-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathemitter.ts
More file actions
67 lines (59 loc) · 2.3 KB
/
Copy pathemitter.ts
File metadata and controls
67 lines (59 loc) · 2.3 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
import { emitFile, getDirectoryPath, resolvePath, type EmitContext } from "@typespec/compiler";
import { createSdkContext } from "@azure-tools/typespec-client-generator-core";
import { stringify as stringifyYaml } from "yaml";
import packageJson from "../package.json" with { type: "json" };
import { buildSpecMetadata, collectLanguagePackages } from "./collector.js";
import type { MetadataSnapshot } from "./metadata.js";
import {
normalizeOptions,
type MetadataEmitterOptions,
type NormalizedMetadataEmitterOptions,
} from "./options.js";
const SNAPSHOT_VERSION = packageJson.version;
export async function $onEmit(context: EmitContext<MetadataEmitterOptions>): Promise<void> {
const options = normalizeOptions(context.options);
const typespecMetadata = buildSpecMetadata(context.program);
// Get the common tsp-output directory (parent of this emitter's output dir)
const commonOutputDir = getDirectoryPath(getDirectoryPath(context.emitterOutputDir));
// Resolve API version using TCGC
const sdkContext = await createSdkContext(context as any);
const apiVersionsMap = sdkContext.sdkPackage.metadata.apiVersions;
let resolvedApiVersion: string | undefined;
if (apiVersionsMap && apiVersionsMap.size > 0) {
if (apiVersionsMap.size > 1) {
resolvedApiVersion = "multiple-versions";
} else {
resolvedApiVersion = [...apiVersionsMap.values()][0];
}
}
const languageResult = await collectLanguagePackages(
context.program,
commonOutputDir,
resolvedApiVersion,
);
const snapshot: MetadataSnapshot = {
emitterVersion: SNAPSHOT_VERSION,
generatedAt: new Date().toISOString(),
typespec: typespecMetadata,
languages: languageResult.languages,
sourceConfigPath: languageResult.sourceConfigPath,
};
await writeSnapshot(context, options, snapshot);
}
async function writeSnapshot(
context: EmitContext<MetadataEmitterOptions>,
options: NormalizedMetadataEmitterOptions,
snapshot: MetadataSnapshot,
): Promise<void> {
const serialized =
options.format === "json"
? JSON.stringify(snapshot, null, 2) + "\n"
: stringifyYaml(snapshot, {
lineWidth: 0,
});
const outputPath = resolvePath(context.emitterOutputDir, options.outputFile);
await emitFile(context.program, {
path: outputPath,
content: serialized,
});
}