无法使用path.resolve Node Js的编译脚本解析文件

时间:2018-10-29 17:22:59

标签: node.js solidity

当我运行一个编译脚本来创建用于固态部署的JSON时遇到问题。

基本上,当我尝试导入外部文件时,它们都必须位于同一文件夹中。如果我尝试在文件夹结构外部导入文件,则可以在Solidity文件中进行导入,但是solc.compile调用会出错。

let mut x = Level2 { val: Level1 { val: Base { val: "World".to_string(), }, }, };

代码如下:

errors:
   [ 'Leaderboard.sol:3:1: ParserError: Source "math/SafeMath.sol" not found: File not supplied initially.\nimport "../math/SafeMath.sol";\n^----------------------------^\n' ],
  sourceList: [ 'Leaderboard.sol', 'SafeMath.sol' ],
  sources: {} }

这当前有效,但是我想执行以下操作:

const path = require("path");
const solc = require("solc");
const fs = require("fs-extra");

const buildPath = path.resolve(__dirname, "build");

// Remove build folder.
fs.removeSync(buildPath);

const leaderboardPath = path.resolve(__dirname, "contracts", "leaderboard", "Leaderboard.sol");
const safeMathPath = path.resolve(__dirname, "contracts", "leaderboard", "SafeMath.sol");
const input = {
  sources: {
    "Leaderboard.sol": fs.readFileSync(leaderboardPath, "utf8"),
    "SafeMath.sol": fs.readFileSync(safeMathPath, "utf8")
  }
}

const output = solc.compile(input, 1);
console.log('o', output);
fs.ensureDirSync(buildPath);

for (let contract in output) {
  const filename = contract.split(".")[0];
  fs.outputJsonSync(
    path.resolve(buildPath, `${filename}.json`),
    output[contract]
  );
}

console.log('compile successful!');

如何正确地将这些单独的文件合并在一起,而不必将它们全部放在同一文件夹中?

0 个答案:

没有答案