forked from sCrypt-Inc/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscryptMake.js
More file actions
79 lines (74 loc) · 2.6 KB
/
Copy pathscryptMake.js
File metadata and controls
79 lines (74 loc) · 2.6 KB
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
const path = require('path')
const fs = require('fs');
const {
compileContract: compileContractImpl,
buildContractClass
} = require('scryptlib')
const getScryptImportFile = function (scryptFile, ) {
const data = fs.readFileSync(scryptFile, {encoding: 'utf8'})
const scryptImportList= data.split('\n').filter(function (line) {
// file import line
return line.indexOf('import') !== -1 && line.indexOf('scrypt') !== -1
})
let scryptList = []
for (const scryptImport of scryptImportList) {
// extra import file
const regExpMatchArray = scryptImport.match(/import "(\S+)";/)
if (regExpMatchArray) {
const fileName = regExpMatchArray[1]
scryptList.push(fileName)
}
}
return scryptList
}
const recurScryptList = function (scryptFile, fileList, filePath) {
const newList = getScryptImportFile(scryptFile, fileList)
if (newList.length > 0){
for (const newListElement of newList) {
const newFile = path.join(filePath, newListElement)
recurScryptList(newFile, fileList, filePath)
fileList.push(newListElement)
}
}
}
const scryptTreeLastChangeTime = function (scryptFile) {
const filePath = path.dirname(scryptFile)
const allScryptList = [
path.basename(scryptFile)
]
recurScryptList(scryptFile, allScryptList, filePath)
const uniqueScryptList = [...new Set(allScryptList)]
const scryptCtimeList = []
for (const scryptFile of uniqueScryptList) {
const fullPath = path.join(filePath, scryptFile)
if (fs.existsSync(fullPath)) {
const ctime = fs.statSync(fullPath).ctimeMs
scryptCtimeList.push(ctime)
}
}
return Math.max.apply(Math, scryptCtimeList)
}
function scryptMake(scryptFile, outDir, options){
if (!outDir) {
outDir = 'out'
}
const out = path.join(__dirname, outDir)
const sourceFileLastCtime = scryptTreeLastChangeTime(scryptFile)
const targetDescFile = `${out}/${path.basename(scryptFile).replace('.scrypt', '_desc.json')}`
let descLastCtime = 0
if (fs.existsSync(targetDescFile)) {
descLastCtime = fs.statSync(targetDescFile).ctimeMs
}
let descObj
if (sourceFileLastCtime > descLastCtime) {
console.log('Should recompile source file.')
descObj = compileContractImpl(scryptFile, options ? options: {out: out});
} else {
console.log('Source file not change while use old desc file.')
descObj = JSON.parse(fs.readFileSync(targetDescFile, 'utf8'))
}
return descObj
}
module.exports = {
scryptMake
}