@vltpkg/cli-sdk 0.0.0-3 → 0.0.0-30

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.
Files changed (164) hide show
  1. package/README.md +3 -0
  2. package/dist/esm/commands/cache.d.ts +65 -0
  3. package/dist/esm/commands/cache.d.ts.map +1 -0
  4. package/dist/esm/commands/cache.js +257 -0
  5. package/dist/esm/commands/cache.js.map +1 -0
  6. package/dist/esm/commands/ci.d.ts +10 -0
  7. package/dist/esm/commands/ci.d.ts.map +1 -0
  8. package/dist/esm/commands/ci.js +29 -0
  9. package/dist/esm/commands/ci.js.map +1 -0
  10. package/dist/esm/commands/config.d.ts.map +1 -1
  11. package/dist/esm/commands/config.js +2 -52
  12. package/dist/esm/commands/config.js.map +1 -1
  13. package/dist/esm/commands/exec-cache.d.ts +49 -0
  14. package/dist/esm/commands/exec-cache.d.ts.map +1 -0
  15. package/dist/esm/commands/exec-cache.js +134 -0
  16. package/dist/esm/commands/exec-cache.js.map +1 -0
  17. package/dist/esm/commands/exec-local.d.ts +6 -0
  18. package/dist/esm/commands/exec-local.d.ts.map +1 -0
  19. package/dist/esm/commands/exec-local.js +24 -0
  20. package/dist/esm/commands/exec-local.js.map +1 -0
  21. package/dist/esm/commands/exec.d.ts +4 -0
  22. package/dist/esm/commands/exec.d.ts.map +1 -1
  23. package/dist/esm/commands/exec.js +69 -10
  24. package/dist/esm/commands/exec.js.map +1 -1
  25. package/dist/esm/commands/help.d.ts +1 -1
  26. package/dist/esm/commands/help.d.ts.map +1 -1
  27. package/dist/esm/commands/help.js +25 -3
  28. package/dist/esm/commands/help.js.map +1 -1
  29. package/dist/esm/commands/init.d.ts +4 -5
  30. package/dist/esm/commands/init.d.ts.map +1 -1
  31. package/dist/esm/commands/init.js +95 -9
  32. package/dist/esm/commands/init.js.map +1 -1
  33. package/dist/esm/commands/install/reporter.d.ts +1 -1
  34. package/dist/esm/commands/install/reporter.d.ts.map +1 -1
  35. package/dist/esm/commands/install/reporter.js +10 -11
  36. package/dist/esm/commands/install/reporter.js.map +1 -1
  37. package/dist/esm/commands/install.d.ts +5 -2
  38. package/dist/esm/commands/install.d.ts.map +1 -1
  39. package/dist/esm/commands/install.js +7 -1
  40. package/dist/esm/commands/install.js.map +1 -1
  41. package/dist/esm/commands/list.d.ts +1 -1
  42. package/dist/esm/commands/list.d.ts.map +1 -1
  43. package/dist/esm/commands/list.js +150 -42
  44. package/dist/esm/commands/list.js.map +1 -1
  45. package/dist/esm/commands/pack.d.ts +32 -0
  46. package/dist/esm/commands/pack.d.ts.map +1 -0
  47. package/dist/esm/commands/pack.js +147 -0
  48. package/dist/esm/commands/pack.js.map +1 -0
  49. package/dist/esm/commands/pkg.d.ts +2 -3
  50. package/dist/esm/commands/pkg.d.ts.map +1 -1
  51. package/dist/esm/commands/pkg.js +123 -38
  52. package/dist/esm/commands/pkg.js.map +1 -1
  53. package/dist/esm/commands/publish.d.ts +22 -0
  54. package/dist/esm/commands/publish.d.ts.map +1 -0
  55. package/dist/esm/commands/publish.js +239 -0
  56. package/dist/esm/commands/publish.js.map +1 -0
  57. package/dist/esm/commands/query.d.ts +1 -1
  58. package/dist/esm/commands/query.d.ts.map +1 -1
  59. package/dist/esm/commands/query.js +171 -32
  60. package/dist/esm/commands/query.js.map +1 -1
  61. package/dist/esm/commands/run-exec.d.ts +1 -0
  62. package/dist/esm/commands/run-exec.d.ts.map +1 -1
  63. package/dist/esm/commands/run-exec.js +1 -0
  64. package/dist/esm/commands/run-exec.js.map +1 -1
  65. package/dist/esm/commands/run.d.ts +1 -0
  66. package/dist/esm/commands/run.d.ts.map +1 -1
  67. package/dist/esm/commands/run.js +13 -16
  68. package/dist/esm/commands/run.js.map +1 -1
  69. package/dist/esm/commands/serve.d.ts +14 -0
  70. package/dist/esm/commands/serve.d.ts.map +1 -0
  71. package/dist/esm/commands/serve.js +97 -0
  72. package/dist/esm/commands/serve.js.map +1 -0
  73. package/dist/esm/commands/uninstall.d.ts +7 -1
  74. package/dist/esm/commands/uninstall.d.ts.map +1 -1
  75. package/dist/esm/commands/uninstall.js +7 -1
  76. package/dist/esm/commands/uninstall.js.map +1 -1
  77. package/dist/esm/commands/update.d.ts +10 -0
  78. package/dist/esm/commands/update.d.ts.map +1 -0
  79. package/dist/esm/commands/update.js +25 -0
  80. package/dist/esm/commands/update.js.map +1 -0
  81. package/dist/esm/commands/version.d.ts +26 -0
  82. package/dist/esm/commands/version.d.ts.map +1 -0
  83. package/dist/esm/commands/version.js +226 -0
  84. package/dist/esm/commands/version.js.map +1 -0
  85. package/dist/esm/commands/whoami.d.ts +4 -2
  86. package/dist/esm/commands/whoami.d.ts.map +1 -1
  87. package/dist/esm/commands/whoami.js +1 -1
  88. package/dist/esm/commands/whoami.js.map +1 -1
  89. package/dist/esm/config/definition.d.ts +102 -8
  90. package/dist/esm/config/definition.d.ts.map +1 -1
  91. package/dist/esm/config/definition.js +174 -25
  92. package/dist/esm/config/definition.js.map +1 -1
  93. package/dist/esm/config/index.d.ts +46 -40
  94. package/dist/esm/config/index.d.ts.map +1 -1
  95. package/dist/esm/config/index.js +126 -176
  96. package/dist/esm/config/index.js.map +1 -1
  97. package/dist/esm/config/merge.d.ts +3 -1
  98. package/dist/esm/config/merge.d.ts.map +1 -1
  99. package/dist/esm/config/merge.js +11 -6
  100. package/dist/esm/config/merge.js.map +1 -1
  101. package/dist/esm/config/usage.d.ts +3 -2
  102. package/dist/esm/config/usage.d.ts.map +1 -1
  103. package/dist/esm/config/usage.js.map +1 -1
  104. package/dist/esm/exec-command.d.ts +33 -14
  105. package/dist/esm/exec-command.d.ts.map +1 -1
  106. package/dist/esm/exec-command.js +214 -65
  107. package/dist/esm/exec-command.js.map +1 -1
  108. package/dist/esm/index.d.ts +1 -14
  109. package/dist/esm/index.d.ts.map +1 -1
  110. package/dist/esm/index.js +46 -20
  111. package/dist/esm/index.js.map +1 -1
  112. package/dist/esm/load-command.d.ts +16 -0
  113. package/dist/esm/load-command.d.ts.map +1 -0
  114. package/dist/esm/load-command.js +21 -0
  115. package/dist/esm/load-command.js.map +1 -0
  116. package/dist/esm/output.d.ts +6 -10
  117. package/dist/esm/output.d.ts.map +1 -1
  118. package/dist/esm/output.js +63 -33
  119. package/dist/esm/output.js.map +1 -1
  120. package/dist/esm/pack-tarball.d.ts +22 -0
  121. package/dist/esm/pack-tarball.d.ts.map +1 -0
  122. package/dist/esm/pack-tarball.js +247 -0
  123. package/dist/esm/pack-tarball.js.map +1 -0
  124. package/dist/esm/parse-add-remove-args.d.ts +1 -1
  125. package/dist/esm/parse-add-remove-args.d.ts.map +1 -1
  126. package/dist/esm/parse-add-remove-args.js +1 -1
  127. package/dist/esm/parse-add-remove-args.js.map +1 -1
  128. package/dist/esm/print-err.d.ts +12 -1
  129. package/dist/esm/print-err.d.ts.map +1 -1
  130. package/dist/esm/print-err.js +157 -26
  131. package/dist/esm/print-err.js.map +1 -1
  132. package/dist/esm/query-host-contexts.d.ts +16 -0
  133. package/dist/esm/query-host-contexts.d.ts.map +1 -0
  134. package/dist/esm/query-host-contexts.js +135 -0
  135. package/dist/esm/query-host-contexts.js.map +1 -0
  136. package/dist/esm/start-gui.d.ts +7 -33
  137. package/dist/esm/start-gui.d.ts.map +1 -1
  138. package/dist/esm/start-gui.js +40 -347
  139. package/dist/esm/start-gui.js.map +1 -1
  140. package/dist/esm/view.d.ts +2 -3
  141. package/dist/esm/view.d.ts.map +1 -1
  142. package/dist/esm/view.js +1 -1
  143. package/dist/esm/view.js.map +1 -1
  144. package/package.json +73 -45
  145. package/dist/esm/commands/gui.d.ts +0 -6
  146. package/dist/esm/commands/gui.d.ts.map +0 -1
  147. package/dist/esm/commands/gui.js +0 -14
  148. package/dist/esm/commands/gui.js.map +0 -1
  149. package/dist/esm/commands/install-exec.d.ts +0 -4
  150. package/dist/esm/commands/install-exec.d.ts.map +0 -1
  151. package/dist/esm/commands/install-exec.js +0 -13
  152. package/dist/esm/commands/install-exec.js.map +0 -1
  153. package/dist/esm/ignored-homedir-folder-names.d.ts +0 -7
  154. package/dist/esm/ignored-homedir-folder-names.d.ts.map +0 -1
  155. package/dist/esm/ignored-homedir-folder-names.js +0 -35
  156. package/dist/esm/ignored-homedir-folder-names.js.map +0 -1
  157. package/dist/esm/project-info.d.ts +0 -32
  158. package/dist/esm/project-info.d.ts.map +0 -1
  159. package/dist/esm/project-info.js +0 -90
  160. package/dist/esm/project-info.js.map +0 -1
  161. package/dist/esm/read-project-folders.d.ts +0 -27
  162. package/dist/esm/read-project-folders.d.ts.map +0 -1
  163. package/dist/esm/read-project-folders.js +0 -66
  164. package/dist/esm/read-project-folders.js.map +0 -1
