UNPKG

3.57 kBJavaScriptView Raw
1const path = require("path");
2const { exec } = require("child_process");
3const fs = require("fs");
4
5const async = require("async");
6const colors = require("colors");
7const minimatch = require("minimatch");
8
9const Common = require("@truffle/compile-common");
10
11const compiler = {
12 name: "ligo",
13 version: "next"
14};
15
16const LIGO_PATTERN = "**/*.{ligo,mligo}";
17
18// -------- Pass Common helpers --------
19
20const compile = Object.assign({}, Common);
21
22// -------- Start of compile-ligo specific methods --------
23
24// Check that ligo is available
25function checkLigo(callback) {
26 exec(
27 "docker run --rm -i ligolang/ligo:next --help",
28 (err, stdout, stderr) => {
29 if (err)
30 return callback(`${colors.red("Error executing ligo:")}\n${stderr}`);
31
32 callback(null);
33 }
34 );
35}
36
37// Execute ligo for single source file
38function execLigo(sourcePath, entryPoint, callback) {
39 const command = `docker run -v $PWD:$PWD --rm -i ligolang/ligo:next compile-contract --michelson-format=json ${sourcePath} ${entryPoint}`;
40
41 exec(command, { maxBuffer: 600 * 1024 }, (err, stdout, stderr) => {
42 if (err)
43 return callback(
44 `${stderr}\n${colors.red(
45 `Compilation of ${sourcePath} failed. See above.`
46 )}`
47 );
48
49 const jsonContractOutput = stdout.trim();
50
51 callback(null, jsonContractOutput);
52 });
53}
54
55// compile all options.paths
56function compileAll(options, callback) {
57 const entryPoint = options._[0] || "main";
58 options.logger = options.logger || console;
59
60 compile.display(options.paths, options, entryPoint);
61
62 async.map(
63 options.paths,
64 (sourcePath, c) => {
65 execLigo(sourcePath, entryPoint, (err, compiledContract) => {
66 if (err) return c(err);
67
68 // remove extension from filename
69 const extension = path.extname(sourcePath);
70 const basename = path.basename(sourcePath, extension);
71
72 const contractName = basename;
73
74 const sourceBuffer = fs.readFileSync(sourcePath);
75 const sourceContents = sourceBuffer.toString();
76
77 const contractDefinition = {
78 contractName,
79 abi: [], // TEMP!
80 sourcePath,
81 source: sourceContents,
82 code: compiledContract,
83 compiler
84 };
85
86 c(null, contractDefinition);
87 });
88 },
89 (err, contracts) => {
90 if (err) return callback(err);
91
92 const result = contracts.reduce((result, contract) => {
93 result[contract.contractName] = contract;
94
95 return result;
96 }, {});
97
98 callback(null, result, options.paths, compiler);
99 }
100 );
101}
102
103// Check that ligo is available then forward to internal compile function
104function compileLigo(options, callback) {
105 // filter out non-ligo paths
106 options.paths = options.paths.filter(path => minimatch(path, LIGO_PATTERN));
107
108 // no ligo files found, no need to check ligo
109 if (options.paths.length === 0) return callback(null, {}, []);
110
111 checkLigo(err => {
112 if (err) return callback(err);
113
114 return compileAll(options, callback);
115 });
116}
117
118// wrapper for compile.all. only updates contracts_directory to find .ligo
119compileLigo.all = (options, callback) =>
120 compile.all(
121 compile,
122 compile.updateContractsDirectory(options, LIGO_PATTERN),
123 callback
124 );
125
126// wrapper for compile.necessary. only updates contracts_directory to find .ligo
127compileLigo.necessary = (options, callback) =>
128 compile.necessary(
129 compile,
130 compile.updateContractsDirectory(options, LIGO_PATTERN),
131 callback
132 );
133
134compile.with_dependencies = compileLigo;
135module.exports = compileLigo;