Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: remove cli-ux and lodash from deps
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent 5ea1044 commit 7dbb4c9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 134 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/anycli/config/issues",
"dependencies": {
"cli-ux": "^3.3.13",
"debug": "^3.1.0",
"fs-extra": "^5.0.0",
"fs-extra-debug": "^1.0.4",
"globby": "^7.1.1",
"load-json-file": "^4.0.0",
"lodash": "^4.17.4",
"read-pkg": "^3.0.0"
},
"devDependencies": {
Expand All @@ -21,11 +19,9 @@
"@types/fs-extra": "^5.0.0",
"@types/globby": "^6.1.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",
"chai": "^4.1.2",
"concurrently": "^3.5.1",
Expand Down
8 changes: 4 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Parser from '@anycli/parser'
import * as _ from 'lodash'

import * as Config from '.'
import {mapValues} from './util'

export interface Command {
id: string
Expand Down Expand Up @@ -83,7 +83,7 @@ export namespace Command {
usage: c.usage,
hidden: c.hidden,
aliases: c.aliases || [],
flags: _.mapValues(c.flags || {}, (flag, name) => {
flags: mapValues(c.flags || {}, (flag, name) => {
if (flag.type === 'boolean') {
return {
name,
Expand All @@ -103,15 +103,15 @@ export namespace Command {
required: flag.required,
helpValue: flag.helpValue,
options: flag.options,
default: _.isFunction(flag.default) ? flag.default({options: {}, flags: {}}) : flag.default,
default: typeof flag.default === 'function' ? flag.default({options: {}, flags: {}}) : flag.default,
}
}),
args: c.args ? c.args.map(a => ({
name: a.name,
description: a.description,
required: a.required,
options: a.options,
default: _.isFunction(a.default) ? a.default({}) : a.default,
default: typeof a.default === 'function' ? a.default({}) : a.default,
hidden: a.hidden,
})) : {} as Command['args'],
}
Expand Down
5 changes: 2 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cli from 'cli-ux'
import * as os from 'os'
import * as path from 'path'
import * as readPkg from 'read-pkg'
Expand Down Expand Up @@ -133,7 +132,7 @@ export class Config extends Plugin.Plugin implements IConfig {
const devPlugins = this.pjson.anycli.devPlugins
if (devPlugins) this.loadPlugins(this.root, devPlugins)
} catch (err) {
cli.warn(err)
process.emitWarning(err)
}

try {
Expand All @@ -142,7 +141,7 @@ export class Config extends Plugin.Plugin implements IConfig {
if (!pjson.anycli) pjson.anycli = {schema: 1}
this.loadPlugins(userPJSONPath, pjson.anycli.plugins)
} catch (err) {
if (err.code !== 'ENOENT') cli.warn(err)
if (err.code !== 'ENOENT') process.emitWarning(err)
}

debug('config done')
Expand Down
19 changes: 10 additions & 9 deletions src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import cli from 'cli-ux'
import * as globby from 'globby'
import * as _ from 'lodash'
import * as Globby from 'globby'
import * as path from 'path'

import {Command} from './command'
Expand All @@ -16,29 +14,32 @@ export namespace Manifest {
export type FindCommandCB = (id: string) => Command.Class

export function build(version: string, dir: string, findCommand: FindCommandCB): Manifest {
const globby: typeof Globby = require('globby')
debug(`loading IDs from ${dir}`)
const ids = globby.sync(['**/*.+(js|ts)', '!**/*.+(d.ts|test.ts|test.js)'], {cwd: dir})
.map(file => {
const p = path.parse(file)
const topics = p.dir.split('/')
let command = p.name !== 'index' && p.name
return _([...topics, command]).compact().join(':')
return [...topics, command].filter(f => f).join(':')
})
debug('found ids', ids)
let commands = ids.map(id => {
try {
return [id, Command.toCached(findCommand(id))]
} catch (err) {
cli.warn(err)
process.emitWarning(err)
}
})

return {
version,
commands: _(commands)
.compact()
.fromPairs()
.value()
commands: commands
.filter((f): f is [string, Command] => !!f)
.reduce((commands, [id, c]) => {
commands[id] = c
return commands
}, {} as {[k: string]: Command})
}
}
}
27 changes: 13 additions & 14 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import cli from 'cli-ux'
import * as fs from 'fs-extra'
import * as loadJSON from 'load-json-file'
import * as _ from 'lodash'
import * as path from 'path'
import * as readPkg from 'read-pkg'
import {inspect} from 'util'
Expand All @@ -12,6 +10,7 @@ import {Manifest} from './manifest'
import {PJSON} from './pjson'
import {Topic} from './topic'
import {tsPath} from './ts_node'
import {flatMap, mapValues} from './util'

export interface Options {
root: string
Expand Down Expand Up @@ -118,7 +117,7 @@ export class Plugin implements IPlugin {
this.valid = this.pjson.anycli.schema === 1

this._topics = topicsToArray(this.pjson.anycli.topics || {})
this.hooks = _.mapValues(this.pjson.anycli.hooks || {}, _.castArray)
this.hooks = mapValues(this.pjson.anycli.hooks || {}, i => Array.isArray(i) ? i : [i])

this.manifest = this._manifest()
this.loadPlugins(this.root, this.pjson.anycli.plugins || [])
Expand Down Expand Up @@ -167,9 +166,9 @@ export class Plugin implements IPlugin {

_findCommand(id: string): Command.Class {
const search = (cmd: any) => {
if (_.isFunction(cmd.run)) return cmd
if (typeof cmd.run === 'function') return cmd
if (cmd.default && cmd.default.run) return cmd.default
return Object.values(cmd).find((cmd: any) => _.isFunction(cmd.run))
return Object.values(cmd).find((cmd: any) => typeof cmd.run === 'function')
}
const p = require.resolve(path.join(this.commandsDir!, ...id.split(':')))
debug('require', p)
Expand Down Expand Up @@ -198,15 +197,15 @@ export class Plugin implements IPlugin {
const p = tsPath(this.root, hook)
debug('hook', event, p)
const search = (m: any) => {
if (_.isFunction(m)) return m
if (m.default && _.isFunction(m.default)) return m.default
return Object.values(m).find((m: any) => _.isFunction(m))
if (typeof m === 'function') return m
if (m.default && typeof m.default === 'function') return m.default
return Object.values(m).find((m: any) => typeof m === 'function')
}

await search(require(p))(opts)
} catch (err) {
if (err.code === 'EEXIT') throw err
cli.warn(err)
process.emitWarning(err)
}
})
promises.push(...this.plugins.map(p => p.runHook(event, opts)))
Expand All @@ -225,13 +224,13 @@ export class Plugin implements IPlugin {
const p = path.join(this.root, '.anycli.manifest.json')
const manifest: Manifest = loadJSON.sync(p)
if (manifest.version !== this.version) {
cli.warn(`Mismatched version in ${this.name} plugin manifest. Expected: ${this.version} Received: ${manifest.version}`)
process.emitWarning(`Mismatched version in ${this.name} plugin manifest. Expected: ${this.version} Received: ${manifest.version}`)
} else {
debug('using manifest from', p)
return manifest
}
} catch (err) {
if (err.code !== 'ENOENT') cli.warn(err)
if (err.code !== 'ENOENT') process.emitWarning(err)
}
}
if (!this.ignoreManifest) {
Expand Down Expand Up @@ -259,7 +258,7 @@ export class Plugin implements IPlugin {
}
this.plugins.push(new Plugin(opts))
} catch (err) {
cli.warn(err)
process.emitWarning(err)
}
}
return plugins
Expand All @@ -270,9 +269,9 @@ function topicsToArray(input: any, base?: string): Topic[] {
if (!input) return []
base = base ? `${base}:` : ''
if (Array.isArray(input)) {
return input.concat(_.flatMap(input, t => topicsToArray(t.subtopics, `${base}${t.name}`)))
return input.concat(flatMap(input, t => topicsToArray(t.subtopics, `${base}${t.name}`)))
}
return _.flatMap(Object.keys(input), k => {
return flatMap(Object.keys(input), k => {
return [{...input[k], name: `${base}${k}`}].concat(topicsToArray(input[k].subtopics, `${base}${input[k].name}`))
})
}
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function flatMap<T, U>(arr: T[], fn: (i: T) => U[]): U[] {
return arr.reduce((arr, i) => arr.concat(fn(i)), [] as U[])
}

export function mapValues<T extends object, TResult>(obj: {[P in keyof T]: T[P]}, fn: (i: T[keyof T], k: keyof T) => TResult): {[P in keyof T]: TResult} {
return Object.entries(obj)
.reduce((o, [k, v]) => {
o[k] = fn(v, k as any)
return o
}, {} as any)
}
Loading

0 comments on commit 7dbb4c9

Please sign in to comment.