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

Commit

Permalink
fix: read user plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 3, 2018
1 parent fa07c76 commit 39a534f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
24 changes: 22 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<void>
}
Expand All @@ -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'
Expand All @@ -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')
}

Expand Down
4 changes: 2 additions & 2 deletions src/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
8 changes: 4 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 39a534f

Please sign in to comment.