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

Commit

Permalink
fix: make dependencies optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent f5147eb commit 177bf45
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 166 deletions.
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,20 @@
"bugs": "https://github.com/anycli/config/issues",
"dependencies": {
"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",
"read-pkg": "^3.0.0"
"globby": "^7.1.1"
},
"devDependencies": {
"@anycli/parser": "^3.2.2",
"@anycli/tslint": "^0.2.5",
"@types/chai": "^4.1.2",
"@types/fs-extra": "^5.0.0",
"@types/globby": "^6.1.0",
"@types/load-json-file": "^2.0.7",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/read-pkg": "^3.0.0",
"chai": "^4.1.2",
"concurrently": "^3.5.1",
"eslint": "^4.17.0",
"eslint-config-anycli": "^1.3.2",
"fancy-test": "^0.6.10",
"fancy-test": "^1.0.1",
"mocha": "^5.0.0",
"ts-node": "^4.1.0",
"typescript": "^2.7.1"
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as os from 'os'
import * as path from 'path'
import * as readPkg from 'read-pkg'

import {Hooks} from './hooks'
import {PJSON} from './pjson'
import * as Plugin from './plugin'
import {loadJSONSync} from './util'

export type PlatformTypes = 'darwin' | 'linux' | 'win32' | 'aix' | 'freebsd' | 'openbsd' | 'sunos'
export type ArchTypes = 'arm' | 'arm64' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x32' | 'x64' | 'x86'
Expand Down Expand Up @@ -137,7 +137,7 @@ export class Config extends Plugin.Plugin implements IConfig {

try {
const userPJSONPath = path.join(this.dataDir, 'package.json')
const pjson = this.userPJSON = readPkg.sync(userPJSONPath) as any
const pjson = this.userPJSON = loadJSONSync(userPJSONPath)
if (!pjson.anycli) pjson.anycli = {schema: 1}
this.loadPlugins(userPJSONPath, pjson.anycli.plugins)
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'fs-extra-debug'
// tslint:disable no-implicit-dependencies
try { require('fs-extra-debug') } catch {}

export {IConfig, Config, Options, load} from './config'
export {Command} from './command'
Expand Down
5 changes: 2 additions & 3 deletions src/pjson.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {Package} from 'read-pkg'

export interface PJSON {
[k: string]: any
dependencies?: {[name: string]: string}
anycli: {
schema?: number
}
}

export namespace PJSON {
export interface Plugin extends PJSON, Package {
export interface Plugin extends PJSON {
name: string
version: string
anycli: PJSON['anycli'] & {
Expand Down
12 changes: 5 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as fs from 'fs-extra'
import * as loadJSON from 'load-json-file'
import * as fs from 'fs'
import * as path from 'path'
import * as readPkg from 'read-pkg'
import {inspect} from 'util'

import {Command} from './command'
Expand All @@ -10,7 +8,7 @@ import {Manifest} from './manifest'
import {PJSON} from './pjson'
import {Topic} from './topic'
import {tsPath} from './ts_node'
import {flatMap, mapValues} from './util'
import {flatMap, loadJSONSync, mapValues} from './util'

export interface Options {
root: string
Expand Down Expand Up @@ -108,7 +106,7 @@ export class Plugin implements IPlugin {
Plugin.loadedPlugins[root] = this
this.root = root
debug('reading plugin %s', root)
this.pjson = readPkg.sync(path.join(root, 'package.json')) as any
this.pjson = loadJSONSync(path.join(root, 'package.json')) as any
this.name = this.pjson.name
this.version = this.pjson.version
if (!this.pjson.anycli) {
Expand Down Expand Up @@ -222,7 +220,7 @@ export class Plugin implements IPlugin {
const readManifest = () => {
try {
const p = path.join(this.root, '.anycli.manifest.json')
const manifest: Manifest = loadJSON.sync(p)
const manifest: Manifest = loadJSONSync(p)
if (manifest.version !== this.version) {
process.emitWarning(`Mismatched version in ${this.name} plugin manifest. Expected: ${this.version} Received: ${manifest.version}`)
} else {
Expand Down Expand Up @@ -299,6 +297,6 @@ function findRoot(name: string | undefined, root: string) {
} else {
cur = path.join(next, 'package.json')
}
if (fs.pathExistsSync(cur)) return path.dirname(cur)
if (fs.existsSync(cur)) return path.dirname(cur)
}
}
9 changes: 5 additions & 4 deletions src/ts_node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'fs-extra'
import * as loadJSON from 'load-json-file'
import * as fs from 'fs'
import * as path from 'path'
import * as TSNode from 'ts-node'

import {loadJSONSync} from './util'

const tsconfigs: {[root: string]: TSConfig} = {}
const rootDirs: string[] = []
const typeRoots = [`${__dirname}/../node_modules/@types`]
Expand Down Expand Up @@ -49,7 +50,7 @@ function loadTSConfig(root: string): TSConfig | undefined {
// if (!await fs.pathExists(path.join(this.root, '.git'))) return

const tsconfigPath = path.join(root, 'tsconfig.json')
const tsconfig = loadJSON.sync(tsconfigPath)
const tsconfig = loadJSONSync(tsconfigPath)
if (!tsconfig || !tsconfig.compilerOptions) return
return tsconfig
} catch (err) {
Expand Down Expand Up @@ -84,7 +85,7 @@ export function tsPath(root: string, orig: string | undefined): string | undefin
// For hooks, it might point to a module, not a file. Something like "./hooks/myhook"
// That file doesn't exist, and the real file is "./hooks/myhook.ts"
// In that case we attempt to resolve to the filename. If it fails it will revert back to the lib path
if (fs.pathExistsSync(out) || fs.pathExistsSync(out + '.ts')) return out
if (fs.existsSync(out) || fs.existsSync(out + '.ts')) return out
else return orig
} catch (err) {
debug(err)
Expand Down
10 changes: 10 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// tslint:disable no-implicit-dependencies
import * as fs from 'fs'

export function flatMap<T, U>(arr: T[], fn: (i: T) => U[]): U[] {
return arr.reduce((arr, i) => arr.concat(fn(i)), [] as U[])
}
Expand All @@ -9,3 +12,10 @@ export function mapValues<T extends object, TResult>(obj: {[P in keyof T]: T[P]}
return o
}, {} as any)
}

export function loadJSONSync(path: string): any {
let loadJSON
try { loadJSON = require('load-json-file') } catch {}
if (loadJSON) return loadJSON.sync(path)
return JSON.parse(fs.readFileSync(path, 'utf8'))
}
3 changes: 1 addition & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, fancy as base, FancyTypes, NockScope} from 'fancy-test'
import {expect, fancy as base, FancyTypes} from 'fancy-test'

import * as Config from '../src'

Expand All @@ -13,5 +13,4 @@ export const fancy = base
export {
expect,
FancyTypes,
NockScope,
}
Loading

0 comments on commit 177bf45

Please sign in to comment.