Skip to content

Commit b14f30c

Browse files
committed
feat: add plugin and sorted flags
1 parent c58ef5f commit b14f30c

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

command-snapshot.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
[
22
{
33
"command": "help",
4+
"plugin": "@oclif/plugin-help",
45
"flags": [
56
"all"
67
]
78
},
89
{
910
"command": "snapshot:compare",
11+
"plugin": "@oclif/plugin-command-snapshot",
1012
"flags": [
1113
"filepath"
1214
]
1315
},
1416
{
1517
"command": "snapshot:generate",
18+
"plugin": "@oclif/plugin-command-snapshot",
1619
"flags": [
1720
"filepath"
1821
]

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"dependencies": {
88
"@oclif/command": "^1.6.0",
99
"@oclif/config": "^1",
10-
"@types/fs-extra": "^9.0.1",
11-
"chalk": "^3.0.0",
10+
"chalk": "^4.0.0",
1211
"sinon": "^9.0.0",
1312
"tslib": "^2"
1413
},

src/commands/snapshot/compare.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import {Command, flags} from '@oclif/command'
33
import * as _ from 'lodash'
4-
import * as fsx from 'fs-extra'
4+
import * as fs from 'fs'
55
import {EOL} from 'os'
66
import {SnapshotEntry} from './generate'
77
import * as chalk from 'chalk'
@@ -13,6 +13,7 @@ interface Change {
1313
}
1414

1515
type CommandChange = {
16+
plugin: string;
1617
flags: Change[];
1718
} & Change;
1819

@@ -28,13 +29,18 @@ export default class Compare extends Command {
2829
* Compare a snapshot with the current commands
2930
* @param {CommandChange[]} initialCommands Command list from the snapshot
3031
* @param {CommandChange[]} updatedCommands Command list from runtime
32+
* @returns all the command differences
3133
*/
3234
public async compareSnapshot(initialCommands: SnapshotEntry[], updatedCommands: CommandChange[]) {
3335
const removedCommands: string[] = []
3436
const diffCommands: CommandChange[] = []
3537

3638
initialCommands.forEach(initialCommand => {
37-
const updatedCommand = updatedCommands.find(updatedCommand => initialCommand.command === updatedCommand.name)
39+
const updatedCommand = updatedCommands.find(updatedCommand => {
40+
// Protect against old snapshot files that don't have the plugin entry filled out.
41+
const samePlugin = initialCommand.plugin ? initialCommand.plugin === updatedCommand.plugin : true
42+
return initialCommand.command === updatedCommand.name && samePlugin
43+
})
3844

3945
if (updatedCommand) {
4046
const {changedFlags} = this.diffCommandFlags(initialCommand.flags, updatedCommand.flags)
@@ -123,11 +129,12 @@ export default class Compare extends Command {
123129

124130
public async run() {
125131
const {flags} = this.parse(Compare)
126-
const oldCommandFlags = JSON.parse(fsx.readFileSync(flags.filepath).toString('utf8')) as SnapshotEntry[]
132+
const oldCommandFlags = JSON.parse(fs.readFileSync(flags.filepath).toString('utf8')) as SnapshotEntry[]
127133
const newCommandFlags = this.config.commands
128134
const resultnewCommandFlags: CommandChange[] = _.sortBy(newCommandFlags, 'id').map(command => {
129135
return {
130136
name: command.id,
137+
plugin: command.pluginName || '',
131138
flags: Object.entries(command.flags).map(flagName => ({name: flagName[0]})),
132139
}
133140
})

src/commands/snapshot/generate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as _ from 'lodash'
44

55
export type SnapshotEntry = {
66
command: string;
7+
plugin: string;
78
flags: string[];
89
}
910

@@ -22,7 +23,8 @@ export default class Generate extends Command {
2223
const resultCommands = _.sortBy(commands, 'id').map(command => {
2324
return {
2425
command: command.id,
25-
flags: Object.keys(command.flags),
26+
plugin: command.pluginName,
27+
flags: Object.keys(command.flags).sort(),
2628
}
2729
}) as SnapshotEntry[]
2830
const filePath = flags.filepath.replace('{version}', this.config.version)

test/command-snapshot.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
[
22
{
33
"command": "help",
4+
"plugin": "@oclif/plugin-help",
45
"flags": [
56
"all"
67
]
78
},
89
{
910
"command": "snapshot:compare",
11+
"plugin": "@oclif/plugin-command-snapshot",
1012
"flags": [
1113
"all"
1214
]
1315
},
1416
{
1517
"command": "removed:command",
18+
"plugin": "@oclif/plugin-command-snapshot",
1619
"flags": [
1720
"filepath"
1821
]

yarn.lock

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,6 @@
274274
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
275275
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==
276276

277-
"@types/fs-extra@^9.0.1":
278-
version "9.0.1"
279-
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.1.tgz#91c8fc4c51f6d5dbe44c2ca9ab09310bd00c7918"
280-
integrity sha512-B42Sxuaz09MhC3DDeW5kubRcQ5by4iuVQ0cRRWM2lggLzAa/KVom0Aft/208NgMvNQQZ86s5rVcqDdn/SH0/mg==
281-
dependencies:
282-
"@types/node" "*"
283-
284277
"@types/glob@^7.1.1":
285278
version "7.1.1"
286279
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
@@ -563,10 +556,10 @@ chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.2:
563556
escape-string-regexp "^1.0.5"
564557
supports-color "^5.3.0"
565558

566-
chalk@^3.0.0:
567-
version "3.0.0"
568-
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
569-
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
559+
chalk@^4.0.0:
560+
version "4.1.0"
561+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
562+
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
570563
dependencies:
571564
ansi-styles "^4.1.0"
572565
supports-color "^7.1.0"

0 commit comments

Comments
 (0)