config-parser.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. exports.__esModule = true;
  62. exports.ConfigParser = void 0;
  63. var logger_js_1 = require("../logger.js");
  64. var defaults_js_1 = require("../utils/defaults.js");
  65. var path_js_1 = require("../utils/path.js");
  66. var merge_js_1 = require("../utils/merge.js");
  67. var file_js_1 = require("../utils/file.js");
  68. var ConfigParser = /** @class */ (function () {
  69. function ConfigParser() {
  70. }
  71. /**
  72. * Get runtime config
  73. * @param runtimePaths
  74. * @returns
  75. */
  76. ConfigParser.prototype.getRuntimeConfig = function (runtimePaths) {
  77. return __awaiter(this, void 0, void 0, function () {
  78. var exportMarkerConfig;
  79. return __generator(this, function (_a) {
  80. switch (_a.label) {
  81. case 0: return [4 /*yield*/, (0, file_js_1.loadJSON)(runtimePaths.EXPORT_MARKER, false)["catch"](function (err) {
  82. logger_js_1.Logger.noExportMarker();
  83. throw err;
  84. })];
  85. case 1:
  86. exportMarkerConfig = _a.sent();
  87. return [2 /*return*/, {
  88. trailingSlash: exportMarkerConfig === null || exportMarkerConfig === void 0 ? void 0 : exportMarkerConfig.exportTrailingSlash
  89. }];
  90. }
  91. });
  92. });
  93. };
  94. /**
  95. * Update existing config with runtime config
  96. * @param config
  97. * @param runtimePaths
  98. * @returns
  99. */
  100. ConfigParser.prototype.withRuntimeConfig = function (config, runtimePaths) {
  101. return __awaiter(this, void 0, void 0, function () {
  102. var runtimeConfig, trailingSlashConfig;
  103. return __generator(this, function (_a) {
  104. switch (_a.label) {
  105. case 0: return [4 /*yield*/, this.getRuntimeConfig(runtimePaths)
  106. // Prioritize `trailingSlash` value from `next-sitemap.js`
  107. ];
  108. case 1:
  109. runtimeConfig = _a.sent();
  110. trailingSlashConfig = {};
  111. if ('trailingSlash' in config) {
  112. trailingSlashConfig.trailingSlash = config === null || config === void 0 ? void 0 : config.trailingSlash;
  113. }
  114. return [2 /*return*/, (0, merge_js_1.overwriteMerge)(config, runtimeConfig, trailingSlashConfig)];
  115. }
  116. });
  117. });
  118. };
  119. /**
  120. * Load next-sitemap.config.js as module
  121. * @returns
  122. */
  123. ConfigParser.prototype.loadBaseConfig = function () {
  124. return __awaiter(this, void 0, void 0, function () {
  125. var path, baseConfig;
  126. return __generator(this, function (_a) {
  127. switch (_a.label) {
  128. case 0: return [4 /*yield*/, (0, path_js_1.getConfigFilePath)()
  129. // Config loading message
  130. ];
  131. case 1:
  132. path = _a.sent();
  133. // Config loading message
  134. logger_js_1.Logger.log('✨', "Loading next-sitemap config:", path);
  135. return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(path)); })];
  136. case 2:
  137. baseConfig = _a.sent();
  138. if (!baseConfig["default"]) {
  139. throw new Error('Unable to next-sitemap config file');
  140. }
  141. return [2 /*return*/, (0, defaults_js_1.withDefaultConfig)(baseConfig["default"])];
  142. }
  143. });
  144. });
  145. };
  146. /**
  147. * Load full config
  148. * @returns
  149. */
  150. ConfigParser.prototype.loadConfig = function () {
  151. return __awaiter(this, void 0, void 0, function () {
  152. var baseConfig, runtimePaths, config;
  153. return __generator(this, function (_a) {
  154. switch (_a.label) {
  155. case 0: return [4 /*yield*/, this.loadBaseConfig()
  156. // Find the runtime paths using base config
  157. ];
  158. case 1:
  159. baseConfig = _a.sent();
  160. runtimePaths = (0, path_js_1.getRuntimePaths)(baseConfig);
  161. return [4 /*yield*/, this.withRuntimeConfig(baseConfig, runtimePaths)
  162. // Return full result
  163. ];
  164. case 2:
  165. config = _a.sent();
  166. // Return full result
  167. return [2 /*return*/, {
  168. config: config,
  169. runtimePaths: runtimePaths
  170. }];
  171. }
  172. });
  173. });
  174. };
  175. return ConfigParser;
  176. }());
  177. exports.ConfigParser = ConfigParser;