Skip to content

Commit

Permalink
feat: added default scope to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 1, 2018
1 parent 6f1821c commit 079808b
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 137 deletions.
30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/jdxcode/plugins/issues",
"dependencies": {
"@anycli/command": "^0.2.6",
"@anycli/config": "^0.2.1",
"@anycli/command": "^0.2.21",
"@anycli/config": "^0.2.4",
"@anycli/manifest-file": "^0.2.0",
"@heroku-cli/color": "^1.1.1",
"cli-ux": "^3.3.8",
"cli-ux": "^3.3.10",
"debug": "^3.1.0",
"fs-extra": "^5.0.0",
"http-call": "^5.0.2",
"lodash": "^4.17.4",
"npm-run-path": "^2.0.2",
"tslib": "^1.9.0",
"yarn": "^1.3.2"
},
"devDependencies": {
"@anycli/engine": "^0.1.29",
"@anycli/test": "^0.10.0",
"@anycli/tslint": "^0.2.0",
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@anycli/engine": "^0.1.38",
"@anycli/test": "^0.10.1",
"@anycli/tslint": "^0.2.1",
"@anycli/version": "^0.1.19",
"@commitlint/cli": "^6.0.5",
"@commitlint/config-conventional": "^6.0.4",
"@types/chai": "^4.1.2",
"@types/fs-extra": "^5.0.0",
"@types/lodash": "^4.14.98",
"@types/mocha": "^2.2.47",
"@types/lodash": "^4.14.99",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
Expand All @@ -41,11 +43,15 @@
"nps": "^5.7.1",
"nps-utils": "^1.5.0",
"ts-node": "^4.1.0",
"typescript": "^2.6.2"
"typescript": "^2.7.1"
},
"anycli": {
"commands": "./lib/commands",
"plugins": "./lib/load"
"plugins": "./lib/load",
"pluginScope": "heroku-cli",
"devPlugins": [
"@anycli/version"
]
},
"engines": {
"node": ">=8.0.0"
Expand Down
13 changes: 5 additions & 8 deletions src/commands/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Command, {flags} from '@anycli/command'
import {Command, flags, parse} from '@anycli/command'
import color from '@heroku-cli/color'
import cli from 'cli-ux'
import * as _ from 'lodash'
Expand All @@ -23,24 +23,21 @@ if (g.anycli && g.anycli.config) {
const examplePluginsHelp = Object.entries(examplePlugins).map(([name, p]: [string, any]) => ` ${name} ${p.version}`)

export default class PluginsIndex extends Command {
static flags: flags.Input<PluginsIndex['flags']> = {
static flags = {
core: flags.boolean({description: 'show core plugins'})
}
static description = 'list installed plugins'
static help = `Example:
$ ${bin} plugins
${examplePluginsHelp.join('\n')}
`
plugins: Plugins
flags: {
core: boolean
}
plugins = new Plugins(this.config)
options = parse(this.argv, PluginsIndex)

async run() {
this.plugins = new Plugins(this.config)
let plugins = this.config.engine!.plugins
_.sortBy(plugins, 'name')
if (!this.flags.core) plugins = plugins.filter(p => p.type !== 'core')
if (!this.options.flags.core) plugins = plugins.filter(p => p.type !== 'core' && p.type !== 'dev')
if (!plugins.length) {
cli.info('no plugins installed')
}
Expand Down
48 changes: 37 additions & 11 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Command} from '@anycli/command'
import {Command, parse} from '@anycli/command'
import cli from 'cli-ux'
import HTTP from 'http-call'

import Plugins from '../../plugins'

Expand All @@ -22,21 +23,46 @@ export default class PluginsInstall extends Command {
Example:
$ ${bin} plugins:install ${examplePlugin}
`
static variableArgs = true
static strict = false
static args = [{name: 'plugin', description: 'plugin to install', required: true}]

plugins: Plugins
plugins = new Plugins(this.config)
options = parse(this.argv, PluginsInstall)

async run() {
this.plugins = new Plugins(this.config)
for (let plugin of this.argv) {
let scoped = plugin[0] === '@'
if (scoped) plugin = plugin.slice(1)
let [name, tag = 'latest'] = plugin.split('@')
if (scoped) name = `@${name}`
cli.action.start(`Installing plugin ${plugin}@${tag}`)
await this.plugins.install(name, tag)
for (let plugin of this.options.argv) {
let {name, tag, scope} = parsePlugin(plugin)
plugin = scope ? `@${scope}/${name}@${tag}` : `${name}@${tag}`
cli.action.start(`Installing plugin ${plugin}`)
const defaultScope = this.config.pjson.anycli.pluginScope
if (!scope && defaultScope) {
let version = await this.fetchVersionFromNPM({name, tag, scope: defaultScope})
if (version) scope = defaultScope
}
await this.plugins.install(scope ? `@${scope}/${name}` : name, tag)
cli.action.stop()
}
}

private async fetchVersionFromNPM(plugin: {name: string, scope?: string, tag: string}): Promise<string | undefined> {
try {
let url = plugin.scope ? `${this.config.npmRegistry}/-/package/@${plugin.scope}%2f${plugin.name}/dist-tags` : `${this.config.npmRegistry}/-/package/${plugin.name}/dist-tags`
const {body: pkg} = await HTTP.get(url)
return pkg[plugin.tag]
} catch (err) {
this.debug(err)
}
}
}

function parsePlugin(input: string): {name: string, scope?: string, tag: string} {
if (input.includes('/')) {
let [scope, nameAndTag] = input.split('/')
scope = scope.slice(1)
let [name, tag = 'latest'] = nameAndTag.split('@')
return {scope, name, tag}
} else {
let [name, tag = 'latest'] = input.split('@')
return {name, tag}
}
}
7 changes: 4 additions & 3 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command} from '@anycli/command'
import {Command, parse} from '@anycli/command'
import cli from 'cli-ux'

import Plugins from '../../plugins'
Expand All @@ -25,11 +25,12 @@ export default class PluginsUninstall extends Command {
static variableArgs = true
static args = [{name: 'plugin', description: 'plugin to uninstall', required: true}]

plugins: Plugins
plugins = new Plugins(this.config)
options = parse(this.argv, PluginsUninstall)

async run() {
this.plugins = new Plugins(this.config)
for (let plugin of this.argv) {
for (let plugin of this.options.argv) {
cli.action.start(`Uninstalling ${plugin}`)
await this.plugins.uninstall(plugin)
cli.action.stop()
Expand Down
2 changes: 1 addition & 1 deletion src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Manifest from './manifest'
import Yarn from './yarn'

export default class Plugins {
readonly yarn: Yarn
private readonly manifest: Manifest
private readonly yarn: Yarn
private readonly debug: any

constructor(public config: IConfig) {
Expand Down
34 changes: 32 additions & 2 deletions test/commands/plugins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@ import {expect, test} from '../../test'

describe('command', () => {
test
.command(['plugins:install', '@heroku-cli/status'])
.command(['plugins:install', 'status'])
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.contain('@heroku-cli/status'))
.stdout()
.command(['status'])
.do(output => expect(output.stdout).to.contain('No known issues at this time'))
.command(['plugins:uninstall', '@heroku-cli/status'])
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.equal('no plugins installed\n'))
.it('installs and uninstalls status')

test
.command(['plugins:install', '@heroku-cli/status'])
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.contain('@heroku-cli/status'))
.stdout()
.command(['status'])
.do(output => expect(output.stdout).to.contain('No known issues at this time'))
.command(['plugins:uninstall', '@heroku-cli/status'])
.stderr()
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.equal('no plugins installed\n'))
.it('installs and uninstalls @heroku-cli/status')

test
.skip()
.command(['plugins:install', 'heroku-debug'])
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.contain('heroku-debug'))
.stdout()
.command(['debug'])
.do(output => expect(output.stdout).to.contain('foo'))
.command(['plugins:uninstall', 'heroku-debug'])
.stdout()
.command(['plugins'])
.do(output => expect(output.stdout).to.equal('no plugins installed\n'))
.it('installs and uninstalls heroku-debug')
})
3 changes: 2 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IConfig, read} from '@anycli/config'
import {IConfig, IEngine, read} from '@anycli/config'
import {expect, FancyTypes, NockScope, test as base} from '@anycli/test'
import * as fs from 'fs-extra'

Expand All @@ -13,6 +13,7 @@ export const test = base
})

export {
IEngine,
expect,
IConfig,
FancyTypes,
Expand Down
Loading

0 comments on commit 079808b

Please sign in to comment.