Skip to content

Commit

Permalink
feat: use @oclif/core v2 (#536)
Browse files Browse the repository at this point in the history
* feat: use @oclif/core v2

* chore: bump oclif/test
  • Loading branch information
mdonnalley committed Jan 18, 2023
1 parent 3ad700a commit e32e14f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 45 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/oclif/plugin-plugins/issues",
"dependencies": {
"@oclif/color": "^1.0.2",
"@oclif/core": "^1.23.0",
"@oclif/core": "^2.0.2-beta.6",
"chalk": "^4.1.2",
"debug": "^4.3.4",
"fs-extra": "^9.0",
Expand All @@ -20,7 +20,7 @@
"devDependencies": {
"@commitlint/config-conventional": "^12.1.4",
"@oclif/plugin-help": "^5.1.22",
"@oclif/test": "^2.2.19",
"@oclif/test": "^2.2.21",
"@types/chai": "^4.3.4",
"@types/fs-extra": "^9.0",
"@types/mocha": "^8.2.3",
Expand Down Expand Up @@ -76,7 +76,7 @@
"clean": "shx rm -f oclif.manifest.json",
"lint": "eslint . --ext .ts --config .eslintrc",
"pretest": "yarn build && tsc -p test --noEmit",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"test": "mocha \"test/**/*.test.ts\"",
"posttest": "yarn lint",
"prepublishOnly": "yarn run build && oclif manifest . && oclif readme",
"postpublish": "yarn run clean",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import color from '@oclif/color'
import {Command, Flags, Plugin, CliUx} from '@oclif/core'
import {Command, Flags, Plugin, ux} from '@oclif/core'

import Plugins from '../../plugins'
import {sortBy} from '../../util'
Expand Down Expand Up @@ -42,7 +42,7 @@ export default class PluginsIndex extends Command {
}

private createTree(plugin: Plugin) {
const tree = CliUx.ux.tree()
const tree = ux.tree()
for (const p of plugin.children) {
const name = this.formatPlugin(p)
tree.insert(name, this.createTree(p))
Expand Down
16 changes: 10 additions & 6 deletions src/commands/plugins/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path'
import {Command, Flags, Plugin, CliUx} from '@oclif/core'
import {Args, Command, Flags, Plugin, ux} from '@oclif/core'
import * as chalk from 'chalk'
import * as fs from 'fs-extra'

Expand All @@ -25,9 +25,13 @@ export default class PluginsInspect extends Command {

static strict = false;

static args = [
{name: 'plugin', description: 'Plugin to inspect.', required: true, default: '.'},
];
static args = {
plugin: Args.string({
description: 'Plugin to inspect.',
required: true,
default: '.',
}),
}

static flags = {
help: Flags.help({char: 'h'}),
Expand All @@ -43,7 +47,7 @@ export default class PluginsInspect extends Command {
const {flags, argv} = await this.parse(PluginsInspect)
if (flags.verbose) this.plugins.verbose = true
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv) {
for (let name of argv as string[]) {
if (name === '.') {
const pkgJson = JSON.parse(await fs.readFile('package.json', 'utf-8'))
name = pkgJson.name
Expand Down Expand Up @@ -84,7 +88,7 @@ export default class PluginsInspect extends Command {

async inspect(pluginName: string, verbose = false): Promise<void> {
const plugin = this.findPlugin(pluginName)
const tree = CliUx.ux.tree()
const tree = ux.tree()
const pluginHeader = chalk.bold.cyan(plugin.name)
tree.insert(pluginHeader)
tree.nodes[pluginHeader].insert(`version ${plugin.version}`)
Expand Down
18 changes: 9 additions & 9 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, Flags, CliUx} from '@oclif/core'
import {Command, Flags, ux, Args} from '@oclif/core'
import * as chalk from 'chalk'

import Plugins from '../../plugins'
Expand All @@ -22,9 +22,9 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins

static strict = false;

static args = [
{name: 'plugin', description: 'Plugin to install.', required: true},
];
static args = {
plugin: Args.string({description: 'Plugin to install.', required: true}),
};

static flags = {
help: Flags.help({char: 'h'}),
Expand All @@ -46,7 +46,7 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
const {flags, argv} = await this.parse(PluginsInstall)
if (flags.verbose) this.plugins.verbose = true
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv) {
for (let name of argv as string[]) {
if (aliases[name] === null) this.error(`${name} is blocked`)
name = aliases[name] || name
const p = await this.parsePlugin(name)
Expand All @@ -56,23 +56,23 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
})
try {
if (p.type === 'npm') {
CliUx.ux.action.start(
ux.action.start(
`Installing plugin ${chalk.cyan(this.plugins.friendlyName(p.name))}`,
)
plugin = await this.plugins.install(p.name, {
tag: p.tag,
force: flags.force,
})
} else {
CliUx.ux.action.start(`Installing plugin ${chalk.cyan(p.url)}`)
ux.action.start(`Installing plugin ${chalk.cyan(p.url)}`)
plugin = await this.plugins.install(p.url, {force: flags.force})
}
} catch (error) {
CliUx.ux.action.stop(chalk.bold.red('failed'))
ux.action.stop(chalk.bold.red('failed'))
throw error
}

CliUx.ux.action.stop(`installed v${plugin.version}`)
ux.action.stop(`installed v${plugin.version}`)
}
}
/* eslint-enable no-await-in-loop */
Expand Down
10 changes: 6 additions & 4 deletions src/commands/plugins/link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, Flags, CliUx} from '@oclif/core'
import {Args, Command, Flags, ux} from '@oclif/core'
import * as chalk from 'chalk'

import Plugins from '../../plugins'
Expand All @@ -14,7 +14,9 @@ e.g. If you have a user-installed or core plugin that has a 'hello' command, ins

static examples = ['$ <%= config.bin %> plugins:link <%- config.pjson.oclif.examplePlugin || "myplugin" %> ']

static args = [{name: 'path', description: 'path to plugin', required: true, default: '.'}]
static args = {
path: Args.string({name: 'path', description: 'path to plugin', required: true, default: '.'}),
}

static flags = {
help: Flags.help({char: 'h'}),
Expand All @@ -26,8 +28,8 @@ e.g. If you have a user-installed or core plugin that has a 'hello' command, ins
async run(): Promise<void> {
const {flags, args} = await this.parse(PluginsLink)
this.plugins.verbose = flags.verbose
CliUx.ux.action.start(`Linking plugin ${chalk.cyan(args.path)}`)
ux.action.start(`Linking plugin ${chalk.cyan(args.path)}`)
await this.plugins.link(args.path)
CliUx.ux.action.stop()
ux.action.stop()
}
}
14 changes: 8 additions & 6 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, Flags, Plugin, Interfaces, CliUx} from '@oclif/core'
import {Command, Flags, Plugin, Interfaces, ux, Args} from '@oclif/core'
import * as chalk from 'chalk'

import Plugins from '../../plugins'
Expand All @@ -15,7 +15,9 @@ export default class PluginsUninstall extends Command {

static variableArgs = true

static args = [{name: 'plugin', description: 'plugin to uninstall'}]
static args = {
plugin: Args.string({description: 'plugin to uninstall'}),
}

static flags = {
help: Flags.help({char: 'h'}),
Expand All @@ -34,9 +36,9 @@ export default class PluginsUninstall extends Command {
this.plugins = new Plugins(this.config)
if (flags.verbose) this.plugins.verbose = true
if (argv.length === 0) argv.push('.')
for (const plugin of argv) {
for (const plugin of argv as string[]) {
const friendly = this.removeTags(this.plugins.friendlyName(plugin))
CliUx.ux.action.start(`Uninstalling ${friendly}`)
ux.action.start(`Uninstalling ${friendly}`)
const unfriendly = await this.plugins.hasPlugin(this.removeTags(plugin))
if (!unfriendly) {
const p = this.config.plugins.find(p => p.name === plugin) as Plugin | undefined
Expand All @@ -51,11 +53,11 @@ export default class PluginsUninstall extends Command {
const {name} = unfriendly as Interfaces.PJSON.User | Interfaces.PJSON.PluginTypes.Link
await this.plugins.uninstall(name)
} catch (error) {
CliUx.ux.action.stop(chalk.bold.red('failed'))
ux.action.stop(chalk.bold.red('failed'))
throw error
}

CliUx.ux.action.stop()
ux.action.stop()
}
}
/* eslint-enable no-await-in-loop */
Expand Down
12 changes: 6 additions & 6 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Errors, Config, Interfaces, CliUx} from '@oclif/core'
import {Errors, Config, Interfaces, ux} from '@oclif/core'
import * as fs from 'fs'
import * as fse from 'fs-extra'
import loadJSON from 'load-json-file'
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class Plugins {

async link(p: string): Promise<void> {
const c = await Config.load(path.resolve(p))
CliUx.ux.action.start(`${this.config.name}: linking plugin ${c.name}`)
ux.action.start(`${this.config.name}: linking plugin ${c.name}`)
if (
!c.valid &&
!this.config.plugins.find(p => p.name === '@oclif/plugin-legacy')
Expand Down Expand Up @@ -189,7 +189,7 @@ export default class Plugins {
})
}
} catch (error: any) {
CliUx.ux.warn(error)
ux.warn(error)
} finally {
await this.remove(name)
}
Expand All @@ -203,7 +203,7 @@ export default class Plugins {
(p): p is Interfaces.PJSON.PluginTypes.User => p.type === 'user',
)
if (plugins.length === 0) return
CliUx.ux.action.start(`${this.config.name}: Updating plugins`)
ux.action.start(`${this.config.name}: Updating plugins`)

// migrate deprecated plugins
const aliases = this.config.pjson.oclif.aliases || {}
Expand Down Expand Up @@ -236,7 +236,7 @@ export default class Plugins {
)
}

CliUx.ux.action.stop()
ux.action.stop()
}
/* eslint-enable no-await-in-loop */

Expand Down Expand Up @@ -268,7 +268,7 @@ export default class Plugins {
)
return f.nodeVersion
} catch (error: any) {
if (error.code !== 'ENOENT') CliUx.ux.warn(error)
if (error.code !== 'ENOENT') ux.warn(error)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Interfaces, CliUx} from '@oclif/core'
import {Interfaces, ux} from '@oclif/core'
import NpmRunPath from 'npm-run-path'
import * as path from 'path'

Expand All @@ -23,7 +23,7 @@ export default class Yarn {
forked.stdout.setEncoding('utf8')
forked.stdout.on('data', (d: any) => {
if (options.verbose) process.stdout.write(d)
else CliUx.ux.action.status = d.replace(/\n$/, '').split('\n').pop()
else ux.action.status = d.replace(/\n$/, '').split('\n').pop()
})

forked.on('error', reject)
Expand Down
Loading

0 comments on commit e32e14f

Please sign in to comment.