mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-15 05:50:49 +00:00
* Copy getImport function * Fix manifest * Exit with 1 on errors * Regenerate Cargo.lock correctly
74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
const fs = require("fs");
|
|
const recast = require("recast");
|
|
const parser = require("@babel/parser");
|
|
const traverse = require("@babel/traverse").default;
|
|
|
|
const sourceFilePath = "../marine-js-pkg/marine_js.js";
|
|
const targetFilePath = "./src/marine_js.js";
|
|
|
|
fs.readFile(sourceFilePath, "utf8", (err, sourceData) => {
|
|
if (err) {
|
|
console.error("Error reading source file:", err);
|
|
process.exit(1);
|
|
}
|
|
|
|
const sourceAst = parser.parse(sourceData, { sourceType: "module" });
|
|
let sourceFunction = null;
|
|
|
|
traverse(sourceAst, {
|
|
FunctionDeclaration(path) {
|
|
if (path.node.id.name === "getImports") {
|
|
sourceFunction = path.node;
|
|
path.stop();
|
|
}
|
|
},
|
|
});
|
|
|
|
if (!sourceFunction) {
|
|
console.error("Error: getImports function not found in source file");
|
|
process.exit(1);
|
|
}
|
|
|
|
fs.readFile(targetFilePath, "utf8", (err, targetData) => {
|
|
if (err) {
|
|
console.error("Error reading target file:", err);
|
|
process.exit(1);
|
|
}
|
|
|
|
const targetAst = recast.parse(targetData, {
|
|
parser: {
|
|
parse: (source) => parser.parse(source, { sourceType: "module" }),
|
|
},
|
|
});
|
|
|
|
let targetFunctionPath = null;
|
|
|
|
recast.visit(targetAst, {
|
|
visitFunctionDeclaration(path) {
|
|
if (path.node.id.name === "getImports") {
|
|
targetFunctionPath = path;
|
|
return false;
|
|
}
|
|
this.traverse(path);
|
|
},
|
|
});
|
|
|
|
if (!targetFunctionPath) {
|
|
console.error("Error: getImports function not found in target file");
|
|
process.exit(1);
|
|
}
|
|
|
|
targetFunctionPath.replace(sourceFunction);
|
|
const output = recast.print(targetAst).code;
|
|
|
|
fs.writeFile(targetFilePath, output, "utf8", (err) => {
|
|
if (err) {
|
|
console.error("Error writing to target file:", err);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log("Function getImports replaced successfully in target file.");
|
|
});
|
|
});
|
|
});
|