@@ -21,20 +21,19 @@
21
21
  * @module
22
22
  */
23
23
  import { error } from '@vltpkg/error-cause';
24
+ import { PackageInfoClient } from '@vltpkg/package-info';
24
25
  import { PackageJson } from '@vltpkg/package-json';
26
+ import { resetCaches } from '@vltpkg/dep-id';
27
+ import { assertRecordStringString, assertRecordStringT, isRecordStringString, } from '@vltpkg/types';
28
+ import { find, load, reload, save } from '@vltpkg/vlt-json';
25
29
  import { Monorepo } from '@vltpkg/workspaces';
26
- import { XDG } from '@vltpkg/xdg';
27
- import { readFileSync, rmSync, writeFileSync } from 'fs';
28
- import { lstat, mkdir, readFile, writeFile } from 'fs/promises';
29
- import { homedir } from 'os';
30
- import { dirname, resolve } from 'path';
30
+ import { readFile, rm, writeFile } from 'node:fs/promises';
31
+ import { dirname } from 'node:path';
31
32
  import { PathScurry } from 'path-scurry';
32
- import { kIndent, kNewline, parse as jsonParse, stringify as jsonStringify, } from 'polite-json';
33
- import { walkUp } from 'walk-up-path';
34
33
  import { commands, definition, getCommand, isRecordField, recordFields, } from "./definition.js";
35
34
  import { merge } from "./merge.js";
36
- export { recordFields, isRecordField };
37
- export { definition, commands };
35
+ export { commands, definition, isRecordField, recordFields, };
36
+ export const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
38
37
  // turn a set of pairs into a Record object.
39
38
  // if a kv pair doesn't have a = character, set to `''`
40
39
  const reducePairs = (pairs) => {
@@ -70,7 +69,8 @@ export const recordsToPairs = (obj) => {
70
69
  .filter(([k]) => !(k === 'scurry' ||
71
70
  k === 'packageJson' ||
72
71
  k === 'monorepo' ||
73
- k === 'projectRoot'))
72
+ k === 'projectRoot' ||
73
+ k === 'packageInfo'))
74
74
  .map(([k, v]) => [
75
75
  k,
76
76
  k === 'command' && v && typeof v === 'object' ?
@@ -84,9 +84,6 @@ export const recordsToPairs = (obj) => {
84
84
  ]));
85
85
  };
86
86
  const kRecord = Symbol('parsed key=value record');
87
- const exists = (f) => lstat(f).then(() => true, () => false);
88
- const home = homedir();
89
- const xdg = new XDG('vlt');
90
87
  /**
91
88
  * Class that handles configuration for vlt.
92
89
  *
@@ -98,8 +95,6 @@ export class Config {
98
95
  * representing vlt's configuration
99
96
  */
100
97
  jack;
101
- stringifyOptions = { [kIndent]: ' ', [kNewline]: '\n' };
102
- configFiles = {};
103
98
  /**
104
99
  * Parsed values in effect
105
100
  */
@@ -116,14 +111,31 @@ export class Config {
116
111
  return this.#options;
117
112
  const scurry = new PathScurry(this.projectRoot);
118
113
  const packageJson = new PackageJson();
119
- this.#options = Object.assign(pairsToRecords(this.parse().values), {
114
+ const asRecords = pairsToRecords(this.parse().values);
115
+ const extras = {
120
116
  projectRoot: this.projectRoot,
121
117
  scurry,
122
118
  packageJson,
123
119
  monorepo: Monorepo.maybeLoad(this.projectRoot, {
124
120
  scurry,
125
121
  packageJson,
122
+ load: {
123
+ paths: asRecords.workspace,
124
+ groups: asRecords['workspace-group'],
125
+ },
126
126
  }),
127
+ catalog: load('catalog', assertRecordStringString),
128
+ catalogs: load('catalogs', o => assertRecordStringT(o, isRecordStringString, 'Record<string, Record<string, string>>')),
129
+ };
130
+ const options = Object.assign(asRecords, extras);
131
+ this.#options = Object.assign(options, {
132
+ packageInfo: new PackageInfoClient(options),
133
+ [kCustomInspect]() {
134
+ return Object.fromEntries(Object.entries(options).filter(([k]) => k !== 'monorepo' &&
135
+ k !== 'scurry' &&
136
+ k !== 'packageJson' &&
137
+ k !== 'packageInfo'));
138
+ },
127
139
  });
128
140
  return this.#options;
129
141
  }
