Skip to content

Commit

Permalink
Merge pull request #562 from oclif/mdonnalley/core-v4
Browse files Browse the repository at this point in the history
feat: use @oclif/core v4
  • Loading branch information
iowillhoit committed May 31, 2024
2 parents 5038352 + 451d0df commit f8295ac
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"author": "Salesforce",
"bugs": "https://github.com/oclif/plugin-command-snapshot/issues",
"dependencies": {
"@oclif/core": "3.26.6",
"@types/lodash.difference": "^4.5.9",
"chalk": "^5.3.0",
"@oclif/core": "^4.0.0-beta.13",
"ansis": "^3.2.0",
"globby": "^14.0.1",
"just-diff": "^5.2.0",
"lodash.difference": "^4.5.0",
Expand All @@ -22,6 +21,7 @@
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^4",
"@types/chai": "^4.3.11",
"@types/lodash.difference": "^4.5.9",
"@types/lodash.get": "^4.4.9",
"@types/lodash.sortby": "^4.7.9",
"@types/mocha": "^10.0.6",
Expand Down
22 changes: 11 additions & 11 deletions src/commands/schema/compare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Flags, toConfiguredId} from '@oclif/core'
import chalk from 'chalk'
import {bold, cyan, underline} from 'ansis'
import {Operation, diff} from 'just-diff'
import get from 'lodash.get'
import * as fs from 'node:fs'
import * as path from 'node:path'
import fs from 'node:fs'
import path from 'node:path'
import * as semver from 'semver'
import {Schema} from 'ts-json-schema-generator'

