options.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getWriteFileOptions = exports.writeFileDefaults = exports.getRealpathOptsAndCb = exports.getRealpathOptions = exports.getStatOptsAndCb = exports.getStatOptions = exports.getAppendFileOptsAndCb = exports.getAppendFileOpts = exports.getOpendirOptsAndCb = exports.getOpendirOptions = exports.getReaddirOptsAndCb = exports.getReaddirOptions = exports.getReadFileOptions = exports.getRmOptsAndCb = exports.getRmdirOptions = exports.getDefaultOptsAndCb = exports.getDefaultOpts = exports.optsDefaults = exports.getMkdirOptions = void 0;
  4. exports.getOptions = getOptions;
  5. exports.optsGenerator = optsGenerator;
  6. exports.optsAndCbGenerator = optsAndCbGenerator;
  7. const constants_1 = require("./constants");
  8. const encoding_1 = require("../encoding");
  9. const util_1 = require("./util");
  10. const mkdirDefaults = {
  11. mode: 511 /* MODE.DIR */,
  12. recursive: false,
  13. };
  14. const getMkdirOptions = (options) => {
  15. if (typeof options === 'number')
  16. return Object.assign({}, mkdirDefaults, { mode: options });
  17. return Object.assign({}, mkdirDefaults, options);
  18. };
  19. exports.getMkdirOptions = getMkdirOptions;
  20. const ERRSTR_OPTS = tipeof => `Expected options to be either an object or a string, but got ${tipeof} instead`;
  21. function getOptions(defaults, options) {
  22. let opts;
  23. if (!options)
  24. return defaults;
  25. else {
  26. const tipeof = typeof options;
  27. switch (tipeof) {
  28. case 'string':
  29. opts = Object.assign({}, defaults, { encoding: options });
  30. break;
  31. case 'object':
  32. opts = Object.assign({}, defaults, options);
  33. break;
  34. default:
  35. throw TypeError(ERRSTR_OPTS(tipeof));
  36. }
  37. }
  38. if (opts.encoding !== 'buffer')
  39. (0, encoding_1.assertEncoding)(opts.encoding);
  40. return opts;
  41. }
  42. function optsGenerator(defaults) {
  43. return options => getOptions(defaults, options);
  44. }
  45. function optsAndCbGenerator(getOpts) {
  46. return (options, callback) => typeof options === 'function' ? [getOpts(), options] : [getOpts(options), (0, util_1.validateCallback)(callback)];
  47. }
  48. exports.optsDefaults = {
  49. encoding: 'utf8',
  50. };
  51. exports.getDefaultOpts = optsGenerator(exports.optsDefaults);
  52. exports.getDefaultOptsAndCb = optsAndCbGenerator(exports.getDefaultOpts);
  53. const rmdirDefaults = {
  54. recursive: false,
  55. };
  56. const getRmdirOptions = (options) => {
  57. return Object.assign({}, rmdirDefaults, options);
  58. };
  59. exports.getRmdirOptions = getRmdirOptions;
  60. const getRmOpts = optsGenerator(exports.optsDefaults);
  61. exports.getRmOptsAndCb = optsAndCbGenerator(getRmOpts);
  62. const readFileOptsDefaults = {
  63. flag: 'r',
  64. };
  65. exports.getReadFileOptions = optsGenerator(readFileOptsDefaults);
  66. const readdirDefaults = {
  67. encoding: 'utf8',
  68. recursive: false,
  69. withFileTypes: false,
  70. };
  71. exports.getReaddirOptions = optsGenerator(readdirDefaults);
  72. exports.getReaddirOptsAndCb = optsAndCbGenerator(exports.getReaddirOptions);
  73. const opendirDefaults = {
  74. encoding: 'utf8',
  75. bufferSize: 32,
  76. recursive: false,
  77. };
  78. exports.getOpendirOptions = optsGenerator(opendirDefaults);
  79. exports.getOpendirOptsAndCb = optsAndCbGenerator(exports.getOpendirOptions);
  80. const appendFileDefaults = {
  81. encoding: 'utf8',
  82. mode: 438 /* MODE.DEFAULT */,
  83. flag: constants_1.FLAGS[constants_1.FLAGS.a],
  84. };
  85. exports.getAppendFileOpts = optsGenerator(appendFileDefaults);
  86. exports.getAppendFileOptsAndCb = optsAndCbGenerator(exports.getAppendFileOpts);
  87. const statDefaults = {
  88. bigint: false,
  89. };
  90. const getStatOptions = (options = {}) => Object.assign({}, statDefaults, options);
  91. exports.getStatOptions = getStatOptions;
  92. const getStatOptsAndCb = (options, callback) => typeof options === 'function' ? [(0, exports.getStatOptions)(), options] : [(0, exports.getStatOptions)(options), (0, util_1.validateCallback)(callback)];
  93. exports.getStatOptsAndCb = getStatOptsAndCb;
  94. const realpathDefaults = exports.optsDefaults;
  95. exports.getRealpathOptions = optsGenerator(realpathDefaults);
  96. exports.getRealpathOptsAndCb = optsAndCbGenerator(exports.getRealpathOptions);
  97. exports.writeFileDefaults = {
  98. encoding: 'utf8',
  99. mode: 438 /* MODE.DEFAULT */,
  100. flag: constants_1.FLAGS[constants_1.FLAGS.w],
  101. };
  102. exports.getWriteFileOptions = optsGenerator(exports.writeFileDefaults);
  103. //# sourceMappingURL=options.js.map