@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/index.js CHANGED
@@ -1996,6 +1996,18 @@ var Params = class _Params {
1996
1996
  this._currentModule = prev;
1997
1997
  }
1998
1998
  }
1999
+ /**
2000
+ * Async variant of {@link runWithModule} for modules that await params.get().
2001
+ */
2002
+ async runWithModuleAsync(moduleName, fn) {
2003
+ const prev = this._currentModule;
2004
+ this._currentModule = moduleName;
2005
+ try {
2006
+ return await fn();
2007
+ } finally {
2008
+ this._currentModule = prev;
2009
+ }
2010
+ }
1999
2011
  /**
2000
2012
  * Infer module name from call stack: first caller outside params/index gives path like .../src/<moduleName>/...
2001
2013
  */
@@ -3259,38 +3271,71 @@ var KNEX_DEFAULTS = {
3259
3271
  };
3260
3272
  var Db = class {
3261
3273
  static async init(context, options = {}) {
3262
- const defs = {
3263
- dbName: "string",
3264
- dbConnectionString: "string",
3265
- dbProfile: "boolean default false"
3266
- };
3267
- const discovered = context?.params?.getAllForModule?.("db", defs) ?? {};
3268
- const merged = { ...discovered, ...options };
3269
- let { dbName, dbConnectionString } = merged;
3270
- const { dbProfile } = merged;
3271
- if (!dbName && !dbConnectionString) {
3272
- dbName = "local";
3273
- }
3274
- if (dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
3275
- dbConnectionString = dbName;
3276
- dbName = void 0;
3277
- }
3278
- if (dbName && !dbConnectionString) {
3279
- const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
3280
- dbConnectionString = await context.params.get(paramName, "string");
3274
+ const buildConfig = async () => {
3275
+ const defs = {
3276
+ dbName: "string",
3277
+ dbProfile: "boolean default false"
3278
+ };
3279
+ const discovered = context?.params?.getAllForModule?.("db", defs) ?? {};
3280
+ const merged = { ...discovered, ...options };
3281
+ let { dbName, dbProfile } = merged;
3282
+ let dbConnectionString = options.dbConnectionString ?? options.connectionString;
3283
+ let connectionParam = dbConnectionString ? "options" : null;
3281
3284
  if (!dbConnectionString) {
3282
- throw new ParamError(
3283
- `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
3284
- );
3285
+ const src = context?.args?.getSource?.("dbConnectionString");
3286
+ if (src === "cli" || src === "overrides" || src === "config") {
3287
+ dbConnectionString = await context.params.get("dbConnectionString", "string");
3288
+ connectionParam = "dbConnectionString";
3289
+ }
3285
3290
  }
3286
- }
3287
- const config2 = {
3288
- ...KNEX_DEFAULTS,
3289
- connectionString: dbConnectionString,
3290
- name: dbName || merged.name || "default",
3291
- profile: !!dbProfile,
3292
- logger: context.logger
3291
+ if (!dbConnectionString && dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
3292
+ dbConnectionString = dbName;
3293
+ dbName = void 0;
3294
+ }
3295
+ if (!dbConnectionString && dbName) {
3296
+ const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
3297
+ dbConnectionString = await context.params.get(paramName, "string");
3298
+ connectionParam = paramName;
3299
+ if (!dbConnectionString) {
3300
+ throw new ParamError(
3301
+ `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
3302
+ );
3303
+ }
3304
+ }
3305
+ if (!dbConnectionString) {
3306
+ dbConnectionString = await context.params.get("dbConnectionString", "string");
3307
+ if (dbConnectionString) {
3308
+ connectionParam = "dbConnectionString";
3309
+ }
3310
+ }
3311
+ if (!dbConnectionString) {
3312
+ if (!dbName) {
3313
+ dbName = "local";
3314
+ }
3315
+ const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
3316
+ dbConnectionString = await context.params.get(paramName, "string");
3317
+ connectionParam = paramName;
3318
+ if (!dbConnectionString) {
3319
+ throw new ParamError(
3320
+ `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
3321
+ );
3322
+ }
3323
+ }
3324
+ const displayName = resolveDbDisplayName(
3325
+ dbName,
3326
+ connectionParam,
3327
+ context?.args?.env,
3328
+ merged.name
3329
+ );
3330
+ return {
3331
+ ...KNEX_DEFAULTS,
3332
+ connectionString: dbConnectionString,
3333
+ name: displayName,
3334
+ profile: !!dbProfile,
3335
+ logger: context.logger
3336
+ };
3293
3337
  };
3338
+ const config2 = context?.params?.runWithModuleAsync ? await context.params.runWithModuleAsync("db", buildConfig) : await buildConfig();
3294
3339
  return dbConnect(context, config2);
3295
3340
  }
3296
3341
  constructor(config2) {
@@ -3307,7 +3352,6 @@ var Db = class {
3307
3352
  acquireConnectionTimeout: 1e4,
3308
3353
  ssl: { rejectUnauthorized: false },
3309
3354
  logger: console,
3310
- name: "default",
3311
3355
  ...config2
3312
3356
  };
3313
3357
  this.logger = this.config.logger;
@@ -3407,9 +3451,7 @@ var Db = class {
3407
3451
  await this.testConnection();
3408
3452
  }
3409
3453
  this.isConnected = true;
3410
- this.logger.debug?.(
3411
- `[Db] Connected to database "${this.config.name || this.config.connectionString}"`
3412
- );
3454
+ this.logger.debug?.(formatDbConnectMessage(this.config.name, this.config.connectionString));
3413
3455
  } catch (error) {
3414
3456
  if (error instanceof ParamError) {
3415
3457
  throw error;
@@ -3427,9 +3469,7 @@ var Db = class {
3427
3469
  this.knexInstance = null;
3428
3470
  this.isConnected = false;
3429
3471
  this.queriesLog = [];
3430
- this.logger.debug?.(
3431
- `[Db] Disconnected from database "${this.config.name || this.config.connectionString}"`
3432
- );
3472
+ this.logger.debug?.(formatDbDisconnectMessage(this.config.name, this.config.connectionString));
3433
3473
  } catch (error) {
3434
3474
  const errorMsg = this.getErrorMessage(error);
3435
3475
  this.logger.error?.(`[Db] Error disconnecting: ${errorMsg}`);
@@ -3558,15 +3598,74 @@ var Db = class {
3558
3598
  function capitalizeFirstLetter(str) {
3559
3599
  return str.charAt(0).toUpperCase() + str.slice(1);
3560
3600
  }
3601
+ function resolveDbDisplayName(dbName, connectionParam, argsEnv, mergedName) {
3602
+ if (dbName) {
3603
+ return dbName;
3604
+ }
3605
+ if (mergedName) {
3606
+ return mergedName;
3607
+ }
3608
+ if (connectionParam?.startsWith("dbConnectionString") && connectionParam.length > "dbConnectionString".length) {
3609
+ return connectionParam.slice("dbConnectionString".length).toLowerCase();
3610
+ }
3611
+ if (connectionParam === "dbConnectionString" && argsEnv) {
3612
+ return argsEnv;
3613
+ }
3614
+ return void 0;
3615
+ }
3616
+ function formatDbConnectMessage(name, connectionString) {
3617
+ const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
3618
+ if (name) {
3619
+ return `[Db] Connected to database "${name}"${endpointSuffix}`;
3620
+ }
3621
+ return `[Db] Connected${endpointSuffix}`;
3622
+ }
3623
+ function formatDbDisconnectMessage(name, connectionString) {
3624
+ const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
3625
+ if (name) {
3626
+ return `[Db] Disconnected from database "${name}"${endpointSuffix}`;
3627
+ }
3628
+ return `[Db] Disconnected${endpointSuffix}`;
3629
+ }
3630
+ function formatDbInstanceMessage(action, name) {
3631
+ if (name) {
3632
+ return `[Db] instance "${name}" ${action}`;
3633
+ }
3634
+ return `[Db] instance ${action}`;
3635
+ }
3636
+ function formatConnectionEndpointSuffix(connectionString) {
3637
+ const endpoint = formatConnectionEndpoint(connectionString);
3638
+ return endpoint ? ` (${endpoint})` : "";
3639
+ }
3640
+ function formatConnectionEndpoint(connectionString) {
3641
+ try {
3642
+ const url = new URL(connectionString);
3643
+ const host = url.hostname;
3644
+ if (!host) {
3645
+ return null;
3646
+ }
3647
+ let port = url.port;
3648
+ if (!port) {
3649
+ if (url.protocol === "postgresql:") {
3650
+ port = "5432";
3651
+ } else if (url.protocol === "mysql:") {
3652
+ port = "3306";
3653
+ }
3654
+ }
3655
+ return port ? `${host}:${port}` : host;
3656
+ } catch {
3657
+ return null;
3658
+ }
3659
+ }
3561
3660
  async function dbConnect(context, config2) {
3562
3661
  try {
3563
3662
  const db = new Db(config2);
3564
3663
  context.registerCleanup(async () => {
3565
3664
  await db.disconnect();
3566
- context.logger.debug?.(`[Db] instance "${config2.name}" disconnected`);
3665
+ context.logger.debug?.(formatDbInstanceMessage("disconnected", config2.name));
3567
3666
  });
3568
3667
  await db.connect();
3569
- context.logger.debug?.(`[Db] instance "${config2.name}" initialized`);
3668
+ context.logger.debug?.(formatDbInstanceMessage("initialized", config2.name));
3570
3669
  return db;
3571
3670
  } catch (error) {
3572
3671
  if (error instanceof ParamError) {