-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_motoko.ts
81 lines (74 loc) · 1.96 KB
/
check_motoko.ts
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
import mo from "motoko"
import fs from "fs"
import path from "path"
let base = await mo.fetchPackage(
"base",
"https://github.com/dfinity/motoko-base/master/src"
)
mo.loadPackage(base)
let folders = findFolder("src", "_mo")
console.log(folders)
folders.forEach((folder) => checkMoFiles(folder))
function findFolder(dir: string, folderName: string) {
let array: string[] = []
let files = fs.readdirSync(dir)
for (let i = 0; i < files.length; i++) {
let filePath = path.join(dir, files[i])
let stats = fs.statSync(filePath)
if (stats.isDirectory()) {
if (files[i] === folderName) {
array.push(filePath)
let result = findFolder2(filePath)
if (result) {
result.forEach((value) => array.push(value))
}
} else {
let result = findFolder(filePath, folderName)
if (result) {
result.forEach((value) => array.push(value))
}
}
}
}
return array
}
function findFolder2(dir: string) {
let array: string[] = []
let files = fs.readdirSync(dir)
for (let i = 0; i < files.length; i++) {
let filePath = path.join(dir, files[i])
let stats = fs.statSync(filePath)
if (stats.isDirectory()) {
array.push(filePath)
let result = findFolder2(filePath)
if (result) {
result.forEach((value) => array.push(value))
}
}
}
return array
}
function checkMoFiles(directoryPath: string) {
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log("Unable to scan directory: " + err)
}
files.forEach(function (file) {
if (path.extname(file) === ".mo") {
checkMotoko(path.join(directoryPath, file))
}
})
})
}
function checkMotoko(file: string) {
let f = fs.readFileSync(file).toString()
mo.write(file, f)
let check = mo.check(file)
mo.delete(file)
if (check.length === 0) {
console.log("CORRECT: " + file)
} else {
console.log("ERROR: " + file)
console.log(check)
}
}