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

Commit

Permalink
fix: added baseConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 19, 2018
1 parent 893e552 commit 34da228
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
23 changes: 14 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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}`
Expand Down Expand Up @@ -50,6 +51,7 @@ export interface TSConfig {
export interface ConfigOptions {
name?: string
root?: string
baseConfig?: IConfig
}

const debug = require('debug')('@dxcli/config')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<string>[]])
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<string>[]])
const hooks: {[k: string]: string[]} = {}
for (let [k, v] of promises) {
hooks[k] = await Promise.all(v)
Expand Down Expand Up @@ -249,13 +254,13 @@ export function isIConfig(o: any): o is IConfig {
return !!o._base
}

export async function read({name, root = __dirname}: ConfigOptions): Promise<IConfig> {
export async function read({name, root = __dirname, baseConfig}: ConfigOptions): Promise<IConfig> {
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
}

Expand Down
37 changes: 1 addition & 36 deletions src/pjson.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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),
},
}
}

0 comments on commit 34da228

Please sign in to comment.