@@ -134,6 +146,7 @@ export class Config {
134
146
  resetOptions(projectRoot = process.cwd()) {
135
147
  this.projectRoot = projectRoot;
136
148
  this.#options = undefined;
149
+ resetCaches();
137
150
  }
138
151
  // memoized options() getter value
139
152
  #options;
@@ -142,7 +155,12 @@ export class Config {
142
155
  */
143
156
  positionals;
144
157
  /**
145
- * The root of the project where a vlt.json, vlt-workspaces.json,
158
+ * Original arguments used for parsing (stored for reload purposes)
159
+ * @internal
160
+ */
161
+ #originalArgs;
162
+ /**
163
+ * The root of the project where a vlt.json, vlt.json,
146
164
  * package.json, or .git was found. Not necessarily the `process.cwd()`,
147
165
  * though that is the default location.
148
166
  *
@@ -170,6 +188,8 @@ export class Config {
170
188
  parse(args = process.argv) {
171
189
  if (isParsed(this))
172
190
  return this;
191
+ // Store the original args for potential reload
192
+ this.#originalArgs = [...args];
173
193
  this.jack.loadEnvDefaults();
174
194
  const p = this.jack.parseRaw(args);
175
195
  const fallback = getCommand(p.values['fallback-command']);
@@ -237,86 +257,55 @@ export class Config {
237
257
  * Write the config values to the user or project config file.
238
258
  */
239
259
  async writeConfigFile(which, values) {
240
- const f = this.getFilename(which);
241
- await mkdir(dirname(f), { recursive: true });
242
- const vals = Object.assign(pairsToRecords(values), this.stringifyOptions);
243
- await writeFile(f, jsonStringify(vals));
244
- this.configFiles[f] = vals;
245
- return values;
260
+ save('config', pairsToRecords(values), which);
261
+ await this.#reloadConfig();
246
262
  }
247
263
  /**
248
264
  * Fold in the provided fields with the existing properties
249
265
  * in the config file.
250
266
  */
251
267
  async addConfigToFile(which, values) {
252
- const f = this.getFilename(which);
253
- return this.writeConfigFile(which, merge((await this.#maybeLoadConfigFile(f)) ?? {}, values));
254
- }
255
- /**
256
- * if the file exists, parse and load it. returns object if data was
257
- * loaded, or undefined if not.
258
- */
259
- async #maybeLoadConfigFile(file) {
260
- const result = await this.#readConfigFile(file);
261
- if (result) {
262
- try {
263
- const { command, ...values } = recordsToPairs(result);
264
- if (command) {
265
- for (const [c, opts] of Object.entries(command)) {
266
- const cmd = getCommand(c);
267
- if (cmd) {
268
- this.commandValues[cmd] = merge(this.commandValues[cmd] ?? {}, opts);
269
- }
270
- }
271
- }
272
- this.jack.setConfigValues(values, file);
273
- return result;
274
- }
275
- catch (er) {
276
- throw error('failed to load config values from file', {
277
- path: file,
278
- cause: er,
279
- });
280
- }
281
- }
268
+ return this.writeConfigFile(which, merge((await this.#maybeLoadConfigFile(which)) ?? {}, values));
282
269
  }
283
- async #readConfigFile(file) {
284
- if (this.configFiles[file])
285
- return this.configFiles[file];
286
- const data = await readFile(file, 'utf8').catch(() => { });
287
- if (!data)
288
- return undefined;
289
- let result;
290
- try {
291
- result = jsonParse(data);
292
- if (result && typeof result === 'object') {
293
- if (result[kIndent] !== undefined)
294
- this.stringifyOptions[kIndent] = result[kIndent];
295
- if (result[kNewline] !== undefined)
296
- this.stringifyOptions[kNewline] = result[kNewline];
297
- }
298
- }
299
- catch (er) {
300
- throw error('failed to parse vlt config file', {
270
+ // called in this weird bound way so that it can be used by the
271
+ // vlt-json config loading module.
272
+ #validator = function (c, file) {
273
+ this.#validateConfig(c, file);
274
+ }.bind(this);
275
+ #validateConfig(c, file) {
276
+ if (!c || typeof c !== 'object' || Array.isArray(c)) {
277
+ throw error('invalid config, expected object', {
301
278
  path: file,
302
- cause: er,
279
+ found: c,
280
+ wanted: 'ConfigFileData',
303
281
  });
304
282
  }
305
- this.configFiles[file] = result;
306
- return result;
283
+ const { command, ...values } = recordsToPairs(c);
284
+ if (command) {
285
+ for (const [c, opts] of Object.entries(command)) {
286
+ const cmd = getCommand(c);
287
+ if (cmd) {
288
+ this.commandValues[cmd] = merge(this.commandValues[cmd] ?? {}, opts);
289
+ }
290
+ }
291
+ }
292
+ this.jack.setConfigValues(values, file);
307
293
  }
308
- getFilename(which = 'project') {
309
- return which === 'user' ?
310
- xdg.config('vlt.json')
311
- : resolve(this.projectRoot, 'vlt.json');
294
+ /**
295
+ * if the file exists, parse and load it. returns object if data was
296
+ * loaded, or undefined if not.
297
+ */
298
+ async #maybeLoadConfigFile(whichConfig) {
299
+ return load('config', this.#validator, whichConfig);
312
300
  }
301
+ /**
302
+ * Deletes the specified config fields from the named file
303
+ * Returns `true` if anything was changed.
304
+ */
313
305
  async deleteConfigKeys(which, fields) {
314
- const file = this.getFilename(which);
315
- const data = await this.#maybeLoadConfigFile(file);
316
- if (!data) {
317
- rmSync(file, { force: true });
306
+ const data = await this.#maybeLoadConfigFile(which);
307
+ if (!data)
318
308
  return false;
319
- }
320
309
  let didSomething = false;
321
310
  for (const f of fields) {
322
311
  const [key, ...sk] = f.split('.');
@@ -329,9 +318,10 @@ export class Config {
329
318
  if (Array.isArray(v)) {
330
319
  const i = v.findIndex(subvalue => subvalue.startsWith(`${subs}=`));
331
320
  if (i !== -1) {
332
- v.splice(i, 1);
333
- if (v.length === 0)
321
+ if (v.length === 1)
334
322
  delete data[k];
323
+ else
324
+ v.splice(i, 1);
335
325
  didSomething = true;
336
326
  }
337
327
  }
@@ -349,13 +339,8 @@ export class Config {
349
339
  delete data[k];
350
340
  }
351
341
  }
352
- const d = jsonStringify(data);
353
- if (d.trim() === '{}') {
354
- rmSync(file, { force: true });
355
- }
356
- else {
357
- writeFileSync(file, jsonStringify(data));
358
- }
342
+ if (didSomething)
343
+ await this.writeConfigFile(which, data);
359
344
  return didSomething;
360
345
  }
361
346
  /**
@@ -369,39 +354,33 @@ export class Config {
369
354
  * mean deleting the file.
370
355
  */
371
356
  async editConfigFile(which, edit) {
372
- const file = this.getFilename(which);
373
- const backup = this.configFiles[file];
357
+ // load the file as a backup
358
+ // call the edit function
359
+ // reload it
360
+ const file = find(which);
361
+ const backup = await readFile(file, 'utf8').catch(() => undefined);
374
362
  if (!backup) {
375
- writeFileSync(file, '{\n\n}\n');
363
+ await writeFile(file, JSON.stringify({ config: {} }, null, 2) + '\n');
376
364
  }
377
365
  let valid = false;
378
366
  try {
379
367
  await edit(file);
380
- const res = jsonParse(readFileSync(file, 'utf8'));
381
- if (!res || typeof res !== 'object' || Array.isArray(res)) {
382
- throw error('Invalid configuration, expected object', {
383
- path: file,
384
- found: res,
385
- });
386
- }
387
- if (Object.keys(res).length === 0) {
388
- // nothing there, remove file
389
- delete this.configFiles[file];
390
- rmSync(file, { force: true });
391
- }
392
- else {
393
- this.jack.setConfigValues(recordsToPairs(res));
394
- this.configFiles[file] = res;
395
- }
368
+ // force it to reload the file and validate it again
369
+ // if this fails, we roll back.
370
+ const result = reload('config', which);
371
+ save('config', result ?? {}, which);
396
372
  valid = true;
397
373
  }
398
374
  finally {
399
375
  if (!valid) {
376
+ // TODO: maybe write the file to a re-edit backup location?
377
+ // then you could do `vlt config edit retry` or something.
400
378
  if (backup) {
401
- writeFileSync(file, jsonStringify(backup));
379
+ await writeFile(file, backup);
380
+ reload(which);
402
381
  }
403
382
  else {
404
- rmSync(file, { force: true });
383
+ await rm(file, { force: true });
405
384
  }
406
385
  }
407
386
  }
@@ -409,73 +388,45 @@ export class Config {
409
388
  /**
410
389
  * Find the local config file and load both it and the user-level config in
411
390
  * the XDG config home.
412
- *
413
- * Note: if working in a workspaces monorepo, then the vlt.json file MUST
414
- * be in the same folder as the vlt-workspaces.json file, because we stop
415
- * looking when we find either one.
416
391
  */
417
392
  async loadConfigFile() {
418
- const userConfig = xdg.config('vlt.json');
419
- await this.#maybeLoadConfigFile(userConfig);
420
- let lastKnownRoot = resolve(this.projectRoot);
421
- for (const dir of walkUp(this.projectRoot)) {
422
- // don't look in ~
423
- if (dir === home)
424
- break;
425
- // finding a project config file stops the search
426
- const projectConfig = resolve(dir, 'vlt.json');
427
- if (projectConfig === userConfig)
428
- break;
429
- if ((await exists(projectConfig)) &&
430
- (await this.#maybeLoadConfigFile(projectConfig))) {
431
- lastKnownRoot = dir;
432
- break;
433
- }
434
- // stat existence of these files
435
- const [hasPackage, hasModules, hasWorkspaces, hasGit] = await Promise.all([
436
- exists(resolve(dir, 'package.json')),
437
- exists(resolve(dir, 'node_modules')),
438
- exists(resolve(dir, 'vlt-workspaces.json')),
439
- exists(resolve(dir, '.git')),
440
- ]);
441
- // treat these as potential roots
442
- if (hasPackage || hasModules || hasWorkspaces) {
443
- lastKnownRoot = dir;
444
- }
445
- // define backstops
446
- if (hasWorkspaces || hasGit) {
447
- break;
448
- }
449
- }
450
- this.projectRoot = lastKnownRoot;
393
+ await this.#maybeLoadConfigFile('user');
394
+ this.projectRoot = dirname(find('project', this.projectRoot));
395
+ await this.#maybeLoadConfigFile('project');
451
396
  return this;
452
397
  }
453
398
  /**
454
- * Determine whether we should use colors in the output. Update
455
- * chalk appropriately.
456
- *
457
- * Implicitly calls this.parse() if it not parsed already.
399
+ * Clear cached config values to force re-reading from updated files.
400
+ * @internal
458
401
  */
459
- async loadColor() {
460
- const c = this.get('color');
461
- const chalk = (await import('chalk')).default;
462
- let color;
463
- if (process.env.NO_COLOR !== '1' &&
464
- (c === true || (c === undefined && chalk.level > 0))) {
465
- color = true;
466
- chalk.level = Math.max(chalk.level, 1);
467
- process.env.FORCE_COLOR = String(chalk.level);
468
- delete process.env.NO_COLOR;
469
- }
470
- else {
471
- color = false;
472
- chalk.level = 0;
473
- process.env.FORCE_COLOR = '0';
474
- process.env.NO_COLOR = '1';
475
- }
476
- const { values = this.parse().values } = this;
477
- values.color = color;
478
- return this;
402
+ async #reloadConfig() {
403
+ // Clear the memoized options to force recalculation
404
+ this.#options = undefined;
405
+ }
406
+ /**
407
+ * Force a complete reload of config files from disk.
408
+ * This clears all caches and re-reads config files.
409
+ * Useful for long-running processes that need to pick up config changes.
410
+ */
411
+ async reloadFromDisk() {
412
+ // Clear the memoized options to force recalculation
413
+ this.#options = undefined;
414
+ // Clear the parsed state to force re-parsing
415
+ // This is crucial because parse() returns early if already parsed
416
+ this.values = undefined;
417
+ this.positionals = undefined;
418
+ this.command = undefined;
419
+ // Clear vlt-json caches for both user and project configs
420
+ // This ensures that the next time config files are read, they'll be re-read from disk
421
+ const { unload } = await import('@vltpkg/vlt-json');
422
+ unload('user');
423
+ unload('project');
424
+ // Force reload of config files by calling the load methods again
425
+ // This will re-read the files and re-apply them to the jack parser
426
+ await this.#maybeLoadConfigFile('user');
427
+ await this.#maybeLoadConfigFile('project');
428
+ // Re-parse to pick up the updated config values using the original arguments
429
+ this.parse(this.#originalArgs);
479
430
  }
480
431
  /**
481
432
  * cache of the loaded config
@@ -495,8 +446,7 @@ export class Config {
495
446
  return this.#loaded;
496
447
  const a = new Config(definition, projectRoot);
497
448
  const b = await a.loadConfigFile();
498
- const c = await b.parse(argv).loadColor();
499
- this.#loaded = c;
449
+ this.#loaded = b.parse(argv);
500
450
  return this.#loaded;
501
451
  }
502
452
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/config/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,IAAI,CAAA;AACxD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE/D,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAA;AAC5B,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EACL,OAAO,EACP,QAAQ,EACR,KAAK,IAAI,SAAS,EAClB,SAAS,IAAI,aAAa,GAC3B,MAAM,aAAa,C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OAAO,CAAC,CAAA","sourcesContent":["/**\n * Module that handles all vlt configuration needs\n *\n * Project-level configs are set in a `vlt.json` file in the local project\n * if present. This will override the user-level configs in the appropriate\n * XDG config path.\n *\n * Command-specific configuration can be specified by putting options in a\n * field in the `command` object. For example:\n *\n * ```json\n * {\n * \"registry\": \"https://registry.npmjs.org/\",\n * \"command\": {\n * \"publish\": {\n * \"registry\": \"http://registry.internal\"\n * }\n * }\n * }\n * ```\n * @module\n */\n\nimport { error } from '@vltpkg/error-cause'\nimport { PackageJson } from '@vltpkg/package-json'\nimport { Monorepo } from '@vltpkg/workspaces'\nimport { XDG } from '@vltpkg/xdg'\nimport { readFileSync, rmSync, writeFileSync } from 'fs'\nimport { lstat, mkdir, readFile, writeFile } from 'fs/promises'\nimport type { Jack, OptionsResults, Unwrap } from 'jackspeak'\nimport { homedir } from 'os'\nimport { dirname, resolve } from 'path'\nimport { PathScurry } from 'path-scurry'\nimport {\n kIndent,\n kNewline,\n parse as jsonParse,\n stringify as jsonStringify,\n} from 'polite-json'\nimport type { JSONResult } from 'polite-json'\nimport { walkUp } from 'walk-up-path'\nimport {\n commands,\n definition,\n getCommand,\n isRecordField,\n recordFields,\n} from './definition.ts'\nimport type { Commands, RecordField } from './definition.ts'\nimport { merge } from './merge.ts'\nexport { recordFields, isRecordField }\nexport { definition, commands, type Commands }\n\nexport type RecordPairs = Record<string, unknown>\nexport type RecordString = Record<string, string>\nexport type ConfigFiles = Record<string, ConfigFileData>\n\n// turn a set of pairs into a Record object.\n// if a kv pair doesn't have a = character, set to `''`\nconst reducePairs = <T extends string[]>(\n pairs: T,\n): RecordString | T => {\n const record: RecordString = {}\n for (const kv of pairs) {\n const eq = kv.indexOf('=')\n if (eq === -1) record[kv] = ''\n else {\n const key = kv.substring(0, eq)\n const val = kv.substring(eq + 1)\n record[key] = val\n }\n }\n return record\n}\n\nconst isRecordFieldValue = (k: string, v: unknown): v is string[] =>\n Array.isArray(v) &&\n recordFields.includes(k as (typeof recordFields)[number])\n\nexport type PairsAsRecords = Omit<\n ConfigOptions,\n 'projectRoot' | 'scurry' | 'packageJson' | 'monorepo'\n> & {\n command?: Record<string, ConfigOptions>\n}\n\nexport const pairsToRecords = (\n obj: ConfigFileData,\n): PairsAsRecords => {\n return Object.fromEntries(\n Object.entries(obj).map(([k, v]) => [\n k,\n k === 'command' && v && typeof v === 'object' ?\n Object.fromEntries(\n Object.entries(v).map(([k, v]) => [\n k,\n pairsToRecords(v as ConfigFileData),\n ]),\n )\n : isRecordFieldValue(k, v) ? reducePairs(v)\n : v,\n ]),\n // hard cast because TS can't see through the entries/fromEntries\n ) as unknown as PairsAsRecords\n}\n\nexport const recordsToPairs = (obj: RecordPairs): RecordPairs => {\n return Object.fromEntries(\n Object.entries(obj)\n .filter(\n ([k]) =>\n !(\n k === 'scurry' ||\n k === 'packageJson' ||\n k === 'monorepo' ||\n k === 'projectRoot'\n ),\n )\n .map(([k, v]) => [\n k,\n k === 'command' && v && typeof v === 'object' ?\n recordsToPairs(v as RecordPairs)\n : (\n !v ||\n typeof v !== 'object' ||\n Array.isArray(v) ||\n !isRecordField(k)\n ) ?\n v\n : Object.entries(v).map(([k, v]) => `${k}=${v}`),\n ]),\n )\n}\n\nconst kRecord = Symbol('parsed key=value record')\nconst exists = (f: string) =>\n lstat(f).then(\n () => true,\n () => false,\n )\n\nconst home = homedir()\nconst xdg = new XDG('vlt')\n\n/**\n * Config data can be any options, and also a 'command' field which\n * contains command names and override options for that command.\n */\nexport type ConfigData = OptionsResults<ConfigDefinitions> & {\n command?: Record<string, OptionsResults<ConfigDefinitions>>\n}\n\n/**\n * Config data as it appears in config files, with kv pair lists\n * stored as `Record<string, string>`.\n */\nexport type ConfigFileData = {\n [k in keyof ConfigData]?: k extends OptListKeys<ConfigData> ?\n RecordString | string[]\n : k extends 'command' ? ConfigFiles\n : ConfigData[k]\n}\n\nexport type ConfigOptions = {\n [k in keyof ConfigData]: k extends RecordField ? RecordString\n : k extends 'command' ? never\n : ConfigData[k]\n} & {\n packageJson: PackageJson\n scurry: PathScurry\n projectRoot: string\n monorepo?: Monorepo\n}\n\n/**\n * The base config definition set as a type\n */\nexport type ConfigDefinitions = Unwrap<typeof definition>\n\nexport type StringListKeys<O> = {\n [k in keyof O]: O[k] extends string[] | undefined ? k : never\n}\nexport type OptListKeys<O> = Exclude<\n StringListKeys<O>[keyof StringListKeys<O>],\n undefined\n>\n\n/**\n * Class that handles configuration for vlt.\n *\n * Call {@link Config.load} to get one of these.\n */\nexport class Config {\n /**\n * The {@link https://npmjs.com/jackspeak | JackSpeak} object\n * representing vlt's configuration\n */\n jack: Jack<ConfigDefinitions>\n\n stringifyOptions: {\n [kIndent]: string\n [kNewline]: string\n } = { [kIndent]: ' ', [kNewline]: '\\n' }\n\n configFiles: ConfigFiles = {}\n\n /**\n * Parsed values in effect\n */\n values?: OptionsResults<ConfigDefinitions>\n\n /**\n * Command-specific config values\n */\n commandValues: {\n [cmd in Commands[keyof Commands]]?: ConfigData\n } = {}\n\n /**\n * A flattened object of the parsed configuration\n */\n get options(): ConfigOptions {\n if (this.#options) return this.#options\n const scurry = new PathScurry(this.projectRoot)\n const packageJson = new PackageJson()\n this.#options = Object.assign(\n pairsToRecords(this.parse().values),\n {\n projectRoot: this.projectRoot,\n scurry,\n packageJson,\n monorepo: Monorepo.maybeLoad(this.projectRoot, {\n scurry,\n packageJson,\n }),\n },\n )\n return this.#options\n }\n\n /**\n * Reset the options value, optionally setting a new project root\n * to recalculate the options.\n */\n resetOptions(projectRoot: string = process.cwd()) {\n this.projectRoot = projectRoot\n this.#options = undefined\n }\n\n // memoized options() getter value\n #options?: ConfigOptions\n\n /**\n * positional arguments to the vlt process\n */\n positionals?: string[]\n\n /**\n * The root of the project where a vlt.json, vlt-workspaces.json,\n * package.json, or .git was found. Not necessarily the `process.cwd()`,\n * though that is the default location.\n *\n * Never walks up as far as `$HOME`. So for example, if a project is in\n * `~/projects/xyz`, then the highest dir it will check is `~/projects`\n */\n projectRoot: string\n\n /**\n * `Record<alias, canonical name>` to dereference command aliases.\n */\n commands: Commands\n\n /**\n * Which command name to use for overriding with command-specific values,\n * determined from the argv when parse() is called.\n */\n command?: Commands[keyof Commands]\n\n constructor(\n jack: Jack<ConfigDefinitions> = definition,\n projectRoot = process.cwd(),\n ) {\n this.projectRoot = projectRoot\n this.commands = commands\n this.jack = jack\n }\n\n /**\n * Parse the arguments and set configuration and positionals accordingly.\n */\n parse(args: string[] = process.argv): this & ParsedConfig {\n if (isParsed(this)) return this\n\n this.jack.loadEnvDefaults()\n const p = this.jack.parseRaw(args)\n\n const fallback = getCommand(p.values['fallback-command'])\n this.command = getCommand(p.positionals[0])\n\n const cmdOrFallback = this.command ?? fallback\n const cmdSpecific =\n cmdOrFallback && this.commandValues[cmdOrFallback]\n if (cmdSpecific) {\n this.jack.setConfigValues(recordsToPairs(cmdSpecific))\n }\n\n // ok, applied cmd-specific defaults, do rest of the parse\n this.jack.applyDefaults(p)\n this.jack.writeEnv(p)\n\n if (this.command) p.positionals.shift()\n else this.command = getCommand(p.values['fallback-command'])\n\n Object.assign(this, p)\n\n /* c8 ignore start - unpossible */\n if (!isParsed(this)) throw error('failed to parse config')\n /* c8 ignore stop */\n\n return this\n }\n\n /**\n * Get a `key=value` list option value as an object.\n *\n * For example, a list option with a vlaue of `['key=value', 'xyz=as=df' ]`\n * would be returned as `{key: 'value', xyz: 'as=df'}`\n *\n * Results are memoized, so subsequent calls for the same key will return the\n * same object. If new strings are added to the list, then the memoized value\n * is *not* updated, so only use once configurations have been fully loaded.\n *\n * If the config value is not set at all, an empty object is returned.\n */\n getRecord(k: OptListKeys<ConfigData>): RecordString {\n const pairs = this.get(k) as\n | (string[] & { [kRecord]?: RecordString })\n | undefined\n if (!pairs) return {}\n if (pairs[kRecord]) return pairs[kRecord]\n const kv = pairs.reduce((kv: RecordString, pair) => {\n const eq = pair.indexOf('=')\n if (eq === -1) return kv\n const key = pair.substring(0, eq)\n const val = pair.substring(eq + 1)\n kv[key] = val\n return kv\n }, {})\n Object.assign(pairs, { [kRecord]: kv })\n return kv\n }\n\n /**\n * Get a configuration value.\n *\n * Note: `key=value` pair configs are returned as a string array. To get them\n * as an object, use {@link Config#getRecord}.\n */\n get<K extends keyof OptionsResults<ConfigDefinitions>>(\n k: K,\n ): OptionsResults<ConfigDefinitions>[K] {\n /* c8 ignore next -- impossible but TS doesn't know that */\n return (this.values ?? this.parse().values)[k]\n }\n\n /**\n * Write the config values to the user or project config file.\n */\n async writeConfigFile(\n which: 'project' | 'user',\n values: ConfigFileData,\n ) {\n const f = this.getFilename(which)\n await mkdir(dirname(f), { recursive: true })\n const vals = Object.assign(\n pairsToRecords(values),\n this.stringifyOptions,\n )\n await writeFile(f, jsonStringify(vals))\n this.configFiles[f] = vals\n return values\n }\n\n /**\n * Fold in the provided fields with the existing properties\n * in the config file.\n */\n async addConfigToFile(\n which: 'project' | 'user',\n values: ConfigFileData,\n ) {\n const f = this.getFilename(which)\n return this.writeConfigFile(\n which,\n merge((await this.#maybeLoadConfigFile(f)) ?? {}, values),\n )\n }\n\n /**\n * if the file exists, parse and load it. returns object if data was\n * loaded, or undefined if not.\n */\n async #maybeLoadConfigFile(\n file: string,\n ): Promise<ConfigFileData | undefined> {\n const result = await this.#readConfigFile(file)\n\n if (result) {\n try {\n const { command, ...values } = recordsToPairs(result)\n if (command) {\n for (const [c, opts] of Object.entries(command)) {\n const cmd = getCommand(c)\n if (cmd) {\n this.commandValues[cmd] = merge<ConfigData>(\n this.commandValues[cmd] ?? ({} as ConfigData),\n opts as ConfigData,\n )\n }\n }\n }\n this.jack.setConfigValues(values, file)\n return result\n } catch (er) {\n throw error('failed to load config values from file', {\n path: file,\n cause: er,\n })\n }\n }\n }\n\n async #readConfigFile(\n file: string,\n ): Promise<ConfigFileData | undefined> {\n if (this.configFiles[file]) return this.configFiles[file]\n const data = await readFile(file, 'utf8').catch(() => {})\n if (!data) return undefined\n let result: JSONResult\n try {\n result = jsonParse(data)\n if (result && typeof result === 'object') {\n if (result[kIndent] !== undefined)\n this.stringifyOptions[kIndent] = result[kIndent]\n if (result[kNewline] !== undefined)\n this.stringifyOptions[kNewline] = result[kNewline]\n }\n } catch (er) {\n throw error('failed to parse vlt config file', {\n path: file,\n cause: er,\n })\n }\n this.configFiles[file] = result as ConfigFileData\n return result as ConfigFileData\n }\n\n getFilename(which: 'project' | 'user' = 'project'): string {\n return which === 'user' ?\n xdg.config('vlt.json')\n : resolve(this.projectRoot, 'vlt.json')\n }\n\n async deleteConfigKeys(\n which: 'project' | 'user',\n fields: string[],\n ) {\n const file = this.getFilename(which)\n const data = await this.#maybeLoadConfigFile(file)\n if (!data) {\n rmSync(file, { force: true })\n return false\n }\n let didSomething = false\n for (const f of fields) {\n const [key, ...sk] = f.split('.') as [\n h: string,\n ...rest: string[],\n ]\n const subs = sk.join('.')\n const k = key as keyof ConfigDefinitions\n const v = data[k]\n if (v === undefined) continue\n if (subs && v && typeof v === 'object') {\n if (Array.isArray(v)) {\n const i = v.findIndex(subvalue =>\n subvalue.startsWith(`${subs}=`),\n )\n if (i !== -1) {\n v.splice(i, 1)\n if (v.length === 0) delete data[k]\n didSomething = true\n }\n } else {\n if (v[subs] !== undefined) {\n delete v[subs]\n if (Object.keys(v).length === 0) delete data[k]\n didSomething = true\n }\n }\n } else {\n didSomething = true\n delete data[k]\n }\n }\n const d = jsonStringify(data)\n if (d.trim() === '{}') {\n rmSync(file, { force: true })\n } else {\n writeFileSync(file, jsonStringify(data))\n }\n return didSomething\n }\n\n /**\n * Edit the user or project configuration file.\n *\n * If the file isn't present, then it starts with `{}` so the user has\n * something to work with.\n *\n * If the result is not valid, or no config settings are contained in the\n * file after editing, then it's restored to what it was before, which might\n * mean deleting the file.\n */\n async editConfigFile(\n which: 'project' | 'user',\n edit: (file: string) => Promise<void> | void,\n ) {\n const file = this.getFilename(which)\n const backup = this.configFiles[file]\n if (!backup) {\n writeFileSync(file, '{\\n\\n}\\n')\n }\n let valid = false\n try {\n await edit(file)\n const res = jsonParse(readFileSync(file, 'utf8'))\n if (!res || typeof res !== 'object' || Array.isArray(res)) {\n throw error('Invalid configuration, expected object', {\n path: file,\n found: res,\n })\n }\n if (Object.keys(res).length === 0) {\n // nothing there, remove file\n delete this.configFiles[file]\n rmSync(file, { force: true })\n } else {\n this.jack.setConfigValues(recordsToPairs(res))\n this.configFiles[file] = res as ConfigFileData\n }\n valid = true\n } finally {\n if (!valid) {\n if (backup) {\n writeFileSync(file, jsonStringify(backup))\n } else {\n rmSync(file, { force: true })\n }\n }\n }\n }\n\n /**\n * Find the local config file and load both it and the user-level config in\n * the XDG config home.\n *\n * Note: if working in a workspaces monorepo, then the vlt.json file MUST\n * be in the same folder as the vlt-workspaces.json file, because we stop\n * looking when we find either one.\n */\n async loadConfigFile(): Promise<this> {\n const userConfig = xdg.config('vlt.json')\n await this.#maybeLoadConfigFile(userConfig)\n\n let lastKnownRoot = resolve(this.projectRoot)\n for (const dir of walkUp(this.projectRoot)) {\n // don't look in ~\n if (dir === home) break\n\n // finding a project config file stops the search\n const projectConfig = resolve(dir, 'vlt.json')\n if (projectConfig === userConfig) break\n if (\n (await exists(projectConfig)) &&\n (await this.#maybeLoadConfigFile(projectConfig))\n ) {\n lastKnownRoot = dir\n break\n }\n\n // stat existence of these files\n const [hasPackage, hasModules, hasWorkspaces, hasGit] =\n await Promise.all([\n exists(resolve(dir, 'package.json')),\n exists(resolve(dir, 'node_modules')),\n exists(resolve(dir, 'vlt-workspaces.json')),\n exists(resolve(dir, '.git')),\n ])\n\n // treat these as potential roots\n if (hasPackage || hasModules || hasWorkspaces) {\n lastKnownRoot = dir\n }\n\n // define backstops\n if (hasWorkspaces || hasGit) {\n break\n }\n }\n this.projectRoot = lastKnownRoot\n return this\n }\n\n /**\n * Determine whether we should use colors in the output. Update\n * chalk appropriately.\n *\n * Implicitly calls this.parse() if it not parsed already.\n */\n async loadColor(): Promise<this & LoadedConfig> {\n const c = this.get('color')\n const chalk = (await import('chalk')).default\n let color: boolean\n if (\n process.env.NO_COLOR !== '1' &&\n (c === true || (c === undefined && chalk.level > 0))\n ) {\n color = true\n chalk.level = Math.max(chalk.level, 1) as 0 | 1 | 2 | 3\n process.env.FORCE_COLOR = String(chalk.level)\n delete process.env.NO_COLOR\n } else {\n color = false\n chalk.level = 0\n process.env.FORCE_COLOR = '0'\n process.env.NO_COLOR = '1'\n }\n const { values = this.parse().values } = this\n ;(values as ConfigData & { color: boolean }).color = color\n return this as this & LoadedConfig\n }\n\n /**\n * cache of the loaded config\n */\n static #loaded: LoadedConfig | undefined\n\n /**\n * Load the configuration and return a Promise to a\n * {@link Config} object\n */\n static async load(\n projectRoot = process.cwd(),\n argv = process.argv,\n /**\n * only used in tests, resets the memoization\n * @internal\n */\n reload = false,\n ): Promise<LoadedConfig> {\n if (this.#loaded && !reload) return this.#loaded\n const a = new Config(definition, projectRoot)\n const b = await a.loadConfigFile()\n const c = await b.parse(argv).loadColor()\n this.#loaded = c as LoadedConfig\n return this.#loaded\n }\n}\n\nconst isParsed = (c: Config): c is ParsedConfig =>\n !!(c.values && c.positionals && c.command)\n\nexport type ParsedConfig = Config & {\n command: NonNullable<Config['command']>\n values: OptionsResults<ConfigDefinitions>\n positionals: string[]\n}\n\n/**\n * A fully loaded {@link Config} object\n */\nexport type LoadedConfig = ParsedConfig & {\n get(k: 'color'): boolean\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/config/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE5C,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE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QAC5B,IAAI,CAAC,OAAO,GAAG,SAAS,CAAA;QAExB,0DAA0D;QAC1D,sFAAsF;QACtF,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAA;QACnD,MAAM,CAAC,MAAM,CAAC,CAAA;QACd,MAAM,CAAC,SAAS,CAAC,CAAA;QAEjB,iEAAiE;QACjE,mEAAmE;QACnE,MAAM,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAA;QACvC,MAAM,IAAI,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAA;QAE1C,6EAA6E;QAC7E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;IAChC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAO,CAA0B;IAExC;;;OAGG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CACf,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE,EAC3B,IAAI,GAAG,OAAO,CAAC,IAAI;IACnB;;;OAGG;IACH,MAAM,GAAG,KAAK;QAEd,IAAI,IAAI,CAAC,OAAO,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,OAAO,CAAA;QAChD,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CAAA;QAC7C,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,cAAc,EAAE,CAAA;QAClC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAiB,CAAA;QAC5C,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;CACF;AAED,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAqB,EAAE,CAChD,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,OAAO,CAAC,CAAA","sourcesContent":["/**\n * Module that handles all vlt configuration needs\n *\n * Project-level configs are set in a `vlt.json` file in the local project\n * if present. This will override the user-level configs in the appropriate\n * XDG config path.\n *\n * Command-specific configuration can be specified by putting options in a\n * field in the `command` object. For example:\n *\n * ```json\n * {\n * \"registry\": \"https://registry.npmjs.org/\",\n * \"command\": {\n * \"publish\": {\n * \"registry\": \"http://registry.internal\"\n * }\n * }\n * }\n * ```\n * @module\n */\n\nimport { error } from '@vltpkg/error-cause'\nimport { PackageInfoClient } from '@vltpkg/package-info'\nimport { PackageJson } from '@vltpkg/package-json'\nimport { resetCaches } from '@vltpkg/dep-id'\nimport type { SpecOptions } from '@vltpkg/spec'\nimport {\n assertRecordStringString,\n assertRecordStringT,\n isRecordStringString,\n} from '@vltpkg/types'\nimport type { Validator, WhichConfig } from '@vltpkg/vlt-json'\nimport { find, load, reload, save } from '@vltpkg/vlt-json'\nimport { Monorepo } from '@vltpkg/workspaces'\nimport type { Jack, OptionsResults, Unwrap } from 'jackspeak'\nimport { readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\nimport { PathScurry } from 'path-scurry'\nimport type { Commands, RecordField } from './definition.ts'\nimport {\n commands,\n definition,\n getCommand,\n isRecordField,\n recordFields,\n} from './definition.ts'\nimport { merge } from './merge.ts'\nexport {\n commands,\n definition,\n isRecordField,\n recordFields,\n type Commands,\n}\n\nexport const kCustomInspect = Symbol.for('nodejs.util.inspect.custom')\n\nexport type RecordPairs = Record<string, unknown>\nexport type RecordString = Record<string, string>\n\n// turn a set of pairs into a Record object.\n// if a kv pair doesn't have a = character, set to `''`\nconst reducePairs = <T extends string[]>(\n pairs: T,\n): RecordString | T => {\n const record: RecordString = {}\n for (const kv of pairs) {\n const eq = kv.indexOf('=')\n if (eq === -1) record[kv] = ''\n else {\n const key = kv.substring(0, eq)\n const val = kv.substring(eq + 1)\n record[key] = val\n }\n }\n return record\n}\n\nconst isRecordFieldValue = (k: string, v: unknown): v is string[] =>\n Array.isArray(v) &&\n recordFields.includes(k as (typeof recordFields)[number])\n\nexport type PairsAsRecords = ConfigOptionsNoExtras & {\n command?: {\n [k in keyof Commands]?: ConfigOptionsNoExtras\n }\n}\n\nexport const pairsToRecords = (\n obj:\n | NonNullable<ConfigFileData>\n | OptionsResults<ConfigDefinitions>,\n): PairsAsRecords => {\n return Object.fromEntries(\n Object.entries(obj).map(([k, v]) => [\n k,\n k === 'command' && v && typeof v === 'object' ?\n Object.fromEntries(\n Object.entries(v).map(([k, v]) => [\n k,\n pairsToRecords(v as NonNullable<ConfigFileData>),\n ]),\n )\n : isRecordFieldValue(k, v) ? reducePairs(v)\n : v,\n ]),\n // hard cast because TS can't see through the entries/fromEntries\n ) as unknown as PairsAsRecords\n}\n\nexport const recordsToPairs = (obj: RecordPairs): RecordPairs => {\n return Object.fromEntries(\n Object.entries(obj)\n .filter(\n ([k]) =>\n !(\n k === 'scurry' ||\n k === 'packageJson' ||\n k === 'monorepo' ||\n k === 'projectRoot' ||\n k === 'packageInfo'\n ),\n )\n .map(([k, v]) => [\n k,\n k === 'command' && v && typeof v === 'object' ?\n recordsToPairs(v as RecordPairs)\n : (\n !v ||\n typeof v !== 'object' ||\n Array.isArray(v) ||\n !isRecordField(k)\n ) ?\n v\n : Object.entries(v).map(([k, v]) => `${k}=${v}`),\n ]),\n )\n}\n\nconst kRecord = Symbol('parsed key=value record')\n\nexport type ConfigDataNoCommand = {\n [k in keyof OptionsResults<ConfigDefinitions>]?: OptionsResults<ConfigDefinitions>[k]\n}\n\n/**\n * Config data can be any options, and also a 'command' field which\n * contains command names and override options for that command.\n */\nexport type ConfigData = ConfigDataNoCommand & {\n command?: {\n [k in keyof Commands]?: ConfigDataNoCommand\n }\n}\n\nexport type ConfigFileDataNoCommand = {\n [k in keyof ConfigDataNoCommand]: k extends (\n OptListKeys<ConfigDataNoCommand>\n ) ?\n RecordString | string[]\n : ConfigDataNoCommand[k]\n}\n\n/**\n * Config data as it appears in the config field of the vlt.json, with kv pair\n * lists stored as `Record<string, string>` and\n */\nexport type ConfigFileData = ConfigFileDataNoCommand & {\n command?: {\n [k in keyof Commands]?: ConfigFileDataNoCommand\n }\n}\n\nexport type ConfigOptionsNoExtras = {\n [k in keyof OptionsResults<ConfigDefinitions>]: k extends (\n RecordField\n ) ?\n RecordString\n : k extends 'command' ? never\n : OptionsResults<ConfigDefinitions>[k]\n}\n\nexport type ConfigOptions = ConfigOptionsNoExtras &\n Pick<SpecOptions, 'catalog' | 'catalogs'> & {\n packageJson: PackageJson\n scurry: PathScurry\n projectRoot: string\n monorepo?: Monorepo\n packageInfo: PackageInfoClient\n }\n\n/**\n * The base config definition set as a type\n */\nexport type ConfigDefinitions = Unwrap<typeof definition>\n\nexport type StringListKeys<O> = {\n [k in keyof O]: O[k] extends string[] | undefined ? k : never\n}\nexport type OptListKeys<O> = Exclude<\n StringListKeys<O>[keyof StringListKeys<O>],\n undefined\n>\n\n/**\n * Class that handles configuration for vlt.\n *\n * Call {@link Config.load} to get one of these.\n */\nexport class Config {\n /**\n * The {@link https://npmjs.com/jackspeak | JackSpeak} object\n * representing vlt's configuration\n */\n jack: Jack<ConfigDefinitions>\n\n /**\n * Parsed values in effect\n */\n values?: OptionsResults<ConfigDefinitions>\n\n /**\n * Command-specific config values\n */\n commandValues: {\n [cmd in Commands[keyof Commands]]?: ConfigData\n } = {}\n\n /**\n * A flattened object of the parsed configuration\n */\n get options(): ConfigOptions {\n if (this.#options) return this.#options\n const scurry = new PathScurry(this.projectRoot)\n const packageJson = new PackageJson()\n const asRecords = pairsToRecords(this.parse().values)\n const extras = {\n projectRoot: this.projectRoot,\n scurry,\n packageJson,\n monorepo: Monorepo.maybeLoad(this.projectRoot, {\n scurry,\n packageJson,\n load: {\n paths: asRecords.workspace,\n groups: asRecords['workspace-group'],\n },\n }),\n catalog: load<Record<string, string>>(\n 'catalog',\n assertRecordStringString,\n ),\n catalogs: load<Record<string, Record<string, string>>>(\n 'catalogs',\n o =>\n assertRecordStringT(\n o,\n isRecordStringString,\n 'Record<string, Record<string, string>>',\n ),\n ),\n }\n\n const options: Omit<ConfigOptions, 'packageInfo'> = Object.assign(\n asRecords,\n extras,\n )\n\n this.#options = Object.assign(options, {\n packageInfo: new PackageInfoClient(options),\n [kCustomInspect]() {\n return Object.fromEntries(\n Object.entries(options).filter(\n ([k]) =>\n k !== 'monorepo' &&\n k !== 'scurry' &&\n k !== 'packageJson' &&\n k !== 'packageInfo',\n ),\n )\n },\n })\n return this.#options\n }\n\n /**\n * Reset the options value, optionally setting a new project root\n * to recalculate the options.\n */\n resetOptions(projectRoot: string = process.cwd()) {\n this.projectRoot = projectRoot\n this.#options = undefined\n resetCaches()\n }\n\n // memoized options() getter value\n #options?: ConfigOptions\n\n /**\n * positional arguments to the vlt process\n */\n positionals?: string[]\n\n /**\n * Original arguments used for parsing (stored for reload purposes)\n * @internal\n */\n #originalArgs?: string[]\n\n /**\n * The root of the project where a vlt.json, vlt.json,\n * package.json, or .git was found. Not necessarily the `process.cwd()`,\n * though that is the default location.\n *\n * Never walks up as far as `$HOME`. So for example, if a project is in\n * `~/projects/xyz`, then the highest dir it will check is `~/projects`\n */\n projectRoot: string\n\n /**\n * `Record<alias, canonical name>` to dereference command aliases.\n */\n commands: Commands\n\n /**\n * Which command name to use for overriding with command-specific values,\n * determined from the argv when parse() is called.\n */\n command?: Commands[keyof Commands]\n\n constructor(\n jack: Jack<ConfigDefinitions> = definition,\n projectRoot = process.cwd(),\n ) {\n this.projectRoot = projectRoot\n this.commands = commands\n this.jack = jack\n }\n\n /**\n * Parse the arguments and set configuration and positionals accordingly.\n */\n parse(args: string[] = process.argv): this & ParsedConfig {\n if (isParsed(this)) return this\n\n // Store the original args for potential reload\n this.#originalArgs = [...args]\n\n this.jack.loadEnvDefaults()\n const p = this.jack.parseRaw(args)\n\n const fallback = getCommand(p.values['fallback-command'])\n this.command = getCommand(p.positionals[0])\n\n const cmdOrFallback = this.command ?? fallback\n const cmdSpecific =\n cmdOrFallback && this.commandValues[cmdOrFallback]\n if (cmdSpecific) {\n this.jack.setConfigValues(recordsToPairs(cmdSpecific))\n }\n\n // ok, applied cmd-specific defaults, do rest of the parse\n this.jack.applyDefaults(p)\n this.jack.writeEnv(p)\n\n if (this.command) p.positionals.shift()\n else this.command = getCommand(p.values['fallback-command'])\n\n Object.assign(this, p)\n\n /* c8 ignore start - unpossible */\n if (!isParsed(this)) throw error('failed to parse config')\n /* c8 ignore stop */\n\n return this\n }\n\n /**\n * Get a `key=value` list option value as an object.\n *\n * For example, a list option with a vlaue of `['key=value', 'xyz=as=df' ]`\n * would be returned as `{key: 'value', xyz: 'as=df'}`\n *\n * Results are memoized, so subsequent calls for the same key will return the\n * same object. If new strings are added to the list, then the memoized value\n * is *not* updated, so only use once configurations have been fully loaded.\n *\n * If the config value is not set at all, an empty object is returned.\n */\n getRecord(k: OptListKeys<ConfigData>): RecordString {\n const pairs = this.get(k) as\n | (string[] & { [kRecord]?: RecordString })\n | undefined\n if (!pairs) return {}\n if (pairs[kRecord]) return pairs[kRecord]\n const kv = pairs.reduce((kv: RecordString, pair) => {\n const eq = pair.indexOf('=')\n if (eq === -1) return kv\n const key = pair.substring(0, eq)\n const val = pair.substring(eq + 1)\n kv[key] = val\n return kv\n }, {})\n Object.assign(pairs, { [kRecord]: kv })\n return kv\n }\n\n /**\n * Get a configuration value.\n *\n * Note: `key=value` pair configs are returned as a string array. To get them\n * as an object, use {@link Config#getRecord}.\n */\n get<K extends keyof OptionsResults<ConfigDefinitions>>(\n k: K,\n ): OptionsResults<ConfigDefinitions>[K] {\n /* c8 ignore next -- impossible but TS doesn't know that */\n return (this.values ?? this.parse().values)[k]\n }\n\n /**\n * Write the config values to the user or project config file.\n */\n async writeConfigFile(\n this: LoadedConfig,\n which: WhichConfig,\n values: NonNullable<ConfigFileData>,\n ) {\n save('config', pairsToRecords(values), which)\n await this.#reloadConfig()\n }\n\n /**\n * Fold in the provided fields with the existing properties\n * in the config file.\n */\n async addConfigToFile(\n this: LoadedConfig,\n which: WhichConfig,\n values: NonNullable<ConfigFileData>,\n ) {\n return this.writeConfigFile(\n which,\n merge((await this.#maybeLoadConfigFile(which)) ?? {}, values),\n )\n }\n\n // called in this weird bound way so that it can be used by the\n // vlt-json config loading module.\n #validator: Validator<ConfigFileData> = function (\n this: Config,\n c: unknown,\n file: string,\n ) {\n this.#validateConfig(c, file)\n }.bind(this)\n\n #validateConfig(\n c: unknown,\n file: string,\n ): asserts c is ConfigFileData {\n if (!c || typeof c !== 'object' || Array.isArray(c)) {\n throw error('invalid config, expected object', {\n path: file,\n found: c,\n wanted: 'ConfigFileData',\n })\n }\n\n const { command, ...values } = recordsToPairs(c as RecordPairs)\n if (command) {\n for (const [c, opts] of Object.entries(command)) {\n const cmd = getCommand(c)\n if (cmd) {\n this.commandValues[cmd] = merge<ConfigData>(\n this.commandValues[cmd] ?? ({} as ConfigData),\n opts as ConfigData,\n )\n }\n }\n }\n this.jack.setConfigValues(values, file)\n }\n\n /**\n * if the file exists, parse and load it. returns object if data was\n * loaded, or undefined if not.\n */\n async #maybeLoadConfigFile(whichConfig: WhichConfig) {\n return load('config', this.#validator, whichConfig)\n }\n\n /**\n * Deletes the specified config fields from the named file\n * Returns `true` if anything was changed.\n */\n async deleteConfigKeys(\n this: LoadedConfig,\n which: WhichConfig,\n fields: string[],\n ) {\n const data = await this.#maybeLoadConfigFile(which)\n if (!data) return false\n let didSomething = false\n for (const f of fields) {\n const [key, ...sk] = f.split('.') as [\n h: string,\n ...rest: string[],\n ]\n const subs = sk.join('.')\n const k = key as keyof ConfigDefinitions\n const v = data[k]\n if (v === undefined) continue\n if (subs && v && typeof v === 'object') {\n if (Array.isArray(v)) {\n const i = v.findIndex(subvalue =>\n subvalue.startsWith(`${subs}=`),\n )\n if (i !== -1) {\n if (v.length === 1) delete data[k]\n else v.splice(i, 1)\n didSomething = true\n }\n } else {\n if (v[subs] !== undefined) {\n delete v[subs]\n if (Object.keys(v).length === 0) delete data[k]\n didSomething = true\n }\n }\n } else {\n didSomething = true\n delete data[k]\n }\n }\n if (didSomething) await this.writeConfigFile(which, data)\n return didSomething\n }\n\n /**\n * Edit the user or project configuration file.\n *\n * If the file isn't present, then it starts with `{}` so the user has\n * something to work with.\n *\n * If the result is not valid, or no config settings are contained in the\n * file after editing, then it's restored to what it was before, which might\n * mean deleting the file.\n */\n async editConfigFile(\n this: LoadedConfig,\n which: WhichConfig,\n edit: (file: string) => Promise<void> | void,\n ) {\n // load the file as a backup\n // call the edit function\n // reload it\n const file = find(which)\n const backup = await readFile(file, 'utf8').catch(() => undefined)\n if (!backup) {\n await writeFile(\n file,\n JSON.stringify({ config: {} }, null, 2) + '\\n',\n )\n }\n let valid = false\n try {\n await edit(file)\n // force it to reload the file and validate it again\n // if this fails, we roll back.\n const result = reload('config', which)\n save('config', result ?? {}, which)\n valid = true\n } finally {\n if (!valid) {\n // TODO: maybe write the file to a re-edit backup location?\n // then you could do `vlt config edit retry` or something.\n if (backup) {\n await writeFile(file, backup)\n reload(which)\n } else {\n await rm(file, { force: true })\n }\n }\n }\n }\n\n /**\n * Find the local config file and load both it and the user-level config in\n * the XDG config home.\n */\n async loadConfigFile(): Promise<this> {\n await this.#maybeLoadConfigFile('user')\n this.projectRoot = dirname(find('project', this.projectRoot))\n await this.#maybeLoadConfigFile('project')\n return this\n }\n\n /**\n * Clear cached config values to force re-reading from updated files.\n * @internal\n */\n async #reloadConfig() {\n // Clear the memoized options to force recalculation\n this.#options = undefined\n }\n\n /**\n * Force a complete reload of config files from disk.\n * This clears all caches and re-reads config files.\n * Useful for long-running processes that need to pick up config changes.\n */\n async reloadFromDisk(): Promise<void> {\n // Clear the memoized options to force recalculation\n this.#options = undefined\n\n // Clear the parsed state to force re-parsing\n // This is crucial because parse() returns early if already parsed\n this.values = undefined\n this.positionals = undefined\n this.command = undefined\n\n // Clear vlt-json caches for both user and project configs\n // This ensures that the next time config files are read, they'll be re-read from disk\n const { unload } = await import('@vltpkg/vlt-json')\n unload('user')\n unload('project')\n\n // Force reload of config files by calling the load methods again\n // This will re-read the files and re-apply them to the jack parser\n await this.#maybeLoadConfigFile('user')\n await this.#maybeLoadConfigFile('project')\n\n // Re-parse to pick up the updated config values using the original arguments\n this.parse(this.#originalArgs)\n }\n\n /**\n * cache of the loaded config\n */\n static #loaded: LoadedConfig | undefined\n\n /**\n * Load the configuration and return a Promise to a\n * {@link Config} object\n */\n static async load(\n projectRoot = process.cwd(),\n argv = process.argv,\n /**\n * only used in tests, resets the memoization\n * @internal\n */\n reload = false,\n ): Promise<LoadedConfig> {\n if (this.#loaded && !reload) return this.#loaded\n const a = new Config(definition, projectRoot)\n const b = await a.loadConfigFile()\n this.#loaded = b.parse(argv) as LoadedConfig\n return this.#loaded\n }\n}\n\nconst isParsed = (c: Config): c is ParsedConfig =>\n !!(c.values && c.positionals && c.command)\n\nexport type ParsedConfig = Config & {\n command: NonNullable<Config['command']>\n values: OptionsResults<ConfigDefinitions>\n positionals: string[]\n}\n\n/**\n * A fully loaded {@link Config} object\n */\nexport type LoadedConfig = ParsedConfig\n"]}
@@ -1,2 +1,4 @@
1
- export declare const merge: <T extends Record<string, any>>(base: T, add: T) => T;
1
+ type MergeableObject = Record<string, unknown>;
2
+ export declare const merge: <T extends MergeableObject>(base: T, add: T) => T;
3
+ export {};
2
4
  //# sourceMappingURL=merge.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../../src/config/merge.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAC3C,CAAC,OACF,CAAC,KACL,CAsBK,CAAA"}
1
+ {"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../../src/config/merge.ts"],"names":[],"mappings":"AAAA,KAAK,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAQ9C,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,eAAe,QACvC,CAAC,OACF,CAAC,KACL,CA2BK,CAAA"}
@@ -1,14 +1,19 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
- /* eslint-disable @typescript-eslint/no-unsafe-return */
3
- // deep merge 2 objects
4
- // scalars are overwritten, objects are folded in together
5
- // if nothing to be added, then return the base object.
1
+ /*
2
+ * deep merge 2 objects
3
+ * scalars are overwritten, objects are folded in together
4
+ * if nothing to be added, then return the base object.
5
+ */
6
6
  export const merge = (base, add) => Object.fromEntries(Object.entries(base)
7
7
  .map(([k, v]) => [
8
8
  k,
9
9
  add[k] === undefined ? v
10
10
  : Array.isArray(v) && Array.isArray(add[k]) ?
11
- [...new Set([...v, ...add[k]])]
11
+ [
12
+ ...new Set([
13
+ ...v,
14
+ ...add[k],
15
+ ]),
16
+ ]
12
17
  : Array.isArray(v) || Array.isArray(add[k]) ? add[k]
13
18
  : (!!v &&
14
19
  typeof v === 'object' &&