-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.js
65 lines (63 loc) · 1.61 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
56
57
58
59
60
61
62
63
64
65
const watch = require("watch");
const { basename, join } = require("path");
const { unlinkSync, existsSync } = require("fs");
const { ScriptHelper } = require("./src/lib/sensible_nft/ScriptHelper");
const { glob } = require("glob");
let isDebug = false;
if (process.argv.includes("--debug")) {
isDebug = true;
}
function compile_for(file) {
const fileName = basename(file);
if (fileName.endsWith(".scrypt")) {
try {
clean_description_file(fileName);
ScriptHelper.compileContract(fileName, isDebug);
} catch (error) {
console.log(error);
}
}
}
function clean_description_file(fileName) {
if (fileName.endsWith(".scrypt")) {
try {
const descFile = join(
ScriptHelper.contractJsonPath,
fileName.replace(".scrypt", "_desc.json")
);
if (existsSync(descFile)) {
unlinkSync(descFile);
}
} catch (error) {
console.log(error);
}
}
}
if (process.argv[2] === "--gen-desc") {
glob(ScriptHelper.contractScryptPath + "/**/*.scrypt", (err, files) => {
if (err) return;
files.forEach((f) => {
compile_for(f);
});
});
} else {
watch.watchTree(
ScriptHelper.contractScryptPath,
{ 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);
}
}
);
}