-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(load-cfg): add script as package
- Loading branch information
1 parent
d23f9cf
commit 11e409c
Showing
5 changed files
with
70 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "load-cfg", | ||
"version": "0.0.1", | ||
"main": "dist/index.js", | ||
"umd:main": "dist/index.umd.js", | ||
"module": "dist/index.m.js", | ||
"source": "src/index.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "libundler watch --ts", | ||
"build": "libundler build --ts --compress --sourcemap" | ||
}, | ||
"dependencies": { | ||
"find-up": "^2.1.0", | ||
"deepmerge": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/find-up": "^2.1.1", | ||
"@types/node": "9.6.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import findup from 'find-up' | ||
import merge from 'deepmerge' | ||
|
||
const finds = (name: string): string[] => [ | ||
`${name}.json`, | ||
`.${name}rc`, | ||
`${name}rc.js`, | ||
`${name}rc.json`, | ||
`${name}rc.yml`, | ||
`${name}rc.yaml`, | ||
`${name}.config.js`, | ||
`${name}.config.json`, | ||
] | ||
|
||
export const load = (name: string, defaultConfig: any = {}) => { | ||
let file | ||
const filepath = findup.sync(finds(name)) | ||
|
||
if (filepath) { | ||
try { | ||
const isJS = path.extname(filepath) === '.js' | ||
|
||
file = isJS | ||
? require(filepath) | ||
: JSON.parse(fs.readFileSync(filepath, 'utf-8')) | ||
} catch (err) { | ||
file = defaultConfig | ||
} | ||
} | ||
|
||
return defaultConfig !== null ? merge(defaultConfig, file) : file | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"declaration": false, | ||
"typeRoots": ["node_modules/@types"] | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tslint.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters