Skip to content

Commit

Permalink
feat: add plugin and sorted flags
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Sep 1, 2020
1 parent c58ef5f commit b14f30c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
3 changes: 3 additions & 0 deletions command-snapshot.json
Original file line number Diff line number Diff line change
@@ -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"
]
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
13 changes: 10 additions & 3 deletions src/commands/snapshot/compare.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -13,6 +13,7 @@ interface Change {
}

type CommandChange = {
plugin: string;
flags: Change[];
} & Change;

Expand All @@ -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)
Expand Down Expand Up @@ -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]})),
}
})
Expand Down
4 changes: 3 additions & 1 deletion src/commands/snapshot/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as _ from 'lodash'

export type SnapshotEntry = {
command: string;
plugin: string;
flags: string[];
}

Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/command-snapshot.json
Original file line number Diff line number Diff line change
@@ -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"
]
Expand Down
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b14f30c

Please sign in to comment.