-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
177 lines (142 loc) · 4.97 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// # Copyright (c) 2014-2021 Feudal Code Limitada - MIT license #
"use strict"
// extern import
import { existsSync } from "https://deno.land/[email protected]/fs/exists.ts"
// modules
import { createRat } from "./rat.js"
import { parse } from "./parser.js"
import { checkNames } from "./check-names.js"
// general
var root = ""
var rats = { } // { fullpath: rat }
// html mode
var htmlPath = ""
var htmlLines = [ ] // only the processed ones
// folder mode
var foldersToRead = [ ]
// main ///////////////////////////////////////////////////////////////////////
function main() {
root = Deno.cwd() + "/"
showOpenMessage()
const home = Deno.env.get("HOME")
if (root.length <= home.length) {
console.log("ERROR: you must be in a subdirectory of your home directory")
Deno.exit(1)
}
const options = Deno.args.slice(0, Deno.args.length) // cloning
if (options.length == 0) { // read directory (deno)
mainForFolder()
return
}
const filename = options.shift() // extract and check filename
if (! existsSync(filename)) {
console.log("ERROR: file not found:", filename)
Deno.exit(1)
}
if (options.length != 0) { // check option
console.log("ERROR: not expecting option:", options[0])
Deno.exit(1)
}
if (filename.endsWith(".js")) { mainForSingleJs (filename); return }
if (filename.endsWith(".html")) { mainForHtml(filename); return }
console.log("ERROR: file name must end with '.js' or '.html':", filename)
Deno.exit(1)
}
function showOpenMessage() {
console.log("\u001Bc") // *clears console perfectly* //
console.log("running dirtyrat")
console.log(" -- root is ", root)
console.log(" -- accepts html file, js file or blank (current directory, recursively)")
console.log("")
}
// single js mode /////////////////////////////////////////////////////////////
function mainForSingleJs (filename) {
processJsFile(root + filename, true)
checkNames(rats, true)
}
// folder mode ////////////////////////////////////////////////////////////////
function mainForFolder() {
foldersToRead = [ root ]
while (foldersToRead.length > 0) { readFolder() }
checkNames(rats, false)
}
function readFolder() {
const path = foldersToRead.shift()
if (path.endsWith("/IGNORE/")) { return }
console.log("reading folder:", path)
const elements = Deno.readDirSync(path)
for (const element of elements) { readElementInFolder(path + element.name) }
}
function readElementInFolder(path) {
const stats = Deno.statSync(path)
if (stats.isDirectory) { foldersToRead.push(path + "/"); return }
//
if (! stats.isFile) { return }
//
if (! path.toLowerCase().endsWith(".js")) { return }
//
processJsFile(path, false)
}
// html mode //////////////////////////////////////////////////////////////////
function mainForHtml(filename) {
htmlPath = root + filename
console.log("reading", htmlPath)
const htmlText = Deno.readTextFileSync(htmlPath)
fillRatsHtml(htmlText) // also parses
checkNames(rats, false)
}
function fillRatsHtml(htmlText) {
const lines = htmlText.split("\n")
while (lines.length != 0) {
const line = lines.shift()
htmlLines.push(line)
const trim = line.trim()
if (! trim.startsWith("<script type=\"module\" src=")) { continue }
fillRatsOnceHtml(trim)
}
}
function fillRatsOnceHtml(trim) {
// if there is content after "</script>", path will raise error
let path = trim.replace('<script type=\"module\" src="', "").replace('"></script>', "")
if (path[0] == "/") { path = path.substr(1) }
path = root + path
//
if (! existsSync(path)) {
console.log("ERROR: link path not found:", path)
Deno.exit(1)
}
//
if (! path.toLowerCase().endsWith(".js")) {
console.log("ERROR: bad file name extension:", path)
Deno.exit(1)
}
//
processJsFile(path, false)
}
// processing /////////////////////////////////////////////////////////////////
function processJsFile(path, verbose) { // existence already checked
//
if (! path.endsWith(".js")) {
console.log("ERROR: bad casing for extension:", path)
Deno.exit(1)
}
//
// trimRight exlcudes dirty EOF and despisable blank lines //
const text = Deno.readTextFileSync(path).trimRight()
//
if (verbose) { console.log("reading file:", path) }
//
if (rats[path] != undefined) {
console.log("ERROR: duplicated path:", path)
Deno.exit(1)
}
//
const lines = text.split("\n")
for (let n = 0; n < lines.length; n++) { lines[n] = lines[n].trimRight() }
const rat = createRat(path, lines)
parse(rat)
rats[path] = rat
}
// boot ///////////////////////////////////////////////////////////////////////
main()
//