@sdk-it/cli 0.46.11 → 0.47.0
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/README.md +26 -18
- package/dist/bin.d.ts +1 -1
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +226 -226
- package/dist/bin.js.map +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +120 -91
- package/dist/index.js.map +3 -3
- package/dist/lib/cli.d.ts +0 -1
- package/dist/lib/cli.d.ts.map +1 -1
- package/dist/lib/commands/init.d.ts.map +1 -1
- package/dist/lib/generate.d.ts.map +1 -1
- package/dist/lib/generators/dart.d.ts.map +1 -1
- package/dist/lib/generators/python.d.ts.map +1 -1
- package/dist/lib/generators/readme.d.ts.map +1 -1
- package/dist/lib/generators/typescript.d.ts.map +1 -1
- package/dist/lib/options.d.ts +6 -0
- package/dist/lib/options.d.ts.map +1 -1
- package/dist/lib/project/analysis.d.ts +1 -1
- package/dist/lib/project/analysis.d.ts.map +1 -1
- package/dist/lib/project/cache.d.ts +1 -1
- package/dist/lib/project/cache.d.ts.map +1 -1
- package/dist/lib/project/compiler.d.ts.map +1 -1
- package/dist/lib/project/config.d.ts +3 -1
- package/dist/lib/project/config.d.ts.map +1 -1
- package/dist/lib/project/output.d.ts.map +1 -1
- package/dist/lib/project.d.ts.map +1 -1
- package/dist/lib/types.d.ts +1 -0
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/startup.d.ts +2 -0
- package/dist/startup.d.ts.map +1 -0
- package/package.json +24 -13
- package/dist/lib/commands/find-framework.d.ts +0 -10
- package/dist/lib/commands/find-framework.d.ts.map +0 -1
- package/dist/lib/commands/find-spec-file.d.ts +0 -2
- package/dist/lib/commands/find-spec-file.d.ts.map +0 -1
- package/dist/lib/commands/guess-default-package-name.d.ts +0 -2
- package/dist/lib/commands/guess-default-package-name.d.ts.map +0 -1
- package/dist/lib/generators/apiref.d.ts +0 -13
- package/dist/lib/generators/apiref.d.ts.map +0 -1
package/dist/bin.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// packages/cli/src/lib/cli.ts
|
|
4
|
-
import { Command as
|
|
4
|
+
import { Command as Command6, program } from "commander";
|
|
5
5
|
|
|
6
6
|
// packages/cli/src/lib/commands/init.ts
|
|
7
7
|
import { Command } from "commander";
|
|
@@ -10,25 +10,35 @@ import { Command } from "commander";
|
|
|
10
10
|
import { resolve as resolve3 } from "node:path";
|
|
11
11
|
|
|
12
12
|
// packages/cli/src/lib/project/analysis.ts
|
|
13
|
+
import {
|
|
14
|
+
isImportDeclaration,
|
|
15
|
+
isNamedImports,
|
|
16
|
+
isStringLiteral
|
|
17
|
+
} from "@typescript/native/ast/is";
|
|
13
18
|
import { createRequire } from "node:module";
|
|
14
|
-
import
|
|
15
|
-
import { defaultTypesMap
|
|
19
|
+
import "@sdk-it/core";
|
|
20
|
+
import { defaultTypesMap } from "@sdk-it/core/deriver.js";
|
|
21
|
+
import {
|
|
22
|
+
openAnalysisProject,
|
|
23
|
+
sourceFiles
|
|
24
|
+
} from "@sdk-it/core/program.js";
|
|
16
25
|
import { analyze } from "@sdk-it/generic";
|
|
17
26
|
import { responseAnalyzer as honoResponseAnalyzer } from "@sdk-it/hono";
|
|
18
27
|
async function analyzeProject(tsconfig, config) {
|
|
19
|
-
|
|
28
|
+
using project = openAnalysisProject(tsconfig);
|
|
29
|
+
const framework = resolveFramework(project, config.framework);
|
|
20
30
|
if (framework === "auto") {
|
|
21
31
|
throw new Error(
|
|
22
32
|
`Could not detect a supported framework from ${config.tsconfig}. Set framework to 'hono' to select it explicitly.`
|
|
23
33
|
);
|
|
24
34
|
}
|
|
25
|
-
const prisma = config.preset === "none" ? void 0 : detectPrisma(
|
|
35
|
+
const prisma = config.preset === "none" ? void 0 : detectPrisma(project);
|
|
26
36
|
if (config.preset === "prisma" && !prisma) {
|
|
27
37
|
throw new Error(
|
|
28
38
|
`Prisma preset was requested, but no Prisma client import was found in ${tsconfig}. Run prisma generate or set preset to 'none'.`
|
|
29
39
|
);
|
|
30
40
|
}
|
|
31
|
-
const { paths, components } = await analyze(
|
|
41
|
+
const { paths, components } = await analyze(project, {
|
|
32
42
|
responseAnalyzer: honoResponseAnalyzer,
|
|
33
43
|
securitySchemes: config.securitySchemes,
|
|
34
44
|
middlewareSecurity: config.middlewareSecurity,
|
|
@@ -50,41 +60,35 @@ async function analyzeProject(tsconfig, config) {
|
|
|
50
60
|
components
|
|
51
61
|
};
|
|
52
62
|
}
|
|
53
|
-
function resolveFramework(
|
|
54
|
-
return configured === void 0 || configured === "auto" ? detectFramework(
|
|
63
|
+
function resolveFramework(project, configured) {
|
|
64
|
+
return configured === void 0 || configured === "auto" ? detectFramework(project) : configured;
|
|
55
65
|
}
|
|
56
|
-
function detectFramework(
|
|
57
|
-
const
|
|
58
|
-
for (const sourceFile of program2.getSourceFiles()) {
|
|
59
|
-
if (sourceFile.isDeclarationFile) continue;
|
|
66
|
+
function detectFramework(project) {
|
|
67
|
+
for (const sourceFile of sourceFiles(project.program)) {
|
|
60
68
|
for (const statement of sourceFile.statements) {
|
|
61
|
-
if (
|
|
69
|
+
if (isImportDeclaration(statement) && isStringLiteral(statement.moduleSpecifier) && (statement.moduleSpecifier.text === "hono" || statement.moduleSpecifier.text.startsWith("@sdk-it/hono"))) {
|
|
62
70
|
return "hono";
|
|
63
71
|
}
|
|
64
72
|
}
|
|
65
73
|
}
|
|
66
74
|
return "auto";
|
|
67
75
|
}
|
|
68
|
-
function detectPrisma(
|
|
69
|
-
const program2 = getProgram(tsconfig);
|
|
76
|
+
function detectPrisma(project) {
|
|
70
77
|
const imports = [];
|
|
71
78
|
const reportedModules = /* @__PURE__ */ new Set();
|
|
72
|
-
for (const sourceFile of
|
|
73
|
-
if (sourceFile.isDeclarationFile) continue;
|
|
79
|
+
for (const sourceFile of sourceFiles(project.program)) {
|
|
74
80
|
for (const statement of sourceFile.statements) {
|
|
75
81
|
const prismaImport = getPrismaImport(statement);
|
|
76
82
|
if (!prismaImport) continue;
|
|
77
|
-
const resolvedModule =
|
|
83
|
+
const resolvedModule = project.program.getResolvedModuleFromModuleSpecifier(
|
|
78
84
|
prismaImport.moduleSpecifier,
|
|
79
|
-
sourceFile.fileName
|
|
80
|
-
program2.getCompilerOptions(),
|
|
81
|
-
ts.sys
|
|
85
|
+
sourceFile.fileName
|
|
82
86
|
);
|
|
83
|
-
if (!resolvedModule
|
|
87
|
+
if (!resolvedModule) continue;
|
|
84
88
|
let runtimeModule;
|
|
85
89
|
try {
|
|
86
90
|
runtimeModule = createRequire(sourceFile.fileName).resolve(
|
|
87
|
-
prismaImport.moduleSpecifier
|
|
91
|
+
prismaImport.moduleSpecifier.text
|
|
88
92
|
);
|
|
89
93
|
} catch {
|
|
90
94
|
continue;
|
|
@@ -109,14 +113,17 @@ function detectPrisma(tsconfig) {
|
|
|
109
113
|
return imports.length > 0 ? { imports } : void 0;
|
|
110
114
|
}
|
|
111
115
|
function getPrismaImport(statement) {
|
|
112
|
-
if (!
|
|
116
|
+
if (!isImportDeclaration(statement) || !isStringLiteral(statement.moduleSpecifier) || !statement.importClause?.namedBindings || !isNamedImports(statement.importClause.namedBindings)) {
|
|
113
117
|
return void 0;
|
|
114
118
|
}
|
|
115
119
|
const bindings = statement.importClause.namedBindings.elements.map((element) => ({
|
|
116
120
|
imported: element.propertyName?.text ?? element.name.text,
|
|
117
121
|
local: element.name.text
|
|
118
122
|
})).filter(({ imported }) => imported === "Prisma" || imported === "$Enums");
|
|
119
|
-
return bindings.length > 0 ? {
|
|
123
|
+
return bindings.length > 0 ? {
|
|
124
|
+
moduleSpecifier: statement.moduleSpecifier,
|
|
125
|
+
bindings
|
|
126
|
+
} : void 0;
|
|
120
127
|
}
|
|
121
128
|
|
|
122
129
|
// packages/cli/src/lib/project/output.ts
|
|
@@ -125,22 +132,25 @@ import { join as join3, resolve } from "node:path";
|
|
|
125
132
|
import { generate } from "@sdk-it/typescript";
|
|
126
133
|
|
|
127
134
|
// packages/cli/src/lib/project/cache.ts
|
|
128
|
-
import {
|
|
135
|
+
import { version as compilerVersion } from "@typescript/native";
|
|
136
|
+
import { hash } from "node:crypto";
|
|
129
137
|
import { access, readFile, readdir } from "node:fs/promises";
|
|
130
|
-
import { createRequire as createRequire2 } from "node:module";
|
|
131
138
|
import { join, relative } from "node:path";
|
|
132
|
-
import
|
|
133
|
-
|
|
139
|
+
import cli from "@sdk-it/cli/package.json" with { type: "json" };
|
|
140
|
+
import typescript from "@sdk-it/typescript/package.json" with { type: "json" };
|
|
134
141
|
var projectGeneratorVersions = {
|
|
135
|
-
cli:
|
|
136
|
-
compiler:
|
|
137
|
-
typescript:
|
|
142
|
+
cli: cli.version,
|
|
143
|
+
compiler: compilerVersion,
|
|
144
|
+
typescript: typescript.version
|
|
138
145
|
};
|
|
139
|
-
function hashProject(openapi, packageName) {
|
|
140
|
-
return
|
|
146
|
+
function hashProject(openapi, name, packageName) {
|
|
147
|
+
return hash(
|
|
148
|
+
"sha256",
|
|
149
|
+
JSON.stringify({ openapi, name, packageName, projectGeneratorVersions })
|
|
150
|
+
);
|
|
141
151
|
}
|
|
142
|
-
async function isCurrentGeneratedPackage(output,
|
|
143
|
-
return await readOptionalFile(join(output, ".project-hash")) ===
|
|
152
|
+
async function isCurrentGeneratedPackage(output, hash2) {
|
|
153
|
+
return await readOptionalFile(join(output, ".project-hash")) === hash2 && await generatedPackageExists(output);
|
|
144
154
|
}
|
|
145
155
|
async function generatedPackageExists(output) {
|
|
146
156
|
try {
|
|
@@ -182,7 +192,7 @@ async function readOptionalFile(path) {
|
|
|
182
192
|
try {
|
|
183
193
|
return await readFile(path, "utf8");
|
|
184
194
|
} catch (error) {
|
|
185
|
-
if (error
|
|
195
|
+
if (Error.isError(error) && "code" in error && error.code === "ENOENT") {
|
|
186
196
|
return void 0;
|
|
187
197
|
}
|
|
188
198
|
throw error;
|
|
@@ -190,56 +200,52 @@ async function readOptionalFile(path) {
|
|
|
190
200
|
}
|
|
191
201
|
|
|
192
202
|
// packages/cli/src/lib/project/compiler.ts
|
|
203
|
+
import {
|
|
204
|
+
API,
|
|
205
|
+
DiagnosticCategory,
|
|
206
|
+
formatDiagnosticsWithColorAndContext
|
|
207
|
+
} from "@typescript/native/sync";
|
|
193
208
|
import { readFile as readFile2, writeFile } from "node:fs/promises";
|
|
194
209
|
import { join as join2 } from "node:path";
|
|
195
|
-
import ts3 from "typescript";
|
|
196
210
|
async function compileGeneratedPackage(output, packageName) {
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
using api = new API();
|
|
212
|
+
const config = api.parseJsonConfigFileContent(
|
|
213
|
+
{
|
|
214
|
+
compilerOptions: {
|
|
215
|
+
declaration: true,
|
|
216
|
+
module: "esnext",
|
|
217
|
+
moduleResolution: "bundler",
|
|
218
|
+
noEmitOnError: true,
|
|
219
|
+
outDir: "dist",
|
|
220
|
+
rewriteRelativeImportExtensions: true,
|
|
221
|
+
rootDir: "src",
|
|
222
|
+
skipLibCheck: true,
|
|
223
|
+
target: "esnext",
|
|
224
|
+
verbatimModuleSyntax: true
|
|
225
|
+
},
|
|
226
|
+
include: ["src"]
|
|
227
|
+
},
|
|
228
|
+
{ configDirectory: output }
|
|
229
|
+
);
|
|
230
|
+
using program2 = api.createProgram(config.fileNames, config.options);
|
|
214
231
|
const result = program2.emit();
|
|
215
|
-
const diagnostics = [
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
].filter((diagnostic) => diagnostic.category === ts3.DiagnosticCategory.Error);
|
|
232
|
+
const diagnostics = [...config.errors, ...result.diagnostics].filter(
|
|
233
|
+
(diagnostic) => diagnostic.category === DiagnosticCategory.Error
|
|
234
|
+
);
|
|
219
235
|
if (result.emitSkipped || diagnostics.length > 0) {
|
|
220
|
-
throw new Error(
|
|
236
|
+
throw new Error(
|
|
237
|
+
`Failed to compile generated client:
|
|
238
|
+
${formatDiagnosticsWithColorAndContext(diagnostics, program2)}`
|
|
239
|
+
);
|
|
221
240
|
}
|
|
222
241
|
await synchronizeGeneratedManifest(output, packageName);
|
|
223
242
|
}
|
|
224
|
-
function formatCompilationError(output, diagnostics) {
|
|
225
|
-
return `Failed to compile generated client:
|
|
226
|
-
${ts3.formatDiagnosticsWithColorAndContext(
|
|
227
|
-
diagnostics,
|
|
228
|
-
{
|
|
229
|
-
getCanonicalFileName: (fileName) => fileName,
|
|
230
|
-
getCurrentDirectory: () => output,
|
|
231
|
-
getNewLine: () => "\n"
|
|
232
|
-
}
|
|
233
|
-
)}`;
|
|
234
|
-
}
|
|
235
243
|
async function synchronizeGeneratedManifest(output, packageName) {
|
|
236
244
|
const manifestPath = join2(output, "package.json");
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
JSON.parse(
|
|
240
|
-
await readFile2(manifestPath, "utf8")
|
|
241
|
-
)
|
|
245
|
+
const existing = JSON.parse(
|
|
246
|
+
await readFile2(manifestPath, "utf8")
|
|
242
247
|
);
|
|
248
|
+
const manifest = generatedPackageManifest(packageName, existing);
|
|
243
249
|
await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}
|
|
244
250
|
`);
|
|
245
251
|
}
|
|
@@ -265,7 +271,7 @@ function generatedPackageManifest(packageName, manifest) {
|
|
|
265
271
|
manifest.dependencies = {
|
|
266
272
|
...manifest.dependencies,
|
|
267
273
|
"fast-content-type-parse": "^3.0.0",
|
|
268
|
-
zod: "^4.
|
|
274
|
+
zod: "^4.6.7"
|
|
269
275
|
};
|
|
270
276
|
return manifest;
|
|
271
277
|
}
|
|
@@ -273,24 +279,25 @@ function generatedPackageManifest(packageName, manifest) {
|
|
|
273
279
|
// packages/cli/src/lib/project/output.ts
|
|
274
280
|
async function writeProjectClient(openapi, config) {
|
|
275
281
|
const output = resolve(config.output ?? ".sdk-it");
|
|
276
|
-
const packageName = config
|
|
277
|
-
const
|
|
278
|
-
if (await isCurrentGeneratedPackage(output,
|
|
282
|
+
const { packageName } = config;
|
|
283
|
+
const hash2 = hashProject(openapi, config.name, packageName);
|
|
284
|
+
if (await isCurrentGeneratedPackage(output, hash2)) return;
|
|
279
285
|
await generate(openapi, {
|
|
280
286
|
output,
|
|
281
287
|
mode: "full",
|
|
282
|
-
name:
|
|
288
|
+
name: config.name,
|
|
283
289
|
packageName,
|
|
284
290
|
readme: false
|
|
285
291
|
});
|
|
286
292
|
await compileGeneratedPackage(output, packageName);
|
|
287
|
-
await writeFile2(join3(output, ".project-hash"),
|
|
293
|
+
await writeFile2(join3(output, ".project-hash"), hash2);
|
|
288
294
|
}
|
|
289
295
|
|
|
290
296
|
// packages/cli/src/lib/project/config.ts
|
|
291
297
|
import { access as access2, mkdir, readFile as readFile3, stat, writeFile as writeFile3 } from "node:fs/promises";
|
|
292
298
|
import { basename, dirname, join as join4, relative as relative2, resolve as resolve2 } from "node:path";
|
|
293
299
|
import { pathToFileURL } from "node:url";
|
|
300
|
+
import { clientName } from "@sdk-it/typescript";
|
|
294
301
|
async function loadProjectConfig(options = {}) {
|
|
295
302
|
const cwd = resolve2(options.cwd ?? process.cwd());
|
|
296
303
|
const configPath = options.config ? resolve2(cwd, options.config) : await findProjectConfig(cwd);
|
|
@@ -301,6 +308,17 @@ async function loadProjectConfig(options = {}) {
|
|
|
301
308
|
`Expected ${configPath} to default export an SDK-IT config with a tsconfig path.`
|
|
302
309
|
);
|
|
303
310
|
}
|
|
311
|
+
if (typeof config.name !== "string") {
|
|
312
|
+
throw new Error(
|
|
313
|
+
`Expected ${configPath} to set \`name\`, the name of the generated client (name: 'Books' exports createBooks).`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
clientName(config.name);
|
|
317
|
+
if (typeof config.packageName !== "string") {
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Expected ${configPath} to set \`packageName\`, the npm name of the generated client package (packageName: '@acme/books-client').`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
304
322
|
const directory = dirname(configPath);
|
|
305
323
|
return {
|
|
306
324
|
...config,
|
|
@@ -318,6 +336,7 @@ async function loadProjectConfig(options = {}) {
|
|
|
318
336
|
};
|
|
319
337
|
}
|
|
320
338
|
async function initializeProject(options) {
|
|
339
|
+
clientName(options.name);
|
|
321
340
|
const cwd = resolve2(options.cwd ?? process.cwd());
|
|
322
341
|
const tsconfigPath = resolve2(cwd, options.tsconfig);
|
|
323
342
|
await validateTsconfig(tsconfigPath);
|
|
@@ -337,9 +356,10 @@ async function initializeProject(options) {
|
|
|
337
356
|
const configSource = `import { defineConfig } from '@sdk-it/cli';
|
|
338
357
|
|
|
339
358
|
export default defineConfig({
|
|
359
|
+
name: ${quoted(options.name)},
|
|
340
360
|
tsconfig: '${relativeTsconfig}',
|
|
341
|
-
|
|
342
|
-
|
|
361
|
+
packageName: '${packageName}',
|
|
362
|
+
});
|
|
343
363
|
`;
|
|
344
364
|
const existingConfig = await readOptionalFile2(configPath);
|
|
345
365
|
if (existingConfig !== void 0 && existingConfig !== configSource) {
|
|
@@ -350,9 +370,10 @@ ${projectDirectory === workspaceDirectory ? "" : ` packageName: '${packageName}
|
|
|
350
370
|
const output = join4(projectDirectory, ".sdk-it");
|
|
351
371
|
const manifestPath = join4(output, "package.json");
|
|
352
372
|
const existingManifest = await readOptionalFile2(manifestPath);
|
|
373
|
+
const parsedManifest = existingManifest ? JSON.parse(existingManifest) : {};
|
|
353
374
|
const generatedManifest = generatedPackageManifest(
|
|
354
375
|
packageName,
|
|
355
|
-
|
|
376
|
+
parsedManifest
|
|
356
377
|
);
|
|
357
378
|
const generatedManifestSource = `${JSON.stringify(generatedManifest, null, 2)}
|
|
358
379
|
`;
|
|
@@ -418,10 +439,8 @@ async function findWorkspace(start) {
|
|
|
418
439
|
while (true) {
|
|
419
440
|
const source = await readOptionalFile2(join4(directory, "package.json"));
|
|
420
441
|
if (source) {
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
manifest: JSON.parse(source)
|
|
424
|
-
};
|
|
442
|
+
const manifest = JSON.parse(source);
|
|
443
|
+
const candidate = { directory, manifest };
|
|
425
444
|
nearest ??= candidate;
|
|
426
445
|
if (candidate.manifest.workspaces !== void 0) return candidate;
|
|
427
446
|
}
|
|
@@ -433,16 +452,26 @@ async function findWorkspace(start) {
|
|
|
433
452
|
throw new Error(`Could not find a package.json from ${start}.`);
|
|
434
453
|
}
|
|
435
454
|
function generatedPackageName(workspaceName, projectDirectory, workspaceDirectory) {
|
|
436
|
-
if (projectDirectory === workspaceDirectory)
|
|
455
|
+
if (projectDirectory === workspaceDirectory) {
|
|
456
|
+
if (workspaceName === void 0) {
|
|
457
|
+
throw new Error(
|
|
458
|
+
`Expected ${join4(workspaceDirectory, "package.json")} to set \`name\`: the client package is named after it (name '@acme/api' gives '@acme/api-client').`
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
return `${workspaceName}-client`;
|
|
462
|
+
}
|
|
437
463
|
const project = basename(projectDirectory).toLowerCase().replace(/[^a-z0-9._-]+/g, "-");
|
|
438
464
|
const scope = workspaceName?.match(/^@([^/]+)\//)?.[1];
|
|
439
465
|
return scope ? `@${scope}/${project}-client` : `${project}-client`;
|
|
440
466
|
}
|
|
467
|
+
function quoted(value) {
|
|
468
|
+
return `'${JSON.stringify(value).slice(1, -1).replaceAll("'", "\\'")}'`;
|
|
469
|
+
}
|
|
441
470
|
async function validateTsconfig(path) {
|
|
442
471
|
try {
|
|
443
472
|
if ((await stat(path)).isFile()) return;
|
|
444
473
|
} catch (error) {
|
|
445
|
-
if (!(error
|
|
474
|
+
if (!(Error.isError(error) && "code" in error && error.code === "ENOENT")) {
|
|
446
475
|
throw error;
|
|
447
476
|
}
|
|
448
477
|
}
|
|
@@ -470,7 +499,7 @@ async function readOptionalFile2(path) {
|
|
|
470
499
|
try {
|
|
471
500
|
return await readFile3(path, "utf8");
|
|
472
501
|
} catch (error) {
|
|
473
|
-
if (error
|
|
502
|
+
if (Error.isError(error) && "code" in error && error.code === "ENOENT") {
|
|
474
503
|
return void 0;
|
|
475
504
|
}
|
|
476
505
|
throw error;
|
|
@@ -485,18 +514,20 @@ async function generateProject(config) {
|
|
|
485
514
|
}
|
|
486
515
|
|
|
487
516
|
// packages/cli/src/lib/commands/init.ts
|
|
488
|
-
var init_default = new Command("init").description("Initialize SDK-IT from a backend TypeScript project").requiredOption("--project <tsconfig>", "Backend tsconfig path").action(async ({ project }) => {
|
|
489
|
-
await initializeProject({ tsconfig: project });
|
|
517
|
+
var init_default = new Command("init").description("Initialize SDK-IT from a backend TypeScript project").requiredOption("-n, --name <name>", "Name of the generated client").requiredOption("--project <tsconfig>", "Backend tsconfig path").action(async ({ name, project }) => {
|
|
518
|
+
await initializeProject({ name, tsconfig: project });
|
|
490
519
|
console.log("SDK-IT project configuration initialized.");
|
|
491
520
|
});
|
|
492
521
|
|
|
493
|
-
// packages/cli/src/lib/generators/
|
|
522
|
+
// packages/cli/src/lib/generators/dart.ts
|
|
494
523
|
import { Command as Command2 } from "commander";
|
|
495
|
-
import {
|
|
496
|
-
import {
|
|
524
|
+
import { execSync } from "node:child_process";
|
|
525
|
+
import { generate as generate2 } from "@sdk-it/dart";
|
|
526
|
+
import { loadSpec } from "@sdk-it/spec";
|
|
497
527
|
|
|
498
528
|
// packages/cli/src/lib/options.ts
|
|
499
529
|
import { Option } from "commander";
|
|
530
|
+
import { execFileSync } from "node:child_process";
|
|
500
531
|
var specOption = new Option(
|
|
501
532
|
"-s, --spec <spec>",
|
|
502
533
|
"Path to OpenAPI specification file"
|
|
@@ -508,6 +539,13 @@ var outputOption = new Option(
|
|
|
508
539
|
function shellEnv(name) {
|
|
509
540
|
return process.platform === "win32" ? `%${name}%` : `$${name}`;
|
|
510
541
|
}
|
|
542
|
+
function runFormatter(formatter, output, env2) {
|
|
543
|
+
const [command, ...args] = formatter.split(" ");
|
|
544
|
+
execFileSync(command, args, {
|
|
545
|
+
env: { ...env2, SDK_IT_OUTPUT: output },
|
|
546
|
+
stdio: ["ignore", "ignore", "pipe"]
|
|
547
|
+
});
|
|
548
|
+
}
|
|
511
549
|
function parseDotConfig(incoming) {
|
|
512
550
|
if (incoming === "false") {
|
|
513
551
|
return false;
|
|
@@ -546,30 +584,8 @@ function parsePagination(config) {
|
|
|
546
584
|
return config;
|
|
547
585
|
}
|
|
548
586
|
|
|
549
|
-
// packages/cli/src/lib/generators/apiref.ts
|
|
550
|
-
var apiref_default = new Command2("apiref").description("Generate APIREF").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(true)).action(async (options) => {
|
|
551
|
-
await runApiRef(options.spec, options.output);
|
|
552
|
-
});
|
|
553
|
-
function runApiRef(spec, output) {
|
|
554
|
-
const packageDir = join5(dirname2(import.meta.url), "..", "..", "apiref");
|
|
555
|
-
return execa("nx", ["run", "apiref:build", "--verbose"], {
|
|
556
|
-
stdio: "inherit",
|
|
557
|
-
extendEnv: true,
|
|
558
|
-
cwd: packageDir,
|
|
559
|
-
env: {
|
|
560
|
-
VITE_SPEC: spec,
|
|
561
|
-
VITE_SDK_IT_OUTPUT: output
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
|
|
566
587
|
// packages/cli/src/lib/generators/dart.ts
|
|
567
|
-
|
|
568
|
-
import { execa as execa2 } from "execa";
|
|
569
|
-
import { execSync } from "node:child_process";
|
|
570
|
-
import { generate as generate2 } from "@sdk-it/dart";
|
|
571
|
-
import { loadSpec } from "@sdk-it/spec";
|
|
572
|
-
var dart_default = new Command3("dart").description("Generate Dart SDK").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(true)).option("-n, --name <name>", "Name of the generated client", "Client").option(
|
|
588
|
+
var dart_default = new Command2("dart").description("Generate Dart SDK").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(true)).requiredOption("-n, --name <name>", "Name of the generated client").option(
|
|
573
589
|
"--pagination <pagination>",
|
|
574
590
|
'Configure pagination (e.g., "false", "true", "guess=false")',
|
|
575
591
|
"true"
|
|
@@ -577,17 +593,14 @@ var dart_default = new Command3("dart").description("Generate Dart SDK").addOpti
|
|
|
577
593
|
await runDart(options);
|
|
578
594
|
});
|
|
579
595
|
async function runDart(options) {
|
|
580
|
-
await generate2(await loadSpec(options.spec), {
|
|
596
|
+
await generate2((await loadSpec(options.spec)).spec, {
|
|
581
597
|
output: options.output,
|
|
582
598
|
mode: options.mode || "full",
|
|
583
599
|
name: options.name,
|
|
584
|
-
pagination: typeof options.pagination === "string" ? parsePagination(parseDotConfig(options.pagination
|
|
585
|
-
formatCode:
|
|
600
|
+
pagination: typeof options.pagination === "string" ? parsePagination(parseDotConfig(options.pagination)) : options.pagination,
|
|
601
|
+
formatCode: ({ output }) => {
|
|
586
602
|
if (options.formatter) {
|
|
587
|
-
|
|
588
|
-
await execa2(command, args, {
|
|
589
|
-
env: { ...process.env, SDK_IT_OUTPUT: output }
|
|
590
|
-
});
|
|
603
|
+
runFormatter(options.formatter, output, process.env);
|
|
591
604
|
} else {
|
|
592
605
|
execSync(`dart format ${shellEnv("SDK_IT_OUTPUT")}`, {
|
|
593
606
|
env: { ...process.env, SDK_IT_OUTPUT: output },
|
|
@@ -599,13 +612,12 @@ async function runDart(options) {
|
|
|
599
612
|
}
|
|
600
613
|
|
|
601
614
|
// packages/cli/src/lib/generators/python.ts
|
|
602
|
-
import { Command as
|
|
603
|
-
import { execa as execa3 } from "execa";
|
|
615
|
+
import { Command as Command3 } from "commander";
|
|
604
616
|
import { execSync as execSync2 } from "node:child_process";
|
|
605
617
|
|
|
606
618
|
// packages/python/dist/index.js
|
|
607
619
|
import { readdir as readdir2 } from "node:fs/promises";
|
|
608
|
-
import { join as
|
|
620
|
+
import { join as join5 } from "node:path";
|
|
609
621
|
import { snakecase as snakecase2 } from "stringcase";
|
|
610
622
|
import {
|
|
611
623
|
followRef as followRef2,
|
|
@@ -634,7 +646,7 @@ import {
|
|
|
634
646
|
parseRef,
|
|
635
647
|
pascalcase
|
|
636
648
|
} from "@sdk-it/core";
|
|
637
|
-
import { isPrimitiveSchema } from "@sdk-it/spec";
|
|
649
|
+
import { isPrimitiveSchema, withParameters } from "@sdk-it/spec";
|
|
638
650
|
var dispatcher_default = `"""HTTP dispatcher for making API requests."""
|
|
639
651
|
|
|
640
652
|
import asyncio
|
|
@@ -1856,24 +1868,6 @@ def create_error_from_response(response: httpx.Response) -> ErrorResponse:
|
|
|
1856
1868
|
else:
|
|
1857
1869
|
return error_class(data)
|
|
1858
1870
|
`;
|
|
1859
|
-
function coerceObject(schema) {
|
|
1860
|
-
schema = structuredClone(schema);
|
|
1861
|
-
if (schema["x-properties"]) {
|
|
1862
|
-
schema.properties = {
|
|
1863
|
-
...schema.properties ?? {},
|
|
1864
|
-
...schema["x-properties"] ?? {}
|
|
1865
|
-
};
|
|
1866
|
-
}
|
|
1867
|
-
if (schema["x-required"]) {
|
|
1868
|
-
schema.required = Array.from(
|
|
1869
|
-
/* @__PURE__ */ new Set([
|
|
1870
|
-
...Array.isArray(schema.required) ? schema.required : [],
|
|
1871
|
-
...schema["x-required"] || []
|
|
1872
|
-
])
|
|
1873
|
-
);
|
|
1874
|
-
}
|
|
1875
|
-
return schema;
|
|
1876
|
-
}
|
|
1877
1871
|
var PythonEmitter = class {
|
|
1878
1872
|
#spec;
|
|
1879
1873
|
#emitHandler;
|
|
@@ -1885,12 +1879,10 @@ var PythonEmitter = class {
|
|
|
1885
1879
|
return;
|
|
1886
1880
|
}
|
|
1887
1881
|
this.#emitHistory.add(content);
|
|
1888
|
-
this.#emitHandler
|
|
1882
|
+
this.#emitHandler(name, content, schema);
|
|
1889
1883
|
}
|
|
1890
|
-
constructor(spec) {
|
|
1884
|
+
constructor(spec, emit = () => void 0) {
|
|
1891
1885
|
this.#spec = spec;
|
|
1892
|
-
}
|
|
1893
|
-
onEmit(emit) {
|
|
1894
1886
|
this.#emitHandler = emit;
|
|
1895
1887
|
}
|
|
1896
1888
|
#formatFieldName(name) {
|
|
@@ -1992,7 +1984,7 @@ var PythonEmitter = class {
|
|
|
1992
1984
|
};
|
|
1993
1985
|
}
|
|
1994
1986
|
#object(className, schema, context) {
|
|
1995
|
-
const { properties = {}, required = [] } =
|
|
1987
|
+
const { properties = {}, required = [] } = withParameters(schema);
|
|
1996
1988
|
const fields = [];
|
|
1997
1989
|
let baseClass = "BaseModel";
|
|
1998
1990
|
if (schema.allOf) {
|
|
@@ -2079,8 +2071,8 @@ ${docstring}${fields.length > 0 ? fields.join("\n") : " pass"}${requestConfig
|
|
|
2079
2071
|
}
|
|
2080
2072
|
#primitive(schema) {
|
|
2081
2073
|
const { type, format } = schema;
|
|
2082
|
-
const nullable = schema.nullable;
|
|
2083
|
-
let pythonType
|
|
2074
|
+
const nullable = "nullable" in schema && schema.nullable === true;
|
|
2075
|
+
let pythonType;
|
|
2084
2076
|
switch (type) {
|
|
2085
2077
|
case "string":
|
|
2086
2078
|
if (format === "date-time") {
|
|
@@ -2240,7 +2232,7 @@ ${enumItems.join("\n")}
|
|
|
2240
2232
|
};
|
|
2241
2233
|
async function generate3(openapi, settings) {
|
|
2242
2234
|
const spec = await toIR({ spec: openapi }, true);
|
|
2243
|
-
const
|
|
2235
|
+
const clientName2 = settings.name;
|
|
2244
2236
|
const output = settings.output;
|
|
2245
2237
|
const { writer, files: writtenFiles } = createWriterProxy(
|
|
2246
2238
|
settings.writer ?? writeFiles,
|
|
@@ -2251,18 +2243,17 @@ async function generate3(openapi, settings) {
|
|
|
2251
2243
|
const files = await readdir2(folder, { withFileTypes: true });
|
|
2252
2244
|
return files.map((file) => ({
|
|
2253
2245
|
fileName: file.name,
|
|
2254
|
-
filePath:
|
|
2246
|
+
filePath: join5(file.parentPath, file.name),
|
|
2255
2247
|
isFolder: file.isDirectory()
|
|
2256
2248
|
}));
|
|
2257
2249
|
};
|
|
2258
2250
|
const groups = {};
|
|
2259
2251
|
forEachOperation(spec, (entry, operation) => {
|
|
2260
|
-
console.log(`Processing ${entry.method} ${entry.path}`);
|
|
2261
2252
|
const group = groups[entry.tag] ??= {
|
|
2262
2253
|
className: `${pascalcase2(entry.tag)}Api`,
|
|
2263
2254
|
methods: []
|
|
2264
2255
|
};
|
|
2265
|
-
const input = toInputs(spec, {
|
|
2256
|
+
const input = toInputs(spec, { operation });
|
|
2266
2257
|
const response = toOutput(spec, operation);
|
|
2267
2258
|
const methodName = snakecase2(
|
|
2268
2259
|
operation.operationId || `${entry.method}_${entry.path.replace(/[^a-zA-Z0-9]/g, "_")}`
|
|
@@ -2283,8 +2274,7 @@ ${docstring}
|
|
|
2283
2274
|
${response ? `return await self.receiver.json(response, ${response.successModel || "None"}, ${response.errorModel || "None"})` : "return response"}
|
|
2284
2275
|
`);
|
|
2285
2276
|
});
|
|
2286
|
-
const
|
|
2287
|
-
const models = await serializeModels(spec, emitter);
|
|
2277
|
+
const models = await serializeModels(spec);
|
|
2288
2278
|
const apiClasses = Object.entries(groups).reduce(
|
|
2289
2279
|
(acc, [name, { className, methods }]) => {
|
|
2290
2280
|
const fileName = `api/${snakecase2(name)}_api.py`;
|
|
@@ -2336,7 +2326,7 @@ from .http.interceptors import (
|
|
|
2336
2326
|
)
|
|
2337
2327
|
|
|
2338
2328
|
|
|
2339
|
-
class ${
|
|
2329
|
+
class ${clientName2}:
|
|
2340
2330
|
"""Main API client for the SDK."""
|
|
2341
2331
|
|
|
2342
2332
|
def __init__(
|
|
@@ -2411,9 +2401,9 @@ ${apiProperties}
|
|
|
2411
2401
|
"http/responses.py": responses_default,
|
|
2412
2402
|
"__init__.py": `"""SDK package."""
|
|
2413
2403
|
|
|
2414
|
-
from .client import ${
|
|
2404
|
+
from .client import ${clientName2}
|
|
2415
2405
|
|
|
2416
|
-
__all__ = ['${
|
|
2406
|
+
__all__ = ['${clientName2}']
|
|
2417
2407
|
`
|
|
2418
2408
|
});
|
|
2419
2409
|
if (settings.mode === "full") {
|
|
@@ -2435,10 +2425,11 @@ python-dateutil>=2.8.0
|
|
|
2435
2425
|
}
|
|
2436
2426
|
const metadata = await readWriteMetadata(
|
|
2437
2427
|
settings.output,
|
|
2438
|
-
Array.from(writtenFiles)
|
|
2428
|
+
Array.from(writtenFiles),
|
|
2429
|
+
settings.writer
|
|
2439
2430
|
);
|
|
2440
2431
|
if (settings.cleanup !== false && writtenFiles.size > 0) {
|
|
2441
|
-
await cleanFiles(metadata
|
|
2432
|
+
await cleanFiles(metadata, settings.output, [
|
|
2442
2433
|
"/__init__.py",
|
|
2443
2434
|
"requirements.txt",
|
|
2444
2435
|
"/metadata.json"
|
|
@@ -2446,19 +2437,19 @@ python-dateutil>=2.8.0
|
|
|
2446
2437
|
}
|
|
2447
2438
|
await settings.writer(output, {
|
|
2448
2439
|
"models/__init__.py": await generateModuleInit(
|
|
2449
|
-
|
|
2440
|
+
join5(output, "models"),
|
|
2450
2441
|
settings.readFolder
|
|
2451
2442
|
),
|
|
2452
2443
|
"inputs/__init__.py": await generateModuleInit(
|
|
2453
|
-
|
|
2444
|
+
join5(output, "inputs"),
|
|
2454
2445
|
settings.readFolder
|
|
2455
2446
|
),
|
|
2456
2447
|
"outputs/__init__.py": await generateModuleInit(
|
|
2457
|
-
|
|
2448
|
+
join5(output, "outputs"),
|
|
2458
2449
|
settings.readFolder
|
|
2459
2450
|
),
|
|
2460
2451
|
"api/__init__.py": await generateModuleInit(
|
|
2461
|
-
|
|
2452
|
+
join5(output, "api"),
|
|
2462
2453
|
settings.readFolder
|
|
2463
2454
|
),
|
|
2464
2455
|
"http/__init__.py": `"""HTTP utilities."""
|
|
@@ -2501,8 +2492,8 @@ ${imports}
|
|
|
2501
2492
|
return '"""Package module."""\n';
|
|
2502
2493
|
}
|
|
2503
2494
|
}
|
|
2504
|
-
function toInputs(spec, {
|
|
2505
|
-
const inputName =
|
|
2495
|
+
function toInputs(spec, { operation }) {
|
|
2496
|
+
const inputName = "Input";
|
|
2506
2497
|
const haveInput = !isEmpty2(operation.parameters) || !isEmpty2(operation.requestBody);
|
|
2507
2498
|
let contentType = "json";
|
|
2508
2499
|
if (operation.requestBody && !isRef2(operation.requestBody)) {
|
|
@@ -2580,7 +2571,7 @@ function isBottomSchema(spec, schema) {
|
|
|
2580
2571
|
const resolved = isRef2(schema) ? followRef2(spec, schema.$ref) : schema;
|
|
2581
2572
|
return !!resolved.not && isEmpty2(resolved.not);
|
|
2582
2573
|
}
|
|
2583
|
-
async function serializeModels(spec
|
|
2574
|
+
async function serializeModels(spec) {
|
|
2584
2575
|
const models = {};
|
|
2585
2576
|
const standardImports = [
|
|
2586
2577
|
"from typing import Any, Dict, List, Optional, Union, Literal",
|
|
@@ -2595,7 +2586,7 @@ async function serializeModels(spec, emitter) {
|
|
|
2595
2586
|
"",
|
|
2596
2587
|
"_NeverValue = Annotated[Any, BeforeValidator(_reject_never)]"
|
|
2597
2588
|
].join("\n");
|
|
2598
|
-
emitter
|
|
2589
|
+
const emitter = new PythonEmitter(spec, (name, content, schema) => {
|
|
2599
2590
|
const fullContent = `${standardImports}
|
|
2600
2591
|
${schema["x-inputname"] ? "from ..http.dispatcher import RequestConfig" : ""}
|
|
2601
2592
|
|
|
@@ -2621,21 +2612,18 @@ ${content}`;
|
|
|
2621
2612
|
|
|
2622
2613
|
// packages/cli/src/lib/generators/python.ts
|
|
2623
2614
|
import { loadSpec as loadSpec2, toIR as toIR2 } from "@sdk-it/spec";
|
|
2624
|
-
var python_default = new
|
|
2615
|
+
var python_default = new Command3("python").description("Generate Python SDK").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(true)).requiredOption("-n, --name <name>", "Name of the generated client").option("-v, --verbose", "Verbose output", false).option("--formatter <formatter>", "Formatter to use for the generated code").action(async (options) => {
|
|
2625
2616
|
await runPython(options);
|
|
2626
2617
|
});
|
|
2627
2618
|
async function runPython(options) {
|
|
2628
|
-
const spec = await toIR2({ spec: await loadSpec2(options.spec) }, true);
|
|
2619
|
+
const spec = await toIR2({ spec: (await loadSpec2(options.spec)).spec }, true);
|
|
2629
2620
|
await generate3(spec, {
|
|
2630
2621
|
output: options.output,
|
|
2631
2622
|
mode: options.mode || "full",
|
|
2632
2623
|
name: options.name,
|
|
2633
|
-
formatCode:
|
|
2624
|
+
formatCode: ({ output }) => {
|
|
2634
2625
|
if (options.formatter) {
|
|
2635
|
-
|
|
2636
|
-
await execa3(command, args, {
|
|
2637
|
-
env: { ...process.env, SDK_IT_OUTPUT: output }
|
|
2638
|
-
});
|
|
2626
|
+
runFormatter(options.formatter, output, process.env);
|
|
2639
2627
|
} else {
|
|
2640
2628
|
try {
|
|
2641
2629
|
execSync2(`black ${shellEnv("SDK_IT_OUTPUT")}`, {
|
|
@@ -2662,32 +2650,31 @@ async function runPython(options) {
|
|
|
2662
2650
|
}
|
|
2663
2651
|
|
|
2664
2652
|
// packages/cli/src/lib/generators/readme.ts
|
|
2665
|
-
import { Command as
|
|
2653
|
+
import { Command as Command4 } from "commander";
|
|
2666
2654
|
import { writeFile as writeFile4 } from "node:fs/promises";
|
|
2667
2655
|
import { toReadme } from "@sdk-it/readme";
|
|
2668
2656
|
import { loadSpec as loadSpec3, toIR as toIR3 } from "@sdk-it/spec";
|
|
2669
|
-
var readme_default = new
|
|
2657
|
+
var readme_default = new Command4("readme").description("Generate README").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(true)).action(async (options) => {
|
|
2670
2658
|
await runReadme(options.spec, options.output);
|
|
2671
2659
|
});
|
|
2672
2660
|
async function runReadme(specFile, output) {
|
|
2673
|
-
const spec = await toIR3({ spec: await loadSpec3(specFile) });
|
|
2661
|
+
const spec = await toIR3({ spec: (await loadSpec3(specFile)).spec });
|
|
2674
2662
|
const content = toReadme(spec);
|
|
2675
2663
|
await writeFile4(output, content, "utf-8");
|
|
2676
2664
|
}
|
|
2677
2665
|
|
|
2678
2666
|
// packages/cli/src/lib/generators/typescript.ts
|
|
2679
|
-
import {
|
|
2680
|
-
import {
|
|
2667
|
+
import { TemporaryFiles } from "@zukhruf/fs";
|
|
2668
|
+
import { Command as Command5, Option as Option2 } from "commander";
|
|
2681
2669
|
import { publish } from "libnpmpublish";
|
|
2682
|
-
import { execSync as execSync3 } from "node:child_process";
|
|
2670
|
+
import { execFileSync as execFileSync2, execSync as execSync3 } from "node:child_process";
|
|
2683
2671
|
import { readFile as readFile4 } from "node:fs/promises";
|
|
2684
|
-
import {
|
|
2685
|
-
import { join as join7 } from "node:path";
|
|
2672
|
+
import { join as join6 } from "node:path";
|
|
2686
2673
|
import getAuthToken from "registry-auth-token";
|
|
2687
2674
|
import { writeFiles as writeFiles2 } from "@sdk-it/core/file-system.js";
|
|
2688
2675
|
import { loadSpec as loadSpec4 } from "@sdk-it/spec";
|
|
2689
2676
|
import { generate as generate4 } from "@sdk-it/typescript";
|
|
2690
|
-
var typescript_default = new
|
|
2677
|
+
var typescript_default = new Command5("typescript").alias("ts").description("Generate TypeScript SDK").addOption(specOption.makeOptionMandatory(true)).addOption(outputOption.makeOptionMandatory(false)).option(
|
|
2691
2678
|
"--useTsExtension [value]",
|
|
2692
2679
|
"Use .ts extension for generated files",
|
|
2693
2680
|
(value) => value === "false" ? false : true,
|
|
@@ -2695,7 +2682,7 @@ var typescript_default = new Command6("typescript").alias("ts").description("Gen
|
|
|
2695
2682
|
).option(
|
|
2696
2683
|
"-m, --mode <mode>",
|
|
2697
2684
|
"full: generate a full project including package.json and tsconfig.json. useful for monorepo/workspaces minimal: generate only the client sdk"
|
|
2698
|
-
).
|
|
2685
|
+
).requiredOption("-n, --name <name>", "Name of the generated client").requiredOption("--package-name <name>", "npm name of the generated package").option(
|
|
2699
2686
|
"-f, --framework <framework>",
|
|
2700
2687
|
"Framework that is integrating with the SDK"
|
|
2701
2688
|
).option("--formatter <formatter>", "Formatter to use for the generated code").option(
|
|
@@ -2717,13 +2704,13 @@ var typescript_default = new Command6("typescript").alias("ts").description("Gen
|
|
|
2717
2704
|
"Publish the SDK to a package registry (npm, github, or a custom registry)"
|
|
2718
2705
|
).hideHelp(true).makeOptionMandatory(false)
|
|
2719
2706
|
).action(async (options) => {
|
|
2720
|
-
await runTypescript(options);
|
|
2707
|
+
await runTypescript(options, process.env.NPM_TOKEN);
|
|
2721
2708
|
});
|
|
2722
|
-
async function runTypescript(options) {
|
|
2709
|
+
async function runTypescript(options, npmToken) {
|
|
2723
2710
|
if (!options.publish && !options.output) {
|
|
2724
2711
|
throw new Error("Error: --publish or --output option is required.");
|
|
2725
2712
|
}
|
|
2726
|
-
const spec = await loadSpec4(options.spec);
|
|
2713
|
+
const { spec } = await loadSpec4(options.spec);
|
|
2727
2714
|
if (options.output) {
|
|
2728
2715
|
await emitLocal(spec, {
|
|
2729
2716
|
...options,
|
|
@@ -2731,10 +2718,14 @@ async function runTypescript(options) {
|
|
|
2731
2718
|
});
|
|
2732
2719
|
}
|
|
2733
2720
|
if (options.publish) {
|
|
2734
|
-
await emitRemote(
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2721
|
+
await emitRemote(
|
|
2722
|
+
spec,
|
|
2723
|
+
{
|
|
2724
|
+
...options,
|
|
2725
|
+
publish: options.publish
|
|
2726
|
+
},
|
|
2727
|
+
npmToken
|
|
2728
|
+
);
|
|
2738
2729
|
}
|
|
2739
2730
|
}
|
|
2740
2731
|
async function emitLocal(spec, options) {
|
|
@@ -2743,25 +2734,17 @@ async function emitLocal(spec, options) {
|
|
|
2743
2734
|
output: options.output,
|
|
2744
2735
|
mode: options.mode || "minimal",
|
|
2745
2736
|
name: options.name,
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
name: "github"
|
|
2749
|
-
},
|
|
2737
|
+
packageName: options.packageName,
|
|
2738
|
+
pagination: typeof options.pagination === "string" ? parsePagination(parseDotConfig(options.pagination)) : options.pagination,
|
|
2750
2739
|
readme: options.readme,
|
|
2751
2740
|
useTsExtension: options.useTsExtension,
|
|
2752
|
-
formatCode:
|
|
2741
|
+
formatCode: ({ env: env2, output }) => {
|
|
2753
2742
|
if (options.formatter) {
|
|
2754
|
-
|
|
2755
|
-
await execa4(command, args, {
|
|
2756
|
-
env: { ...env, SDK_IT_OUTPUT: output }
|
|
2757
|
-
});
|
|
2743
|
+
runFormatter(options.formatter, output, env2);
|
|
2758
2744
|
} else if (options.defaultFormatter) {
|
|
2759
|
-
|
|
2760
|
-
env: {
|
|
2761
|
-
|
|
2762
|
-
SDK_IT_OUTPUT: output
|
|
2763
|
-
},
|
|
2764
|
-
stdio: options.verbose ? "inherit" : "pipe"
|
|
2745
|
+
execFileSync2("npx", ["-y", "prettier", output, "--write"], {
|
|
2746
|
+
env: { ...env2, SDK_IT_OUTPUT: output },
|
|
2747
|
+
stdio: options.verbose ? "inherit" : ["ignore", "ignore", "pipe"]
|
|
2765
2748
|
});
|
|
2766
2749
|
}
|
|
2767
2750
|
}
|
|
@@ -2774,10 +2757,16 @@ async function emitLocal(spec, options) {
|
|
|
2774
2757
|
});
|
|
2775
2758
|
}
|
|
2776
2759
|
}
|
|
2777
|
-
async function emitRemote(spec, options) {
|
|
2778
|
-
|
|
2760
|
+
async function emitRemote(spec, options, npmToken) {
|
|
2761
|
+
let registry = options.publish;
|
|
2762
|
+
if (options.publish === "npm") {
|
|
2763
|
+
registry = "https://registry.npmjs.org/";
|
|
2764
|
+
} else if (options.publish === "github") {
|
|
2765
|
+
registry = "https://npm.pkg.github.com/";
|
|
2766
|
+
}
|
|
2779
2767
|
console.log("Publishing to registry:", registry);
|
|
2780
|
-
|
|
2768
|
+
await using folder = await new TemporaryFiles().directory("sdk-it-publish-");
|
|
2769
|
+
const { path } = folder;
|
|
2781
2770
|
await emitLocal(spec, {
|
|
2782
2771
|
...options,
|
|
2783
2772
|
output: path,
|
|
@@ -2785,13 +2774,13 @@ async function emitRemote(spec, options) {
|
|
|
2785
2774
|
mode: "full"
|
|
2786
2775
|
});
|
|
2787
2776
|
const manifest = JSON.parse(
|
|
2788
|
-
await readFile4(
|
|
2777
|
+
await readFile4(join6(path, "package.json"), "utf-8")
|
|
2789
2778
|
);
|
|
2790
2779
|
const registryUrl = new URL(registry);
|
|
2791
|
-
const npmrc =
|
|
2780
|
+
const npmrc = npmToken ? {
|
|
2792
2781
|
npmrc: {
|
|
2793
2782
|
registry,
|
|
2794
|
-
[`//${registryUrl.hostname}:_authToken`]:
|
|
2783
|
+
[`//${registryUrl.hostname}:_authToken`]: npmToken
|
|
2795
2784
|
}
|
|
2796
2785
|
} : registry;
|
|
2797
2786
|
const auth = getAuthToken(npmrc);
|
|
@@ -2802,7 +2791,7 @@ async function emitRemote(spec, options) {
|
|
|
2802
2791
|
}
|
|
2803
2792
|
const packResult = execSync3("npm pack --pack-destination .", { cwd: path });
|
|
2804
2793
|
const [tgzName] = packResult.toString().trim().split("\n");
|
|
2805
|
-
await publish(manifest, await readFile4(
|
|
2794
|
+
await publish(manifest, await readFile4(join6(path, tgzName)), {
|
|
2806
2795
|
registry,
|
|
2807
2796
|
defaultTag: "latest",
|
|
2808
2797
|
forceAuth: {
|
|
@@ -2814,14 +2803,25 @@ async function emitRemote(spec, options) {
|
|
|
2814
2803
|
}
|
|
2815
2804
|
|
|
2816
2805
|
// packages/cli/src/lib/cli.ts
|
|
2817
|
-
var generate5 = new
|
|
2806
|
+
var generate5 = new Command6("generate").description("Generate SDKs from configuration or OpenAPI").option("-c, --config <path>", "Path to an SDK-IT configuration file").action(async (options) => {
|
|
2818
2807
|
const config = await loadProjectConfig({ config: options.config });
|
|
2819
2808
|
await generateProject(config);
|
|
2820
2809
|
console.log("Client generated successfully!");
|
|
2821
|
-
}).addCommand(typescript_default).addCommand(python_default).addCommand(dart_default).addCommand(
|
|
2822
|
-
var
|
|
2823
|
-
new
|
|
2810
|
+
}).addCommand(typescript_default).addCommand(python_default).addCommand(dart_default).addCommand(readme_default);
|
|
2811
|
+
var cli2 = program.name("sdk-it").description(`CLI tool to interact with SDK-IT.`).addCommand(generate5, { isDefault: true }).addCommand(init_default).addCommand(
|
|
2812
|
+
new Command6("_internal").action(() => {
|
|
2824
2813
|
}),
|
|
2825
2814
|
{ hidden: true }
|
|
2826
|
-
)
|
|
2815
|
+
);
|
|
2816
|
+
var cli_default = cli2;
|
|
2817
|
+
|
|
2818
|
+
// packages/cli/src/startup.ts
|
|
2819
|
+
import { z } from "zod";
|
|
2820
|
+
var env = z.object({
|
|
2821
|
+
NPM_TOKEN: z.string().optional()
|
|
2822
|
+
});
|
|
2823
|
+
env.parse(process.env);
|
|
2824
|
+
|
|
2825
|
+
// packages/cli/src/bin.ts
|
|
2826
|
+
await cli_default.parseAsync(process.argv);
|
|
2827
2827
|
//# sourceMappingURL=bin.js.map
|