-
Notifications
You must be signed in to change notification settings - Fork 10
/
manifest-generator.js
300 lines (272 loc) · 9.93 KB
/
manifest-generator.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'use strict'
////////////////////
// CONFIG //
////////////////////
const forceUnixLineEndings = true //force \n instead of \r\n (default: true)
const removeOldDefs = true // remove undetected defs (default: true)
const onlyHighestDefVer = false // only greatest def version added to manifest (default: false)
const addAutoUpdateDisableBool = true // add ("disableAutoUpdate": false) to module.json if missing (default: true)
const addFolderNameAsName = true // add ("name": <folder name>) to module.json if missing (default: true)
const addAuthorName = "" // add ("author": <addAuthorName>) to module.json if missing (default: "")
const addDescription = "Description goes here." // add ("description": <addDescription>) to module.json if missing (default: "Description goes here.")
const addServers = [""] // add ("servers": <addServers>) to module.json if missing (default: [""])
const addSupportUrl = "" // add ("supportURL": <addSupportURL>) to module.json if missing (default: "")
////////////////////
// LISTS //
////////////////////
// ignore these files for making manifest
const IGNORED_FILES = [
'manifest.json',
'manifest-generator.js',
'manifest-generator.bat',
'manifest-generator.exe',
'node.exe'
]
// ignore files/folders which start with these characters: i.e. ".git" or "_old"
const IGNORED_CHARACTERS = [
'.',
'_'
]
// filetypes to force unix line ending if enabled
const FORCE_UNIX_FILE_TYPES = [
'.txt',
'.text',
'.js',
'.json',
'.jsn',
'.xml',
'.md',
'.htm',
'.html',
'.css',
'.csv',
'.php',
'.cfg',
'.ini',
'.list',
'.lst'
]
////////////////////
// CODE //
////////////////////
const crypto = require('crypto'),
fs = require('fs'),
path = require('path')
// set directory to launch argument or local directory
let directory = __dirname
if (process.argv[2]) {
directory = process.argv[2]
// check if valid directory
try {
fs.readdirSync(directory, 'utf8')
}
catch (err) {
console.log(`"${directory}" is not a valid folder.`)
return
}
}
// read existing module.json
let modulejson
try {
modulejson = require(path.join(directory, 'module.json'))
}
catch (error) {
modulejson = {}
}
if (addAutoUpdateDisableBool && modulejson.disableAutoUpdate === undefined) {
modulejson.disableAutoUpdate = false
}
if (addFolderNameAsName && modulejson.name === undefined) {
modulejson.name = path.basename(directory)
}
if (addAuthorName && modulejson.author === undefined) {
modulejson.author = addAuthorName
}
if (addDescription && modulejson.description === undefined) {
modulejson.description = addDescription
}
if (addServers && addServers[0] && modulejson.servers === undefined) {
modulejson.servers = addServers
}
if (addSupportUrl && modulejson.supportUrl === undefined) {
modulejson.supportUrl = addSupportUrl
}
fs.writeFileSync(path.join(directory, 'module.json'), jsonify(modulejson), 'utf8')
// read existing manifest.json
let manifest
try {
// sanitize input
manifest = require(path.join(directory, 'manifest.json'))
if (manifest && typeof manifest === 'object') {
if (!manifest.files) manifest.files = {}
if (removeOldDefs) delete manifest.defs
}
else {
manifest = {
files: {}
}
}
}
catch (error) {
// make new manifest
manifest = {
files: {}
}
}
// delete removed file entries
let checking = 0
for (let entry of Object.keys(manifest.files)) {
// check if file exists
checking += 1
fs.access(path.join(directory, entry), fs.constants.F_OK, (err) => {
checking -= 1
if (err) delete manifest.files[entry]
checkProg()
return
})
}
let reading = 0
getFiles()
// get all files in folder and subfolder
function getFiles(relativePath = '', files) {
let dir = path.join(directory, relativePath)
if (!files) files = fs.readdirSync(dir, 'utf8')
for (let file of files) {
// if not ignored file or begins with ignored character
if (!IGNORED_FILES.includes(file) && !IGNORED_CHARACTERS.includes(file[0])) {
reading += 1
fs.readdir(path.join(dir, file), 'utf8', (err, moreFiles) => {
if (moreFiles) {
getFiles(path.join(relativePath, file), moreFiles)
}
else {
getHash(path.join(relativePath, file))
}
reading -= 1
checkProg()
})
}
}
}
// get sha256 hash
function getHash(file, type = 'sha256') {
file = file.replace(/\\/g, '/')
// force unix line endings
if (forceUnixLineEndings) forceUnix(file)
// get defs
getDefs(file)
if (manifest.files[file] && typeof manifest.files[file] === 'object') {
manifest.files[file].hash = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest('hex')
}
else {
manifest.files[file] = crypto.createHash(type).update(fs.readFileSync(path.join(directory, file))).digest('hex')
}
}
// get defs
function getDefs(file) {
if (file.slice(-4).includes('.js')) {
let data = fs.readFileSync(path.join(directory, file), 'utf8')
// ignore comments
data = data.replace(/\/\/.*\/\*(?!.*\*\/)/g,'') // ignore: // ... /* ...
data = data.replace(/\/\*[^]*?\*\//gm,'') // ignore: /* ... */
data = data.replace(/\/\/.*/g,'') // ignore: // ...
data = data.replace(/[A-Z\.]+\.majorPatchVersion/igm,'majorPatchVersion')
let packets = data.match(/['"`][CS]_[A-Z_]+['"`][ \t]*,([ \t\n]*\d+|[ \t\n]*[`'"]raw[`'"`]|[^\n,\]]+)/igm)
//console.log(packets)
if (packets) {
if (!Array.isArray(packets)) packets = [packets]
for (let packet of packets) {
// make defs object for manifest
if (!manifest.defs) manifest.defs = {}
// get def versions
packet = packet.split(',')
let versions = []
for (let majorPatchVersion = 60; majorPatchVersion <= 100; majorPatchVersion += 1) {
let v
try {v = eval(packet[1])}
catch (err) {
console.log(`eval(${packet[1]})`)
console.log(err)
majorPatchVersion = 101
}
if (v && !versions.includes(v)) versions.push(v)
}
for (let packetVer of versions) {
// if NaN
if (isNaN(packetVer)) packetVer = String(packetVer).toLowerCase()
// if in manifest
packet[0] = packet[0].replace(/['"` \t\n]/igm, '')
if (manifest.defs[packet[0]]) {
// add to list
if (Array.isArray(manifest.defs[packet[0]])) {
if (!manifest.defs[packet[0]].includes(packetVer)) {
manifest.defs[packet[0]].push(packetVer)
manifest.defs[packet[0]].sort((a,b)=>{return a-b})
}
}
// change to list
else if (['number', 'string'].includes(typeof manifest.defs[packet[0]]) && manifest.defs[packet[0]] != packetVer) {
manifest.defs[packet[0]] = [manifest.defs[packet[0]], packetVer]
manifest.defs[packet[0]].sort((a,b)=>{return a-b})
}
// delete other versions
if (onlyHighestDefVer && Array.isArray(manifest.defs[packet[0]])) {
let index = manifest.defs[packet[0]].length - 1
if (typeof manifest.defs[packet[0]][index] == "string") index -= 1
manifest.defs[packet[0]] = manifest.defs[packet[0]][index]
}
}
else {
manifest.defs[packet[0]] = packetVer
}
}
}
}
}
}
// force unix line endings
function forceUnix(file) {
// check if read and writable
for (let type of FORCE_UNIX_FILE_TYPES) {
if (file.slice(-6).includes(type)) {
try {
let data = fs.readFileSync(path.join(directory, file), 'utf8')
data = data.replace(/\r\n/g, '\n')
fs.writeFileSync(path.join(directory, file), data, 'utf8')
}
catch (err) {
//console.log(err)
console.log('Cannot edit protected file: ' + file)
}
return
}
}
}
// alphabetize object keys
function alphabetizeObject(obj) {
let keys = Object.keys(obj)
keys.sort()
let newObj = {}
for (let key of keys) {
newObj[key] = obj[key]
}
return newObj
}
// JSON.stringify but make lists single line
function jsonify(obj) {
obj = JSON.stringify(obj, null, ' ')
let lists = obj.match(/\[[^]+?\].*/igm)
if (lists) for (let list of lists) {
obj = obj.substring(0,obj.indexOf(list)) + list.replace(/[ \n\t]*/igm, '') + obj.substring(obj.indexOf(list) + list.length)
}
return obj
}
// check if process completed
function checkProg() {
if (reading === 0 && checking === 0) {
manifest.files = alphabetizeObject(manifest.files)
if (manifest.defs) manifest.defs = alphabetizeObject(manifest.defs)
fs.writeFileSync(path.join(directory, 'manifest.json'), jsonify(manifest), 'utf8')
console.log('"manifest.json" generation complete.')
}
}