Comparing version 0.8.0 to 0.9.0
@@ -46,3 +46,4 @@ "use strict"; | ||
name: "vendor", | ||
read: opts => pMap(glob(j(opts.appDir, "config.*")), readFile), | ||
dir: opts => opts.appDir, | ||
read: (_, dir) => pMap(glob(j(dir, "config.*")), readFile), | ||
}, | ||
@@ -53,3 +54,4 @@ | ||
name: "system", | ||
read: opts => pMap(glob(j("/etc", opts.appName, "config.*")), readFile), | ||
dir: opts => j("/etc", opts.appName), | ||
read: (_, dir) => pMap(glob(j(dir, "config.*")), readFile), | ||
}, | ||
@@ -60,8 +62,8 @@ | ||
name: "global", | ||
read: opts => { | ||
dir: opts => { | ||
const configDir = xdgBasedir.config; | ||
return configDir === undefined | ||
? [] | ||
: pMap(glob(j(configDir, opts.appName, "config.*")), readFile); | ||
return configDir && j(configDir, opts.appName); | ||
}, | ||
read: (_, dir) => | ||
dir === undefined ? [] : pMap(glob(j(dir, "config.*")), readFile), | ||
}, | ||
@@ -68,0 +70,0 @@ |
56
index.js
@@ -5,2 +5,3 @@ "use strict"; | ||
const chokidar = require("chokidar"); | ||
const dirname = require("path").dirname; | ||
@@ -65,7 +66,12 @@ const homedir = require("os").homedir; | ||
} | ||
return pMap(entries, entry => | ||
useWhitelist && !whitelist.has(entry.name) | ||
? [] | ||
: entry.read({ appDir, appName }) | ||
) | ||
const entryOpts = { appDir, appName }; | ||
return pMap(entries, entry => { | ||
if (useWhitelist && !whitelist.has(entry.name)) { | ||
return []; | ||
} | ||
const dirFn = entry.dir; | ||
const dir = typeof dirFn === "function" ? dirFn(entryOpts) : dirFn; | ||
return entry.read(entryOpts, dir); | ||
}) | ||
.then(files => { | ||
@@ -94,5 +100,43 @@ files = flatten(files); | ||
} | ||
exports.load = load; | ||
// =================================================================== | ||
exports.load = load; | ||
exports.watch = function watch({ appName, ...opts }, cb) { | ||
return new Promise((resolve, reject) => { | ||
const { appDir } = opts; | ||
if (appDir === undefined) { | ||
throw new TypeError("appDir must be defined"); | ||
} | ||
const dirs = []; | ||
const entryOpts = { appName, appDir }; | ||
entries.forEach(entry => { | ||
const dirFn = entry.dir; | ||
const dir = typeof dirFn === "function" ? dirFn(entryOpts) : dirFn; | ||
if (dir !== undefined) { | ||
dirs.push(dir); | ||
} | ||
}); | ||
const watcher = chokidar.watch(dirs, { | ||
depth: 0, | ||
ignoreInitial: true, | ||
ignorePermissionErrors: true, | ||
}); | ||
const loadWrapper = () => { | ||
load(appName, opts).then(config => cb(undefined, config), cb); | ||
}; | ||
watcher | ||
.on("all", loadWrapper) | ||
.once("error", reject) | ||
.once("ready", (...args) => { | ||
loadWrapper(); | ||
resolve(function unsubscribe() { | ||
return watcher.close(); | ||
}); | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "app-conf", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"license": "ISC", | ||
@@ -22,5 +22,6 @@ "author": "Julien Fontanet <julien.fontanet@isonoe.net> (http://julien.isonoe.net/)", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8.10" | ||
}, | ||
"dependencies": { | ||
"chokidar": "^3.5.1", | ||
"debug": "^4.1.0", | ||
@@ -30,3 +31,3 @@ "glob": "^7.1.3", | ||
"make-error": "^1.3.5", | ||
"promise-toolbox": "^0.14.0", | ||
"promise-toolbox": "^0.16.0", | ||
"xdg-basedir": "^3.0.0" | ||
@@ -33,0 +34,0 @@ }, |
@@ -54,2 +54,34 @@ # app-conf | ||
### `watch(opts, cb)` | ||
This method reload the configuration every time it might have changed. | ||
```js | ||
const watchConfig = require("app-conf").watch; | ||
const stopWatching = await watchConfig( | ||
{ | ||
// contrary to `load`, this option is required | ||
appDir: __dirname, | ||
// contrary to `load`, this is part of the options | ||
appName: "my-application", | ||
// all other options are passed to load() | ||
}, | ||
(error, config) => { | ||
if (error !== undefined) { | ||
console.warn("loading config has failed"); | ||
// we might not want to retry on changes | ||
stopWatching(); | ||
return; | ||
} | ||
console.log("config has been loaded", config); | ||
} | ||
); | ||
``` | ||
## Contributing | ||
@@ -56,0 +88,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14717
318
100
7
+ Addedchokidar@^3.5.1
+ Addedanymatch@3.1.3(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addednormalize-path@3.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpromise-toolbox@0.16.0(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
- Removedpromise-toolbox@0.14.0(transitive)
Updatedpromise-toolbox@^0.16.0