forked from sensible-contract/token_sensible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.js
58 lines (52 loc) · 1.37 KB
/
watcher.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
const watch = require('watch');
const { basename, join } = require('path');
const { unlinkSync, existsSync } = require('fs');
const { compileContract } = require('./helper');
const { glob } = require('glob');
function compile_for(file) {
const fileName = basename(file);
if(fileName.endsWith('.scrypt')) {
try {
clean_description_file(fileName)
compileContract(fileName);
} catch (error) {
console.log(error)
}
}
}
function clean_description_file(fileName) {
if(fileName.endsWith('.scrypt')) {
try {
const descFile = join(__dirname, 'deployments/fixture/autoGen', fileName.replace('.scrypt', '_desc.json'));
if (existsSync(descFile)) {
unlinkSync(descFile)
}
} catch (error) {
console.log(error)
}
}
}
if (process.argv[2] === '--gen-desc') {
glob('contracts/**/*.scrypt', (err, files) => {
if(err) return
files.forEach(f => {
compile_for(f)
})
})
} else {
watch.watchTree('contracts', { interval: 2 }, function (f, curr, prev) {
if (typeof f == "object" && prev === null && curr === null) {
// Finished walking the tree
Object.keys(f).forEach( (file) => {
compile_for(file)
})
} else if (prev === null) {
// f is a new file
} else if (curr.nlink === 0) {
// f was removed
} else {
// f was changed
compile_for(f)
}
})
}