file.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __generator = (this && this.__generator) || function (thisArg, body) {
  35. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  36. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  37. function verb(n) { return function (v) { return step([n, v]); }; }
  38. function step(op) {
  39. if (f) throw new TypeError("Generator is already executing.");
  40. while (_) try {
  41. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  42. if (y = 0, t) op = [op[0] & 2, t.value];
  43. switch (op[0]) {
  44. case 0: case 1: t = op; break;
  45. case 4: _.label++; return { value: op[1], done: false };
  46. case 5: _.label++; y = op[1]; op = [0]; continue;
  47. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  48. default:
  49. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  50. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  51. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  52. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  53. if (t[2]) _.ops.pop();
  54. _.trys.pop(); continue;
  55. }
  56. op = body.call(thisArg, _);
  57. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  58. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  59. }
  60. };
  61. var __importDefault = (this && this.__importDefault) || function (mod) {
  62. return (mod && mod.__esModule) ? mod : { "default": mod };
  63. };
  64. exports.__esModule = true;
  65. exports.exportFile = exports.loadJSON = void 0;
  66. var fs = __importStar(require("node:fs/promises"));
  67. var node_path_1 = __importDefault(require("node:path"));
  68. /**
  69. * Load file
  70. * @param path
  71. * @param throwError
  72. * @returns
  73. */
  74. var loadJSON = function (path, throwError) {
  75. if (throwError === void 0) { throwError = true; }
  76. return __awaiter(void 0, void 0, void 0, function () {
  77. var stat, jsonString;
  78. return __generator(this, function (_a) {
  79. switch (_a.label) {
  80. case 0: return [4 /*yield*/, fs.stat(path)
  81. // Return undefined or throw error
  82. ];
  83. case 1:
  84. stat = _a.sent();
  85. // Return undefined or throw error
  86. if (!stat.isFile()) {
  87. // Handle error
  88. if (throwError) {
  89. throw new Error("".concat(path, " does not exist."));
  90. }
  91. return [2 /*return*/];
  92. }
  93. return [4 /*yield*/, fs.readFile(path, { encoding: 'utf-8' })];
  94. case 2:
  95. jsonString = _a.sent();
  96. return [2 /*return*/, JSON.parse(jsonString)];
  97. }
  98. });
  99. });
  100. };
  101. exports.loadJSON = loadJSON;
  102. /**
  103. * Export file
  104. * @param filePath
  105. * @param content
  106. * @returns
  107. */
  108. var exportFile = function (filePath, content) { return __awaiter(void 0, void 0, void 0, function () {
  109. var folder, stat;
  110. return __generator(this, function (_a) {
  111. switch (_a.label) {
  112. case 0:
  113. folder = node_path_1["default"].dirname(filePath);
  114. return [4 /*yield*/, fs.stat(folder)["catch"](function () { return ({
  115. isDirectory: function () { return false; }
  116. }); })
  117. // Directory
  118. ];
  119. case 1:
  120. stat = _a.sent();
  121. if (!!stat.isDirectory()) return [3 /*break*/, 3];
  122. return [4 /*yield*/, fs.mkdir(folder)["catch"](function () {
  123. return;
  124. })];
  125. case 2:
  126. _a.sent();
  127. _a.label = 3;
  128. case 3:
  129. // Write file
  130. return [2 /*return*/, fs.writeFile(filePath, content)];
  131. }
  132. });
  133. }); };
  134. exports.exportFile = exportFile;