Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

64 lines
1.6 KiB

'use strict';
const path = require('path');
const findUp = require('find-up');
const loadJsonFile = require('load-json-file');
const filepaths = new WeakMap();
const filepath = conf => filepaths.get(conf);
const findNextCwd = pkgPath => path.resolve(path.dirname(pkgPath), '..');
const addFp = (obj, fp) => {
filepaths.set(obj, fp);
return obj;
};
const pkgConf = (namespace, opts) => {
if (!namespace) {
return Promise.reject(new TypeError('Expected a namespace'));
}
opts = opts || {};
return findUp('package.json', opts.cwd ? {cwd: opts.cwd} : {})
.then(fp => {
if (!fp) {
return addFp(Object.assign({}, opts.defaults), fp);
}
return loadJsonFile(fp).then(pkg => {
if (opts.skipOnFalse && pkg[namespace] === false) {
const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)});
return pkgConf(namespace, newOpts);
}
return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp);
});
});
};
const sync = (namespace, opts) => {
if (!namespace) {
throw new TypeError('Expected a namespace');
}
opts = opts || {};
const fp = findUp.sync('package.json', opts.cwd ? {cwd: opts.cwd} : {});
if (!fp) {
return addFp(Object.assign({}, opts.defaults), fp);
}
const pkg = loadJsonFile.sync(fp);
if (opts.skipOnFalse && pkg[namespace] === false) {
const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)});
return sync(namespace, newOpts);
}
return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp);
};
module.exports = pkgConf;
module.exports.filepath = filepath;
module.exports.sync = sync;