generateNodeIndex.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * This script generates the exports functionality for the node API.
  3. *
  4. * it exports chef as default, but all the wrapped operations as
  5. * other top level exports.
  6. *
  7. * @author d98762656 [d98762625@gmail.com]
  8. * @copyright Crown Copyright 2018
  9. * @license Apache-2.0
  10. */
  11. /*eslint no-console: 0 */
  12. import fs from "fs";
  13. import path from "path";
  14. import * as operations from "../../../core/operations/index";
  15. import { decapitalise } from "../../apiUtils";
  16. import excludedOperations from "../excludedOperations";
  17. const includedOperations = Object.keys(operations).filter((op => excludedOperations.indexOf(op) === -1));
  18. const dir = path.join(`${process.cwd()}/src/node`);
  19. if (!fs.existsSync(dir)) {
  20. console.log("\nCWD: " + process.cwd());
  21. console.log("Error: generateNodeIndex.mjs should be run from the project root");
  22. console.log("Example> node --experimental-modules src/core/config/scripts/generateNodeIndex.mjs");
  23. process.exit(1);
  24. }
  25. let code = `/**
  26. * THIS FILE IS AUTOMATICALLY GENERATED BY src/node/config/scripts/generateNodeIndex.mjs
  27. *
  28. * @author d98762625 [d98762625@gmail.com]
  29. * @copyright Crown Copyright ${new Date().getUTCFullYear()}
  30. * @license Apache-2.0
  31. */
  32. /* eslint camelcase: 0 */
  33. import "babel-polyfill";
  34. import { wrap, help, bake } from "./api";
  35. import {
  36. `;
  37. includedOperations.forEach((op) => {
  38. // prepend with core_ to avoid name collision later.
  39. code += ` ${op} as core_${op},\n`;
  40. });
  41. code +=`
  42. } from "../core/operations/index";
  43. // Define global environment functions
  44. global.ENVIRONMENT_IS_WORKER = function() {
  45. return typeof importScripts === "function";
  46. };
  47. global.ENVIRONMENT_IS_NODE = function() {
  48. return typeof process === "object" && typeof require === "function";
  49. };
  50. global.ENVIRONMENT_IS_WEB = function() {
  51. return typeof window === "object";
  52. };
  53. /**
  54. * generateChef
  55. *
  56. * Creates decapitalised, wrapped ops in chef object for default export.
  57. */
  58. function generateChef() {
  59. return {
  60. `;
  61. includedOperations.forEach((op) => {
  62. code += ` "${decapitalise(op)}": wrap(core_${op}),\n`;
  63. });
  64. code += ` };
  65. }
  66. const chef = generateChef();
  67. chef.help = help;
  68. `;
  69. includedOperations.forEach((op) => {
  70. code += `const ${decapitalise(op)} = chef.${decapitalise(op)};\n`;
  71. });
  72. code +=`
  73. const operations = [\n`;
  74. includedOperations.forEach((op) => {
  75. code += ` ${decapitalise(op)},\n`;
  76. });
  77. code += `];
  78. chef.bake = bake(operations);
  79. export default chef;
  80. export {
  81. operations,
  82. `;
  83. includedOperations.forEach((op) => {
  84. code += ` ${decapitalise(op)},\n`;
  85. });
  86. code += "};\n";
  87. fs.writeFileSync(
  88. path.join(dir, "./index.mjs"),
  89. code
  90. );