版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

index.cjs 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const node_fs = require('node:fs');
  3. const node_path = require('node:path');
  4. const node_os = require('node:os');
  5. const destr = require('destr');
  6. const flat = require('flat');
  7. const defu = require('defu');
  8. const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;
  9. const RE_LINES = /\n|\r|\r\n/;
  10. const defaults = {
  11. name: ".conf",
  12. dir: process.cwd(),
  13. flat: false
  14. };
  15. function withDefaults(options) {
  16. if (typeof options === "string") {
  17. options = { name: options };
  18. }
  19. return { ...defaults, ...options };
  20. }
  21. function parse(contents, options = {}) {
  22. const config = {};
  23. const lines = contents.split(RE_LINES);
  24. for (const line of lines) {
  25. const match = line.match(RE_KEY_VAL);
  26. if (!match) {
  27. continue;
  28. }
  29. const key = match[1];
  30. if (!key || key === "__proto__" || key === "constructor") {
  31. continue;
  32. }
  33. const value = destr(
  34. match[2].trim()
  35. /* val */
  36. );
  37. if (key.endsWith("[]")) {
  38. const nkey = key.slice(0, Math.max(0, key.length - 2));
  39. config[nkey] = (config[nkey] || []).concat(value);
  40. continue;
  41. }
  42. config[key] = value;
  43. }
  44. return options.flat ? config : flat.unflatten(config, { overwrite: true });
  45. }
  46. function parseFile(path, options) {
  47. if (!node_fs.existsSync(path)) {
  48. return {};
  49. }
  50. return parse(node_fs.readFileSync(path, "utf8"), options);
  51. }
  52. function read(options) {
  53. options = withDefaults(options);
  54. return parseFile(node_path.resolve(options.dir, options.name), options);
  55. }
  56. function readUser(options) {
  57. options = withDefaults(options);
  58. options.dir = process.env.XDG_CONFIG_HOME || node_os.homedir();
  59. return read(options);
  60. }
  61. function serialize(config) {
  62. return Object.entries(flat.flatten(config)).map(
  63. ([key, value]) => `${key}=${typeof value === "string" ? value : JSON.stringify(value)}`
  64. ).join("\n");
  65. }
  66. function write(config, options) {
  67. options = withDefaults(options);
  68. node_fs.writeFileSync(node_path.resolve(options.dir, options.name), serialize(config), {
  69. encoding: "utf8"
  70. });
  71. }
  72. function writeUser(config, options) {
  73. options = withDefaults(options);
  74. options.dir = process.env.XDG_CONFIG_HOME || node_os.homedir();
  75. write(config, options);
  76. }
  77. function update(config, options) {
  78. options = withDefaults(options);
  79. if (!options.flat) {
  80. config = flat.unflatten(config, { overwrite: true });
  81. }
  82. const newConfig = defu.defu(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 || node_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;