From 39a534fc1748f38ecc75dc28672174b3745911f5 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Sat, 3 Feb 2018 08:45:55 -0800 Subject: [PATCH] fix: read user plugins --- src/config.ts | 24 ++++++++++++++++++++++-- src/pjson.ts | 4 ++-- src/plugin.ts | 8 ++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/config.ts b/src/config.ts index 05357718..995e0630 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,9 @@ +import cli from 'cli-ux' import * as os from 'os' import * as path from 'path' +import * as readPkg from 'read-pkg' +import {PJSON} from './pjson' import * as Plugin from './plugin' export type PlatformTypes = 'darwin' | 'linux' | 'win32' | 'aix' | 'freebsd' | 'openbsd' | 'sunos' @@ -80,6 +83,7 @@ export interface IConfig extends Plugin.IPlugin { * npm registry to use for installing plugins */ npmRegistry: string + userPJSON?: PJSON runCommand(id: string, argv?: string[]): Promise } @@ -99,12 +103,11 @@ export class Config extends Plugin.Plugin implements IConfig { userAgent: string debug: number = 0 npmRegistry: string + userPJSON?: PJSON constructor(opts: Plugin.Options) { super(opts) - this.loadPlugins(true) - this.arch = (os.arch() === 'ia32' ? 'x86' : os.arch() as any) this.platform = os.platform() as any this.windows = this.platform === 'win32' @@ -121,6 +124,23 @@ export class Config extends Plugin.Plugin implements IConfig { this.errlog = path.join(this.cacheDir, 'error.log') this.npmRegistry = this.scopedEnvVar('NPM_REGISTRY') || this.pjson.anycli.npmRegistry || 'https://registry.yarnpkg.com' + + try { + const devPlugins = this.pjson.anycli.devPlugins + if (devPlugins) this.loadPlugins(...devPlugins) + } catch (err) { + cli.warn(err) + } + + try { + const userPJSONPath = path.join(this.dataDir, 'package.json') + const pjson = this.userPJSON = readPkg.sync(userPJSONPath) as any + if (!pjson.anycli) pjson.anycli = {schema: 1} + this.loadPlugins(...pjson.anycli.plugins) + } catch (err) { + if (err.code !== 'ENOENT') cli.warn(err) + } + debug('config done') } diff --git a/src/pjson.ts b/src/pjson.ts index e8e7408f..eb917f36 100644 --- a/src/pjson.ts +++ b/src/pjson.ts @@ -10,12 +10,12 @@ export interface PJSON extends Package { pluginScope?: string dirname?: string commands?: string - hooks: { [name: string]: string[] } + hooks?: { [name: string]: string[] } plugins?: PJSON.Plugin[] devPlugins?: PJSON.Plugin[] title?: string description?: string - topics: { + topics?: { [k: string]: { description?: string subtopics?: PJSON['anycli']['topics'] diff --git a/src/plugin.ts b/src/plugin.ts index c5739526..4218f4a3 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -113,7 +113,7 @@ export class Plugin implements IPlugin { this.hooks = _.mapValues(this.pjson.anycli.hooks || {}, _.castArray) this.manifest = this._manifest() - this.loadPlugins() + this.loadPlugins(...this.pjson.anycli.plugins || []) } get commandsDir() { @@ -226,10 +226,10 @@ export class Plugin implements IPlugin { return {version: this.version, commands: {}} } - protected loadPlugins(dev = false) { - const plugins = this.pjson.anycli[dev ? 'devPlugins' : 'plugins'] + protected loadPlugins(...plugins: Config.PJSON.Plugin[]) { + if (!plugins.length) return if (!plugins || !plugins.length) return - debug(`loading ${dev ? 'dev ' : ''}plugins`, plugins) + debug('loading plugins', plugins) for (let plugin of plugins || []) { try { let opts: Options = {type: this.type, root: this.root}