-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency-tree.js
49 lines (39 loc) · 1.28 KB
/
dependency-tree.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
const {getContractCode, getProjectConfig} = require("./utils")
// Read a contract from ./contracts and determine what other contracts need to be imported
const getImports = (contractName) => {
const projectConfig = getProjectConfig()
const contract = projectConfig.contracts[contractName]
if (!contract) {
throw new Error(`Contract "${contractName}" could not be found`)
}
const contractCode = getContractCode(contractName)
const linesWithImport = contractCode.match(/^import ".+/gm);
if (!linesWithImport) {
return []
}
// we need to split these up next so that we can get the contract name
// from each import statement
// import NonFungibleToken from "..."
const imports = {}
// keep track of all imports we find in all dependencies
const allImports = {}
if (!linesWithImport) {
return []
}
linesWithImport.forEach(line => {
const foundName = line.split(" ")[1].replace(/^"|"$/g, '')
imports[foundName] = true
allImports[foundName] = true
})
Object.keys(imports).forEach(importedContract => {
allImports[importedContract] = true
const subImports = getImports(importedContract)
for (const item of subImports) {
allImports[item] = true
}
})
return Object.keys(allImports)
}
module.exports = {
getImports
}