123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /**
- * This script generates the exports functionality for the node API.
- *
- * it exports chef as default, but all the wrapped operations as
- * other top level exports.
- *
- * @author d98762656 [d98762625@gmail.com]
- * @copyright Crown Copyright 2018
- * @license Apache-2.0
- */
- /*eslint no-console: 0 */
- import fs from "fs";
- import path from "path";
- import * as operations from "../../../core/operations/index";
- import { decapitalise } from "../../apiUtils";
- import excludedOperations from "../excludedOperations";
- const includedOperations = Object.keys(operations).filter((op => excludedOperations.indexOf(op) === -1));
- const dir = path.join(`${process.cwd()}/src/node`);
- if (!fs.existsSync(dir)) {
- console.log("\nCWD: " + process.cwd());
- console.log("Error: generateNodeIndex.mjs should be run from the project root");
- console.log("Example> node --experimental-modules src/core/config/scripts/generateNodeIndex.mjs");
- process.exit(1);
- }
- let code = `/**
- * THIS FILE IS AUTOMATICALLY GENERATED BY src/node/config/scripts/generateNodeIndex.mjs
- *
- * @author d98762625 [d98762625@gmail.com]
- * @copyright Crown Copyright ${new Date().getUTCFullYear()}
- * @license Apache-2.0
- */
- /* eslint camelcase: 0 */
- import "babel-polyfill";
- import { wrap, help, bake } from "./api";
- import {
- `;
- includedOperations.forEach((op) => {
- // prepend with core_ to avoid name collision later.
- code += ` ${op} as core_${op},\n`;
- });
- code +=`
- } from "../core/operations/index";
- // Define global environment functions
- global.ENVIRONMENT_IS_WORKER = function() {
- return typeof importScripts === "function";
- };
- global.ENVIRONMENT_IS_NODE = function() {
- return typeof process === "object" && typeof require === "function";
- };
- global.ENVIRONMENT_IS_WEB = function() {
- return typeof window === "object";
- };
- /**
- * generateChef
- *
- * Creates decapitalised, wrapped ops in chef object for default export.
- */
- function generateChef() {
- return {
- `;
- includedOperations.forEach((op) => {
- code += ` "${decapitalise(op)}": wrap(core_${op}),\n`;
- });
- code += ` };
- }
- const chef = generateChef();
- chef.help = help;
- `;
- includedOperations.forEach((op) => {
- code += `const ${decapitalise(op)} = chef.${decapitalise(op)};\n`;
- });
- code +=`
- const operations = [\n`;
- includedOperations.forEach((op) => {
- code += ` ${decapitalise(op)},\n`;
- });
- code += `];
- chef.bake = bake(operations);
- export default chef;
- export {
- operations,
- `;
- includedOperations.forEach((op) => {
- code += ` ${decapitalise(op)},\n`;
- });
- code += "};\n";
- fs.writeFileSync(
- path.join(dir, "./index.mjs"),
- code
- );
|