@kubb/core 1.2.3 → 1.2.4
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/dist/index.cjs +68 -56
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -20
- package/dist/index.js +69 -56
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -14,6 +14,7 @@ var url = require('url');
|
|
|
14
14
|
var pc3 = require('picocolors');
|
|
15
15
|
var seedrandom = require('seedrandom');
|
|
16
16
|
var tsCodegen = require('@kubb/ts-codegen');
|
|
17
|
+
var events = require('events');
|
|
17
18
|
|
|
18
19
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
20
|
|
|
@@ -109,19 +110,6 @@ function getPathMode(path) {
|
|
|
109
110
|
async function read(path) {
|
|
110
111
|
return fs__default.default.readFile(path, { encoding: "utf8" });
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
-
// src/utils/isURL.ts
|
|
114
|
-
function isURL(data) {
|
|
115
|
-
try {
|
|
116
|
-
const url = new URL(data);
|
|
117
|
-
if (url?.href) {
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
} catch (error) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
113
|
function objectToParameters(data, options = {}) {
|
|
126
114
|
const { typed } = options;
|
|
127
115
|
return data.reduce((acc, [key, value]) => {
|
|
@@ -411,7 +399,7 @@ var reservedWords = [
|
|
|
411
399
|
"valueOf"
|
|
412
400
|
];
|
|
413
401
|
function transformReservedWord(word) {
|
|
414
|
-
if (word && reservedWords.includes(word)) {
|
|
402
|
+
if (word && reservedWords.includes(word) || word?.match(/^\d/)) {
|
|
415
403
|
return `_${word}`;
|
|
416
404
|
}
|
|
417
405
|
return word;
|
|
@@ -579,6 +567,9 @@ var URLPath = class {
|
|
|
579
567
|
get URL() {
|
|
580
568
|
return this.toURLPath();
|
|
581
569
|
}
|
|
570
|
+
get isUrl() {
|
|
571
|
+
return URLPath.isURL(this.path);
|
|
572
|
+
}
|
|
582
573
|
/**
|
|
583
574
|
* Convert Swagger path to template literals/ template strings(camelcase)
|
|
584
575
|
* @example /pet/{petId} => `/pet/${petId}`
|
|
@@ -625,6 +616,17 @@ var URLPath = class {
|
|
|
625
616
|
static toURLPath(path) {
|
|
626
617
|
return path.replaceAll("{", ":").replaceAll("}", "");
|
|
627
618
|
}
|
|
619
|
+
static isURL(path) {
|
|
620
|
+
try {
|
|
621
|
+
const url = new URL(path);
|
|
622
|
+
if (url?.href) {
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
} catch (error) {
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
628
630
|
};
|
|
629
631
|
function writeIndexes(root, options = {}) {
|
|
630
632
|
const tree = TreeNode.build(root, { extensions: /\.ts/, ...options });
|
|
@@ -896,7 +898,7 @@ function createPlugin(factory) {
|
|
|
896
898
|
}
|
|
897
899
|
var pluginName = "core";
|
|
898
900
|
var definePlugin = createPlugin((options) => {
|
|
899
|
-
const { fileManager, resolvePath, resolveName,
|
|
901
|
+
const { fileManager, resolvePath, resolveName, logger } = options;
|
|
900
902
|
return {
|
|
901
903
|
name: pluginName,
|
|
902
904
|
options,
|
|
@@ -940,7 +942,6 @@ var definePlugin = createPlugin((options) => {
|
|
|
940
942
|
const name = resolveName(params);
|
|
941
943
|
return transformReservedWord(name);
|
|
942
944
|
},
|
|
943
|
-
load,
|
|
944
945
|
cache: createPluginCache()
|
|
945
946
|
};
|
|
946
947
|
},
|
|
@@ -965,6 +966,9 @@ var ParallelPluginError = class extends Error {
|
|
|
965
966
|
this.pluginManager = options.pluginManager;
|
|
966
967
|
}
|
|
967
968
|
findError(searchError) {
|
|
969
|
+
if (!searchError) {
|
|
970
|
+
return void 0;
|
|
971
|
+
}
|
|
968
972
|
return this.errors.find((error) => {
|
|
969
973
|
if (error.cause) {
|
|
970
974
|
if (error.cause.name == searchError.name) {
|
|
@@ -988,6 +992,30 @@ var PluginError = class extends Error {
|
|
|
988
992
|
this.pluginManager = options.pluginManager;
|
|
989
993
|
}
|
|
990
994
|
};
|
|
995
|
+
var EventEmitter = class {
|
|
996
|
+
emitter = new events.EventEmitter();
|
|
997
|
+
emit(eventName, ...eventArg) {
|
|
998
|
+
this.emitter.emit(eventName, ...eventArg);
|
|
999
|
+
}
|
|
1000
|
+
on(eventName, handler) {
|
|
1001
|
+
this.emitter.on(eventName, handler);
|
|
1002
|
+
}
|
|
1003
|
+
off(eventName, handler) {
|
|
1004
|
+
this.emitter.off(eventName, handler);
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
// src/managers/pluginManager/pluginParser.ts
|
|
1009
|
+
function pluginParser(plugin, context) {
|
|
1010
|
+
if (plugin.api && typeof plugin.api === "function") {
|
|
1011
|
+
const api = plugin.api.call(context);
|
|
1012
|
+
return {
|
|
1013
|
+
...plugin,
|
|
1014
|
+
api
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
return null;
|
|
1018
|
+
}
|
|
991
1019
|
|
|
992
1020
|
// src/managers/pluginManager/PluginManager.ts
|
|
993
1021
|
var hookNames = {
|
|
@@ -1001,26 +1029,15 @@ var hookNames = {
|
|
|
1001
1029
|
buildEnd: 1
|
|
1002
1030
|
};
|
|
1003
1031
|
var hooks = Object.keys(hookNames);
|
|
1004
|
-
var convertKubbUserPluginToKubbPlugin = (plugin, context) => {
|
|
1005
|
-
if (plugin.api && typeof plugin.api === "function") {
|
|
1006
|
-
const api = plugin.api.call(context);
|
|
1007
|
-
return {
|
|
1008
|
-
...plugin,
|
|
1009
|
-
api
|
|
1010
|
-
};
|
|
1011
|
-
}
|
|
1012
|
-
return null;
|
|
1013
|
-
};
|
|
1014
1032
|
var PluginManager = class {
|
|
1015
1033
|
plugins;
|
|
1016
1034
|
fileManager;
|
|
1017
|
-
onExecute;
|
|
1018
1035
|
core;
|
|
1019
1036
|
queue;
|
|
1020
1037
|
executed = [];
|
|
1021
1038
|
logger;
|
|
1039
|
+
eventEmitter = new EventEmitter();
|
|
1022
1040
|
constructor(config, options) {
|
|
1023
|
-
this.onExecute = options.onExecute?.bind(this);
|
|
1024
1041
|
this.logger = options.logger;
|
|
1025
1042
|
this.queue = new Queue(100, options.debug);
|
|
1026
1043
|
this.fileManager = new FileManager({ task: options.task, queue: this.queue });
|
|
@@ -1028,14 +1045,12 @@ var PluginManager = class {
|
|
|
1028
1045
|
config,
|
|
1029
1046
|
logger: this.logger,
|
|
1030
1047
|
fileManager: this.fileManager,
|
|
1031
|
-
load: this.load,
|
|
1032
1048
|
resolvePath: this.resolvePath,
|
|
1033
1049
|
resolveName: this.resolveName
|
|
1034
1050
|
});
|
|
1035
|
-
|
|
1036
|
-
this.core = convertedCore;
|
|
1051
|
+
this.core = pluginParser(core, core.api.call(null));
|
|
1037
1052
|
this.plugins = [this.core, ...config.plugins || []].reduce((prev, plugin) => {
|
|
1038
|
-
const convertedApi =
|
|
1053
|
+
const convertedApi = pluginParser(plugin, this.core?.api);
|
|
1039
1054
|
if (convertedApi) {
|
|
1040
1055
|
return [...prev, convertedApi];
|
|
1041
1056
|
}
|
|
@@ -1068,12 +1083,9 @@ var PluginManager = class {
|
|
|
1068
1083
|
parameters: [params.name]
|
|
1069
1084
|
}).result;
|
|
1070
1085
|
};
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
parameters: [id]
|
|
1075
|
-
});
|
|
1076
|
-
};
|
|
1086
|
+
on(eventName, handler) {
|
|
1087
|
+
this.eventEmitter.on(eventName, handler);
|
|
1088
|
+
}
|
|
1077
1089
|
/**
|
|
1078
1090
|
*
|
|
1079
1091
|
* Run only hook for a specific plugin name
|
|
@@ -1246,8 +1258,8 @@ var PluginManager = class {
|
|
|
1246
1258
|
return pluginByPluginName;
|
|
1247
1259
|
}
|
|
1248
1260
|
addExecutedToCallStack(executer) {
|
|
1249
|
-
this.onExecute?.call(this, executer, this);
|
|
1250
1261
|
if (executer) {
|
|
1262
|
+
this.eventEmitter.emit("execute", executer);
|
|
1251
1263
|
this.executed.push(executer);
|
|
1252
1264
|
}
|
|
1253
1265
|
}
|
|
@@ -1269,6 +1281,7 @@ var PluginManager = class {
|
|
|
1269
1281
|
if (!hook) {
|
|
1270
1282
|
return null;
|
|
1271
1283
|
}
|
|
1284
|
+
this.eventEmitter.emit("execute", { strategy, hookName, parameters, plugin });
|
|
1272
1285
|
const task = Promise.resolve().then(() => {
|
|
1273
1286
|
if (typeof hook === "function") {
|
|
1274
1287
|
const possiblePromiseResult = hook.apply(this.core.api, parameters);
|
|
@@ -1286,7 +1299,7 @@ var PluginManager = class {
|
|
|
1286
1299
|
return null;
|
|
1287
1300
|
}).finally(() => {
|
|
1288
1301
|
this.addExecutedToCallStack({
|
|
1289
|
-
|
|
1302
|
+
parameters,
|
|
1290
1303
|
output,
|
|
1291
1304
|
strategy,
|
|
1292
1305
|
hookName,
|
|
@@ -1313,6 +1326,7 @@ var PluginManager = class {
|
|
|
1313
1326
|
if (!hook) {
|
|
1314
1327
|
return null;
|
|
1315
1328
|
}
|
|
1329
|
+
this.eventEmitter.emit("execute", { strategy, hookName, parameters, plugin });
|
|
1316
1330
|
try {
|
|
1317
1331
|
if (typeof hook === "function") {
|
|
1318
1332
|
const fn = hook.apply(this.core.api, parameters);
|
|
@@ -1326,7 +1340,7 @@ var PluginManager = class {
|
|
|
1326
1340
|
return null;
|
|
1327
1341
|
} finally {
|
|
1328
1342
|
this.addExecutedToCallStack({
|
|
1329
|
-
|
|
1343
|
+
parameters,
|
|
1330
1344
|
output,
|
|
1331
1345
|
strategy,
|
|
1332
1346
|
hookName,
|
|
@@ -1337,7 +1351,9 @@ var PluginManager = class {
|
|
|
1337
1351
|
catcher(e, plugin, hookName) {
|
|
1338
1352
|
const text = `${e.message} (plugin: ${plugin.name}, hook: ${hookName})
|
|
1339
1353
|
`;
|
|
1340
|
-
|
|
1354
|
+
const pluginError = new PluginError(text, { cause: e, pluginManager: this });
|
|
1355
|
+
this.eventEmitter.emit("error", pluginError);
|
|
1356
|
+
throw pluginError;
|
|
1341
1357
|
}
|
|
1342
1358
|
};
|
|
1343
1359
|
function noReturn() {
|
|
@@ -1376,7 +1392,7 @@ async function transformReducer(_previousCode, result, _plugin) {
|
|
|
1376
1392
|
async function build(options) {
|
|
1377
1393
|
const { config, debug, logger = createLogger() } = options;
|
|
1378
1394
|
try {
|
|
1379
|
-
if (!isURL(config.input.path)) {
|
|
1395
|
+
if (!URLPath.isURL(config.input.path)) {
|
|
1380
1396
|
await read(config.input.path);
|
|
1381
1397
|
}
|
|
1382
1398
|
} catch (e) {
|
|
@@ -1417,32 +1433,29 @@ async function build(options) {
|
|
|
1417
1433
|
}
|
|
1418
1434
|
}
|
|
1419
1435
|
};
|
|
1420
|
-
const
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
}
|
|
1424
|
-
const { hookName, plugin, output, input } = executer;
|
|
1436
|
+
const pluginManager = new PluginManager(config, { debug, logger, task: queueTask });
|
|
1437
|
+
const { plugins, fileManager } = pluginManager;
|
|
1438
|
+
pluginManager.on("execute", (executer) => {
|
|
1439
|
+
const { hookName, plugin, output, parameters } = executer;
|
|
1425
1440
|
const messsage = `${randomPicoColour(plugin.name)} Executing ${hookName}`;
|
|
1426
|
-
if (config.logLevel === LogLevel.info && logger?.spinner &&
|
|
1441
|
+
if (config.logLevel === LogLevel.info && logger?.spinner && parameters) {
|
|
1427
1442
|
if (debug) {
|
|
1428
1443
|
logger.info(messsage);
|
|
1429
1444
|
} else {
|
|
1430
1445
|
logger.spinner.suffixText = messsage;
|
|
1431
1446
|
}
|
|
1432
1447
|
}
|
|
1433
|
-
if (config.logLevel === LogLevel.stacktrace && logger?.spinner &&
|
|
1448
|
+
if (config.logLevel === LogLevel.stacktrace && logger?.spinner && parameters) {
|
|
1434
1449
|
logger.info(messsage);
|
|
1435
1450
|
const logs = [
|
|
1436
|
-
|
|
1437
|
-
JSON.stringify(
|
|
1451
|
+
parameters && `${pc3__default.default.bgWhite(`Parameters`)} ${randomPicoColour(plugin.name)} ${hookName}`,
|
|
1452
|
+
JSON.stringify(parameters, void 0, 2),
|
|
1438
1453
|
output && `${pc3__default.default.bgWhite("Output")} ${randomPicoColour(plugin.name)} ${hookName}`,
|
|
1439
1454
|
output
|
|
1440
1455
|
].filter(Boolean);
|
|
1441
1456
|
console.log(logs.join("\n"));
|
|
1442
1457
|
}
|
|
1443
|
-
};
|
|
1444
|
-
const pluginManager = new PluginManager(config, { debug, logger, task: queueTask, onExecute });
|
|
1445
|
-
const { plugins, fileManager } = pluginManager;
|
|
1458
|
+
});
|
|
1446
1459
|
await pluginManager.hookParallel({
|
|
1447
1460
|
hookName: "validate",
|
|
1448
1461
|
parameters: [plugins]
|
|
@@ -1525,7 +1538,6 @@ exports.importModule = importModule;
|
|
|
1525
1538
|
exports.isPromise = isPromise;
|
|
1526
1539
|
exports.isPromiseFulfilledResult = isPromiseFulfilledResult;
|
|
1527
1540
|
exports.isPromiseRejectedResult = isPromiseRejectedResult;
|
|
1528
|
-
exports.isURL = isURL;
|
|
1529
1541
|
exports.name = pluginName;
|
|
1530
1542
|
exports.nameSorter = nameSorter;
|
|
1531
1543
|
exports.normalizeDirectory = normalizeDirectory;
|