diff --git a/src/config.ts b/src/config.ts index 0ac22687..93ce01f0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,12 +1,13 @@ import * as fs from 'fs-extra' import * as readJSON from 'load-json-file' +import * as _ from 'lodash' import * as os from 'os' import * as path from 'path' import * as readPkg from 'read-pkg' import {inspect} from 'util' import {IEngine} from './engine' -import {IPJSON, normalizePJSON} from './pjson' +import {IPJSON} from './pjson' const _pjson = require('../package.json') const _base = `${_pjson.name}@${_pjson.version}` @@ -50,6 +51,7 @@ export interface TSConfig { export interface ConfigOptions { name?: string root?: string + baseConfig?: IConfig } const debug = require('debug')('@dxcli/config') @@ -89,14 +91,16 @@ export class Config { this.windows = this.platform === 'win32' } - async load(root: string, pjson: readPkg.Package) { + async load(root: string, pjson: readPkg.Package, baseConfig?: IConfig) { + const base: IConfig = baseConfig || {} as any this.root = root - this.pjson = normalizePJSON(pjson) + this.pjson = pjson this.name = this.pjson.name this.version = this.pjson.version - this.bin = this.pjson.dxcli.bin - this.dirname = this.pjson.dxcli.dirname + if (!this.pjson.dxcli) this.pjson.dxcli = this.pjson.dxcli || this.pjson['cli-engine'] || {} + this.bin = this.pjson.dxcli.bin || base.bin || this.name + this.dirname = this.pjson.dxcli.dirname || base.dirname || this.name this.userAgent = `${this.name}/${this.version} (${this.platform}-${this.arch}) node-${process.version}` this.shell = this._shell() this.debug = this._debug() @@ -188,8 +192,9 @@ export class Config { } private async _hooks(): Promise<{[k: string]: string[]}> { - const promises = Object.entries(this.pjson.dxcli.hooks) - .map(([k, v]) => [k, v.map(this._libToSrcPath(v))] as [string, Promise[]]) + const promises = Object.entries(this.pjson.dxcli.hooks || {}) + .map(([k, v]) => [k, _.castArray(v)] as [string, string[]]) + .map(([k, v]) => [k, v.map(h => this._libToSrcPath(h))] as [string, Promise[]]) const hooks: {[k: string]: string[]} = {} for (let [k, v] of promises) { hooks[k] = await Promise.all(v) @@ -249,13 +254,13 @@ export function isIConfig(o: any): o is IConfig { return !!o._base } -export async function read({name, root = __dirname}: ConfigOptions): Promise { +export async function read({name, root = __dirname, baseConfig}: ConfigOptions): Promise { const pkgPath = await findPkg(name, root) if (!pkgPath) throw new Error(`could not find package.json with ${inspect({name, root})}`) debug('found package.json at %s from %s', pkgPath, root) const pkg = await readPkg(pkgPath) const config = new Config() - await config.load(path.dirname(pkgPath), pkg) + await config.load(path.dirname(pkgPath), pkg, baseConfig) return config } diff --git a/src/pjson.ts b/src/pjson.ts index 6e81af7b..27de1402 100644 --- a/src/pjson.ts +++ b/src/pjson.ts @@ -1,27 +1,6 @@ -import * as _ from 'lodash' import {Package} from 'read-pkg' -export interface IRawPJSON extends Package { - name: string - version: string - dxcli?: { - bin?: string - dirname?: string - commands?: string - hooks?: { [name: string]: string | string[] } - npmRegistry?: string - plugins?: string[] | string - topics?: { - [k: string]: { - description?: string - subtopics?: IPJSON['dxcli']['topics'] - hidden?: boolean - } - } - } -} - -export interface IPJSON extends IRawPJSON { +export interface IPJSON extends Package { name: string version: string dxcli: { @@ -40,17 +19,3 @@ export interface IPJSON extends IRawPJSON { } } } - -export function normalizePJSON(input: IRawPJSON): IPJSON { - const dxcli: IRawPJSON['dxcli'] = {...(input.dxcli! || input['cli-engine'])} - return { - ...input, - dxcli: { - topics: {}, - bin: input.name, - dirname: dxcli.bin || input.name, - ...dxcli, - hooks: _.mapValues(dxcli.hooks, _.castArray), - }, - } -}