2018-05-24 17:02:54 +01:00
|
|
|
/**
|
|
|
|
* 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: ["off"] */
|
|
|
|
|
|
|
|
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import * as operations from "../../operations/index";
|
|
|
|
import { decapitalise } from "../../../node/apiUtils";
|
|
|
|
|
|
|
|
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/core/config/scripts/generateOpsIndex.mjs
|
|
|
|
*
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright ${new Date().getUTCFullYear()}
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2018-05-25 12:20:56 +01:00
|
|
|
import "babel-polyfill";
|
2018-05-24 17:02:54 +01:00
|
|
|
import { wrap } from "./apiUtils";
|
|
|
|
import {
|
|
|
|
`;
|
|
|
|
|
|
|
|
Object.keys(operations).forEach((op) => {
|
|
|
|
// prepend with core_ to avoid name collision later.
|
|
|
|
code += ` ${op} as core_${op},\n`;
|
|
|
|
});
|
|
|
|
|
|
|
|
code +=`
|
2018-05-25 12:20:56 +01:00
|
|
|
} from "../core/operations/index";
|
2018-05-24 17:02:54 +01:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
`;
|
|
|
|
|
|
|
|
Object.keys(operations).forEach((op) => {
|
2018-05-30 11:12:57 +01:00
|
|
|
code += ` '${decapitalise(op)}': wrap(core_${op}),\n`;
|
2018-05-24 17:02:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
code += ` };
|
|
|
|
}
|
|
|
|
|
|
|
|
const chef = generateChef();
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
Object.keys(operations).forEach((op) => {
|
2018-05-30 11:12:57 +01:00
|
|
|
code += `const ${decapitalise(op)} = chef['${decapitalise(op)}'];\n`;
|
2018-05-24 17:02:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
code +=`
|
|
|
|
export default chef;
|
|
|
|
export {
|
|
|
|
`;
|
|
|
|
|
|
|
|
Object.keys(operations).forEach((op) => {
|
|
|
|
code += ` ${decapitalise(op)},\n`;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
code += "};\n";
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(dir, "/index.mjs"),
|
|
|
|
code
|
|
|
|
);
|