forked from NomicFoundation/truffle-flattener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·217 lines (172 loc) · 5.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#! /usr/bin/env node
const process = require("process");
const fs = require("fs");
const path = require("path");
const semver = require("semver");
const Config = require("truffle-config");
const Resolver = require("truffle-resolver");
const tsort = require("tsort");
const SolidityParser = require("solidity-parser");
const PRAGAMA_SOLIDITY_VERSION_REGEX = /^\s*pragma\ssolidity\s+(.*?)\s*;/;
const SUPPORTED_VERSION_DECLARATION_REGEX = /^\^?\d+(\.\d+){1,2}$/;
function unique(array) {
return [...new Set(array)];
}
function resolve(importPath) {
const config = Config.default();
const resolver = new Resolver(config);
return new Promise((resolve, reject) => {
resolver.resolve(importPath, (err, fileContents, filePath) => {
if (err) {
reject(err);
return;
}
resolve({ fileContents, filePath });
});
});
}
function getDependencies(filePath, fileContents) {
const dependencies = [];
let imports;
try {
imports = SolidityParser.parse(fileContents, "imports");
} catch (error) {
throw new Error(
"Could not parse " + filePath + " for extracting its imports."
);
}
for (let dependency of imports) {
if (dependency.startsWith("./") || dependency.startsWith("../")) {
dependency =
filePath.substring(0, filePath.lastIndexOf("/") + 1) + dependency;
dependency = path.normalize(dependency);
}
dependencies.push(dependency);
}
return dependencies;
}
async function dependenciesDfs(graph, visitedFiles, filePath) {
visitedFiles.push(filePath);
const resolved = await resolve(filePath);
const dependencies = getDependencies(
resolved.filePath,
resolved.fileContents
);
for (let dependency of dependencies) {
graph.add(dependency, filePath);
const resolvedDependency = await resolve(dependency);
if (!visitedFiles.includes(dependency)) {
await dependenciesDfs(graph, visitedFiles, dependency);
}
}
}
async function getSortedFilePaths(entryPoints) {
const graph = tsort();
const visitedFiles = [];
for (const entryPoint of entryPoints) {
await dependenciesDfs(graph, visitedFiles, entryPoint);
}
const topologicalSortedFiles = graph.sort();
// If an enrty has no dependency it won't be included in the graph, so we
// add them and then dedup the array
const withEntries = topologicalSortedFiles.concat(entryPoints);
const files = unique(withEntries);
return files;
}
async function printFileWithoutPragma(filePath) {
const resolved = await resolve(filePath);
const output = resolved.fileContents.replace(
PRAGAMA_SOLIDITY_VERSION_REGEX,
""
);
console.log(output.trim());
}
async function getFileCompilerVersionDeclaration(filePath) {
const resolved = await resolve(filePath);
const matched = resolved.fileContents.match(PRAGAMA_SOLIDITY_VERSION_REGEX);
if (matched === null) {
return undefined;
}
const version = matched[1];
if (!SUPPORTED_VERSION_DECLARATION_REGEX.test(version)) {
throw new Error(
"Unsupported compiler version declaration in " +
filePath +
": " +
version +
". Only pinned or ^ versions are supported."
);
}
return version;
}
async function normalizeCompilerVersionDeclarations(files) {
let pinnedVersion;
let pinnedVersionFile;
let maxCaretVersion;
let maxCaretVersionFile;
for (const file of files) {
const version = await getFileCompilerVersionDeclaration(file);
if (version === undefined) {
continue;
}
if (version.startsWith("^")) {
if (maxCaretVersion == undefined) {
maxCaretVersion = version;
maxCaretVersionFile = file;
} else {
if (semver.gt(version.substr(1), maxCaretVersion.substr(1))) {
maxCaretVersion = version;
maxCaretVersionFile = file;
}
}
} else {
if (pinnedVersion === undefined) {
pinnedVersion = version;
pinnedVersionFile = file;
} else if (pinnedVersion !== version) {
throw new Error(
"Differernt pinned compiler versions in " +
pinnedVersionFile +
" and " +
file
);
}
}
if (maxCaretVersion !== undefined && pinnedVersion !== undefined) {
if (!semver.satisfies(pinnedVersion, maxCaretVersion)) {
throw new Error(
"Incompatible compiler version declarations in " +
maxCaretVersionFile +
" and " +
pinnedVersionFile
);
}
}
}
if (pinnedVersion !== undefined) {
return pinnedVersion;
}
return maxCaretVersion;
}
async function printContactenation(files) {
const version = await normalizeCompilerVersionDeclarations(files);
if (version) {
console.log("pragma solidity " + version + ";");
}
for (const file of files) {
console.log("\n// File: " + file + "\n");
await printFileWithoutPragma(file);
}
}
async function main(files) {
if (files.length == 0) {
console.error("Usage: truffle-flattener <files>");
}
try {
const sortedFiles = await getSortedFilePaths(files);
await printContactenation(sortedFiles);
} catch (error) {
console.log(error);
}
}
main(process.argv.slice(2));