@kubb/cli 2.11.0 → 2.12.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/bin/{kubb.js → kubb.cjs} +1 -0
- package/dist/index.cjs +5581 -260
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +266 -244
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/generate.ts +40 -21
- package/src/index.ts +9 -4
- package/src/init.ts +7 -5
- package/src/utils/getConfig.ts +4 -1
- package/src/utils/getSummary.ts +12 -10
- package/src/utils/renderErrors.ts +5 -2
package/dist/index.js
CHANGED
|
@@ -1,197 +1,39 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import path3 from "path";
|
|
3
|
-
import {
|
|
3
|
+
import { PromiseManager, Warning, isInputPath } from "@kubb/core";
|
|
4
4
|
import { cac } from "cac";
|
|
5
5
|
import c6 from "tinyrainbow";
|
|
6
6
|
|
|
7
7
|
// package.json
|
|
8
|
-
var version = "2.
|
|
8
|
+
var version = "2.12.0";
|
|
9
9
|
|
|
10
|
-
// src/
|
|
11
|
-
import {
|
|
10
|
+
// src/generate.ts
|
|
11
|
+
import { safeBuild } from "@kubb/core";
|
|
12
|
+
import { LogLevel as LogLevel2, createLogger, randomCliColour as randomCliColour2 } from "@kubb/core/logger";
|
|
13
|
+
import { execa } from "execa";
|
|
14
|
+
import { parseArgsStringToArgv } from "string-argv";
|
|
15
|
+
import c3 from "tinyrainbow";
|
|
12
16
|
|
|
13
|
-
// src/utils/
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return plugins instanceof Object && !Array.isArray(plugins);
|
|
22
|
-
}
|
|
23
|
-
async function importPlugin(name, options) {
|
|
24
|
-
const packageManager = new PackageManager(process.cwd());
|
|
25
|
-
const importedPlugin = process.env.NODE_ENV === "test" ? await import(name) : await packageManager.import(name);
|
|
26
|
-
return importedPlugin?.default ? importedPlugin.default(options) : importedPlugin(options);
|
|
27
|
-
}
|
|
28
|
-
function getPlugins(plugins) {
|
|
29
|
-
if (isObjectPlugins(plugins)) {
|
|
30
|
-
throw new Error("Object plugins are not supported anymore, best to use http://kubb.dev/guide/configure#json");
|
|
31
|
-
}
|
|
32
|
-
if (isJSONPlugins(plugins)) {
|
|
33
|
-
const jsonPlugins = plugins;
|
|
34
|
-
const promises = jsonPlugins.map((plugin) => {
|
|
35
|
-
const [name, options = {}] = plugin;
|
|
36
|
-
return importPlugin(name, options);
|
|
37
|
-
});
|
|
38
|
-
return Promise.all(promises);
|
|
17
|
+
// src/utils/OraWritable.ts
|
|
18
|
+
import { Writable } from "stream";
|
|
19
|
+
import c from "tinyrainbow";
|
|
20
|
+
var OraWritable = class extends Writable {
|
|
21
|
+
constructor(spinner2, command, opts) {
|
|
22
|
+
super(opts);
|
|
23
|
+
this.command = command;
|
|
24
|
+
this.spinner = spinner2;
|
|
39
25
|
}
|
|
40
|
-
|
|
41
|
-
|
|
26
|
+
_write(chunk, _encoding, callback2) {
|
|
27
|
+
this.spinner.suffixText = `
|
|
42
28
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const config = result?.config;
|
|
46
|
-
let kubbUserConfig = Promise.resolve(config);
|
|
47
|
-
if (typeof config === "function") {
|
|
48
|
-
const possiblePromise = config(CLIOptions);
|
|
49
|
-
if (isPromise(possiblePromise)) {
|
|
50
|
-
kubbUserConfig = possiblePromise;
|
|
51
|
-
}
|
|
52
|
-
kubbUserConfig = Promise.resolve(possiblePromise);
|
|
53
|
-
}
|
|
54
|
-
let JSONConfig = await kubbUserConfig;
|
|
55
|
-
if (Array.isArray(JSONConfig)) {
|
|
56
|
-
const promises = JSONConfig.map(async (item) => {
|
|
57
|
-
return { ...item, plugins: item.plugins ? await getPlugins(item.plugins) : void 0 };
|
|
58
|
-
});
|
|
59
|
-
return Promise.all(promises);
|
|
29
|
+
${c.bold(c.blue(this.command))}: ${chunk?.toString()}`;
|
|
30
|
+
callback2();
|
|
60
31
|
}
|
|
61
|
-
JSONConfig = {
|
|
62
|
-
...JSONConfig,
|
|
63
|
-
plugins: JSONConfig.plugins ? await getPlugins(JSONConfig.plugins) : void 0
|
|
64
|
-
};
|
|
65
|
-
return JSONConfig;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// src/utils/getCosmiConfig.ts
|
|
69
|
-
import { bundleRequire } from "bundle-require";
|
|
70
|
-
import { cosmiconfig } from "cosmiconfig";
|
|
71
|
-
var tsLoader = async (configFile) => {
|
|
72
|
-
const { mod } = await bundleRequire({
|
|
73
|
-
filepath: configFile,
|
|
74
|
-
preserveTemporaryFile: false,
|
|
75
|
-
// makes it possible to use React and hooks
|
|
76
|
-
// format: 'cjs',
|
|
77
|
-
external: [/@mgx/]
|
|
78
|
-
});
|
|
79
|
-
return mod.default;
|
|
80
32
|
};
|
|
81
|
-
async function getCosmiConfig(moduleName2, config) {
|
|
82
|
-
const searchPlaces = [
|
|
83
|
-
"package.json",
|
|
84
|
-
`.${moduleName2}rc`,
|
|
85
|
-
`.${moduleName2}rc.json`,
|
|
86
|
-
`.${moduleName2}rc.yaml`,
|
|
87
|
-
`.${moduleName2}rc.yml`,
|
|
88
|
-
`.${moduleName2}rc.ts`,
|
|
89
|
-
`.${moduleName2}rc.js`,
|
|
90
|
-
`.${moduleName2}rc.mjs`,
|
|
91
|
-
`.${moduleName2}rc.cjs`,
|
|
92
|
-
`${moduleName2}.config.ts`,
|
|
93
|
-
`${moduleName2}.config.js`,
|
|
94
|
-
`${moduleName2}.config.mjs`,
|
|
95
|
-
`${moduleName2}.config.cjs`
|
|
96
|
-
];
|
|
97
|
-
const explorer = cosmiconfig(moduleName2, {
|
|
98
|
-
cache: false,
|
|
99
|
-
searchPlaces: [
|
|
100
|
-
...searchPlaces.map((searchPlace) => {
|
|
101
|
-
return `.config/${searchPlace}`;
|
|
102
|
-
}),
|
|
103
|
-
...searchPlaces.map((searchPlace) => {
|
|
104
|
-
return `configs/${searchPlace}`;
|
|
105
|
-
}),
|
|
106
|
-
...searchPlaces
|
|
107
|
-
],
|
|
108
|
-
loaders: {
|
|
109
|
-
".ts": tsLoader
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
const result = config ? await explorer.load(config) : await explorer.search();
|
|
113
|
-
if (result?.isEmpty || !result || !result.config) {
|
|
114
|
-
throw new Error("Config not defined, create a kubb.config.js or pass through your config with the option --config");
|
|
115
|
-
}
|
|
116
|
-
return result;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// src/utils/renderErrors.ts
|
|
120
|
-
import { LogLevel } from "@kubb/core/logger";
|
|
121
|
-
import PrettyError from "pretty-error";
|
|
122
|
-
var prettyError = new PrettyError().skipPackage("commander").skip(function callback(traceLine) {
|
|
123
|
-
const pattern = new RegExp("renderErrors");
|
|
124
|
-
const hasMatch = traceLine?.file?.match(pattern);
|
|
125
|
-
if (typeof traceLine.packageName !== "undefined" && hasMatch) {
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
}).start();
|
|
129
|
-
function getErrorCauses(errors) {
|
|
130
|
-
return errors.reduce((prev, error) => {
|
|
131
|
-
const causedError = error?.cause;
|
|
132
|
-
if (causedError) {
|
|
133
|
-
prev = [...prev, ...getErrorCauses([causedError])];
|
|
134
|
-
return prev;
|
|
135
|
-
}
|
|
136
|
-
prev = [...prev, error];
|
|
137
|
-
return prev;
|
|
138
|
-
}, []).filter(Boolean);
|
|
139
|
-
}
|
|
140
|
-
function renderErrors(error, { logLevel = LogLevel.silent }) {
|
|
141
|
-
if (!error) {
|
|
142
|
-
return "";
|
|
143
|
-
}
|
|
144
|
-
if (logLevel === LogLevel.silent) {
|
|
145
|
-
prettyError.skipNodeFiles();
|
|
146
|
-
prettyError.skip(function skip() {
|
|
147
|
-
return true;
|
|
148
|
-
});
|
|
149
|
-
return [prettyError.render(error)].filter(Boolean).join("\n");
|
|
150
|
-
}
|
|
151
|
-
const errors = getErrorCauses([error]);
|
|
152
|
-
if (logLevel === LogLevel.debug) {
|
|
153
|
-
console.log(errors);
|
|
154
|
-
}
|
|
155
|
-
return errors.filter(Boolean).map((error2) => prettyError.render(error2)).join("\n");
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// src/utils/spinner.ts
|
|
159
|
-
import ora from "ora";
|
|
160
|
-
var spinner = ora({
|
|
161
|
-
spinner: "clock"
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// src/utils/watcher.ts
|
|
165
|
-
import c from "tinyrainbow";
|
|
166
|
-
async function startWatcher(path4, cb) {
|
|
167
|
-
const { watch } = await import("chokidar");
|
|
168
|
-
const ignored = ["**/{.git,node_modules}/**"];
|
|
169
|
-
const watcher = watch(path4, {
|
|
170
|
-
ignorePermissionErrors: true,
|
|
171
|
-
ignored
|
|
172
|
-
});
|
|
173
|
-
watcher.on("all", (type, file) => {
|
|
174
|
-
spinner.succeed(c.yellow(c.bold(`Change detected: ${type} ${file}`)));
|
|
175
|
-
spinner.spinner = "clock";
|
|
176
|
-
try {
|
|
177
|
-
cb(path4);
|
|
178
|
-
} catch (e) {
|
|
179
|
-
spinner.warn(c.red("Watcher failed"));
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// src/generate.ts
|
|
186
|
-
import { safeBuild } from "@kubb/core";
|
|
187
|
-
import { createLogger, LogLevel as LogLevel3, randomCliColour as randomCliColour2 } from "@kubb/core/logger";
|
|
188
|
-
import { execa } from "execa";
|
|
189
|
-
import { parseArgsStringToArgv } from "string-argv";
|
|
190
|
-
import c4 from "tinyrainbow";
|
|
191
33
|
|
|
192
34
|
// src/utils/getSummary.ts
|
|
193
35
|
import path from "path";
|
|
194
|
-
import { LogLevel
|
|
36
|
+
import { LogLevel, randomCliColour } from "@kubb/core/logger";
|
|
195
37
|
import c2 from "tinyrainbow";
|
|
196
38
|
|
|
197
39
|
// src/utils/parseHrtimeToSeconds.ts
|
|
@@ -227,24 +69,25 @@ function getSummary({ pluginManager, status, hrstart, config, logger }) {
|
|
|
227
69
|
pluginsFailed: status === "failed" ? failedPlugins?.map((name) => randomCliColour(name))?.join(", ") : void 0,
|
|
228
70
|
filesCreated: files.length,
|
|
229
71
|
time: c2.yellow(`${elapsedSeconds}s`),
|
|
230
|
-
output: path.resolve(config.root, config.output.path)
|
|
72
|
+
output: path.isAbsolute(config.root) ? path.resolve(config.root, config.output.path) : config.root
|
|
231
73
|
};
|
|
232
|
-
if (logLevel ===
|
|
74
|
+
if (logLevel === LogLevel.debug) {
|
|
233
75
|
logger.emit("debug", ["\nGenerated files:\n"]);
|
|
234
|
-
logger.emit(
|
|
76
|
+
logger.emit(
|
|
77
|
+
"debug",
|
|
78
|
+
files.map((file) => `${randomCliColour(JSON.stringify(file.meta?.pluginKey))} ${file.path}`)
|
|
79
|
+
);
|
|
235
80
|
}
|
|
236
81
|
logs.push(
|
|
237
82
|
[
|
|
238
|
-
[
|
|
239
|
-
`, true],
|
|
83
|
+
["\n", true],
|
|
240
84
|
[` ${c2.bold("Name:")} ${meta.name}`, !!meta.name],
|
|
241
85
|
[` ${c2.bold("Plugins:")} ${meta.plugins}`, true],
|
|
242
86
|
[` ${c2.dim("Failed:")} ${meta.pluginsFailed || "none"}`, !!meta.pluginsFailed],
|
|
243
87
|
[`${c2.bold("Generated:")} ${meta.filesCreated} files`, true],
|
|
244
88
|
[` ${c2.bold("Time:")} ${meta.time}`, true],
|
|
245
89
|
[` ${c2.bold("Output:")} ${meta.output}`, true],
|
|
246
|
-
[
|
|
247
|
-
`, true]
|
|
90
|
+
["\n", true]
|
|
248
91
|
].map((item) => {
|
|
249
92
|
if (item.at(1)) {
|
|
250
93
|
return item.at(0);
|
|
@@ -255,22 +98,11 @@ function getSummary({ pluginManager, status, hrstart, config, logger }) {
|
|
|
255
98
|
return logs;
|
|
256
99
|
}
|
|
257
100
|
|
|
258
|
-
// src/utils/
|
|
259
|
-
import
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
super(opts);
|
|
264
|
-
this.command = command;
|
|
265
|
-
this.spinner = spinner2;
|
|
266
|
-
}
|
|
267
|
-
_write(chunk, _encoding, callback2) {
|
|
268
|
-
this.spinner.suffixText = `
|
|
269
|
-
|
|
270
|
-
${c3.bold(c3.blue(this.command))}: ${chunk?.toString()}`;
|
|
271
|
-
callback2();
|
|
272
|
-
}
|
|
273
|
-
};
|
|
101
|
+
// src/utils/spinner.ts
|
|
102
|
+
import ora from "ora";
|
|
103
|
+
var spinner = ora({
|
|
104
|
+
spinner: "clock"
|
|
105
|
+
});
|
|
274
106
|
|
|
275
107
|
// src/generate.ts
|
|
276
108
|
async function executeHooks({ hooks, logLevel }) {
|
|
@@ -278,8 +110,8 @@ async function executeHooks({ hooks, logLevel }) {
|
|
|
278
110
|
return;
|
|
279
111
|
}
|
|
280
112
|
const commands = Array.isArray(hooks.done) ? hooks.done : [hooks.done];
|
|
281
|
-
if (logLevel ===
|
|
282
|
-
spinner.start(
|
|
113
|
+
if (logLevel === LogLevel2.silent) {
|
|
114
|
+
spinner.start("Executing hooks");
|
|
283
115
|
}
|
|
284
116
|
const executers = commands.map(async (command) => {
|
|
285
117
|
const oraWritable = new OraWritable(spinner, command);
|
|
@@ -288,32 +120,41 @@ async function executeHooks({ hooks, logLevel }) {
|
|
|
288
120
|
if (!cmd) {
|
|
289
121
|
return null;
|
|
290
122
|
}
|
|
291
|
-
spinner.start(`Executing hook ${logLevel !== "silent" ?
|
|
292
|
-
const subProcess = await execa(cmd, _args, {
|
|
123
|
+
spinner.start(`Executing hook ${logLevel !== "silent" ? c3.dim(command) : ""}`);
|
|
124
|
+
const subProcess = await execa(cmd, _args, {
|
|
125
|
+
detached: true,
|
|
126
|
+
signal: abortController.signal
|
|
127
|
+
}).pipeStdout?.(oraWritable);
|
|
293
128
|
spinner.suffixText = "";
|
|
294
|
-
if (logLevel ===
|
|
295
|
-
spinner.succeed(`Executing hook ${logLevel !== "silent" ?
|
|
296
|
-
|
|
129
|
+
if (logLevel === LogLevel2.silent) {
|
|
130
|
+
spinner.succeed(`Executing hook ${logLevel !== "silent" ? c3.dim(command) : ""}`);
|
|
131
|
+
if (subProcess) {
|
|
132
|
+
console.log(subProcess.stdout);
|
|
133
|
+
}
|
|
297
134
|
}
|
|
298
135
|
oraWritable.destroy();
|
|
299
136
|
return { subProcess, abort: abortController.abort.bind(abortController) };
|
|
300
137
|
}).filter(Boolean);
|
|
301
138
|
await Promise.all(executers);
|
|
302
|
-
if (logLevel ===
|
|
303
|
-
spinner.succeed(
|
|
139
|
+
if (logLevel === LogLevel2.silent) {
|
|
140
|
+
spinner.succeed("Executing hooks");
|
|
304
141
|
}
|
|
305
142
|
}
|
|
306
143
|
async function generate({ input, config, CLIOptions }) {
|
|
307
|
-
const logger = createLogger({
|
|
144
|
+
const logger = createLogger({
|
|
145
|
+
logLevel: CLIOptions.logLevel || LogLevel2.silent,
|
|
146
|
+
name: config.name,
|
|
147
|
+
spinner
|
|
148
|
+
});
|
|
308
149
|
if (logger.name) {
|
|
309
150
|
spinner.prefixText = randomCliColour2(logger.name);
|
|
310
151
|
}
|
|
311
152
|
const hrstart = process.hrtime();
|
|
312
|
-
if (CLIOptions.logLevel ===
|
|
153
|
+
if (CLIOptions.logLevel === LogLevel2.debug) {
|
|
313
154
|
const { performance, PerformanceObserver } = await import("perf_hooks");
|
|
314
155
|
const performanceOpserver = new PerformanceObserver((items) => {
|
|
315
156
|
const message = `${items.getEntries()[0]?.duration.toFixed(0)}ms`;
|
|
316
|
-
spinner.suffixText =
|
|
157
|
+
spinner.suffixText = c3.yellow(message);
|
|
317
158
|
performance.clearMarks();
|
|
318
159
|
});
|
|
319
160
|
performanceOpserver.observe({ type: "measure" });
|
|
@@ -321,49 +162,56 @@ async function generate({ input, config, CLIOptions }) {
|
|
|
321
162
|
const { root: _root, ...userConfig } = config;
|
|
322
163
|
const logLevel = logger.logLevel;
|
|
323
164
|
const inputPath = input ?? ("path" in userConfig.input ? userConfig.input.path : void 0);
|
|
324
|
-
spinner.start(`\u{1F680} Building ${logLevel !== "silent" ?
|
|
165
|
+
spinner.start(`\u{1F680} Building ${logLevel !== "silent" ? c3.dim(inputPath) : ""}`);
|
|
166
|
+
const definedConfig = {
|
|
167
|
+
root: process.cwd(),
|
|
168
|
+
...userConfig,
|
|
169
|
+
input: inputPath ? {
|
|
170
|
+
...userConfig.input,
|
|
171
|
+
path: inputPath
|
|
172
|
+
} : userConfig.input,
|
|
173
|
+
output: {
|
|
174
|
+
write: true,
|
|
175
|
+
...userConfig.output
|
|
176
|
+
}
|
|
177
|
+
};
|
|
325
178
|
const { pluginManager, error } = await safeBuild({
|
|
326
|
-
config:
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
write: true,
|
|
335
|
-
...userConfig.output
|
|
336
|
-
}
|
|
337
|
-
},
|
|
179
|
+
config: definedConfig,
|
|
180
|
+
logger
|
|
181
|
+
});
|
|
182
|
+
const summary = getSummary({
|
|
183
|
+
pluginManager,
|
|
184
|
+
config: definedConfig,
|
|
185
|
+
status: error ? "failed" : "success",
|
|
186
|
+
hrstart,
|
|
338
187
|
logger
|
|
339
188
|
});
|
|
340
|
-
const summary = getSummary({ pluginManager, config, status: error ? "failed" : "success", hrstart, logger });
|
|
341
189
|
if (error) {
|
|
342
190
|
spinner.suffixText = "";
|
|
343
|
-
spinner.fail(`\u{1F680} Build failed ${logLevel !== "silent" ?
|
|
191
|
+
spinner.fail(`\u{1F680} Build failed ${logLevel !== "silent" ? c3.dim(inputPath) : ""}`);
|
|
344
192
|
console.log(summary.join(""));
|
|
345
193
|
throw error;
|
|
346
194
|
}
|
|
347
195
|
await executeHooks({ hooks: config.hooks, logLevel });
|
|
348
196
|
spinner.suffixText = "";
|
|
349
|
-
spinner.succeed(`\u{1F680} Build completed ${logLevel !== "silent" ?
|
|
197
|
+
spinner.succeed(`\u{1F680} Build completed ${logLevel !== "silent" ? c3.dim(inputPath) : ""}`);
|
|
350
198
|
console.log(summary.join(""));
|
|
351
199
|
}
|
|
352
200
|
|
|
353
201
|
// src/init.ts
|
|
354
202
|
import path2 from "path";
|
|
355
203
|
import { write } from "@kubb/core/fs";
|
|
356
|
-
import { LogLevel as
|
|
204
|
+
import { LogLevel as LogLevel3 } from "@kubb/core/logger";
|
|
357
205
|
import { isPromiseFulfilledResult } from "@kubb/core/utils";
|
|
358
206
|
import { $ } from "execa";
|
|
359
|
-
import
|
|
207
|
+
import c4 from "tinyrainbow";
|
|
360
208
|
var presets = {
|
|
361
209
|
simple: {
|
|
362
210
|
"kubb.config": `
|
|
363
211
|
import { defineConfig } from '@kubb/core'
|
|
364
|
-
import createSwagger from '@kubb/swagger'
|
|
365
|
-
import createSwaggerTS from '@kubb/swagger-ts'
|
|
366
|
-
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
|
|
212
|
+
import { definePlugin as createSwagger } from '@kubb/swagger'
|
|
213
|
+
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
|
|
214
|
+
import { definePlugin as createSwaggerTanstackQuery } from '@kubb/swagger-tanstack-query'
|
|
367
215
|
|
|
368
216
|
export default defineConfig({
|
|
369
217
|
root: '.',
|
|
@@ -383,31 +231,205 @@ export default defineConfig({
|
|
|
383
231
|
packages: ["@kubb/core", "@kubb/cli", "@kubb/swagger", "@kubb/swagger-ts", "@kubb/swagger-tanstack-query"]
|
|
384
232
|
}
|
|
385
233
|
};
|
|
386
|
-
async function init({ preset = "simple", logLevel =
|
|
234
|
+
async function init({ preset = "simple", logLevel = LogLevel3.silent, packageManager = "pnpm" }) {
|
|
387
235
|
spinner.start("\u{1F4E6} Initializing Kubb");
|
|
388
236
|
const presetMeta = presets[preset];
|
|
389
237
|
const configPath = path2.resolve(process.cwd(), "./kubb.config.js");
|
|
390
238
|
const installCommand = packageManager === "npm" ? "install" : "add";
|
|
391
|
-
spinner.start(`\u{1F4C0} Writing \`kubb.config.js\` ${
|
|
239
|
+
spinner.start(`\u{1F4C0} Writing \`kubb.config.js\` ${c4.dim(configPath)}`);
|
|
392
240
|
await write(presetMeta["kubb.config"], configPath);
|
|
393
|
-
spinner.succeed(`\u{1F4C0} Wrote \`kubb.config.js\` ${
|
|
241
|
+
spinner.succeed(`\u{1F4C0} Wrote \`kubb.config.js\` ${c4.dim(configPath)}`);
|
|
394
242
|
const results = await Promise.allSettled([
|
|
395
243
|
$`npm init es6 -y`,
|
|
396
244
|
...presetMeta.packages.map(async (pack) => {
|
|
397
|
-
spinner.start(`\u{1F4C0} Installing ${
|
|
398
|
-
const { stdout } = await $({
|
|
399
|
-
|
|
245
|
+
spinner.start(`\u{1F4C0} Installing ${c4.dim(pack)}`);
|
|
246
|
+
const { stdout } = await $({
|
|
247
|
+
preferLocal: false
|
|
248
|
+
})`${packageManager} ${installCommand} ${pack}`;
|
|
249
|
+
spinner.succeed(`\u{1F4C0} Installed ${c4.dim(pack)}`);
|
|
400
250
|
return stdout;
|
|
401
251
|
})
|
|
402
252
|
]);
|
|
403
|
-
if (logLevel ===
|
|
253
|
+
if (logLevel === LogLevel3.info) {
|
|
404
254
|
results.forEach((result) => {
|
|
405
255
|
if (isPromiseFulfilledResult(result)) {
|
|
406
256
|
console.log(result.value);
|
|
407
257
|
}
|
|
408
258
|
});
|
|
409
259
|
}
|
|
410
|
-
spinner.succeed(
|
|
260
|
+
spinner.succeed("\u{1F4E6} initialized Kubb");
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/utils/getConfig.ts
|
|
265
|
+
import { isPromise } from "@kubb/core/utils";
|
|
266
|
+
|
|
267
|
+
// src/utils/getPlugins.ts
|
|
268
|
+
import { PackageManager } from "@kubb/core";
|
|
269
|
+
function isJSONPlugins(plugins) {
|
|
270
|
+
return !!plugins?.some((plugin) => {
|
|
271
|
+
return Array.isArray(plugin) && typeof plugin?.at(0) === "string";
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
function isObjectPlugins(plugins) {
|
|
275
|
+
return plugins instanceof Object && !Array.isArray(plugins);
|
|
276
|
+
}
|
|
277
|
+
async function importPlugin(name, options) {
|
|
278
|
+
const packageManager = new PackageManager(process.cwd());
|
|
279
|
+
const importedPlugin = process.env.NODE_ENV === "test" ? await import(name) : await packageManager.import(name);
|
|
280
|
+
return importedPlugin?.default ? importedPlugin.default(options) : importedPlugin(options);
|
|
281
|
+
}
|
|
282
|
+
function getPlugins(plugins) {
|
|
283
|
+
if (isObjectPlugins(plugins)) {
|
|
284
|
+
throw new Error("Object plugins are not supported anymore, best to use http://kubb.dev/guide/configure#json");
|
|
285
|
+
}
|
|
286
|
+
if (isJSONPlugins(plugins)) {
|
|
287
|
+
const jsonPlugins = plugins;
|
|
288
|
+
const promises = jsonPlugins.map((plugin) => {
|
|
289
|
+
const [name, options = {}] = plugin;
|
|
290
|
+
return importPlugin(name, options);
|
|
291
|
+
});
|
|
292
|
+
return Promise.all(promises);
|
|
293
|
+
}
|
|
294
|
+
return Promise.resolve(plugins);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/utils/getConfig.ts
|
|
298
|
+
async function getConfig(result, CLIOptions) {
|
|
299
|
+
const config = result?.config;
|
|
300
|
+
let kubbUserConfig = Promise.resolve(config);
|
|
301
|
+
if (typeof config === "function") {
|
|
302
|
+
const possiblePromise = config(CLIOptions);
|
|
303
|
+
if (isPromise(possiblePromise)) {
|
|
304
|
+
kubbUserConfig = possiblePromise;
|
|
305
|
+
}
|
|
306
|
+
kubbUserConfig = Promise.resolve(possiblePromise);
|
|
307
|
+
}
|
|
308
|
+
let JSONConfig = await kubbUserConfig;
|
|
309
|
+
if (Array.isArray(JSONConfig)) {
|
|
310
|
+
const promises = JSONConfig.map(async (item) => {
|
|
311
|
+
return {
|
|
312
|
+
...item,
|
|
313
|
+
plugins: item.plugins ? await getPlugins(item.plugins) : void 0
|
|
314
|
+
};
|
|
315
|
+
});
|
|
316
|
+
return Promise.all(promises);
|
|
317
|
+
}
|
|
318
|
+
JSONConfig = {
|
|
319
|
+
...JSONConfig,
|
|
320
|
+
plugins: JSONConfig.plugins ? await getPlugins(JSONConfig.plugins) : void 0
|
|
321
|
+
};
|
|
322
|
+
return JSONConfig;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/utils/getCosmiConfig.ts
|
|
326
|
+
import { bundleRequire } from "bundle-require";
|
|
327
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
328
|
+
var tsLoader = async (configFile) => {
|
|
329
|
+
const { mod } = await bundleRequire({
|
|
330
|
+
filepath: configFile,
|
|
331
|
+
preserveTemporaryFile: false,
|
|
332
|
+
// makes it possible to use React and hooks
|
|
333
|
+
// format: 'cjs',
|
|
334
|
+
external: [/@mgx/]
|
|
335
|
+
});
|
|
336
|
+
return mod.default;
|
|
337
|
+
};
|
|
338
|
+
async function getCosmiConfig(moduleName2, config) {
|
|
339
|
+
const searchPlaces = [
|
|
340
|
+
"package.json",
|
|
341
|
+
`.${moduleName2}rc`,
|
|
342
|
+
`.${moduleName2}rc.json`,
|
|
343
|
+
`.${moduleName2}rc.yaml`,
|
|
344
|
+
`.${moduleName2}rc.yml`,
|
|
345
|
+
`.${moduleName2}rc.ts`,
|
|
346
|
+
`.${moduleName2}rc.js`,
|
|
347
|
+
`.${moduleName2}rc.mjs`,
|
|
348
|
+
`.${moduleName2}rc.cjs`,
|
|
349
|
+
`${moduleName2}.config.ts`,
|
|
350
|
+
`${moduleName2}.config.js`,
|
|
351
|
+
`${moduleName2}.config.mjs`,
|
|
352
|
+
`${moduleName2}.config.cjs`
|
|
353
|
+
];
|
|
354
|
+
const explorer = cosmiconfig(moduleName2, {
|
|
355
|
+
cache: false,
|
|
356
|
+
searchPlaces: [
|
|
357
|
+
...searchPlaces.map((searchPlace) => {
|
|
358
|
+
return `.config/${searchPlace}`;
|
|
359
|
+
}),
|
|
360
|
+
...searchPlaces.map((searchPlace) => {
|
|
361
|
+
return `configs/${searchPlace}`;
|
|
362
|
+
}),
|
|
363
|
+
...searchPlaces
|
|
364
|
+
],
|
|
365
|
+
loaders: {
|
|
366
|
+
".ts": tsLoader
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
const result = config ? await explorer.load(config) : await explorer.search();
|
|
370
|
+
if (result?.isEmpty || !result || !result.config) {
|
|
371
|
+
throw new Error("Config not defined, create a kubb.config.js or pass through your config with the option --config");
|
|
372
|
+
}
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// src/utils/renderErrors.ts
|
|
377
|
+
import { LogLevel as LogLevel4 } from "@kubb/core/logger";
|
|
378
|
+
import PrettyError from "pretty-error";
|
|
379
|
+
var prettyError = new PrettyError().skipPackage("commander").skip(function callback(traceLine) {
|
|
380
|
+
const pattern = /renderErrors/;
|
|
381
|
+
const hasMatch = traceLine?.file?.match(pattern);
|
|
382
|
+
if (typeof traceLine.packageName !== "undefined" && hasMatch) {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
}).start();
|
|
386
|
+
function getErrorCauses(errors) {
|
|
387
|
+
return errors.reduce((prev, error) => {
|
|
388
|
+
const causedError = error?.cause;
|
|
389
|
+
if (causedError) {
|
|
390
|
+
prev = [...prev, ...getErrorCauses([causedError])];
|
|
391
|
+
return prev;
|
|
392
|
+
}
|
|
393
|
+
prev = [...prev, error];
|
|
394
|
+
return prev;
|
|
395
|
+
}, []).filter(Boolean);
|
|
396
|
+
}
|
|
397
|
+
function renderErrors(error, { logLevel = LogLevel4.silent }) {
|
|
398
|
+
if (!error) {
|
|
399
|
+
return "";
|
|
400
|
+
}
|
|
401
|
+
if (logLevel === LogLevel4.silent) {
|
|
402
|
+
prettyError.skipNodeFiles();
|
|
403
|
+
prettyError.skip(function skip() {
|
|
404
|
+
return true;
|
|
405
|
+
});
|
|
406
|
+
return [prettyError.render(error)].filter(Boolean).join("\n");
|
|
407
|
+
}
|
|
408
|
+
const errors = getErrorCauses([error]);
|
|
409
|
+
if (logLevel === LogLevel4.debug) {
|
|
410
|
+
console.log(errors);
|
|
411
|
+
}
|
|
412
|
+
return errors.filter(Boolean).map((error2) => prettyError.render(error2)).join("\n");
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/utils/watcher.ts
|
|
416
|
+
import c5 from "tinyrainbow";
|
|
417
|
+
async function startWatcher(path4, cb) {
|
|
418
|
+
const { watch } = await import("chokidar");
|
|
419
|
+
const ignored = ["**/{.git,node_modules}/**"];
|
|
420
|
+
const watcher = watch(path4, {
|
|
421
|
+
ignorePermissionErrors: true,
|
|
422
|
+
ignored
|
|
423
|
+
});
|
|
424
|
+
watcher.on("all", (type, file) => {
|
|
425
|
+
spinner.succeed(c5.yellow(c5.bold(`Change detected: ${type} ${file}`)));
|
|
426
|
+
spinner.spinner = "clock";
|
|
427
|
+
try {
|
|
428
|
+
cb(path4);
|
|
429
|
+
} catch (e) {
|
|
430
|
+
spinner.warn(c5.red("Watcher failed"));
|
|
431
|
+
}
|
|
432
|
+
});
|
|
411
433
|
return;
|
|
412
434
|
}
|
|
413
435
|
|
|
@@ -450,7 +472,7 @@ async function generateAction(input, CLIOptions) {
|
|
|
450
472
|
}
|
|
451
473
|
async function run(argv) {
|
|
452
474
|
const program = cac(moduleName);
|
|
453
|
-
program.command("[input]", "Path of the input file(overrides the one in `kubb.config.js`)").action(generateAction);
|
|
475
|
+
program.command("[input]", "Path of the input file(overrides the one in `kubb.config.js`)").option("-c, --config <path>", "Path to the Kubb config").option("-l, --log-level <type>", "Info, silent or debug").option("-w, --watch", "Watch mode based on the input file").action(generateAction);
|
|
454
476
|
program.command("generate [input]", "Path of the input file(overrides the one in `kubb.config.js`)").option("-c, --config <path>", "Path to the Kubb config").option("-l, --log-level <type>", "Info, silent or debug").option("-w, --watch", "Watch mode based on the input file").action(generateAction);
|
|
455
477
|
program.command("init", "Init Kubb").action(async () => {
|
|
456
478
|
return init({ logLevel: "info" });
|