@oclif/plugin-command-snapshot 3.3.14 → 4.0.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.
@@ -23,7 +23,7 @@ class SchemaCompare extends snapshot_command_1.SnapshotCommand {
23
23
  try {
24
24
  fs.accessSync(flags.filepath);
25
25
  }
26
- catch (_a) {
26
+ catch {
27
27
  this.log(`${flags.filepath} not found.`);
28
28
  return [];
29
29
  }
@@ -13,7 +13,7 @@ function getAllFiles(dirPath, ext, allFiles = []) {
13
13
  try {
14
14
  files = fs.readdirSync(dirPath);
15
15
  }
16
- catch (_a) { }
16
+ catch { }
17
17
  for (const file of files) {
18
18
  const fPath = path.join(dirPath, file);
19
19
  if (fs.statSync(fPath).isDirectory()) {
@@ -73,9 +73,8 @@ class SchemaGenerator {
73
73
  return getAllFiles(path.join(rootDir, 'commands'), '.ts');
74
74
  }
75
75
  getAllHookFiles() {
76
- var _a, _b;
77
76
  // eslint-disable-next-line unicorn/no-array-reduce
78
- const hookFiles = Object.values((_b = (_a = this.base.config.pjson.oclif) === null || _a === void 0 ? void 0 : _a.hooks) !== null && _b !== void 0 ? _b : {}).reduce((x, y) => {
77
+ const hookFiles = Object.values(this.base.config.pjson.oclif?.hooks ?? {}).reduce((x, y) => {
79
78
  return Array.isArray(y) ? [...x, ...y] : [...x, y];
80
79
  }, []);
81
80
  const { rootDir, outDir } = this.getDirs();
@@ -107,14 +106,13 @@ class SchemaGenerator {
107
106
  * @returns Returns the name of the return type and the hook id.
108
107
  */
109
108
  parseHookFile(file) {
110
- var _a, _b;
111
109
  const returnTypeRegex = /(?<=const\shook:\s(.*?)<)[^'](.*?)[^'](>*)(?=>)/g;
112
110
  const contents = fs.readFileSync(file, 'utf8');
113
111
  const [returnType] = returnTypeRegex.exec(contents) || [];
114
112
  if (!returnType || returnType === 'void') {
115
113
  return { returnType: null, hookId: null };
116
114
  }
117
- const hooks = (_b = (_a = this.base.config.pjson.oclif) === null || _a === void 0 ? void 0 : _a.hooks) !== null && _b !== void 0 ? _b : {};
115
+ const hooks = this.base.config.pjson.oclif?.hooks ?? {};
118
116
  const hookId = Object.keys(hooks).find(key => {
119
117
  const hookFiles = (Array.isArray(hooks[key]) ? hooks[key] : [hooks[key]]);
120
118
  const hookFileNames = hookFiles.map(f => path.basename(f).split('.')[0]);
@@ -160,7 +158,7 @@ class SchemaGenerator {
160
158
  }
161
159
  return dirs;
162
160
  }
163
- catch (_a) { }
161
+ catch { }
164
162
  return dirs;
165
163
  }
166
164
  }
@@ -6,6 +6,7 @@ interface Change {
6
6
  }
7
7
  type CommandChange = {
8
8
  plugin: string;
9
+ chars: Change[];
9
10
  flags: Change[];
10
11
  alias: Change[];
11
12
  } & Change;
@@ -26,19 +27,6 @@ export default class Compare extends SnapshotCommand {
26
27
  * @returns all the command differences
27
28
  */
28
29
  compareSnapshot(initialCommands: SnapshotEntry[], updatedCommands: CommandChange[]): Promise<CompareResponse>;
29
- /**
30
- * @deprecated in favor of diffCommandProperty
31
- * Compares a flag snapshot with the current command's flags
32
- * @param {string[]} initialFlags Flag list from the snapshot
33
- * @param {string[]} updatedFlags Flag list from runtime
34
- * @return {boolean} true if no changes, false otherwise
35
- */
36
- diffCommandFlags(initialFlags: string[], updatedFlags: Change[]): {
37
- addedFlags: string[];
38
- removedFlags: string[];
39
- updatedFlags: Change[];
40
- changedFlags: Change[];
41
- };
42
30
  /**
43
31
  * compares two command's properties to each other
44
32
  * @return a list of added, removed, updated, and changed properties
@@ -25,15 +25,20 @@ class Compare extends snapshot_command_1.SnapshotCommand {
25
25
  if (updatedCommand) {
26
26
  const changedFlags = this.diffCommandProperty(initialCommand.flags, updatedCommand.flags).changedProperty;
27
27
  const changedAlias = this.diffCommandProperty(initialCommand.alias, updatedCommand.alias).changedProperty;
28
+ const changedChars = this.diffCommandProperty(initialCommand.flagChars, updatedCommand.chars).changedProperty;
28
29
  const flagsChanged = changedFlags.length > 0;
29
30
  const aliasChanged = changedAlias.length > 0;
31
+ const charsChanged = changedChars.length > 0;
30
32
  if (aliasChanged) {
31
33
  updatedCommand.alias = changedAlias;
32
34
  }
35
+ if (charsChanged) {
36
+ updatedCommand.chars = changedChars;
37
+ }
33
38
  if (flagsChanged) {
34
39
  updatedCommand.flags = changedFlags;
35
40
  }
36
- if (flagsChanged || aliasChanged) {
41
+ if (flagsChanged || aliasChanged || charsChanged) {
37
42
  diffCommands.push(updatedCommand);
38
43
  }
39
44
  }
@@ -73,26 +78,16 @@ class Compare extends snapshot_command_1.SnapshotCommand {
73
78
  for (const command of diffCommands) {
74
79
  this.log(`\t${command.name}`);
75
80
  printCommandDiff(command.flags, 'Flags');
81
+ printCommandDiff(command.chars, 'Short Flags');
76
82
  printCommandDiff(command.alias, 'Aliases');
77
83
  }
78
- this.log(`${os_1.EOL}Command, flag, or alias differences detected. If intended, please update the snapshot file and run again.`);
84
+ this.log(`${os_1.EOL}Command, flag, flag characters, or alias differences detected. If intended, please update the snapshot file and run again.`);
79
85
  // Check if existent commands, or properties (flags, aliases) have been deleted
80
86
  if (removedCommands.length > 0 || removedProperties.length > 0) {
81
87
  this.log(chalk.red(`${os_1.EOL}Since there are deletions, a major version bump is required.`));
82
88
  }
83
89
  return { addedCommands, removedCommands, removedFlags: removedCommands, diffCommands };
84
90
  }
85
- /**
86
- * @deprecated in favor of diffCommandProperty
87
- * Compares a flag snapshot with the current command's flags
88
- * @param {string[]} initialFlags Flag list from the snapshot
89
- * @param {string[]} updatedFlags Flag list from runtime
90
- * @return {boolean} true if no changes, false otherwise
91
- */
92
- diffCommandFlags(initialFlags, updatedFlags) {
93
- const diffedFlags = this.diffCommandProperty(initialFlags, updatedFlags);
94
- return { addedFlags: diffedFlags.addedProperty, removedFlags: diffedFlags.removedProperty, updatedFlags: diffedFlags.updated, changedFlags: diffedFlags.changedProperty };
95
- }
96
91
  /**
97
92
  * compares two command's properties to each other
98
93
  * @return a list of added, removed, updated, and changed properties
@@ -125,6 +120,8 @@ class Compare extends snapshot_command_1.SnapshotCommand {
125
120
  plugin: command.pluginName || '',
126
121
  flags: Object.entries(command.flags).map(flagName => ({ name: flagName[0] })),
127
122
  alias: command.aliases.map(alias => ({ name: alias })),
123
+ // we can assert as string because of the filter
124
+ chars: Object.values(command.flags).map(flag => flag.char).filter(char => typeof char === 'string').map(char => ({ name: char })),
128
125
  };
129
126
  });
130
127
  }
@@ -8,6 +8,15 @@ class Generate extends snapshot_command_1.SnapshotCommand {
8
8
  const numberOfSpaceChar = 4;
9
9
  const { flags } = await this.parse(Generate);
10
10
  const resultCommands = this.entries;
11
+ const duplicatedChar = resultCommands.find(command => command.flags.length > 1 && new Set(command.flagChars).size !== command.flagChars.length);
12
+ if (duplicatedChar) {
13
+ throw new Error(`Command "${duplicatedChar.command}" has duplicate short-flag characters "${duplicatedChar.flagChars.filter((item, index) => duplicatedChar.flagChars.indexOf(item) !== index)}"`);
14
+ }
15
+ const charConflictCommand = resultCommands.find(command => command.flags.length > 1 && new Set([...command.flags, ...command.flagChars, ...command.flagAliases]).size !== [...command.flags, ...command.flagChars, ...command.flagAliases].length);
16
+ if (charConflictCommand) {
17
+ const conflictFlags = [...charConflictCommand.flags, ...charConflictCommand.flagChars, ...charConflictCommand.flagAliases].filter((item, index, array) => array.indexOf(item) !== index);
18
+ throw new Error(`Command "${charConflictCommand.command}" has conflicting flags "${conflictFlags}"`);
19
+ }
11
20
  const filePath = flags.filepath.replace('{version}', this.config.version);
12
21
  fs.writeFileSync(filePath, JSON.stringify(resultCommands, null, numberOfSpaceChar));
13
22
  this.log(`Generated snapshot file "${filePath}"`);
@@ -4,6 +4,8 @@ export type SnapshotEntry = {
4
4
  plugin: string;
5
5
  flags: string[];
6
6
  alias: string[];
7
+ flagChars: string[];
8
+ flagAliases: string[];
7
9
  };
8
10
  export declare abstract class SnapshotCommand extends Command {
9
11
  get commands(): Command.Loadable[];
@@ -5,11 +5,10 @@ const core_1 = require("@oclif/core");
5
5
  const _ = require("lodash");
6
6
  class SnapshotCommand extends core_1.Command {
7
7
  get commands() {
8
- var _a;
9
- const devPlugins = (_a = this.config.pjson.oclif.devPlugins) !== null && _a !== void 0 ? _a : [];
8
+ const devPlugins = this.config.pjson.oclif.devPlugins ?? [];
10
9
  const commands = this.config.commands
11
10
  // Ignore dev plugins
12
- .filter(command => { var _a; return !devPlugins.includes((_a = command.pluginName) !== null && _a !== void 0 ? _a : ''); })
11
+ .filter(command => !devPlugins.includes(command.pluginName ?? ''))
13
12
  // remove aliases that reference themselves
14
13
  .filter(command => !command.aliases.includes(command.id));
15
14
  return _.sortBy(commands, 'id');
@@ -21,6 +20,8 @@ class SnapshotCommand extends core_1.Command {
21
20
  plugin: command.pluginName,
22
21
  flags: Object.keys(command.flags).sort(),
23
22
  alias: command.aliases,
23
+ flagChars: Object.values(command.flags).map(flag => flag.char).filter(char => char).sort(),
24
+ flagAliases: Object.values(command.flags).flatMap(flag => flag.aliases).filter(alias => alias).sort(),
24
25
  };
25
26
  });
26
27
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.3.14",
2
+ "version": "4.0.1",
3
3
  "commands": {
4
4
  "schema:compare": {
5
5
  "id": "schema:compare",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/plugin-command-snapshot",
3
3
  "description": "generates and compares OCLIF plugins snapshot files",
4
- "version": "3.3.14",
4
+ "version": "4.0.1",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/plugin-command-snapshot/issues",
7
7
  "dependencies": {
@@ -9,18 +9,18 @@
9
9
  "chalk": "^4.1.2",
10
10
  "just-diff": "^5.2.0",
11
11
  "lodash": "^4.17.21",
12
- "semver": "^7.5.0",
12
+ "semver": "^7.5.1",
13
13
  "ts-json-schema-generator": "^1.2.0",
14
14
  "tslib": "^2.5.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@commitlint/config-conventional": "^12.1.4",
18
18
  "@oclif/plugin-help": "^5.2.9",
19
- "@oclif/test": "^2.3.17",
19
+ "@oclif/test": "^2.3.19",
20
20
  "@types/chai": "^4.3.5",
21
21
  "@types/mocha": "^8",
22
22
  "@types/node": "^15",
23
- "@types/semver": "^7.3.13",
23
+ "@types/semver": "^7.5.0",
24
24
  "chai": "^4",
25
25
  "commitlint": "^12.1.4",
26
26
  "eslint": "^7.32.0",
@@ -29,13 +29,13 @@
29
29
  "husky": "6",
30
30
  "mocha": "^8",
31
31
  "nyc": "^15",
32
- "oclif": "^3.8.2",
32
+ "oclif": "^3.9.0",
33
33
  "sinon": "^11.1.2",
34
34
  "ts-node": "^10",
35
35
  "typescript": "^4.9.5"
36
36
  },
37
37
  "engines": {
38
- "node": ">=12.0.0"
38
+ "node": ">=16.0.0"
39
39
  },
40
40
  "files": [
41
41
  "/lib",
@@ -41,6 +41,27 @@
41
41
  "plugin": {
42
42
  "type": "string"
43
43
  },
44
+ "chars": {
45
+ "type": "array",
46
+ "items": {
47
+ "type": "object",
48
+ "properties": {
49
+ "name": {
50
+ "type": "string"
51
+ },
52
+ "removed": {
53
+ "type": "boolean"
54
+ },
55
+ "added": {
56
+ "type": "boolean"
57
+ }
58
+ },
59
+ "required": [
60
+ "name"
61
+ ],
62
+ "additionalProperties": false
63
+ }
64
+ },
44
65
  "flags": {
45
66
  "type": "array",
46
67
  "items": {
@@ -86,6 +107,7 @@
86
107
  },
87
108
  "required": [
88
109
  "alias",
110
+ "chars",
89
111
  "flags",
90
112
  "name",
91
113
  "plugin"
@@ -28,13 +28,27 @@
28
28
  "items": {
29
29
  "type": "string"
30
30
  }
31
+ },
32
+ "flagChars": {
33
+ "type": "array",
34
+ "items": {
35
+ "type": "string"
36
+ }
37
+ },
38
+ "flagAliases": {
39
+ "type": "array",
40
+ "items": {
41
+ "type": "string"
42
+ }
31
43
  }
32
44
  },
33
45
  "required": [
34
46
  "command",
35
47
  "plugin",
36
48
  "flags",
37
- "alias"
49
+ "alias",
50
+ "flagChars",
51
+ "flagAliases"
38
52
  ],
39
53
  "additionalProperties": false
40
54
  }