Skip to content

Commit

Permalink
fix: updated with config/command changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent 0a90165 commit 1d86582
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 923 deletions.
27 changes: 9 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,41 @@
"commands": "./lib/commands",
"scope": "heroku-cli",
"devPlugins": [
"@anycli/plugin-version",
"@anycli/plugin-help"
]
},
"bugs": "https://github.com/anycli/plugin-plugins/issues",
"dependencies": {
"@anycli/command": "^1.2.3",
"@anycli/command": "^1.2.6",
"@heroku-cli/color": "^1.1.3",
"chalk": "^2.3.0",
"cli-ux": "^3.3.13",
"cli-ux": "^3.3.16",
"debug": "^3.1.0",
"fs-extra": "^5.0.0",
"http-call": "^5.0.2",
"load-json-file": "^4.0.0",
"lodash": "^4.17.4",
"npm-run-path": "^2.0.2",
"semver": "^5.5.0",
"tslib": "^1.9.0",
"yarn": "^1.3.2"
},
"devDependencies": {
"@anycli/config": "^1.1.0",
"@anycli/dev-cli": "^0.1.4",
"@anycli/engine": "^0.3.6",
"@anycli/plugin-help": "^0.6.0",
"@anycli/plugin-version": "^0.1.34",
"@anycli/test": "^0.10.9",
"@anycli/config": "^1.2.3",
"@anycli/dev-cli": "^0.1.6",
"@anycli/plugin-help": "^0.6.3",
"@anycli/test": "^0.10.11",
"@anycli/tslint": "^0.2.5",
"@types/chai": "^4.1.2",
"@types/fs-extra": "^5.0.0",
"@types/load-json-file": "^2.0.7",
"@types/lodash": "^4.14.100",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
"@types/read-pkg": "^3.0.0",
"@types/semver": "^5.5.0",
"@types/supports-color": "^3.1.0",
"chai": "^4.1.2",
"concurrently": "^3.5.1",
"eslint": "^4.17.0",
"eslint-config-anycli": "^1.3.2",
"fancy-test": "^0.6.10",
"fancy-test": "^1.0.1",
"globby": "^7.1.1",
"mocha": "^5.0.0",
"ts-node": "^4.1.0",
"typescript": "^2.7.1"
Expand All @@ -71,7 +62,7 @@
"repository": "anycli/plugin-plugins",
"scripts": {
"build": "rm -rf lib && tsc",
"lint": "concurrently -p command \"eslint .\" \"tsc -p test --noEmit\" \"tslint -p test\"",
"lint": "concurrently -p command \"tsc -p test --noEmit\" \"tslint -p test\"",
"postpublish": "rm .anycli.manifest.json",
"posttest": "yarn run lint",
"prepublishOnly": "yarn run build && anycli-dev manifest",
Expand Down
15 changes: 7 additions & 8 deletions src/commands/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Command, flags, parse} from '@anycli/command'
import {Command, flags} from '@anycli/command'
import color from '@heroku-cli/color'
import cli from 'cli-ux'
import * as _ from 'lodash'

import Plugins from '../../plugins'
import {sortBy} from '../../util'

export default class PluginsIndex extends Command {
static flags = {
Expand All @@ -24,24 +23,24 @@ const examplePluginsHelp = Object.entries(examplePlugins).map(([name, p]: [strin
`]

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

async run() {
const {flags} = this.parse(PluginsIndex)
let plugins = this.config.plugins
_.sortBy(plugins, 'name')
if (!this.options.flags.core) {
sortBy(plugins, p => p.name)
if (!flags.core) {
plugins = plugins.filter(p => p.type !== 'core' && p.type !== 'dev')
}
if (!plugins.length) {
cli.info('no plugins installed')
this.log('no plugins installed')
return
}
for (let plugin of plugins) {
let output = `${this.plugins.friendlyName(plugin.name)} ${color.dim(plugin.version)}`
if (plugin.type !== 'user') output += color.dim(` (${plugin.type})`)
if (plugin.type === 'link') output += ` ${plugin.root}`
else if (plugin.tag && plugin.tag !== 'latest') output += color.dim(` (${String(plugin.tag)})`)
cli.log(output)
this.log(output)
}
}
}
6 changes: 3 additions & 3 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, parse} from '@anycli/command'
import {Command} from '@anycli/command'
import chalk from 'chalk'
import cli from 'cli-ux'

Expand Down Expand Up @@ -27,10 +27,10 @@ Example:
static args = [{name: 'plugin', description: 'plugin to install', required: true}]

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

async run() {
for (let plugin of this.options.argv) {
const {argv} = this.parse(PluginsInstall)
for (let plugin of argv) {
let {name, tag} = parsePlugin(plugin)
cli.action.start(`Installing plugin ${chalk.cyan(this.plugins.friendlyName(name))}`)
await this.plugins.install(name, tag)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, parse} from '@anycli/command'
import {Command} from '@anycli/command'
import cli from 'cli-ux'

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

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

async run() {
const {argv} = this.parse(PluginsUninstall)
this.plugins = new Plugins(this.config)
for (let plugin of this.options.argv) {
for (let plugin of argv) {
const friendly = this.plugins.friendlyName(plugin)
cli.action.start(`Uninstalling ${friendly}`)
const unfriendly = await this.plugins.hasPlugin(plugin)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/plugins/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, parse} from '@anycli/command'
import {Command} from '@anycli/command'

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

Expand All @@ -7,10 +7,10 @@ export default class PluginsUpdate extends Command {
static command = 'update'
static description = 'update installed plugins'

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

async run() {
this.parse(PluginsUpdate)
await this.plugins.update()
}
}
20 changes: 11 additions & 9 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as Config from '@anycli/config'
import cli from 'cli-ux'
import * as fs from 'fs-extra'
import * as fs from 'fs'
import * as fse from 'fs-extra'
import HTTP from 'http-call'
import loadJSON = require('load-json-file')
import * as _ from 'lodash'
import * as path from 'path'
import * as semver from 'semver'

import {uniq, uniqWith} from './util'
import Yarn from './yarn'

const initPJSON: Config.PJSON.User = {private: true, anycli: {schema: 1, plugins: []}, dependencies: {}}
Expand Down Expand Up @@ -34,7 +35,7 @@ export default class Plugins {
}
} catch (err) {
this.debug(err)
if (err.code !== 'ENOENT') cli.warn(err)
if (err.code !== 'ENOENT') process.emitWarning(err)
return initPJSON
}
}
Expand Down Expand Up @@ -66,16 +67,15 @@ export default class Plugins {

async add(plugin: Config.PJSON.PluginTypes) {
const pjson = await this.pjson()
pjson.anycli.plugins = _.uniq([...pjson.anycli.plugins || [], plugin])
pjson.anycli.plugins = uniq([...pjson.anycli.plugins || [], plugin])
await this.savePJSON(pjson)
}

async remove(name: string) {
const pjson = await this.pjson()
if (pjson.dependencies) delete pjson.dependencies[name]
pjson.anycli.plugins = _(this.normalizePlugins(pjson.anycli.plugins))
pjson.anycli.plugins = this.normalizePlugins(pjson.anycli.plugins)
.filter(p => p.name !== name)
.value()
await this.savePJSON(pjson)
}

Expand Down Expand Up @@ -132,7 +132,7 @@ export default class Plugins {
// }

private async createPJSON() {
if (!await fs.pathExists(this.pjsonPath)) {
if (!fs.existsSync(this.pjsonPath)) {
await this.savePJSON(initPJSON)
}
}
Expand All @@ -143,8 +143,9 @@ export default class Plugins {

private async npmHasPackage(name: string): Promise<boolean> {
try {
const http: typeof HTTP = require('http-call').HTTP
let url = `${this.config.npmRegistry}/-/package/${name.replace('/', '%2f')}/dist-tags`
await HTTP.get(url)
await http.get(url)
return true
} catch (err) {
this.debug(err)
Expand All @@ -154,6 +155,7 @@ export default class Plugins {

private async savePJSON(pjson: Config.PJSON.User) {
pjson.anycli.plugins = this.normalizePlugins(pjson.anycli.plugins)
const fs: typeof fse = require('fs-extra')
await fs.outputJSON(this.pjsonPath, pjson, {spaces: 2})
}

Expand All @@ -163,7 +165,7 @@ export default class Plugins {
return {name: p, type: 'user', tag: 'latest'} as Config.PJSON.PluginTypes.User
} else return p
})
plugins = _.uniqWith(plugins, (a, b) => a.name === b.name || (a.type === 'link' && b.type === 'link' && a.root === b.root))
plugins = uniqWith(plugins, (a, b) => a.name === b.name || (a.type === 'link' && b.type === 'link' && a.root === b.root))
return plugins
}
}
34 changes: 34 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function sortBy<T>(arr: T[], fn: (i: T) => sortBy.Types | sortBy.Types[]): T[] {
function compare(a: sortBy.Types | sortBy.Types[], b: sortBy.Types | sortBy.Types[]): number {
a = a === undefined ? 0 : a
b = b === undefined ? 0 : b

if (Array.isArray(a) && Array.isArray(b)) {
if (a.length === 0 && b.length === 0) return 0
let diff = compare(a[0], b[0])
if (diff !== 0) return diff
return compare(a.slice(1), b.slice(1))
}

if (a < b) return -1
if (a > b) return 1
return 0
}

return arr.sort((a, b) => compare(fn(a), fn(b)))
}
export namespace sortBy {
export type Types = string | number | undefined | boolean
}

export function uniq<T>(arr: T[]): T[] {
return arr.filter((a, i) => {
return !arr.find((b, j) => j !== i && b === a)
})
}

export function uniqWith<T>(arr: T[], fn: (a: T, b: T) => boolean): T[] {
return arr.filter((a, i) => {
return !arr.find((b, j) => j !== i && fn(a, b))
})
}
Loading

0 comments on commit 1d86582

Please sign in to comment.