@zuplo/cli 1.95.0 → 1.97.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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="18229bd2-cc3c-521a-93e5-30a1d8f03044")}catch(e){}}();
|
|
3
3
|
import { captureEvent } from "../../common/analytics/lib.js";
|
|
4
4
|
import setBlocking from "../../common/output.js";
|
|
5
5
|
import { ZuploProjectValidator } from "../../common/validators/file-system-validator.js";
|
|
@@ -27,11 +27,7 @@ export default {
|
|
|
27
27
|
describe: "The OpenAPI file to import",
|
|
28
28
|
conflicts: ["source-url"],
|
|
29
29
|
})
|
|
30
|
-
.
|
|
31
|
-
type: "string",
|
|
32
|
-
describe: "The URL of the OpenAPI file to import",
|
|
33
|
-
conflicts: ["source"],
|
|
34
|
-
})
|
|
30
|
+
.demandOption(["source"])
|
|
35
31
|
.middleware([setBlocking])
|
|
36
32
|
.check(async (argv) => {
|
|
37
33
|
return await new YargsChecker(new ZuploProjectValidator()).check(argv);
|
|
@@ -43,4 +39,4 @@ export default {
|
|
|
43
39
|
},
|
|
44
40
|
};
|
|
45
41
|
//# sourceMappingURL=import-openapi.js.map
|
|
46
|
-
//# debugId=
|
|
42
|
+
//# debugId=18229bd2-cc3c-521a-93e5-30a1d8f03044
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="df049bf3-685f-5110-ad16-56cbf1132b2c")}catch(e){}}();
|
|
3
3
|
import { confirm } from "@inquirer/prompts";
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import { readFile } from "node:fs/promises";
|
|
4
|
+
import jsYaml from "js-yaml";
|
|
5
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
7
7
|
import { tmpdir } from "node:os";
|
|
8
8
|
import { basename, dirname, extname, join, relative } from "node:path";
|
|
9
|
-
import { Readable } from "node:stream";
|
|
10
|
-
import { finished } from "node:stream/promises";
|
|
11
9
|
import prettier from "prettier";
|
|
12
10
|
import { logger } from "../../common/logger.js";
|
|
13
11
|
import { printCriticalFailureToConsoleAndExit, printDiagnosticsToConsole, printResultToConsoleAndExitGracefully, } from "../../common/output.js";
|
|
@@ -24,105 +22,112 @@ const BASE_TEMPLATE = `
|
|
|
24
22
|
|
|
25
23
|
`;
|
|
26
24
|
export async function importOpenApi(argv) {
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
let normalizedFilePath;
|
|
26
|
+
let handleAsUrl = true;
|
|
27
|
+
try {
|
|
28
|
+
const _url = new URL(argv.source);
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
catch (err) {
|
|
31
|
+
handleAsUrl = false;
|
|
32
|
+
}
|
|
33
|
+
if (handleAsUrl) {
|
|
34
|
+
const parsedUrl = new URL(argv.source);
|
|
35
|
+
const tempDir = tmpdir();
|
|
36
|
+
const guessedFileName = basename(parsedUrl.pathname).replace(/\.[^/.]+$/, "") ||
|
|
37
|
+
`imported-${Buffer.from(argv.source).toString("base64")}`;
|
|
38
|
+
normalizedFilePath = join(tempDir, guessedFileName);
|
|
39
|
+
await mkdir(dirname(normalizedFilePath), { recursive: true });
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(parsedUrl);
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
await printCriticalFailureToConsoleAndExit(`Failed to download the remote OpenAPI file. Server responded with ${response.status} ${response.statusText}`);
|
|
37
44
|
}
|
|
38
|
-
|
|
39
|
-
else {
|
|
40
|
-
const url = argv["source-url"];
|
|
41
|
-
let parsedUrl;
|
|
45
|
+
const text = await response.text();
|
|
42
46
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
mkdirSync(dirname(normalizedFilePath), { recursive: true });
|
|
47
|
+
const _json = JSON.parse(text);
|
|
48
|
+
normalizedFilePath = normalizedFilePath + ".json";
|
|
49
|
+
await writeFile(normalizedFilePath, text, { flag: "w+" });
|
|
47
50
|
}
|
|
48
51
|
catch (err) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
52
|
+
try {
|
|
53
|
+
const _yaml = jsYaml.load(text);
|
|
54
|
+
normalizedFilePath = normalizedFilePath + ".yaml";
|
|
55
|
+
await writeFile(normalizedFilePath, text, { flag: "w+" });
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
await printCriticalFailureToConsoleAndExit(`Failed to parse the remote OpenAPI file as either JSON or YAML.`);
|
|
57
59
|
}
|
|
58
|
-
const fileStream = createWriteStream(normalizedFilePath, {
|
|
59
|
-
flags: "wx",
|
|
60
|
-
});
|
|
61
|
-
await finished(Readable.fromWeb(response.body).pipe(fileStream));
|
|
62
|
-
}
|
|
63
|
-
catch (err) {
|
|
64
|
-
logger.error(err);
|
|
65
|
-
await printCriticalFailureToConsoleAndExit(`Failed to download the remote OpenAPI file.`);
|
|
66
60
|
}
|
|
67
61
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const fileContent = rawOpenApiSpec.toString();
|
|
72
|
-
const parsedOpenApiSpec = await parseOpenApiFile(extName, fileContent);
|
|
73
|
-
const destinationFilePath = join(process.cwd(), "config", `${basePath.replace("oas", "")}.oas.json`);
|
|
74
|
-
let originalDocument;
|
|
75
|
-
if (!existsSync(destinationFilePath)) {
|
|
76
|
-
originalDocument = (await parseOpenApiFile(".json", BASE_TEMPLATE));
|
|
62
|
+
catch (err) {
|
|
63
|
+
logger.error(err);
|
|
64
|
+
await printCriticalFailureToConsoleAndExit(`Failed to download the remote OpenAPI file.`);
|
|
77
65
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const filePath = argv.source;
|
|
69
|
+
normalizedFilePath = join(relative(process.cwd(), filePath));
|
|
70
|
+
if (!existsSync(normalizedFilePath)) {
|
|
71
|
+
await printCriticalFailureToConsoleAndExit(`The file ${normalizedFilePath} to import does not exist.`);
|
|
82
72
|
}
|
|
83
|
-
|
|
84
|
-
|
|
73
|
+
}
|
|
74
|
+
const rawOpenApiSpec = await readFile(normalizedFilePath);
|
|
75
|
+
const extName = extname(normalizedFilePath);
|
|
76
|
+
const basePath = basename(normalizedFilePath, extName);
|
|
77
|
+
const fileContent = rawOpenApiSpec.toString();
|
|
78
|
+
const parsedOpenApiSpec = await parseOpenApiFile(extName, fileContent);
|
|
79
|
+
const destinationFilePath = join(process.cwd(), "config", `${basePath.replace("oas", "")}.oas.json`);
|
|
80
|
+
let originalDocument;
|
|
81
|
+
if (!existsSync(destinationFilePath)) {
|
|
82
|
+
originalDocument = (await parseOpenApiFile(".json", BASE_TEMPLATE));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const existingOpenApiSpec = await readFile(destinationFilePath);
|
|
86
|
+
const existingFileContent = existingOpenApiSpec.toString();
|
|
87
|
+
originalDocument = (await parseOpenApiFile(extName, existingFileContent));
|
|
88
|
+
}
|
|
89
|
+
const { created, merged, retained } = generateMergeChangeset(originalDocument, parsedOpenApiSpec);
|
|
90
|
+
printDiagnosticsToConsole("This import will...");
|
|
91
|
+
printDiagnosticsToConsole("");
|
|
92
|
+
if (created.size > 0) {
|
|
93
|
+
printDiagnosticsToConsole(`Create ${created.size} new ${created.size > 1 ? "operations" : "operation"}`);
|
|
85
94
|
printDiagnosticsToConsole("");
|
|
86
|
-
|
|
87
|
-
printDiagnosticsToConsole(
|
|
88
|
-
printDiagnosticsToConsole("");
|
|
89
|
-
created.forEach((operation) => {
|
|
90
|
-
printDiagnosticsToConsole(operation);
|
|
91
|
-
});
|
|
92
|
-
printDiagnosticsToConsole("");
|
|
93
|
-
}
|
|
94
|
-
if (merged.size > 0) {
|
|
95
|
-
printDiagnosticsToConsole(`Merge ${merged.size} new ${merged.size > 1 ? "operations" : "operation"}`);
|
|
96
|
-
printDiagnosticsToConsole("");
|
|
97
|
-
merged.forEach((operation) => {
|
|
98
|
-
printDiagnosticsToConsole(operation);
|
|
99
|
-
});
|
|
100
|
-
printDiagnosticsToConsole("");
|
|
101
|
-
}
|
|
102
|
-
if (retained.size > 0) {
|
|
103
|
-
printDiagnosticsToConsole(`Retain ${retained.size} new ${retained.size > 1 ? "operations" : "operation"}`);
|
|
104
|
-
printDiagnosticsToConsole("");
|
|
105
|
-
retained.forEach((operation) => {
|
|
106
|
-
printDiagnosticsToConsole(operation);
|
|
107
|
-
});
|
|
108
|
-
printDiagnosticsToConsole("");
|
|
109
|
-
}
|
|
110
|
-
if (argv.prompt) {
|
|
111
|
-
printDiagnosticsToConsole("");
|
|
112
|
-
const answer = await confirm({ message: "Proceed?", default: true });
|
|
113
|
-
if (!answer) {
|
|
114
|
-
await printResultToConsoleAndExitGracefully("Import cancelled.");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
addOperationIdsAsNecessary(parsedOpenApiSpec);
|
|
118
|
-
const formattedOpenApi = prettier.format(JSON.stringify(parsedOpenApiSpec), {
|
|
119
|
-
parser: "json-stringify",
|
|
95
|
+
created.forEach((operation) => {
|
|
96
|
+
printDiagnosticsToConsole(operation);
|
|
120
97
|
});
|
|
121
|
-
|
|
122
|
-
|
|
98
|
+
printDiagnosticsToConsole("");
|
|
99
|
+
}
|
|
100
|
+
if (merged.size > 0) {
|
|
101
|
+
printDiagnosticsToConsole(`Merge ${merged.size} new ${merged.size > 1 ? "operations" : "operation"}`);
|
|
102
|
+
printDiagnosticsToConsole("");
|
|
103
|
+
merged.forEach((operation) => {
|
|
104
|
+
printDiagnosticsToConsole(operation);
|
|
123
105
|
});
|
|
124
|
-
|
|
106
|
+
printDiagnosticsToConsole("");
|
|
107
|
+
}
|
|
108
|
+
if (retained.size > 0) {
|
|
109
|
+
printDiagnosticsToConsole(`Retain ${retained.size} new ${retained.size > 1 ? "operations" : "operation"}`);
|
|
110
|
+
printDiagnosticsToConsole("");
|
|
111
|
+
retained.forEach((operation) => {
|
|
112
|
+
printDiagnosticsToConsole(operation);
|
|
113
|
+
});
|
|
114
|
+
printDiagnosticsToConsole("");
|
|
115
|
+
}
|
|
116
|
+
if (argv.prompt) {
|
|
117
|
+
printDiagnosticsToConsole("");
|
|
118
|
+
const answer = await confirm({ message: "Proceed?", default: true });
|
|
119
|
+
if (!answer) {
|
|
120
|
+
await printResultToConsoleAndExitGracefully("Import cancelled.");
|
|
121
|
+
}
|
|
125
122
|
}
|
|
123
|
+
addOperationIdsAsNecessary(parsedOpenApiSpec);
|
|
124
|
+
const formattedOpenApi = prettier.format(JSON.stringify(parsedOpenApiSpec), {
|
|
125
|
+
parser: "json-stringify",
|
|
126
|
+
});
|
|
127
|
+
writeFileSync(destinationFilePath, formattedOpenApi, {
|
|
128
|
+
flag: "w",
|
|
129
|
+
});
|
|
130
|
+
await printResultToConsoleAndExitGracefully(`Import successful. File written to ${destinationFilePath}`);
|
|
126
131
|
}
|
|
127
132
|
//# sourceMappingURL=handler.js.map
|
|
128
|
-
//# debugId=
|
|
133
|
+
//# debugId=df049bf3-685f-5110-ad16-56cbf1132b2c
|