@c7-digital/ledger 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BUILD.md +125 -0
- package/LICENSE +17 -0
- package/README.md +97 -0
- package/lib/scripts/build.d.ts +9 -0
- package/lib/scripts/build.js +366 -0
- package/lib/scripts/enhance-types.d.ts +3 -0
- package/lib/scripts/enhance-types.js +174 -0
- package/lib/scripts/generate-asyncapi-types.d.ts +9 -0
- package/lib/scripts/generate-asyncapi-types.js +129 -0
- package/lib/scripts/generate-schema-modules.d.ts +3 -0
- package/lib/scripts/generate-schema-modules.js +33 -0
- package/lib/src/TemplateEmitterMap.d.ts +40 -0
- package/lib/src/TemplateEmitterMap.js +57 -0
- package/lib/src/client.d.ts +55 -0
- package/lib/src/client.js +62 -0
- package/lib/src/generated/api.d.ts +7147 -0
- package/lib/src/generated/api.js +1 -0
- package/lib/src/generated/async-api.d.ts +1665 -0
- package/lib/src/generated/async-api.js +1 -0
- package/lib/src/generated/asyncapi-schema.d.ts +1 -0
- package/lib/src/generated/asyncapi-schema.js +2233 -0
- package/lib/src/generated/openapi-schema.d.ts +1 -0
- package/lib/src/generated/openapi-schema.js +7321 -0
- package/lib/src/generated/sdk-version.d.ts +1 -0
- package/lib/src/generated/sdk-version.js +3 -0
- package/lib/src/index.d.ts +8 -0
- package/lib/src/index.js +8 -0
- package/lib/src/ledger.d.ts +188 -0
- package/lib/src/ledger.js +849 -0
- package/lib/src/ledger.test.d.ts +1 -0
- package/lib/src/ledger.test.js +23 -0
- package/lib/src/logger.d.ts +41 -0
- package/lib/src/logger.js +56 -0
- package/lib/src/multistream.d.ts +47 -0
- package/lib/src/multistream.js +123 -0
- package/lib/src/translate.d.ts +5 -0
- package/lib/src/translate.js +30 -0
- package/lib/src/types.d.ts +201 -0
- package/lib/src/types.js +1 -0
- package/lib/src/util.d.ts +3 -0
- package/lib/src/util.js +7 -0
- package/lib/src/validation.d.ts +27 -0
- package/lib/src/validation.js +182 -0
- package/lib/src/valueTypes.d.ts +34 -0
- package/lib/src/valueTypes.js +76 -0
- package/lib/src/websocket.d.ts +69 -0
- package/lib/src/websocket.js +125 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib-lite/scripts/build.d.ts +9 -0
- package/lib-lite/scripts/build.js +366 -0
- package/lib-lite/scripts/enhance-types.d.ts +3 -0
- package/lib-lite/scripts/enhance-types.js +174 -0
- package/lib-lite/scripts/generate-asyncapi-types.d.ts +9 -0
- package/lib-lite/scripts/generate-asyncapi-types.js +129 -0
- package/lib-lite/scripts/generate-schema-modules.d.ts +3 -0
- package/lib-lite/scripts/generate-schema-modules.js +33 -0
- package/lib-lite/src/TemplateEmitterMap.d.ts +40 -0
- package/lib-lite/src/TemplateEmitterMap.js +57 -0
- package/lib-lite/src/client.d.ts +55 -0
- package/lib-lite/src/client.js +62 -0
- package/lib-lite/src/generated/api.d.ts +7147 -0
- package/lib-lite/src/generated/api.js +1 -0
- package/lib-lite/src/generated/async-api.d.ts +1665 -0
- package/lib-lite/src/generated/async-api.js +1 -0
- package/lib-lite/src/generated/asyncapi-schema.d.ts +1 -0
- package/lib-lite/src/generated/asyncapi-schema.js +2 -0
- package/lib-lite/src/generated/openapi-schema.d.ts +1 -0
- package/lib-lite/src/generated/openapi-schema.js +2 -0
- package/lib-lite/src/generated/sdk-version.d.ts +1 -0
- package/lib-lite/src/generated/sdk-version.js +3 -0
- package/lib-lite/src/index.d.ts +8 -0
- package/lib-lite/src/index.js +8 -0
- package/lib-lite/src/ledger.d.ts +188 -0
- package/lib-lite/src/ledger.js +849 -0
- package/lib-lite/src/ledger.test.d.ts +1 -0
- package/lib-lite/src/ledger.test.js +23 -0
- package/lib-lite/src/logger.d.ts +41 -0
- package/lib-lite/src/logger.js +56 -0
- package/lib-lite/src/multistream.d.ts +47 -0
- package/lib-lite/src/multistream.js +123 -0
- package/lib-lite/src/translate.d.ts +5 -0
- package/lib-lite/src/translate.js +30 -0
- package/lib-lite/src/types.d.ts +201 -0
- package/lib-lite/src/types.js +1 -0
- package/lib-lite/src/util.d.ts +3 -0
- package/lib-lite/src/util.js +7 -0
- package/lib-lite/src/validation.d.ts +14 -0
- package/lib-lite/src/validation.js +31 -0
- package/lib-lite/src/valueTypes.d.ts +34 -0
- package/lib-lite/src/valueTypes.js +76 -0
- package/lib-lite/src/websocket.d.ts +69 -0
- package/lib-lite/src/websocket.js +125 -0
- package/lib-lite/tsconfig.temp.tsbuildinfo +1 -0
- package/package.json +72 -0
- package/scripts/build.ts +456 -0
- package/scripts/enhance-types.ts +223 -0
- package/scripts/generate-asyncapi-types.ts +158 -0
- package/scripts/generate-schema-modules.ts +52 -0
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@c7-digital/ledger",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/src/index.js",
|
|
7
|
+
"types": "lib/src/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib",
|
|
10
|
+
"lib-lite",
|
|
11
|
+
"spec",
|
|
12
|
+
"scripts",
|
|
13
|
+
"BUILD.md"
|
|
14
|
+
],
|
|
15
|
+
"description": "OpenAPI v2 compatible Daml ledger client",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./lib/src/index.js",
|
|
19
|
+
"types": "./lib/src/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./lite": {
|
|
22
|
+
"import": "./lib-lite/src/index.js",
|
|
23
|
+
"types": "./lib-lite/src/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"bin": {
|
|
27
|
+
"ledger-build": "./scripts/build.ts"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsx scripts/build.ts --sdk-version=3.4.7",
|
|
31
|
+
"compile": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json",
|
|
32
|
+
"watch": "tsc --watch",
|
|
33
|
+
"clean": "rm -rf lib lib-lite src/generated",
|
|
34
|
+
"prepare": "pnpm build",
|
|
35
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
36
|
+
"lint": "eslint --ext .ts src/"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@daml/types": "3.4.8",
|
|
40
|
+
"ajv": "^6.12.6",
|
|
41
|
+
"cross-fetch": "^3.2.0",
|
|
42
|
+
"eventemitter3": "^5.0.1",
|
|
43
|
+
"isomorphic-ws": "^5.0.0",
|
|
44
|
+
"jose": "^6.0.10"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"yaml": "^2.8.1"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"yaml": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=18.0.0",
|
|
56
|
+
"pnpm": ">=8.0.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/ajv": "^0.0.5",
|
|
60
|
+
"@types/jest": "^30.0.0",
|
|
61
|
+
"@types/node": "^18.15.11",
|
|
62
|
+
"@types/ws": "^8.18.1",
|
|
63
|
+
"eslint": "^9.39.0",
|
|
64
|
+
"jest": "^29.7.0",
|
|
65
|
+
"openapi-typescript": "^7.4.2",
|
|
66
|
+
"ts-jest": "^29.4.6",
|
|
67
|
+
"tsc-alias": "^1.8.16",
|
|
68
|
+
"tsx": "^4.11.0",
|
|
69
|
+
"typescript": "^5.8.3",
|
|
70
|
+
"yaml": "^2.8.1"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/scripts/build.ts
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unified build script that uses SDK version-based spec files and runs all build steps
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { join, dirname } from "path";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
import { exec } from "child_process";
|
|
10
|
+
import { promisify } from "util";
|
|
11
|
+
import { readdir, writeFile, mkdir, unlink, copyFile } from "fs/promises";
|
|
12
|
+
import { existsSync } from "fs";
|
|
13
|
+
|
|
14
|
+
import { brandTypes } from "./enhance-types.js";
|
|
15
|
+
import { generateSchemaModule } from "./generate-schema-modules.js";
|
|
16
|
+
import { generateAsyncApiTypes } from "./generate-asyncapi-types.js";
|
|
17
|
+
|
|
18
|
+
const execAsync = promisify(exec);
|
|
19
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const __dirname = dirname(__filename);
|
|
21
|
+
const projectRoot = join(__dirname, "..");
|
|
22
|
+
|
|
23
|
+
interface SpecFiles {
|
|
24
|
+
openapi?: string;
|
|
25
|
+
asyncapi?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get SDK version from command line arguments or discover latest
|
|
30
|
+
*/
|
|
31
|
+
function getSdkVersion(): string | undefined {
|
|
32
|
+
const args = process.argv.slice(2);
|
|
33
|
+
const versionArg = args.find(arg => arg.startsWith("--sdk-version="));
|
|
34
|
+
|
|
35
|
+
if (versionArg) {
|
|
36
|
+
return versionArg.split("=")[1];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Discover spec files based on SDK version or find latest version-based specs
|
|
44
|
+
*/
|
|
45
|
+
async function discoverSpecFiles(
|
|
46
|
+
sdkVersion?: string
|
|
47
|
+
): Promise<{ specs: SpecFiles; version: string }> {
|
|
48
|
+
const specsDir = join(projectRoot, "specs");
|
|
49
|
+
const files = await readdir(specsDir);
|
|
50
|
+
|
|
51
|
+
if (sdkVersion) {
|
|
52
|
+
console.log(`🔍 Looking for spec files with SDK version: ${sdkVersion}`);
|
|
53
|
+
|
|
54
|
+
const openApiFile = `openapi_${sdkVersion}.yaml`;
|
|
55
|
+
const asyncApiFile = `asyncapi_${sdkVersion}.yaml`;
|
|
56
|
+
|
|
57
|
+
const result: SpecFiles = {};
|
|
58
|
+
|
|
59
|
+
if (files.includes(openApiFile)) {
|
|
60
|
+
result.openapi = join(specsDir, openApiFile);
|
|
61
|
+
console.log(`📄 Found OpenAPI spec: ${openApiFile}`);
|
|
62
|
+
} else {
|
|
63
|
+
console.warn(`⚠️ OpenAPI spec not found: ${openApiFile}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (files.includes(asyncApiFile)) {
|
|
67
|
+
result.asyncapi = join(specsDir, asyncApiFile);
|
|
68
|
+
console.log(`📄 Found AsyncAPI spec: ${asyncApiFile}`);
|
|
69
|
+
} else {
|
|
70
|
+
console.warn(`⚠️ AsyncAPI spec not found: ${asyncApiFile}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { specs: result, version: sdkVersion };
|
|
74
|
+
} else {
|
|
75
|
+
console.log("🔍 Auto-discovering latest version-based spec files...");
|
|
76
|
+
|
|
77
|
+
// Look for files with version pattern (not date pattern)
|
|
78
|
+
const versionPattern = /\d+\.\d+\.\d+(-snapshot\.\d{8}\.\d+)?/;
|
|
79
|
+
const openApiFiles = files
|
|
80
|
+
.filter(f => f.startsWith("openapi_") && f.endsWith(".yaml") && versionPattern.test(f))
|
|
81
|
+
.sort()
|
|
82
|
+
.reverse();
|
|
83
|
+
const asyncApiFiles = files
|
|
84
|
+
.filter(f => f.startsWith("asyncapi_") && f.endsWith(".yaml") && versionPattern.test(f))
|
|
85
|
+
.sort()
|
|
86
|
+
.reverse();
|
|
87
|
+
|
|
88
|
+
const result: SpecFiles = {};
|
|
89
|
+
let detectedVersion = "";
|
|
90
|
+
|
|
91
|
+
if (openApiFiles.length > 0) {
|
|
92
|
+
result.openapi = join(specsDir, openApiFiles[0]!);
|
|
93
|
+
console.log(`📄 Found OpenAPI spec: ${openApiFiles[0]}`);
|
|
94
|
+
// Extract version from filename
|
|
95
|
+
const match = openApiFiles[0]!.match(/openapi_(.+)\.yaml/);
|
|
96
|
+
if (match) detectedVersion = match[1]!;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (asyncApiFiles.length > 0) {
|
|
100
|
+
result.asyncapi = join(specsDir, asyncApiFiles[0]!);
|
|
101
|
+
console.log(`📄 Found AsyncAPI spec: ${asyncApiFiles[0]}`);
|
|
102
|
+
// Use version from AsyncAPI if OpenAPI wasn't found
|
|
103
|
+
if (!detectedVersion) {
|
|
104
|
+
const match = asyncApiFiles[0]!.match(/asyncapi_(.+)\.yaml/);
|
|
105
|
+
if (match) detectedVersion = match[1]!;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!detectedVersion) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
"Could not detect SDK version from spec files. Please specify --sdk-version=<version>"
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
console.log(`🎯 Detected SDK version: ${detectedVersion}`);
|
|
116
|
+
return { specs: result, version: detectedVersion };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Generate OpenAPI types using openapi-typescript
|
|
122
|
+
*/
|
|
123
|
+
async function generateTypes(specPath: string): Promise<void> {
|
|
124
|
+
console.log("🔄 Generating OpenAPI types...");
|
|
125
|
+
const outputPath = join(projectRoot, "src", "generated", "api.ts");
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const { stdout, stderr } = await execAsync(
|
|
129
|
+
`pnpm exec openapi-typescript "${specPath}" --output "${outputPath}"`
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (stderr && !stderr.includes("Warning")) {
|
|
133
|
+
console.warn("openapi-typescript warnings:", stderr);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (stdout) {
|
|
137
|
+
console.log(stdout);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log("✅ Generated OpenAPI types");
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error("❌ Error generating OpenAPI types:", (error as Error).message);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Generate schema modules for both OpenAPI and AsyncAPI
|
|
149
|
+
*/
|
|
150
|
+
function generateSchemaModules(specs: SpecFiles): void {
|
|
151
|
+
console.log("🔄 Generating schema modules...");
|
|
152
|
+
const outputDir = join(projectRoot, "src", "generated");
|
|
153
|
+
|
|
154
|
+
if (specs.openapi) {
|
|
155
|
+
generateSchemaModule(specs.openapi, join(outputDir, "openapi-schema.ts"), "OPENAPI_SCHEMA");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (specs.asyncapi) {
|
|
159
|
+
generateSchemaModule(specs.asyncapi, join(outputDir, "asyncapi-schema.ts"), "ASYNCAPI_SCHEMA");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
console.log("✅ All schema modules generated");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Ensure the generated directory exists
|
|
167
|
+
*/
|
|
168
|
+
async function ensureGeneratedDirectory(): Promise<void> {
|
|
169
|
+
const outputDir = join(projectRoot, "src", "generated");
|
|
170
|
+
await mkdir(outputDir, { recursive: true });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Clean up any temporary files
|
|
175
|
+
*/
|
|
176
|
+
async function cleanupTempFiles(): Promise<void> {
|
|
177
|
+
const generatedDir = join(projectRoot, "src", "generated");
|
|
178
|
+
const tempFiles = [
|
|
179
|
+
join(generatedDir, "api.ts.tmp"),
|
|
180
|
+
join(generatedDir, "async-api.ts.tmp"),
|
|
181
|
+
join(projectRoot, "temp-asyncapi-as-openapi.yaml"),
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
for (const tempFile of tempFiles) {
|
|
185
|
+
if (existsSync(tempFile)) {
|
|
186
|
+
try {
|
|
187
|
+
await unlink(tempFile);
|
|
188
|
+
console.log(`🧹 Cleaned up ${tempFile}`);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
// Ignore cleanup errors, but log them
|
|
191
|
+
console.warn(`⚠️ Could not clean up ${tempFile}:`, (error as Error).message);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Generate SDK version constant file
|
|
199
|
+
*/
|
|
200
|
+
async function generateSdkVersionFile(version: string): Promise<void> {
|
|
201
|
+
console.log("🔄 Generating SDK version constant...");
|
|
202
|
+
const outputDir = join(projectRoot, "src", "generated");
|
|
203
|
+
const outputPath = join(outputDir, "sdk-version.ts");
|
|
204
|
+
|
|
205
|
+
const content = `// Auto-generated file - do not edit manually
|
|
206
|
+
// Generated from SDK version: ${version}
|
|
207
|
+
|
|
208
|
+
export const SDK_VERSION = "${version}";
|
|
209
|
+
`;
|
|
210
|
+
|
|
211
|
+
await writeFile(outputPath, content, "utf-8");
|
|
212
|
+
console.log(`✅ Generated SDK version constant: ${version}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Brand API types with value.proto constraints (in-place)
|
|
217
|
+
*/
|
|
218
|
+
function brandApiTypes(): void {
|
|
219
|
+
console.log("🔄 Branding API types in-place...");
|
|
220
|
+
|
|
221
|
+
// Brand OpenAPI types (overwrite original file)
|
|
222
|
+
const apiPath = join(projectRoot, "src", "generated", "api.ts");
|
|
223
|
+
brandTypes(apiPath, apiPath, "OpenAPI");
|
|
224
|
+
|
|
225
|
+
// Brand AsyncAPI types (overwrite original file)
|
|
226
|
+
const asyncApiPath = join(projectRoot, "src", "generated", "async-api.ts");
|
|
227
|
+
brandTypes(asyncApiPath, asyncApiPath, "AsyncAPI");
|
|
228
|
+
|
|
229
|
+
console.log("✅ All types branded in-place");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Create stub schema files for lite build
|
|
234
|
+
*/
|
|
235
|
+
async function createStubSchemaFiles(): Promise<void> {
|
|
236
|
+
console.log("🔄 Creating stub schema files for lite build...");
|
|
237
|
+
const outputDir = join(projectRoot, "src", "generated");
|
|
238
|
+
|
|
239
|
+
const openApiStub = `// Stub file for lite build - no schema content
|
|
240
|
+
export const OPENAPI_SCHEMA = "";
|
|
241
|
+
`;
|
|
242
|
+
|
|
243
|
+
const asyncApiStub = `// Stub file for lite build - no schema content
|
|
244
|
+
export const ASYNCAPI_SCHEMA = "";
|
|
245
|
+
`;
|
|
246
|
+
|
|
247
|
+
await writeFile(join(outputDir, "openapi-schema.ts"), openApiStub, "utf-8");
|
|
248
|
+
await writeFile(join(outputDir, "asyncapi-schema.ts"), asyncApiStub, "utf-8");
|
|
249
|
+
|
|
250
|
+
console.log("✅ Created stub schema files");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Replace validation module with lite version in lib-lite
|
|
255
|
+
*/
|
|
256
|
+
async function useLiteValidation(): Promise<void> {
|
|
257
|
+
console.log("🔄 Replacing validation with lite version...");
|
|
258
|
+
const libLiteDir = join(projectRoot, "lib-lite", "src");
|
|
259
|
+
|
|
260
|
+
// Copy validation-lite.js over validation.js
|
|
261
|
+
const liteValidationJs = join(libLiteDir, "validation-lite.js");
|
|
262
|
+
const validationJs = join(libLiteDir, "validation.js");
|
|
263
|
+
|
|
264
|
+
if (existsSync(liteValidationJs)) {
|
|
265
|
+
await copyFile(liteValidationJs, validationJs);
|
|
266
|
+
await unlink(liteValidationJs);
|
|
267
|
+
console.log("✅ Replaced validation.js with lite version");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Copy validation-lite.d.ts over validation.d.ts
|
|
271
|
+
const liteValidationDts = join(libLiteDir, "validation-lite.d.ts");
|
|
272
|
+
const validationDts = join(libLiteDir, "validation.d.ts");
|
|
273
|
+
|
|
274
|
+
if (existsSync(liteValidationDts)) {
|
|
275
|
+
await copyFile(liteValidationDts, validationDts);
|
|
276
|
+
await unlink(liteValidationDts);
|
|
277
|
+
console.log("✅ Replaced validation.d.ts with lite version");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Also remove validation-lite files from lib/ (they shouldn't be there)
|
|
281
|
+
const libDir = join(projectRoot, "lib", "src");
|
|
282
|
+
const libLiteValidationJs = join(libDir, "validation-lite.js");
|
|
283
|
+
const libLiteValidationDts = join(libDir, "validation-lite.d.ts");
|
|
284
|
+
|
|
285
|
+
if (existsSync(libLiteValidationJs)) {
|
|
286
|
+
await unlink(libLiteValidationJs);
|
|
287
|
+
}
|
|
288
|
+
if (existsSync(libLiteValidationDts)) {
|
|
289
|
+
await unlink(libLiteValidationDts);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Compile TypeScript code
|
|
295
|
+
*/
|
|
296
|
+
async function compileTypeScript(outDir: string = "lib"): Promise<void> {
|
|
297
|
+
console.log(`🔄 Compiling TypeScript to ${outDir}...`);
|
|
298
|
+
|
|
299
|
+
try {
|
|
300
|
+
let tscCommand = `pnpm exec tsc -p ${join(projectRoot, "tsconfig.json")}`;
|
|
301
|
+
let aliasCommand = `pnpm exec tsc-alias -p ${join(projectRoot, "tsconfig.json")}`;
|
|
302
|
+
|
|
303
|
+
if (outDir !== "lib") {
|
|
304
|
+
// Create a temporary tsconfig for different output directory
|
|
305
|
+
const tempTsConfig: any = {
|
|
306
|
+
extends: "./tsconfig.json",
|
|
307
|
+
compilerOptions: {
|
|
308
|
+
outDir: `./${outDir}`,
|
|
309
|
+
declarationDir: `./${outDir}`,
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const tempConfigPath = join(projectRoot, "tsconfig.temp.json");
|
|
314
|
+
await writeFile(tempConfigPath, JSON.stringify(tempTsConfig, null, 2), "utf-8");
|
|
315
|
+
|
|
316
|
+
tscCommand = `pnpm exec tsc -p ${tempConfigPath}`;
|
|
317
|
+
aliasCommand = `pnpm exec tsc-alias -p ${tempConfigPath}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Run tsc first (tsc-alias depends on tsc output)
|
|
321
|
+
const { stdout: tscStdout, stderr: tscStderr } = await execAsync(tscCommand);
|
|
322
|
+
|
|
323
|
+
if (tscStderr) {
|
|
324
|
+
console.error("TypeScript compiler errors/warnings:");
|
|
325
|
+
console.error(tscStderr);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (tscStdout) {
|
|
329
|
+
console.log("TypeScript compiler output:");
|
|
330
|
+
console.log(tscStdout);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Run tsc-alias after tsc completes
|
|
334
|
+
const { stdout: aliasStdout, stderr: aliasStderr } = await execAsync(aliasCommand);
|
|
335
|
+
|
|
336
|
+
if (aliasStderr) {
|
|
337
|
+
console.warn("tsc-alias warnings:", aliasStderr);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (aliasStdout) {
|
|
341
|
+
console.log(aliasStdout);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Clean up temp config if created
|
|
345
|
+
if (outDir !== "lib") {
|
|
346
|
+
const tempConfigPath = join(projectRoot, "tsconfig.temp.json");
|
|
347
|
+
if (existsSync(tempConfigPath)) {
|
|
348
|
+
await unlink(tempConfigPath);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
console.log(`✅ TypeScript compilation complete for ${outDir}`);
|
|
353
|
+
} catch (error) {
|
|
354
|
+
console.error(`❌ Error compiling TypeScript to ${outDir}:`);
|
|
355
|
+
console.error((error as Error).message);
|
|
356
|
+
|
|
357
|
+
// If the error has stdout/stderr, display them
|
|
358
|
+
if (error && typeof error === "object" && "stdout" in error && error.stdout) {
|
|
359
|
+
console.error("TypeScript stdout:");
|
|
360
|
+
console.error(error.stdout);
|
|
361
|
+
}
|
|
362
|
+
if (error && typeof error === "object" && "stderr" in error && error.stderr) {
|
|
363
|
+
console.error("TypeScript stderr:");
|
|
364
|
+
console.error(error.stderr);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
console.error("💡 Note: TypeScript compilation failed, but code generation was successful.");
|
|
368
|
+
console.error("💡 Check the errors above and fix any type issues in your source files.");
|
|
369
|
+
throw error;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Main build function - builds both regular and lite versions
|
|
375
|
+
*/
|
|
376
|
+
async function build(): Promise<void> {
|
|
377
|
+
console.log("🚀 Starting SDK version-aware build process...");
|
|
378
|
+
|
|
379
|
+
try {
|
|
380
|
+
// Step 1: Get SDK version and discover spec files
|
|
381
|
+
const sdkVersion = getSdkVersion();
|
|
382
|
+
const { specs, version } = await discoverSpecFiles(sdkVersion);
|
|
383
|
+
|
|
384
|
+
if (!specs.openapi && !specs.asyncapi) {
|
|
385
|
+
throw new Error(`No spec files found for SDK version: ${version}`);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Step 2: Ensure generated directory exists
|
|
389
|
+
await ensureGeneratedDirectory();
|
|
390
|
+
|
|
391
|
+
// Step 3: Generate SDK version constant (independent, can run early)
|
|
392
|
+
const sdkVersionPromise = generateSdkVersionFile(version);
|
|
393
|
+
|
|
394
|
+
// Step 4: Run type generation in parallel (all independent)
|
|
395
|
+
const parallelTasks: Promise<any>[] = [];
|
|
396
|
+
|
|
397
|
+
if (specs.openapi) {
|
|
398
|
+
parallelTasks.push(generateTypes(specs.openapi));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (specs.asyncapi) {
|
|
402
|
+
parallelTasks.push(generateAsyncApiTypes(specs.asyncapi));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Wait for SDK version file and all parallel type generation
|
|
406
|
+
await Promise.all([sdkVersionPromise, ...parallelTasks]);
|
|
407
|
+
|
|
408
|
+
// Step 5: Brand types (depends on generated types)
|
|
409
|
+
brandApiTypes();
|
|
410
|
+
|
|
411
|
+
console.log("🎯 Building regular version with full schemas...");
|
|
412
|
+
|
|
413
|
+
// Step 6a: Generate full schema modules for regular build
|
|
414
|
+
generateSchemaModules(specs);
|
|
415
|
+
|
|
416
|
+
// Step 6b: Compile regular TypeScript (depends on all previous steps)
|
|
417
|
+
await compileTypeScript("lib");
|
|
418
|
+
|
|
419
|
+
console.log("🎯 Building lite version without schemas...");
|
|
420
|
+
|
|
421
|
+
// Step 7a: Create stub schema files for lite build
|
|
422
|
+
await createStubSchemaFiles();
|
|
423
|
+
|
|
424
|
+
// Step 7b: Compile lite TypeScript to separate directory
|
|
425
|
+
await compileTypeScript("lib-lite");
|
|
426
|
+
|
|
427
|
+
// Step 7c: Replace validation with lite version (no yaml dependency)
|
|
428
|
+
await useLiteValidation();
|
|
429
|
+
|
|
430
|
+
// Step 8: Clean up any temporary files
|
|
431
|
+
await cleanupTempFiles();
|
|
432
|
+
|
|
433
|
+
console.log(`🎉 Both regular and lite builds completed successfully for version: ${version}!`);
|
|
434
|
+
console.log(`📦 Regular build: lib/ (with validation schemas)`);
|
|
435
|
+
console.log(`📦 Lite build: lib-lite/ (without validation schemas)`);
|
|
436
|
+
} catch (error) {
|
|
437
|
+
console.error("❌ Build failed:", (error as Error).message);
|
|
438
|
+
|
|
439
|
+
// Attempt cleanup even on failure
|
|
440
|
+
try {
|
|
441
|
+
await cleanupTempFiles();
|
|
442
|
+
} catch (cleanupError) {
|
|
443
|
+
console.warn("⚠️ Could not clean up temp files after build failure");
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
process.exit(1);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Run the build if this script is executed directly
|
|
451
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
452
|
+
build();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Export for use in other scripts
|
|
456
|
+
export { build };
|