@nmakarov/cli-toolkit 0.23.0 → 0.25.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/dist/cli-runner.cjs +137 -38
- package/dist/cli-runner.cjs.map +1 -1
- package/dist/cli-runner.js +137 -38
- package/dist/cli-runner.js.map +1 -1
- package/dist/db.cjs +125 -38
- package/dist/db.cjs.map +1 -1
- package/dist/db.js +125 -38
- package/dist/db.js.map +1 -1
- package/dist/index.cjs +137 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +137 -38
- package/dist/index.js.map +1 -1
- package/dist/init.cjs +12 -0
- package/dist/init.cjs.map +1 -1
- package/dist/init.js +12 -0
- package/dist/init.js.map +1 -1
- package/dist/params.cjs +12 -0
- package/dist/params.cjs.map +1 -1
- package/dist/params.js +12 -0
- package/dist/params.js.map +1 -1
- package/package.json +1 -1
package/dist/cli-runner.js
CHANGED
|
@@ -2049,6 +2049,18 @@ var Params = class _Params {
|
|
|
2049
2049
|
this._currentModule = prev;
|
|
2050
2050
|
}
|
|
2051
2051
|
}
|
|
2052
|
+
/**
|
|
2053
|
+
* Async variant of {@link runWithModule} for modules that await params.get().
|
|
2054
|
+
*/
|
|
2055
|
+
async runWithModuleAsync(moduleName, fn) {
|
|
2056
|
+
const prev = this._currentModule;
|
|
2057
|
+
this._currentModule = moduleName;
|
|
2058
|
+
try {
|
|
2059
|
+
return await fn();
|
|
2060
|
+
} finally {
|
|
2061
|
+
this._currentModule = prev;
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2052
2064
|
/**
|
|
2053
2065
|
* Infer module name from call stack: first caller outside params/index gives path like .../src/<moduleName>/...
|
|
2054
2066
|
*/
|
|
@@ -2577,38 +2589,71 @@ var KNEX_DEFAULTS = {
|
|
|
2577
2589
|
};
|
|
2578
2590
|
var Db = class {
|
|
2579
2591
|
static async init(context, options = {}) {
|
|
2580
|
-
const
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
dbName = "local";
|
|
2591
|
-
}
|
|
2592
|
-
if (dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
|
|
2593
|
-
dbConnectionString = dbName;
|
|
2594
|
-
dbName = void 0;
|
|
2595
|
-
}
|
|
2596
|
-
if (dbName && !dbConnectionString) {
|
|
2597
|
-
const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
|
|
2598
|
-
dbConnectionString = await context.params.get(paramName, "string");
|
|
2592
|
+
const buildConfig = async () => {
|
|
2593
|
+
const defs2 = {
|
|
2594
|
+
dbName: "string",
|
|
2595
|
+
dbProfile: "boolean default false"
|
|
2596
|
+
};
|
|
2597
|
+
const discovered = context?.params?.getAllForModule?.("db", defs2) ?? {};
|
|
2598
|
+
const merged = { ...discovered, ...options };
|
|
2599
|
+
let { dbName, dbProfile } = merged;
|
|
2600
|
+
let dbConnectionString = options.dbConnectionString ?? options.connectionString;
|
|
2601
|
+
let connectionParam = dbConnectionString ? "options" : null;
|
|
2599
2602
|
if (!dbConnectionString) {
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
+
const src = context?.args?.getSource?.("dbConnectionString");
|
|
2604
|
+
if (src === "cli" || src === "overrides" || src === "config") {
|
|
2605
|
+
dbConnectionString = await context.params.get("dbConnectionString", "string");
|
|
2606
|
+
connectionParam = "dbConnectionString";
|
|
2607
|
+
}
|
|
2603
2608
|
}
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2609
|
+
if (!dbConnectionString && dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
|
|
2610
|
+
dbConnectionString = dbName;
|
|
2611
|
+
dbName = void 0;
|
|
2612
|
+
}
|
|
2613
|
+
if (!dbConnectionString && dbName) {
|
|
2614
|
+
const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
|
|
2615
|
+
dbConnectionString = await context.params.get(paramName, "string");
|
|
2616
|
+
connectionParam = paramName;
|
|
2617
|
+
if (!dbConnectionString) {
|
|
2618
|
+
throw new ParamError(
|
|
2619
|
+
`Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
|
|
2620
|
+
);
|
|
2621
|
+
}
|
|
2622
|
+
}
|
|
2623
|
+
if (!dbConnectionString) {
|
|
2624
|
+
dbConnectionString = await context.params.get("dbConnectionString", "string");
|
|
2625
|
+
if (dbConnectionString) {
|
|
2626
|
+
connectionParam = "dbConnectionString";
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
if (!dbConnectionString) {
|
|
2630
|
+
if (!dbName) {
|
|
2631
|
+
dbName = "local";
|
|
2632
|
+
}
|
|
2633
|
+
const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
|
|
2634
|
+
dbConnectionString = await context.params.get(paramName, "string");
|
|
2635
|
+
connectionParam = paramName;
|
|
2636
|
+
if (!dbConnectionString) {
|
|
2637
|
+
throw new ParamError(
|
|
2638
|
+
`Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
|
|
2639
|
+
);
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
const displayName = resolveDbDisplayName(
|
|
2643
|
+
dbName,
|
|
2644
|
+
connectionParam,
|
|
2645
|
+
context?.args?.env,
|
|
2646
|
+
merged.name
|
|
2647
|
+
);
|
|
2648
|
+
return {
|
|
2649
|
+
...KNEX_DEFAULTS,
|
|
2650
|
+
connectionString: dbConnectionString,
|
|
2651
|
+
name: displayName,
|
|
2652
|
+
profile: !!dbProfile,
|
|
2653
|
+
logger: context.logger
|
|
2654
|
+
};
|
|
2611
2655
|
};
|
|
2656
|
+
const config2 = context?.params?.runWithModuleAsync ? await context.params.runWithModuleAsync("db", buildConfig) : await buildConfig();
|
|
2612
2657
|
return dbConnect(context, config2);
|
|
2613
2658
|
}
|
|
2614
2659
|
constructor(config2) {
|
|
@@ -2625,7 +2670,6 @@ var Db = class {
|
|
|
2625
2670
|
acquireConnectionTimeout: 1e4,
|
|
2626
2671
|
ssl: { rejectUnauthorized: false },
|
|
2627
2672
|
logger: console,
|
|
2628
|
-
name: "default",
|
|
2629
2673
|
...config2
|
|
2630
2674
|
};
|
|
2631
2675
|
this.logger = this.config.logger;
|
|
@@ -2725,9 +2769,7 @@ var Db = class {
|
|
|
2725
2769
|
await this.testConnection();
|
|
2726
2770
|
}
|
|
2727
2771
|
this.isConnected = true;
|
|
2728
|
-
this.logger.debug?.(
|
|
2729
|
-
`[Db] Connected to database "${this.config.name || this.config.connectionString}"`
|
|
2730
|
-
);
|
|
2772
|
+
this.logger.debug?.(formatDbConnectMessage(this.config.name, this.config.connectionString));
|
|
2731
2773
|
} catch (error) {
|
|
2732
2774
|
if (error instanceof ParamError) {
|
|
2733
2775
|
throw error;
|
|
@@ -2745,9 +2787,7 @@ var Db = class {
|
|
|
2745
2787
|
this.knexInstance = null;
|
|
2746
2788
|
this.isConnected = false;
|
|
2747
2789
|
this.queriesLog = [];
|
|
2748
|
-
this.logger.debug?.(
|
|
2749
|
-
`[Db] Disconnected from database "${this.config.name || this.config.connectionString}"`
|
|
2750
|
-
);
|
|
2790
|
+
this.logger.debug?.(formatDbDisconnectMessage(this.config.name, this.config.connectionString));
|
|
2751
2791
|
} catch (error) {
|
|
2752
2792
|
const errorMsg = this.getErrorMessage(error);
|
|
2753
2793
|
this.logger.error?.(`[Db] Error disconnecting: ${errorMsg}`);
|
|
@@ -2876,15 +2916,74 @@ var Db = class {
|
|
|
2876
2916
|
function capitalizeFirstLetter(str) {
|
|
2877
2917
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2878
2918
|
}
|
|
2919
|
+
function resolveDbDisplayName(dbName, connectionParam, argsEnv, mergedName) {
|
|
2920
|
+
if (dbName) {
|
|
2921
|
+
return dbName;
|
|
2922
|
+
}
|
|
2923
|
+
if (mergedName) {
|
|
2924
|
+
return mergedName;
|
|
2925
|
+
}
|
|
2926
|
+
if (connectionParam?.startsWith("dbConnectionString") && connectionParam.length > "dbConnectionString".length) {
|
|
2927
|
+
return connectionParam.slice("dbConnectionString".length).toLowerCase();
|
|
2928
|
+
}
|
|
2929
|
+
if (connectionParam === "dbConnectionString" && argsEnv) {
|
|
2930
|
+
return argsEnv;
|
|
2931
|
+
}
|
|
2932
|
+
return void 0;
|
|
2933
|
+
}
|
|
2934
|
+
function formatDbConnectMessage(name, connectionString) {
|
|
2935
|
+
const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
|
|
2936
|
+
if (name) {
|
|
2937
|
+
return `[Db] Connected to database "${name}"${endpointSuffix}`;
|
|
2938
|
+
}
|
|
2939
|
+
return `[Db] Connected${endpointSuffix}`;
|
|
2940
|
+
}
|
|
2941
|
+
function formatDbDisconnectMessage(name, connectionString) {
|
|
2942
|
+
const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
|
|
2943
|
+
if (name) {
|
|
2944
|
+
return `[Db] Disconnected from database "${name}"${endpointSuffix}`;
|
|
2945
|
+
}
|
|
2946
|
+
return `[Db] Disconnected${endpointSuffix}`;
|
|
2947
|
+
}
|
|
2948
|
+
function formatDbInstanceMessage(action, name) {
|
|
2949
|
+
if (name) {
|
|
2950
|
+
return `[Db] instance "${name}" ${action}`;
|
|
2951
|
+
}
|
|
2952
|
+
return `[Db] instance ${action}`;
|
|
2953
|
+
}
|
|
2954
|
+
function formatConnectionEndpointSuffix(connectionString) {
|
|
2955
|
+
const endpoint = formatConnectionEndpoint(connectionString);
|
|
2956
|
+
return endpoint ? ` (${endpoint})` : "";
|
|
2957
|
+
}
|
|
2958
|
+
function formatConnectionEndpoint(connectionString) {
|
|
2959
|
+
try {
|
|
2960
|
+
const url = new URL(connectionString);
|
|
2961
|
+
const host = url.hostname;
|
|
2962
|
+
if (!host) {
|
|
2963
|
+
return null;
|
|
2964
|
+
}
|
|
2965
|
+
let port = url.port;
|
|
2966
|
+
if (!port) {
|
|
2967
|
+
if (url.protocol === "postgresql:") {
|
|
2968
|
+
port = "5432";
|
|
2969
|
+
} else if (url.protocol === "mysql:") {
|
|
2970
|
+
port = "3306";
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
return port ? `${host}:${port}` : host;
|
|
2974
|
+
} catch {
|
|
2975
|
+
return null;
|
|
2976
|
+
}
|
|
2977
|
+
}
|
|
2879
2978
|
async function dbConnect(context, config2) {
|
|
2880
2979
|
try {
|
|
2881
2980
|
const db = new Db(config2);
|
|
2882
2981
|
context.registerCleanup(async () => {
|
|
2883
2982
|
await db.disconnect();
|
|
2884
|
-
context.logger.debug?.(
|
|
2983
|
+
context.logger.debug?.(formatDbInstanceMessage("disconnected", config2.name));
|
|
2885
2984
|
});
|
|
2886
2985
|
await db.connect();
|
|
2887
|
-
context.logger.debug?.(
|
|
2986
|
+
context.logger.debug?.(formatDbInstanceMessage("initialized", config2.name));
|
|
2888
2987
|
return db;
|
|
2889
2988
|
} catch (error) {
|
|
2890
2989
|
if (error instanceof ParamError) {
|