From b14f30c40d64262c297b1d85868722c042b342bb Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 1 Sep 2020 16:00:41 -0700 Subject: [PATCH] feat: add plugin and sorted flags --- command-snapshot.json | 3 +++ package.json | 3 +-- src/commands/snapshot/compare.ts | 13 ++++++++++--- src/commands/snapshot/generate.ts | 4 +++- test/command-snapshot.json | 3 +++ yarn.lock | 15 ++++----------- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/command-snapshot.json b/command-snapshot.json index 2bc8e189..34f8689f 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -1,18 +1,21 @@ [ { "command": "help", + "plugin": "@oclif/plugin-help", "flags": [ "all" ] }, { "command": "snapshot:compare", + "plugin": "@oclif/plugin-command-snapshot", "flags": [ "filepath" ] }, { "command": "snapshot:generate", + "plugin": "@oclif/plugin-command-snapshot", "flags": [ "filepath" ] diff --git a/package.json b/package.json index 230e5258..dfdf9ce8 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,7 @@ "dependencies": { "@oclif/command": "^1.6.0", "@oclif/config": "^1", - "@types/fs-extra": "^9.0.1", - "chalk": "^3.0.0", + "chalk": "^4.0.0", "sinon": "^9.0.0", "tslib": "^2" }, diff --git a/src/commands/snapshot/compare.ts b/src/commands/snapshot/compare.ts index dcb06c50..28b28429 100644 --- a/src/commands/snapshot/compare.ts +++ b/src/commands/snapshot/compare.ts @@ -1,7 +1,7 @@ import {Command, flags} from '@oclif/command' import * as _ from 'lodash' -import * as fsx from 'fs-extra' +import * as fs from 'fs' import {EOL} from 'os' import {SnapshotEntry} from './generate' import * as chalk from 'chalk' @@ -13,6 +13,7 @@ interface Change { } type CommandChange = { + plugin: string; flags: Change[]; } & Change; @@ -28,13 +29,18 @@ export default class Compare extends Command { * Compare a snapshot with the current commands * @param {CommandChange[]} initialCommands Command list from the snapshot * @param {CommandChange[]} updatedCommands Command list from runtime + * @returns all the command differences */ public async compareSnapshot(initialCommands: SnapshotEntry[], updatedCommands: CommandChange[]) { const removedCommands: string[] = [] const diffCommands: CommandChange[] = [] initialCommands.forEach(initialCommand => { - const updatedCommand = updatedCommands.find(updatedCommand => initialCommand.command === updatedCommand.name) + const updatedCommand = updatedCommands.find(updatedCommand => { + // Protect against old snapshot files that don't have the plugin entry filled out. + const samePlugin = initialCommand.plugin ? initialCommand.plugin === updatedCommand.plugin : true + return initialCommand.command === updatedCommand.name && samePlugin + }) if (updatedCommand) { const {changedFlags} = this.diffCommandFlags(initialCommand.flags, updatedCommand.flags) @@ -123,11 +129,12 @@ export default class Compare extends Command { public async run() { const {flags} = this.parse(Compare) - const oldCommandFlags = JSON.parse(fsx.readFileSync(flags.filepath).toString('utf8')) as SnapshotEntry[] + const oldCommandFlags = JSON.parse(fs.readFileSync(flags.filepath).toString('utf8')) as SnapshotEntry[] const newCommandFlags = this.config.commands const resultnewCommandFlags: CommandChange[] = _.sortBy(newCommandFlags, 'id').map(command => { return { name: command.id, + plugin: command.pluginName || '', flags: Object.entries(command.flags).map(flagName => ({name: flagName[0]})), } }) diff --git a/src/commands/snapshot/generate.ts b/src/commands/snapshot/generate.ts index a57b64f8..8bd78820 100644 --- a/src/commands/snapshot/generate.ts +++ b/src/commands/snapshot/generate.ts @@ -4,6 +4,7 @@ import * as _ from 'lodash' export type SnapshotEntry = { command: string; + plugin: string; flags: string[]; } @@ -22,7 +23,8 @@ export default class Generate extends Command { const resultCommands = _.sortBy(commands, 'id').map(command => { return { command: command.id, - flags: Object.keys(command.flags), + plugin: command.pluginName, + flags: Object.keys(command.flags).sort(), } }) as SnapshotEntry[] const filePath = flags.filepath.replace('{version}', this.config.version) diff --git a/test/command-snapshot.json b/test/command-snapshot.json index 23c6cd36..f3c7944e 100644 --- a/test/command-snapshot.json +++ b/test/command-snapshot.json @@ -1,18 +1,21 @@ [ { "command": "help", + "plugin": "@oclif/plugin-help", "flags": [ "all" ] }, { "command": "snapshot:compare", + "plugin": "@oclif/plugin-command-snapshot", "flags": [ "all" ] }, { "command": "removed:command", + "plugin": "@oclif/plugin-command-snapshot", "flags": [ "filepath" ] diff --git a/yarn.lock b/yarn.lock index 631aa586..7dc8685f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -274,13 +274,6 @@ resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== -"@types/fs-extra@^9.0.1": - version "9.0.1" - resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.1.tgz#91c8fc4c51f6d5dbe44c2ca9ab09310bd00c7918" - integrity sha512-B42Sxuaz09MhC3DDeW5kubRcQ5by4iuVQ0cRRWM2lggLzAa/KVom0Aft/208NgMvNQQZ86s5rVcqDdn/SH0/mg== - dependencies: - "@types/node" "*" - "@types/glob@^7.1.1": version "7.1.1" 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: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4" - integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg== +chalk@^4.0.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a" + integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A== dependencies: ansi-styles "^4.1.0" supports-color "^7.1.0"