forked from NomicFoundation/truffle-flattener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·253 lines (208 loc) · 6.62 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#! /usr/bin/env node
"use strict";
const process = require("process");
const fs = require("fs");
const path = require("path");
const findUp = require("find-up");
const tsort = require("tsort");
const parser = require("@solidity-parser/parser");
const mkdirp = require("mkdirp");
const Resolver = require("@resolver-engine/imports-fs").ImportsFsEngine;
const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm;
function unique(array) {
return [...new Set(array)];
}
async function resolve(importPath) {
const resolver = Resolver();
const filePath = await resolver.resolve(importPath);
const fileContents = fs.readFileSync(filePath).toString();
return { fileContents, filePath };
}
function getDirPath(filePath) {
let index1 = filePath.lastIndexOf(path.sep);
let index2 = filePath.lastIndexOf("/");
return filePath.substring(0, Math.max(index1, index2));
}
function getDependencies(filePath, fileContents) {
try {
let ast = parser.parse(fileContents);
let imports = [];
parser.visit(ast, {
ImportDirective: function(node) {
imports.push(getNormalizedDependencyPath(node.path, filePath));
}
});
return imports;
} catch (error) {
throw new Error(
"Could not parse " + filePath + " for extracting its imports: " + error
);
}
}
function getNormalizedDependencyPath(dependency, filePath) {
if (dependency.startsWith("./") || dependency.startsWith("../")) {
dependency = path.join(getDirPath(filePath), dependency);
dependency = path.normalize(dependency);
}
return dependency.replace(/\\/g, "/");
}
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) {
if (!visitedFiles.includes(dependency)) {
graph.add(dependency, filePath);
await dependenciesDfs(graph, visitedFiles, dependency);
}
}
}
async function getSortedFilePaths(entryPoints, projectRoot) {
const graph = tsort();
const visitedFiles = [];
for (const entryPoint of entryPoints) {
await dependenciesDfs(graph, visitedFiles, entryPoint);
}
let topologicalSortedFiles;
try {
topologicalSortedFiles = graph.sort();
} catch (e) {
if (e.toString().includes("Error: There is a cycle in the graph.")) {
const message =
"There is a cycle in the dependency" +
" graph, can't compute topological ordering. Files:\n\t" +
visitedFiles.join("\n\t");
throw new Error(message);
}
}
// If an entry 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)
.map(f => fileNameToGlobalName(f, projectRoot));
const files = unique(withEntries);
return files;
}
async function fileContentWithoutImports(filePath) {
const resolved = await resolve(filePath);
const output = resolved.fileContents.replace(IMPORT_SOLIDITY_REGEX, "");
// normalize whitespace to a single trailing newline
return output.trim() + "\n";
}
function fileNameToGlobalName(fileName, projectRoot) {
let globalName = getFilePathsFromProjectRoot([fileName], projectRoot)[0];
if (globalName.indexOf("node_modules/") !== -1) {
globalName = globalName.substr(
globalName.indexOf("node_modules/") + "node_modules/".length
);
}
return globalName;
}
async function printContactenation(files, log) {
const parts = await Promise.all(
files.map(async file => {
return (
"// File: " + file + "\n\n" + (await fileContentWithoutImports(file))
);
})
);
// add a single empty line between parts
log(parts.join("\n"));
}
async function getProjectRoot() {
let configFilePath = await findUp([
"truffle.js",
"truffle-config.js",
"buidler.config.js",
"buidler.config.ts"
]);
if (!configFilePath) {
throw new Error(`
Truffle Flattener must be run inside a Truffle or Buidler project:
truffle.js, truffle-config.js, buidler.config.js, nor buidler.config.ts found
`);
}
return getDirPath(configFilePath);
}
function getFilePathsFromProjectRoot(filePaths, projectRoot) {
return filePaths.map(f => path.relative(projectRoot, path.resolve(f)));
}
async function flatten(filePaths, log, root) {
if (root && !fs.existsSync(root)) {
throw new Error("The specified root directory does not exist");
}
const projectRoot = root || (await getProjectRoot());
const filePathsFromProjectRoot = getFilePathsFromProjectRoot(
filePaths,
projectRoot
);
// TODO: Remove this WD manipulation.
// If this is used as a tool this is OK, but it's not right
// when used as a library.
const wd = process.cwd();
process.chdir(projectRoot);
const sortedFiles = await getSortedFilePaths(
filePathsFromProjectRoot,
projectRoot
);
await printContactenation(sortedFiles, log);
process.chdir(wd);
}
async function main(args) {
let filePaths = args;
let outputFileIndex = args.indexOf("--output");
let outputFilePath;
if (outputFileIndex >= 0) {
outputFilePath = args[outputFileIndex + 1];
if (!outputFilePath) {
console.warn(
"you havn't provided output file path, ignoring. Usage: truffle-flattener <files> --output <output file path>"
);
}
filePaths = args.filter(
(arg, index) => index !== outputFileIndex && index !== outputFileIndex + 1
);
if (outputFilePath) {
let outputDirPath = path.dirname(outputFilePath);
let isOutputDirExists =
fs.existsSync(outputDirPath) &&
fs.lstatSync(outputDirPath).isDirectory();
if (!isOutputDirExists) {
console.log(
`output directory not found, creating directory tree "${outputDirPath}"`
);
mkdirp.sync(outputDirPath);
}
let isOutputFileExists =
fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isFile();
if (isOutputFileExists) {
console.log(
`output file already exists, removing file "${outputFilePath}"`
);
fs.unlinkSync(outputFilePath);
}
}
}
if (!filePaths.length) {
console.error("Usage: truffle-flattener <files>");
return;
}
await flatten(filePaths, outputChunk => {
if (outputFilePath) {
fs.appendFileSync(outputFilePath, outputChunk);
} else {
process.stdout.write(outputChunk);
}
});
}
if (require.main === module) {
main(process.argv.slice(2)).catch(console.error);
}
module.exports = async function(filePaths, root) {
let res = "";
await flatten(filePaths, str => (res += str), root);
return res;
};