From 11e409cfc44ebd1f5961633793da3d2fd83c6e6c Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Wed, 11 Apr 2018 14:08:08 -0300 Subject: [PATCH] feat(load-cfg): add script as package --- packages/load-cfg/package.json | 21 ++++++++++ packages/load-cfg/src/index.ts | 34 +++++++++++++++++ packages/load-cfg/tsconfig.json | 11 ++++++ packages/load-cfg/tslint.json | 3 ++ yarn.lock | 68 +-------------------------------- 5 files changed, 70 insertions(+), 67 deletions(-) create mode 100644 packages/load-cfg/package.json create mode 100644 packages/load-cfg/src/index.ts create mode 100644 packages/load-cfg/tsconfig.json create mode 100644 packages/load-cfg/tslint.json diff --git a/packages/load-cfg/package.json b/packages/load-cfg/package.json new file mode 100644 index 000000000..520a3a133 --- /dev/null +++ b/packages/load-cfg/package.json @@ -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" + } +} diff --git a/packages/load-cfg/src/index.ts b/packages/load-cfg/src/index.ts new file mode 100644 index 000000000..a6c6094e5 --- /dev/null +++ b/packages/load-cfg/src/index.ts @@ -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 +} diff --git a/packages/load-cfg/tsconfig.json b/packages/load-cfg/tsconfig.json new file mode 100644 index 000000000..92de631f6 --- /dev/null +++ b/packages/load-cfg/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "declaration": false, + "typeRoots": ["node_modules/@types"] + }, + "include": ["src/**/*"], + "exclude": ["node_modules/**"] +} diff --git a/packages/load-cfg/tslint.json b/packages/load-cfg/tslint.json new file mode 100644 index 000000000..0946f2096 --- /dev/null +++ b/packages/load-cfg/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/yarn.lock b/yarn.lock index e39f14efe..d40782058 100644 --- a/yarn.lock +++ b/yarn.lock @@ -722,12 +722,6 @@ dependencies: execa "^0.2.2" -"@types/accepts@*": - version "1.3.5" - resolved "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575" - dependencies: - "@types/node" "*" - "@types/acorn@^4.0.3": version "4.0.3" resolved "https://registry.npmjs.org/@types/acorn/-/acorn-4.0.3.tgz#d1f3e738dde52536f9aad3d3380d14e448820afd" @@ -757,25 +751,12 @@ "@types/express" "*" "@types/node" "*" -"@types/clean-css@*": - version "3.4.30" - resolved "https://registry.npmjs.org/@types/clean-css/-/clean-css-3.4.30.tgz#0052c136f5248002428e3638b37de4a39818641d" - "@types/connect@*": version "3.4.31" resolved "https://registry.npmjs.org/@types/connect/-/connect-3.4.31.tgz#1f92d6b117ecc05076c49ecd024f7976e528bad9" dependencies: "@types/node" "*" -"@types/cookies@*": - version "0.7.1" - resolved "https://registry.npmjs.org/@types/cookies/-/cookies-0.7.1.tgz#f9f204bd6767d389eea3b87609e30c090c77a540" - dependencies: - "@types/connect" "*" - "@types/express" "*" - "@types/keygrip" "*" - "@types/node" "*" - "@types/deepmerge@^2.1.0": version "2.1.0" resolved "https://registry.npmjs.org/@types/deepmerge/-/deepmerge-2.1.0.tgz#22f175e5cb55874fe818caa6fd50a1d98fc3d748" @@ -825,25 +806,6 @@ version "4.6.2" resolved "https://registry.npmjs.org/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0" -"@types/html-minifier@*": - version "3.5.2" - resolved "https://registry.npmjs.org/@types/html-minifier/-/html-minifier-3.5.2.tgz#f897a13d847a774e9b6fd91497e9b0e0ead71c35" - dependencies: - "@types/clean-css" "*" - "@types/relateurl" "*" - "@types/uglify-js" "*" - -"@types/html-webpack-plugin@^2.30.3": - version "2.30.3" - resolved "https://registry.npmjs.org/@types/html-webpack-plugin/-/html-webpack-plugin-2.30.3.tgz#848baba8e35010c6ef6d36b04174820b9d884583" - dependencies: - "@types/html-minifier" "*" - "@types/webpack" "*" - -"@types/http-assert@*": - version "1.2.2" - resolved "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.2.2.tgz#17dfe5a82184a8898935d96fe2eaedd37d22d9a4" - "@types/http-proxy-middleware@*": version "0.17.4" resolved "https://registry.npmjs.org/@types/http-proxy-middleware/-/http-proxy-middleware-0.17.4.tgz#788bd64270c8c1a0e67e6e7d8131dcf852fff3cb" @@ -859,26 +821,6 @@ "@types/events" "*" "@types/node" "*" -"@types/keygrip@*": - version "1.0.1" - resolved "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.1.tgz#ff540462d2fb4d0a88441ceaf27d287b01c3d878" - -"@types/koa-compose@*": - version "3.2.2" - resolved "https://registry.npmjs.org/@types/koa-compose/-/koa-compose-3.2.2.tgz#dc106e000bbf92a3ac900f756df47344887ee847" - -"@types/koa@^2.0.45": - version "2.0.45" - resolved "https://registry.npmjs.org/@types/koa/-/koa-2.0.45.tgz#133cbda6cc8d12b73434b5d9663898c833f80aa2" - dependencies: - "@types/accepts" "*" - "@types/cookies" "*" - "@types/events" "*" - "@types/http-assert" "*" - "@types/keygrip" "*" - "@types/koa-compose" "*" - "@types/node" "*" - "@types/mime@*": version "2.0.0" resolved "https://registry.npmjs.org/@types/mime/-/mime-2.0.0.tgz#5a7306e367c539b9f6543499de8dd519fac37a8b" @@ -897,7 +839,7 @@ version "9.6.0" resolved "https://registry.npmjs.org/@types/node/-/node-9.6.0.tgz#d3480ee666df9784b1001a1872a2f6ccefb6c2d7" -"@types/node@^9.6.4": +"@types/node@9.6.4", "@types/node@^9.6.4": version "9.6.4" resolved "https://registry.npmjs.org/@types/node/-/node-9.6.4.tgz#0ef7b4cfc3499881c81e0ea1ce61a23f6f4f5b42" @@ -933,10 +875,6 @@ dependencies: csstype "^2.0.0" -"@types/relateurl@*": - version "0.2.28" - resolved "https://registry.npmjs.org/@types/relateurl/-/relateurl-0.2.28.tgz#6bda7db8653fa62643f5ee69e9f69c11a392e3a6" - "@types/serve-static@*": version "1.13.1" resolved "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.1.tgz#1d2801fa635d274cd97d4ec07e26b21b44127492" @@ -954,10 +892,6 @@ version "1.0.0" resolved "https://registry.npmjs.org/@types/tapable/-/tapable-1.0.0.tgz#b76254453021be05681f6213416766bac9afb99c" -"@types/trash@^4.3.0": - version "4.3.0" - resolved "https://registry.npmjs.org/@types/trash/-/trash-4.3.0.tgz#ddc8f0265889d7147aeef5b65107cb65d91a26c1" - "@types/uglify-js@*": version "3.0.0" resolved "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.0.0.tgz#b765cf23abdc6ab2a67f2b72a9e94a1d43c6432e"