@oclif/plugin-commands 3.3.4 → 4.0.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/README.md CHANGED
@@ -21,7 +21,7 @@ $ npm install -g @oclif/plugin-commands
21
21
  $ oclif-example COMMAND
22
22
  running command...
23
23
  $ oclif-example (--version)
24
- @oclif/plugin-commands/3.3.4 linux-x64 node-v20.12.2
24
+ @oclif/plugin-commands/4.0.0 linux-x64 node-v20.13.1
25
25
  $ oclif-example --help [COMMAND]
26
26
  USAGE
27
27
  $ oclif-example COMMAND
@@ -36,36 +36,32 @@ USAGE
36
36
 
37
37
  ## `oclif-example commands`
38
38
 
39
- list all the commands
39
+ List all oclif-example commands.
40
40
 
41
41
  ```
42
42
  USAGE
43
- $ oclif-example commands [--json] [--deprecated] [-h] [--hidden] [--tree] [--columns <value> | -x] [--filter
44
- <value>] [--no-header | [--csv | --no-truncate]] [--output csv|json|yaml | | ] [--sort <value>]
43
+ $ oclif-example commands [--json] [-c id|plugin|summary|type | --tree] [--deprecated] [-x | ] [--hidden]
44
+ [--no-truncate | ] [--sort id|plugin|summary|type | ]
45
45
 
46
46
  FLAGS
47
- -h, --help Show CLI help.
48
- -x, --extended show extra columns
49
- --columns=<value> only show provided columns (comma-separated)
50
- --csv output is csv format [alias: --output=csv]
51
- --deprecated show deprecated commands
52
- --filter=<value> filter property by partial string matching, ex: name=foo
53
- --hidden show hidden commands
54
- --no-header hide table header from output
55
- --no-truncate do not truncate output to fit screen
56
- --output=<option> output in a more machine friendly format
57
- <options: csv|json|yaml>
58
- --sort=<value> property to sort by (prepend '-' for descending)
59
- --tree show tree of commands
47
+ -c, --columns=<option>... Only show provided columns (comma-separated).
48
+ <options: id|plugin|summary|type>
49
+ -x, --extended Show extra columns.
50
+ --deprecated Show deprecated commands.
51
+ --hidden Show hidden commands.
52
+ --no-truncate Do not truncate output.
53
+ --sort=<option> [default: id] Property to sort by.
54
+ <options: id|plugin|summary|type>
55
+ --tree Show tree of commands.
60
56
 
61
57
  GLOBAL FLAGS
62
58
  --json Format output as json.
63
59
 
64
60
  DESCRIPTION
65
- list all the commands
61
+ List all oclif-example commands.
66
62
  ```
67
63
 
68
- _See code: [src/commands/commands.ts](https://github.com/oclif/plugin-commands/blob/v3.3.4/src/commands/commands.ts)_
64
+ _See code: [src/commands/commands.ts](https://github.com/oclif/plugin-commands/blob/v4.0.0/src/commands/commands.ts)_
69
65
  <!-- commandsstop -->
70
66
 
71
67
  # Contributing
@@ -3,20 +3,15 @@ export default class Commands extends Command {
3
3
  static description: string;
4
4
  static enableJsonFlag: boolean;
5
5
  static flags: {
6
- columns: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined>;
7
- csv: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
8
- extended: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
9
- filter: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined>;
10
- 'no-header': import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
11
- 'no-truncate': import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
12
- output: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined>;
13
- sort: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string | undefined>;
14
- deprecated: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
15
- help: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<void>;
16
- hidden: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
17
- tree: import("@oclif/core/lib/interfaces/parser.js").BooleanFlag<boolean>;
6
+ columns: import("@oclif/core/interfaces").OptionFlag<("type" | "id" | "summary" | "plugin")[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
7
+ deprecated: import("@oclif/core/interfaces").BooleanFlag<boolean>;
8
+ extended: import("@oclif/core/interfaces").BooleanFlag<boolean>;
9
+ hidden: import("@oclif/core/interfaces").BooleanFlag<boolean>;
10
+ 'no-truncate': import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
+ sort: import("@oclif/core/interfaces").OptionFlag<"type" | "id" | "summary" | "plugin", import("@oclif/core/interfaces").CustomOptions>;
12
+ tree: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
13
  };
19
- run(): Promise<unknown[] | import("@oclif/core/lib/cli-ux/styled/tree.js").Tree | undefined>;
14
+ run(): Promise<unknown[]>;
20
15
  private getCommands;
21
16
  private removeCycles;
22
17
  }
@@ -1,16 +1,59 @@
1
- import { Command, Flags, toConfiguredId, ux } from '@oclif/core';
1
+ import { Command, Flags, toConfiguredId } from '@oclif/core';
2
2
  import _ from 'lodash';
3
- import { EOL } from 'node:os';
4
- import createCommandTree from '../utils/tree.js';
3
+ // @ts-expect-error because object-treeify does not have types: https://github.com/blackflux/object-treeify/issues/1077
4
+ import treeify from 'object-treeify';
5
+ import TtyTable from 'tty-table';
6
+ const COLUMNS = ['id', 'plugin', 'summary', 'type'];
7
+ function createTree(commands) {
8
+ const tree = {};
9
+ for (const command of commands) {
10
+ const parts = command.id.split(':');
11
+ let current = tree;
12
+ for (const part of parts) {
13
+ current[part] = current[part] || {};
14
+ current = current[part];
15
+ }
16
+ }
17
+ return tree;
18
+ }
19
+ function determineHeaders(columns, extended) {
20
+ const columnConfigs = {
21
+ id: { align: 'left', value: 'ID', width: '25%' },
22
+ plugin: { align: 'left', value: 'Plugin' },
23
+ summary: { align: 'left', value: 'Summary', width: '75%' },
24
+ type: { align: 'left', value: 'Type' },
25
+ };
26
+ if (columns) {
27
+ return columns.map((column) => columnConfigs[column]);
28
+ }
29
+ if (extended) {
30
+ return [columnConfigs.id, columnConfigs.summary, columnConfigs.plugin, columnConfigs.type];
31
+ }
32
+ return [columnConfigs.id, columnConfigs.summary];
33
+ }
5
34
  export default class Commands extends Command {
6
- static description = 'list all the commands';
35
+ static description = 'List all <%= config.bin %> commands.';
7
36
  static enableJsonFlag = true;
8
37
  static flags = {
9
- deprecated: Flags.boolean({ description: 'show deprecated commands' }),
10
- help: Flags.help({ char: 'h' }),
11
- hidden: Flags.boolean({ description: 'show hidden commands' }),
12
- tree: Flags.boolean({ description: 'show tree of commands' }),
13
- ...ux.table.flags(),
38
+ columns: Flags.custom({
39
+ char: 'c',
40
+ delimiter: ',',
41
+ description: 'Only show provided columns (comma-separated).',
42
+ exclusive: ['tree'],
43
+ multiple: true,
44
+ options: COLUMNS,
45
+ })(),
46
+ deprecated: Flags.boolean({ description: 'Show deprecated commands.' }),
47
+ extended: Flags.boolean({ char: 'x', description: 'Show extra columns.', exclusive: ['tree'] }),
48
+ hidden: Flags.boolean({ description: 'Show hidden commands.' }),
49
+ 'no-truncate': Flags.boolean({ description: 'Do not truncate output.', exclusive: ['tree'] }),
50
+ sort: Flags.option({
51
+ default: 'id',
52
+ description: 'Property to sort by.',
53
+ exclusive: ['tree'],
54
+ options: COLUMNS,
55
+ })(),
56
+ tree: Flags.boolean({ description: 'Show tree of commands.' }),
14
57
  };
15
58
  async run() {
16
59
  const { flags } = await this.parse(Commands);
@@ -23,82 +66,70 @@ export default class Commands extends Command {
23
66
  commands = commands.filter((c) => c.state !== 'deprecated' && !deprecatedAliases.has(c.id));
24
67
  }
25
68
  const { config } = this;
26
- commands = _.sortBy(commands, 'id').map((command) => {
27
- // Template supported fields.
28
- command.description =
29
- (typeof command.description === 'string' && _.template(command.description)({ command, config })) || undefined;
30
- command.summary =
31
- (typeof command.summary === 'string' && _.template(command.summary)({ command, config })) || undefined;
32
- command.usage = (typeof command.usage === 'string' && _.template(command.usage)({ command, config })) || undefined;
33
- command.id = toConfiguredId(command.id, config);
34
- return command;
35
- });
36
- if (this.jsonEnabled() && !flags.tree) {
37
- const formatted = await Promise.all(commands.map(async (cmd) => {
38
- let commandClass;
39
- try {
40
- commandClass = await cmd.load();
41
- }
42
- catch (error) {
43
- this.debug(error);
44
- }
45
- const obj = { ...cmd, ...commandClass };
46
- // Load all properties on all extending classes.
47
- while (commandClass !== undefined) {
48
- commandClass = Object.getPrototypeOf(commandClass) ?? undefined;
49
- // ES2022 will return all unset static properties on the prototype as undefined. This is different from ES2021
50
- // which only returns the static properties that are set by defaults. In order to prevent
51
- // Object.assign from overwriting the properties on the object, we need to filter out the undefined values.
52
- Object.assign(obj, _.pickBy(commandClass, (v) => v !== undefined));
69
+ commands = _.sortBy(commands, flags.sort).map((command) =>
70
+ // Template supported fields.
71
+ ({
72
+ ...command,
73
+ description: (typeof command.description === 'string' && _.template(command.description)({ command, config })) || undefined,
74
+ summary: (typeof command.summary === 'string' && _.template(command.summary)({ command, config })) || undefined,
75
+ usage: (typeof command.usage === 'string' && _.template(command.usage)({ command, config })) || undefined,
76
+ }));
77
+ if (flags.tree) {
78
+ const tree = createTree(commands);
79
+ this.log(treeify(tree));
80
+ }
81
+ else {
82
+ const headers = determineHeaders(flags.columns, flags.extended);
83
+ const extractData = (command) => headers.map((header) => {
84
+ switch (header.value) {
85
+ case 'ID': {
86
+ return toConfiguredId(command.id, config);
87
+ }
88
+ case 'Plugin': {
89
+ return command.pluginName;
90
+ }
91
+ case 'Type': {
92
+ return command.pluginType;
93
+ }
94
+ case 'Summary': {
95
+ return command.summary ?? command.description;
96
+ }
97
+ default: {
98
+ throw new Error('Unknown column');
99
+ }
53
100
  }
54
- // The plugin property on the loaded class contains a LOT of information including all the commands again. Remove it.
55
- delete obj.plugin;
56
- // If Command classes have circular references, don't break the commands command.
57
- return this.removeCycles(obj);
58
- }));
59
- return _.uniqBy(formatted, 'id');
101
+ });
102
+ // eslint-disable-next-line new-cap
103
+ const table = TtyTable(headers, commands.map((c) => extractData(c)), {
104
+ compact: true,
105
+ defaultValue: '',
106
+ truncate: flags['no-truncate'] ? undefined : '...',
107
+ });
108
+ this.log(table.render());
60
109
  }
61
- if (flags.tree) {
62
- const tree = createCommandTree(commands, config.topicSeparator);
63
- if (!this.jsonEnabled()) {
64
- tree.display();
110
+ const json = _.uniqBy(await Promise.all(commands.map(async (cmd) => {
111
+ let commandClass;
112
+ try {
113
+ commandClass = await cmd.load();
65
114
  }
66
- return tree;
67
- }
68
- ux.table(commands.map((command) => {
69
- // Massage some fields so it looks good in the table
70
- command.description = (command.description ?? '').split(EOL)[0];
71
- command.summary = command.summary ?? (command.description ?? '').split(EOL)[0];
72
- command.hidden = Boolean(command.hidden);
73
- command.usage ??= '';
74
- return command;
75
- }), {
76
- description: {
77
- extended: true,
78
- },
79
- hidden: {
80
- extended: true,
81
- },
82
- id: {
83
- header: 'Command',
84
- },
85
- pluginName: {
86
- extended: true,
87
- header: 'Plugin',
88
- },
89
- pluginType: {
90
- extended: true,
91
- header: 'Type',
92
- },
93
- summary: {},
94
- usage: {
95
- extended: true,
96
- },
97
- }, {
98
- // to-do: investigate this oclif/core error when printLine is enabled
99
- // printLine: this.log,
100
- ...flags, // parsed flags
101
- });
115
+ catch (error) {
116
+ this.debug(error);
117
+ }
118
+ const obj = { ...cmd, ...commandClass };
119
+ // Load all properties on all extending classes.
120
+ while (commandClass !== undefined) {
121
+ commandClass = Object.getPrototypeOf(commandClass) ?? undefined;
122
+ // ES2022 will return all unset static properties on the prototype as undefined. This is different from ES2021
123
+ // which only returns the static properties that are set by defaults. In order to prevent
124
+ // Object.assign from overwriting the properties on the object, we need to filter out the undefined values.
125
+ Object.assign(obj, _.pickBy(commandClass, (v) => v !== undefined));
126
+ }
127
+ // The plugin property on the loaded class contains a LOT of information including all the commands again. Remove it.
128
+ delete obj.plugin;
129
+ // If Command classes have circular references, don't break the commands command.
130
+ return this.removeCycles(obj);
131
+ })), 'id');
132
+ return json;
102
133
  }
103
134
  getCommands() {
104
135
  return this.config.commands;
@@ -3,7 +3,7 @@
3
3
  "commands": {
4
4
  "aliases": [],
5
5
  "args": {},
6
- "description": "list all the commands",
6
+ "description": "List all <%= config.bin %> commands.",
7
7
  "flags": {
8
8
  "json": {
9
9
  "description": "Format output as json.",
@@ -12,107 +12,77 @@
12
12
  "allowNo": false,
13
13
  "type": "boolean"
14
14
  },
15
- "deprecated": {
16
- "description": "show deprecated commands",
17
- "name": "deprecated",
18
- "allowNo": false,
19
- "type": "boolean"
20
- },
21
- "help": {
22
- "char": "h",
23
- "description": "Show CLI help.",
24
- "name": "help",
25
- "allowNo": false,
26
- "type": "boolean"
27
- },
28
- "hidden": {
29
- "description": "show hidden commands",
30
- "name": "hidden",
31
- "allowNo": false,
32
- "type": "boolean"
33
- },
34
- "tree": {
35
- "description": "show tree of commands",
36
- "name": "tree",
37
- "allowNo": false,
38
- "type": "boolean"
39
- },
40
15
  "columns": {
41
- "description": "only show provided columns (comma-separated)",
16
+ "char": "c",
17
+ "description": "Only show provided columns (comma-separated).",
42
18
  "exclusive": [
43
- "extended"
19
+ "tree"
44
20
  ],
45
21
  "name": "columns",
22
+ "delimiter": ",",
46
23
  "hasDynamicHelp": false,
47
- "multiple": false,
24
+ "multiple": true,
25
+ "options": [
26
+ "id",
27
+ "plugin",
28
+ "summary",
29
+ "type"
30
+ ],
48
31
  "type": "option"
49
32
  },
50
- "csv": {
51
- "description": "output is csv format [alias: --output=csv]",
52
- "exclusive": [
53
- "no-truncate"
54
- ],
55
- "name": "csv",
33
+ "deprecated": {
34
+ "description": "Show deprecated commands.",
35
+ "name": "deprecated",
56
36
  "allowNo": false,
57
37
  "type": "boolean"
58
38
  },
59
39
  "extended": {
60
40
  "char": "x",
61
- "description": "show extra columns",
41
+ "description": "Show extra columns.",
62
42
  "exclusive": [
63
- "columns"
43
+ "tree"
64
44
  ],
65
45
  "name": "extended",
66
46
  "allowNo": false,
67
47
  "type": "boolean"
68
48
  },
69
- "filter": {
70
- "description": "filter property by partial string matching, ex: name=foo",
71
- "name": "filter",
72
- "hasDynamicHelp": false,
73
- "multiple": false,
74
- "type": "option"
75
- },
76
- "no-header": {
77
- "description": "hide table header from output",
78
- "exclusive": [
79
- "csv"
80
- ],
81
- "name": "no-header",
49
+ "hidden": {
50
+ "description": "Show hidden commands.",
51
+ "name": "hidden",
82
52
  "allowNo": false,
83
53
  "type": "boolean"
84
54
  },
85
55
  "no-truncate": {
86
- "description": "do not truncate output to fit screen",
56
+ "description": "Do not truncate output.",
87
57
  "exclusive": [
88
- "csv"
58
+ "tree"
89
59
  ],
90
60
  "name": "no-truncate",
91
61
  "allowNo": false,
92
62
  "type": "boolean"
93
63
  },
94
- "output": {
95
- "description": "output in a more machine friendly format",
64
+ "sort": {
65
+ "description": "Property to sort by.",
96
66
  "exclusive": [
97
- "no-truncate",
98
- "csv"
67
+ "tree"
99
68
  ],
100
- "name": "output",
69
+ "name": "sort",
70
+ "default": "id",
101
71
  "hasDynamicHelp": false,
102
72
  "multiple": false,
103
73
  "options": [
104
- "csv",
105
- "json",
106
- "yaml"
74
+ "id",
75
+ "plugin",
76
+ "summary",
77
+ "type"
107
78
  ],
108
79
  "type": "option"
109
80
  },
110
- "sort": {
111
- "description": "property to sort by (prepend '-' for descending)",
112
- "name": "sort",
113
- "hasDynamicHelp": false,
114
- "multiple": false,
115
- "type": "option"
81
+ "tree": {
82
+ "description": "Show tree of commands.",
83
+ "name": "tree",
84
+ "allowNo": false,
85
+ "type": "boolean"
116
86
  }
117
87
  },
118
88
  "hasDynamicHelp": false,
@@ -135,5 +105,5 @@
135
105
  ]
136
106
  }
137
107
  },
138
- "version": "3.3.4"
108
+ "version": "4.0.0"
139
109
  }
package/package.json CHANGED
@@ -1,23 +1,27 @@
1
1
  {
2
2
  "name": "@oclif/plugin-commands",
3
3
  "description": "plugin to show the list of all the commands",
4
- "version": "3.3.4",
4
+ "version": "4.0.0",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/plugin-commands/issues",
7
7
  "dependencies": {
8
- "@oclif/core": "^3.26.6",
9
- "lodash": "^4.17.21"
8
+ "@oclif/core": "^4.0.0-beta.13",
9
+ "lodash": "^4.17.21",
10
+ "object-treeify": "^4.0.1",
11
+ "tty-table": "^4.2.3"
10
12
  },
11
13
  "devDependencies": {
12
- "@commitlint/config-conventional": "^18",
14
+ "@commitlint/config-conventional": "^19",
13
15
  "@oclif/plugin-help": "^6",
14
16
  "@oclif/prettier-config": "^0.2.1",
15
- "@oclif/test": "^3.2.12",
17
+ "@oclif/test": "^4.0.2",
16
18
  "@types/chai": "^4.3.11",
19
+ "@types/lodash": "^4.17.4",
17
20
  "@types/mocha": "^10.0.6",
18
21
  "@types/node": "^18",
22
+ "@types/sinon": "^17.0.3",
19
23
  "chai": "^4.4.1",
20
- "commitlint": "^18",
24
+ "commitlint": "^19",
21
25
  "eslint": "^8.57.0",
22
26
  "eslint-config-oclif": "^5.2.0",
23
27
  "eslint-config-oclif-typescript": "^3.1.7",
@@ -26,9 +30,10 @@
26
30
  "lint-staged": "^15",
27
31
  "mocha": "^10.4.0",
28
32
  "nyc": "^15.1.0",
29
- "oclif": "^4.10.7",
33
+ "oclif": "^4.11.0",
30
34
  "prettier": "^3.2.5",
31
35
  "shx": "^0.3.3",
36
+ "sinon": "^18.0.0",
32
37
  "ts-node": "^10.9.2",
33
38
  "typescript": "^5.4.5"
34
39
  },
@@ -1,3 +0,0 @@
1
- import { Command } from '@oclif/core';
2
- declare const createCommandTree: (commands: Command.Loadable[], topicSeparator?: string) => import("@oclif/core/lib/cli-ux/styled/tree.js").Tree;
3
- export default createCommandTree;
package/lib/utils/tree.js DELETED
@@ -1,25 +0,0 @@
1
- import { ux } from '@oclif/core';
2
- const addNodes = (tree, commandParts) => {
3
- const existingNode = tree.search(commandParts[0]);
4
- // If the node exists and there's another part, add it to the node
5
- if (existingNode && commandParts[1]) {
6
- addNodes(existingNode, commandParts.slice(1));
7
- }
8
- else {
9
- // The node doesn't exist, create it
10
- tree.insert(commandParts[0]);
11
- // If there are more parts, add them to the node
12
- if (commandParts.length > 1) {
13
- addNodes(tree.search(commandParts[0]), commandParts.slice(1));
14
- }
15
- }
16
- };
17
- const createCommandTree = (commands, topicSeparator = ':') => {
18
- const tree = ux.tree();
19
- for (const command of commands) {
20
- const commandParts = command.id.split(topicSeparator);
21
- addNodes(tree, commandParts);
22
- }
23
- return tree;
24
- };
25
- export default createCommandTree;