example-redirects.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function _interopRequireDefault(obj) {
  4. return obj && obj.__esModule ? obj : { default: obj };
  5. }
  6. function _nullishCoalesce(lhs, rhsFn) {
  7. if (lhs != null) {
  8. return lhs;
  9. } else {
  10. return rhsFn();
  11. }
  12. }
  13. function _optionalChain(ops) {
  14. let lastAccessLHS = undefined;
  15. let value = ops[0];
  16. let i = 1;
  17. while (i < ops.length) {
  18. const op = ops[i];
  19. const fn = ops[i + 1];
  20. i += 2;
  21. if (
  22. (op === "optionalAccess" || op === "optionalCall") &&
  23. value == null
  24. ) {
  25. return undefined;
  26. }
  27. if (op === "access" || op === "optionalAccess") {
  28. lastAccessLHS = value;
  29. value = fn(value);
  30. } else if (op === "call" || op === "optionalCall") {
  31. value = fn((...args) => value.call(lastAccessLHS, ...args));
  32. lastAccessLHS = undefined;
  33. }
  34. }
  35. return value;
  36. }
  37. var _fsextra = require("fs-extra");
  38. var _fsextra2 = _interopRequireDefault(_fsextra);
  39. function pluginExampleRedirectsPages() {
  40. return {
  41. name: "refine-plugin-handle-example-redirects",
  42. async postBuild() {
  43. const redirects = collectRedirects();
  44. const redirectFiles = generateRedirectFiles(redirects);
  45. // Write files only at the end: make code more easy to test without IO
  46. await Promise.all(
  47. redirectFiles.map((file) => writeRedirectFile(file)),
  48. );
  49. },
  50. };
  51. }
  52. exports.default = pluginExampleRedirectsPages;
  53. async function writeRedirectFile(file) {
  54. try {
  55. // User-friendly security to prevent file overrides
  56. if (await _fsextra2.default.pathExists(file.fileAbsolutePath)) {
  57. throw new Error(
  58. "The redirect plugin is not supposed to override existing files.",
  59. );
  60. }
  61. await _fsextra2.default.outputFile(
  62. file.fileAbsolutePath,
  63. file.fileContent,
  64. // Hard security to prevent file overrides
  65. // See https://stackoverflow.com/a/34187712/82609
  66. { flag: "wx" },
  67. );
  68. } catch (err) {
  69. // logger.error`Redirect file creation error for path=${file.fileAbsolutePath}.`;
  70. throw err;
  71. }
  72. }
  73. const htmlTemplate = (to) => `
  74. <!DOCTYPE html>
  75. <html>
  76. <head>
  77. <meta charset="UTF-8">
  78. </head>
  79. <script>
  80. window.location.href = '${to}';
  81. </script>
  82. </html>
  83. `;
  84. const collectRedirects = () => {
  85. const redirects = _fsextra2.default.readJSONSync(
  86. "./example-redirects.json",
  87. );
  88. return _nullishCoalesce(
  89. _optionalChain([redirects, "optionalAccess", (_) => _.redirects]),
  90. () => [],
  91. );
  92. };
  93. const generateRedirectFiles = (redirects) => {
  94. return redirects.map((redirect) => {
  95. const path = `${redirect.from}/index.html`;
  96. return {
  97. fileAbsolutePath: `./build/${path}`,
  98. fileContent: htmlTemplate(redirect.to),
  99. };
  100. });
  101. };