index.cjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const fs = require('fs');
  4. const path = require('path');
  5. const os = require('os');
  6. const destr = require('destr');
  7. const flat = require('flat');
  8. const defu = require('defu');
  9. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
  10. const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr);
  11. const flat__default = /*#__PURE__*/_interopDefaultLegacy(flat);
  12. const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
  13. const RE_KEY_VAL = /^\s*([^=\s]+)\s*=\s*(.*)?\s*$/;
  14. const RE_LINES = /\n|\r|\r\n/;
  15. const defaults = {
  16. name: ".conf",
  17. dir: process.cwd(),
  18. flat: false
  19. };
  20. function withDefaults(options) {
  21. if (typeof options === "string") {
  22. options = { name: options };
  23. }
  24. return { ...defaults, ...options };
  25. }
  26. function parse(contents, options = {}) {
  27. const config = {};
  28. const lines = contents.split(RE_LINES);
  29. for (const line of lines) {
  30. const match = line.match(RE_KEY_VAL);
  31. if (!match) {
  32. continue;
  33. }
  34. const key = match[1];
  35. if (!key || key === "__proto__" || key === "constructor") {
  36. continue;
  37. }
  38. const val = destr__default(match[2].trim());
  39. if (key.endsWith("[]")) {
  40. const nkey = key.substr(0, key.length - 2);
  41. config[nkey] = (config[nkey] || []).concat(val);
  42. continue;
  43. }
  44. config[key] = val;
  45. }
  46. return options.flat ? config : flat__default.unflatten(config, { overwrite: true });
  47. }
  48. function parseFile(path, options) {
  49. if (!fs.existsSync(path)) {
  50. return {};
  51. }
  52. return parse(fs.readFileSync(path, "utf-8"), options);
  53. }
  54. function read(options) {
  55. options = withDefaults(options);
  56. return parseFile(path.resolve(options.dir, options.name), options);
  57. }
  58. function readUser(options) {
  59. options = withDefaults(options);
  60. options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
  61. return read(options);
  62. }
  63. function serialize(config) {
  64. return Object.entries(flat__default.flatten(config)).map(([key, val]) => `${key}=${typeof val === "string" ? val : JSON.stringify(val)}`).join("\n");
  65. }
  66. function write(config, options) {
  67. options = withDefaults(options);
  68. fs.writeFileSync(path.resolve(options.dir, options.name), serialize(config), {
  69. encoding: "utf-8"
  70. });
  71. }
  72. function writeUser(config, options) {
  73. options = withDefaults(options);
  74. options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
  75. write(config, options);
  76. }
  77. function update(config, options) {
  78. options = withDefaults(options);
  79. if (!options.flat) {
  80. config = flat__default.unflatten(config, { overwrite: true });
  81. }
  82. const newConfig = defu__default(config, read(options));
  83. write(newConfig, options);
  84. return newConfig;
  85. }
  86. function updateUser(config, options) {
  87. options = withDefaults(options);
  88. options.dir = process.env.XDG_CONFIG_HOME || os.homedir();
  89. return update(config, options);
  90. }
  91. exports.defaults = defaults;
  92. exports.parse = parse;
  93. exports.parseFile = parseFile;
  94. exports.read = read;
  95. exports.readUser = readUser;
  96. exports.serialize = serialize;
  97. exports.update = update;
  98. exports.updateUser = updateUser;
  99. exports.write = write;
  100. exports.writeUser = writeUser;