create-tezos-smart-contract
Version:
Node.js toolset to write, test and deploy Tezos smart contracts
131 lines (130 loc) • 5.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compileWithLigo = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const console_1 = require("../../console");
const docker_1 = require("../docker");
const parameters_1 = require("./parameters");
const _compileFile = async (contractFileName, ligoVersion, bundle) => {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const ligoImage = `ligolang/ligo:${ligoVersion}`;
const image = await (0, docker_1.ensureImageIsPresent)(ligoImage);
if (!image) {
(0, console_1.error)('Unable to find LIGO image, compilation failed.');
return;
}
(0, console_1.log)(`🚀 Compiling contract "${contractFileName}"...`);
(0, console_1.debug)("\t👓 Reading source...");
const source = await bundle.readContract(contractFileName);
(0, console_1.debug)("\t\t✅ Done.");
if (source === "") {
(0, console_1.error)('The specified contract file is empty, skipping compilation.');
return;
}
const hash = bundle.generateHash(source);
const sourcePath = bundle.getContractFile(contractFileName);
if (bundle.buildFileExists(contractFileName)) {
const oldBuildFile = await bundle.readBuildFile(contractFileName);
if (oldBuildFile.sourcePath !== sourcePath) {
(0, console_1.error)(`There is a compiled version of a contract with the same name which code is located at:\n\n${oldBuildFile.sourcePath}`);
return;
}
if (oldBuildFile.hash === hash) {
(0, console_1.log)("\tNo changes were made to this contract, skipping compilation.");
return;
}
if ((0, parameters_1.isLigoVersionLT)(ligoVersion, oldBuildFile.compiler.version)) {
(0, console_1.error)(`You are trying to recompile a smart contract with an earlier version of LIGO. This is forbidden, but you can force this passing -f to compile command.`);
return;
}
}
const cwd = (0, console_1.getCWD)();
const built = {
contractName: contractFileName,
sourcePath: sourcePath,
hash: hash,
updatedAt: (new Date()).toISOString(),
compiler: {
name: "ligo",
version: ligoVersion,
},
michelson: "",
};
const mappedFolder = os_1.default.platform() === "win32" ? '/cd' : cwd;
/**
* Handle both old (0.24.0) and new (0.25.0) LIGO CLI interfaces
*/
const isOldCLI = (0, parameters_1.isLigoVersionLT)(ligoVersion, "0.25.0");
const args = isOldCLI ? [
"run",
"--rm",
"-v", `${cwd}:${mappedFolder}`,
"-w", `${mappedFolder}`,
ligoImage,
"compile-contract",
"--michelson-format=json",
`${sourcePath}`,
"main",
] : [
"run",
"--rm",
"-v", `${cwd}:${mappedFolder}`,
"-w", `${mappedFolder}`,
ligoImage,
"compile", "contract",
`${sourcePath}`,
"-e", "main",
"--michelson-format", "json",
];
(0, console_1.debug)(`\t🔥 Compiling with LIGO (${ligoVersion})...`);
(0, console_1.debug)(`\t Running LIGO compile command:\ndocker ${args.join(' ')}`);
const ligo = (0, child_process_1.spawn)("docker", args, {});
ligo.on("close", async () => {
(0, console_1.debug)("\t\t✅ Done.");
const outFile = bundle.getBuildFile(contractFileName);
(0, console_1.debug)(`\t📦 Writing output file "${path_1.default.relative(cwd, outFile)}"...`);
await bundle.writeBuildFile(contractFileName, built);
(0, console_1.debug)("\t\t✅ Done.");
(0, console_1.debug)("\t🥖 Contract compiled succesfully.");
resolve();
});
ligo.stdout.on("data", (data) => {
built.michelson += data.toString();
});
ligo.stderr.on("data", (data) => {
const message = data.toString();
const hasWarnings = (0, docker_1.handleDockerWarnings)(message);
if (hasWarnings) {
return;
}
(0, console_1.error)(message);
reject(ligo.stderr);
process.exit(1);
});
});
};
const compileWithLigo = async (bundle, options) => {
const config = bundle.config;
// Check the existence of build folder
if (!bundle.exists(config.outputDirectory)) {
(0, console_1.log)(`Creating output directory "${config.outputDirectory}" since it was not present.`);
await bundle.makeDir(config.outputDirectory);
}
// Either build all the contracts in config.contractsDirectory or the specified contract
if (options.contract) {
await _compileFile(options.contract, options.ligoVersion, bundle);
}
else {
const contracts = await bundle.getContractsFiles();
for (const contract of contracts) {
await _compileFile(contract, options.ligoVersion, bundle);
}
}
};
exports.compileWithLigo = compileWithLigo;