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

  1. 'use strict';
  2. const path = require('path');
  3. const findUp = require('find-up');
  4. const loadJsonFile = require('load-json-file');
  5. const filepaths = new WeakMap();
  6. const filepath = conf => filepaths.get(conf);
  7. const findNextCwd = pkgPath => path.resolve(path.dirname(pkgPath), '..');
  8. const addFp = (obj, fp) => {
  9. filepaths.set(obj, fp);
  10. return obj;
  11. };
  12. const pkgConf = (namespace, opts) => {
  13. if (!namespace) {
  14. return Promise.reject(new TypeError('Expected a namespace'));
  15. }
  16. opts = opts || {};
  17. return findUp('package.json', opts.cwd ? {cwd: opts.cwd} : {})
  18. .then(fp => {
  19. if (!fp) {
  20. return addFp(Object.assign({}, opts.defaults), fp);
  21. }
  22. return loadJsonFile(fp).then(pkg => {
  23. if (opts.skipOnFalse && pkg[namespace] === false) {
  24. const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)});
  25. return pkgConf(namespace, newOpts);
  26. }
  27. return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp);
  28. });
  29. });
  30. };
  31. const sync = (namespace, opts) => {
  32. if (!namespace) {
  33. throw new TypeError('Expected a namespace');
  34. }
  35. opts = opts || {};
  36. const fp = findUp.sync('package.json', opts.cwd ? {cwd: opts.cwd} : {});
  37. if (!fp) {
  38. return addFp(Object.assign({}, opts.defaults), fp);
  39. }
  40. const pkg = loadJsonFile.sync(fp);
  41. if (opts.skipOnFalse && pkg[namespace] === false) {
  42. const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)});
  43. return sync(namespace, newOpts);
  44. }
  45. return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp);
  46. };
  47. module.exports = pkgConf;
  48. module.exports.filepath = filepath;
  49. module.exports.sync = sync;