Expand Down Expand Up @@ -88,25 +88,25 @@ export default class SchemaCompare extends SnapshotCommand {
switch (change.op) {
case 'replace': {
humanReadableChanges[commandId].push(
`${chalk.underline(readablePath)} was changed from ${chalk.cyan(existing)} to ${chalk.cyan(latest)}`,
`${underline(readablePath)} was changed from ${cyan(existing)} to ${cyan(latest)}`,
)
break
}

case 'add': {
humanReadableChanges[commandId].push(
lastElementIsNum
? `Array item at ${chalk.underline(basePath)} was ${chalk.cyan('added')} to latest schema`
: `${chalk.underline(readablePath)} was ${chalk.cyan('added')} to latest schema`,
? `Array item at ${underline(basePath)} was ${cyan('added')} to latest schema`
: `${underline(readablePath)} was ${cyan('added')} to latest schema`,
)
break
}

case 'remove': {
humanReadableChanges[commandId].push(
lastElementIsNum
? `Array item at ${chalk.underline(basePath)} was ${chalk.cyan('not found')} in latest schema`
: `${chalk.underline(readablePath)} was ${chalk.cyan('not found')} in latest schema`,
? `Array item at ${underline(basePath)} was ${cyan('not found')} in latest schema`
: `${underline(readablePath)} was ${cyan('not found')} in latest schema`,
)
break
}
Expand All @@ -123,10 +123,10 @@ export default class SchemaCompare extends SnapshotCommand {
}

this.log()
this.log(chalk.bold.red('Found the following schema changes:'))
this.log(bold.red('Found the following schema changes:'))
for (const [commandId, changes] of Object.entries(humanReadableChanges)) {
this.log()
this.log(chalk.bold(commandId))
this.log(bold(commandId))
for (const change of changes) {
this.log(` - ${change}`)
}
Expand All @@ -136,7 +136,7 @@ export default class SchemaCompare extends SnapshotCommand {
const bin = process.platform === 'win32' ? 'bin\\dev.cmd' : 'bin/dev.js'
this.log(
'If intended, please update the schema file(s) and run again:',
chalk.bold(`${bin} ${toConfiguredId('schema:generate', this.config)}`),
bold(`${bin} ${toConfiguredId('schema:generate', this.config)}`),
)
process.exitCode = 1
return changes
Expand Down
34 changes: 24 additions & 10 deletions src/commands/schema/generate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Flags} from '@oclif/core'
import chalk from 'chalk'
import {Flags, ux} from '@oclif/core'
import {red} from 'ansis'
import {globbySync} from 'globby'
import * as fs from 'node:fs'
import * as path from 'node:path'
import fs from 'node:fs'
import path from 'node:path'
import {Schema, createGenerator} from 'ts-json-schema-generator'

import SnapshotCommand from '../../snapshot-command.js'
Expand Down Expand Up @@ -84,7 +84,7 @@ export class SchemaGenerator {
if (error instanceof Error) {
const error_ = error.message.toLowerCase().includes('no root type')
? new Error(
`Schema generator could not find the ${chalk.red(returnType)} type. Please make sure that ${chalk.red(
`Schema generator could not find the ${red(returnType)} type. Please make sure that ${red(
returnType,
)} is exported.`,
)
Expand All @@ -107,11 +107,25 @@ export class SchemaGenerator {
}

private getAllHookFiles(): string[] {
// eslint-disable-next-line unicorn/no-array-reduce
const hookFiles = Object.values(this.base.config.pjson.oclif?.hooks ?? {}).reduce(
(x: string[], y: string | string[]) => (Array.isArray(y) ? [...x, ...y] : [...x, y]),
[],
)
const hookFiles = Object.values(this.base.config.pjson.oclif?.hooks ?? {})
.flatMap((hook) => {
if (Array.isArray(hook)) {
return hook.map((h) => {
if (typeof h === 'string') return h
ux.warn(`Identifier/target based hooks are not supported: ${h}`)
return null
})
}

if (typeof hook === 'string') {
return hook
}

ux.warn(`Identifier/target based hooks are not supported: ${hook}`)
return null
})
.filter((h): h is string => typeof h === 'string')

const {outDir, rootDir} = this.getDirs()
return hookFiles.map((h) => `${path.resolve(h)}.ts`.replace(outDir, rootDir))
}
Expand Down
16 changes: 7 additions & 9 deletions src/commands/snapshot/compare.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Flags} from '@oclif/core'
import chalk from 'chalk'
import {green, red} from 'ansis'
import difference from 'lodash.difference'
import * as fs from 'node:fs'
import fs from 'node:fs'
import {EOL} from 'node:os'

import SnapshotCommand, {SnapshotEntry} from '../../snapshot-command.js'
Expand Down Expand Up @@ -108,16 +108,14 @@ export default class Compare extends SnapshotCommand {
// Fail the process since there are changes to the snapshot file
process.exitCode = 1

this.log(
`The following commands and flags have modified: (${chalk.green('+')} added, ${chalk.red('-')} removed)${EOL}`,
)
this.log(`The following commands and flags have modified: (${green('+')} added, ${red('-')} removed)${EOL}`)

for (const command of removedCommands) {
this.log(chalk.red(`\t-${command}`))
this.log(red(`\t-${command}`))
}

for (const command of addedCommands) {
this.log(chalk.green(`\t+${command}`))
this.log(green(`\t+${command}`))
}

const removedProperties: string[] = []
Expand All @@ -126,7 +124,7 @@ export default class Compare extends SnapshotCommand {
if (properties.some((prop) => prop.added || prop.removed)) this.log(`\t ${propertyName}:`)
for (const prop of properties) {
if (prop.added || prop.removed) {
const color = prop.added ? chalk.green : chalk.red
const color = prop.added ? green : red
this.log(color(`\t\t${prop.added ? '+' : '-'}${prop.name}`))
}

Expand All @@ -147,7 +145,7 @@ export default class Compare extends SnapshotCommand {

// Check if existent commands, or properties (flags, aliases) have been deleted
if (removedCommands.length > 0 || removedProperties.length > 0) {
this.log(chalk.red(`${EOL}Since there are deletions, a major version bump is required.`))
this.log(red(`${EOL}Since there are deletions, a major version bump is required.`))
}

return {addedCommands, diffCommands, removedCommands, removedFlags: removedCommands}
Expand Down
32 changes: 30 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@oclif/core@3.26.6", "@oclif/core@^3.26.5", "@oclif/core@^3.26.6":
"@oclif/core@^3.26.5", "@oclif/core@^3.26.6":
version "3.26.6"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-3.26.6.tgz#f371868cfa0fe150a6547e6af98b359065d2f971"
integrity sha512-+FiTw1IPuJTF9tSAlTsY8bGK4sgthehjz7c2SvYdgQncTkxI2xvUch/8QpjNYGLEmUneNygvYMRBax2KJcLccA==
Expand Down Expand Up @@ -1544,6 +1544,29 @@
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/core@^4.0.0-beta.13":
version "4.0.0-beta.13"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-4.0.0-beta.13.tgz#0e0a6431cfe612db77073d2f9ef361f14e4090b8"
integrity sha512-ug8CZUCJphgetSZVgd4HQgyewlYVGGG1LIeFXGxjgYsjZ/f5I3nSCj7xpAMEDqjVD/lwmSujtVwa7tvEgLGICw==
dependencies:
ansi-escapes "^4.3.2"
ansis "^3.0.1"
clean-stack "^3.0.1"
cli-spinners "^2.9.2"
cosmiconfig "^9.0.0"
debug "^4.3.4"
ejs "^3.1.10"
get-package-type "^0.1.0"
globby "^11.1.0"
indent-string "^4.0.0"
is-wsl "^2.2.0"
minimatch "^9.0.4"
string-width "^4.2.3"
supports-color "^9.4.0"
widest-line "^3.1.0"
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"

"@oclif/plugin-help@^6", "@oclif/plugin-help@^6.0.21":
version "6.0.22"
resolved "https://registry.yarnpkg.com/@oclif/plugin-help/-/plugin-help-6.0.22.tgz#dca96ad1c28a68170e0bbf9f8f93f7dd171e4667"
Expand Down Expand Up @@ -2491,7 +2514,7 @@ ansicolors@~0.3.2:
resolved "https://registry.npmjs.org/ansicolors/-/ansicolors-0.3.2.tgz#665597de86a9ffe3aa9bfbe6cae5c6ea426b4979"
integrity sha1-ZlWX3oap/+Oqm/vmyuXG6kJrSXk=

ansis@^3.2.0:
ansis@^3.0.1, ansis@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.2.0.tgz#0e050c5be94784f32ffdac4b84fccba064aeae4b"
integrity sha512-Yk3BkHH9U7oPyCN3gL5Tc7CpahG/+UFv/6UG03C311Vy9lzRmA5uoxDTpU9CO3rGHL6KzJz/pdDeXZCZ5Mu/Sg==
Expand Down Expand Up @@ -6143,6 +6166,11 @@ supports-color@^7, supports-color@^7.0.0, supports-color@^7.1.0:
dependencies:
has-flag "^4.0.0"

supports-color@^9.4.0:
version "9.4.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-9.4.0.tgz#17bfcf686288f531db3dea3215510621ccb55954"
integrity sha512-VL+lNrEoIXww1coLPOmiEmK/0sGigko5COxI09KzHc2VJXJsQ37UaQ+8quuxjDeA7+KnLGTWRyOXSLLR2Wb4jw==

supports-hyperlinks@^2.2.0:
version "2.2.0"
resolved "https://registry.npmjs.org/supports-hyperlinks/-/supports-hyperlinks-2.2.0.tgz#4f77b42488765891774b70c79babd87f9bd594bb"
Expand Down

0 comments on commit f8295ac

Please sign in to comment.