@oclif/core 1.5.0 → 1.5.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.5.1](https://github.com/oclif/core/compare/v1.5.0...v1.5.1) (2022-03-03)
6
+
5
7
  ## [1.5.0](https://github.com/oclif/core/compare/v1.4.0...v1.5.0) (2022-03-02)
6
8
 
7
9
 
@@ -473,6 +473,28 @@ class Config {
473
473
  }
474
474
  }
475
475
  exports.Config = Config;
476
+ // when no manifest exists, the default is calculated. This may throw, so we need to catch it
477
+ const defaultToCached = async (flag) => {
478
+ // Prefer the helpDefaultValue function (returns a friendly string for complex types)
479
+ if (typeof flag.defaultHelp === 'function') {
480
+ try {
481
+ return await flag.defaultHelp();
482
+ }
483
+ catch {
484
+ return;
485
+ }
486
+ }
487
+ // if not specified, try the default function
488
+ if (typeof flag.default === 'function') {
489
+ try {
490
+ return await flag.default({ options: {}, flags: {} });
491
+ }
492
+ catch { }
493
+ }
494
+ else {
495
+ return flag.default;
496
+ }
497
+ };
476
498
  async function toCached(c, plugin) {
477
499
  const flags = {};
478
500
  for (const [name, flag] of Object.entries(c.flags || {})) {
@@ -508,8 +530,12 @@ async function toCached(c, plugin) {
508
530
  options: flag.options,
509
531
  dependsOn: flag.dependsOn,
510
532
  exclusive: flag.exclusive,
511
- default: typeof flag.default === 'function' ? await flag.default({ options: {}, flags: {} }) : flag.default,
533
+ default: await defaultToCached(flag),
512
534
  };
535
+ // a command-level placeholder in the manifest so that oclif knows it should regenerate the command during help-time
536
+ if (typeof flag.defaultHelp === 'function') {
537
+ c.hasDynamicHelp = true;
538
+ }
513
539
  }
514
540
  }
515
541
  const argsPromise = (c.args || []).map(async (a) => ({
@@ -25,6 +25,7 @@ export declare class Help extends HelpBase {
25
25
  protected get sortedTopics(): Interfaces.Topic[];
26
26
  constructor(config: Interfaces.Config, opts?: Partial<Interfaces.HelpOptions>);
27
27
  showHelp(argv: string[]): Promise<void>;
28
+ private getDynamicCommand;
28
29
  showCommandHelp(command: Interfaces.Command): Promise<void>;
29
30
  protected showRootHelp(): Promise<void>;
30
31
  protected showTopicHelp(topic: Interfaces.Topic): Promise<void>;
package/lib/help/index.js CHANGED
@@ -8,6 +8,8 @@ const root_1 = require("./root");
8
8
  const util_1 = require("../util");
9
9
  const util_2 = require("./util");
10
10
  const formatter_1 = require("./formatter");
11
+ const plugin_1 = require("../config/plugin");
12
+ const config_1 = require("../config/config");
11
13
  var command_2 = require("./command");
12
14
  Object.defineProperty(exports, "CommandHelp", { enumerable: true, get: function () { return command_2.CommandHelp; } });
13
15
  var util_3 = require("./util");
@@ -89,7 +91,13 @@ class Help extends HelpBase {
89
91
  }
90
92
  const command = this.config.findCommand(subject);
91
93
  if (command) {
92
- await this.showCommandHelp(command);
94
+ if (command.hasDynamicHelp) {
95
+ const dynamicCommand = await (0, config_1.toCached)(await this.getDynamicCommand(command.id));
96
+ await this.showCommandHelp(dynamicCommand);
97
+ }
98
+ else {
99
+ await this.showCommandHelp(command);
100
+ }
93
101
  return;
94
102
  }
95
103
  const topic = this.config.findTopic(subject);
@@ -99,6 +107,15 @@ class Help extends HelpBase {
99
107
  }
100
108
  (0, errors_1.error)(`Command ${subject} not found.`);
101
109
  }
110
+ async getDynamicCommand(cmdName) {
111
+ const plugin = new plugin_1.Plugin({ ignoreManifest: true, root: this.config.root });
112
+ await plugin.load();
113
+ const cmd = await plugin.findCommand(cmdName);
114
+ if (!cmd) {
115
+ throw new Error(`Command ${cmdName} not found.`);
116
+ }
117
+ return cmd;
118
+ }
102
119
  async showCommandHelp(command) {
103
120
  var _a, _b, _c, _d;
104
121
  const name = command.id;
@@ -110,8 +127,9 @@ class Help extends HelpBase {
110
127
  if (state)
111
128
  console.log(`This command is in ${state}.\n`);
112
129
  const summary = this.summary(command);
113
- if (summary)
130
+ if (summary) {
114
131
  console.log(summary + '\n');
132
+ }
115
133
  console.log(this.formatCommand(command));
116
134
  console.log('');
117
135
  if (subTopics.length > 0) {
@@ -54,6 +54,7 @@ export interface Command extends CommandProps {
54
54
  };
55
55
  args: Command.Arg[];
56
56
  strict: boolean;
57
+ hasDynamicHelp?: boolean;
57
58
  }
58
59
  export declare namespace Command {
59
60
  interface Arg {
@@ -70,6 +71,7 @@ export declare namespace Command {
70
71
  }
71
72
  interface Option extends OptionFlagProps {
72
73
  default?: string;
74
+ defaultHelp?: () => Promise<string>;
73
75
  }
74
76
  }
75
77
  interface Base extends CommandProps {
@@ -80,6 +82,7 @@ export declare namespace Command {
80
82
  flags?: FlagInput<any>;
81
83
  args?: ArgInput;
82
84
  strict: boolean;
85
+ hasDynamicHelp?: boolean;
83
86
  new (argv: string[], config: Config): Instance;
84
87
  run(argv?: string[], config?: LoadOptions): PromiseLike<any>;
85
88
  }
@@ -85,6 +85,7 @@ export declare type DefaultContext<T> = {
85
85
  };
86
86
  };
87
87
  export declare type Default<T> = T | ((context: DefaultContext<T>) => Promise<T>);
88
+ export declare type DefaultHelp<T> = T | ((context: DefaultContext<T>) => Promise<string | undefined>);
88
89
  export declare type FlagProps = {
89
90
  name: string;
90
91
  char?: AlphabetLowercase | AlphabetUppercase;
@@ -139,6 +140,7 @@ export declare type BooleanFlag<T> = FlagBase<T, boolean> & BooleanFlagProps & {
139
140
  };
140
141
  export declare type OptionFlag<T> = FlagBase<T, string> & OptionFlagProps & {
141
142
  default?: Default<T | undefined>;
143
+ defaultHelp?: DefaultHelp<T>;
142
144
  input: string[];
143
145
  };
144
146
  export declare type Definition<T> = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/core",
3
3
  "description": "base library for oclif CLIs",
4
- "version": "1.5.0",
4
+ "version": "1.5.1",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/core/issues",
7
7
  "dependencies": {