-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (123 loc) · 5.12 KB
/
index.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
const { resolve } = require("node:path")
const path = require("node:path")
var beginTime = new Date().getTime()
function logTime(label) {
console.log(`"${label}" took ${new Date().getTime() - beginTime}ms`)
}
function fixHTML(html) {
html = html.replaceAll(/<script>.*<\/script>/gi, '');
html = html.replaceAll(/(\s+on\w+="[^"]*")/gi, '');
html = html.replaceAll(/href="javascript:.*"/gi, '');
return html.replaceAll(/<a\s(?![^>]*\btarget\b)([^>]*)>/gi, '<a target="_blank" $1>');
}
var stats = {
unprocessed: [],
processed: [],
totalTime: 0
}
const fs = require("node:fs")
function writef(p, c) {
return fs.writeFile(resolve(p), Buffer.from(c), 'utf8', new Function)
}
function readf(p) {
// var fc = "";
// fs.readFile(resolve(p), 'utf8', (err, data) => {
// if (err) {
// console.error(err);
// return;
// }
// fc = data;
// console.log("A", typeof data)
// });
return fs.readFileSync(p, "utf8").toString();
}
function mkdir(d) {
fs.mkdir(resolve(d), { recursive: true }, new Function)
}
function imageToBase64Url(imagePath) {
// Read the image file
const image = fs.readFileSync(resolve(imagePath));
// Get the file extension
const extname = path.extname(resolve(imagePath)).slice(1);
// Convert the image data to Base64
const base64Image = image.toString('base64');
// Construct the Data URL
const dataUrl = `data:image/${extname};base64,${base64Image}`;
return dataUrl;
}
console.log(readf("./meta.json"))
var meta = JSON.parse(readf("./meta.json"))
var allapps = []
fs.rm(resolve("./_out/"), { recursive: true }, function () {
mkdir("./_out/")
if (!meta.isTestBuild) { meta.version = +(Number(meta.version) + 0.1).toFixed(1) }
console.log(String(meta.version).includes("."))
writef("./meta.json", JSON.stringify(meta, null, 2))
console.log("Beginning build process...")
fs.readdir(resolve("./applications"), (err, cont) => {
cont.forEach(file => {
console.info(`Processing app: ${file}`)
if (fs.lstatSync(resolve(`./applications/${file}`)).isDirectory()) {
logTime(`Start: ${file}`)
mkdir(`./_out/${file}`)
var appmeta = JSON.parse(readf(`./applications/${file}/meta.json`))
appmeta.scripts = []
if(appmeta.hasOwnProperty("icon")) {
appmeta.icon = imageToBase64Url(`./applications/${file}/${appmeta.icon}`)
}
else{
appmeta.icon = imageToBase64Url(`./icon.svg`)
}
if(appmeta.hasOwnProperty("banner")) {
appmeta.icon = imageToBase64Url(`./applications/${file}/${appmeta.banner}`)
}
else{
appmeta.icon = imageToBase64Url(`./banner.svg`)
}
writef(`./_out/${file}/desc.html`, fixHTML(readf(`./applications/${file}/desc.html`)))
mkdir(`./_out/${file}/scripts`)
fs.readdir(resolve(`./applications/${file}/scripts`), (err, cont) => {
cont.forEach(script => {
if (script.endsWith(".js") || script.endsWith(".ts")) {
writef(`./_out/${file}/scripts/${script}`, readf(`./applications/${file}/scripts/${script}`))
appmeta.scripts.push(script)
}
});
writef(`./_out/${file}/meta.json`, JSON.stringify(appmeta))
var fc = JSON.parse(readf(`./applications/${file}/meta.json`))
meta.categories.push(fc.category)
console.log(meta)
logTime(`${file}`)
stats.processed.push(file)
allapps.push(appmeta)
});
} else {
stats.unprocessed.push(file)
console.warn(`Application: ${file}
This application is formatted incorrectly, and will not be outputted. Please ensure that all applications are in the following structure
FOLDER: ${file}
\ FILE: meta.json (this contains the application metadata)
\ FOLDER: scripts (this contains all scripts for the app, the usual 'main.js' etc)`)
}
});
writef("./_out/meta.json", JSON.stringify(meta))
writef("./_out/allapps.json", JSON.stringify(allapps))
writef("./_out/index.html", `
<center>
<h1>${meta.name}</h1>
<h2>You may add this repo by: Opening app store > This Device > Sources > Add and put the URL below.</h2>
<code> ${meta.url} </code>
</center>
`)
stats.totalTime = new Date().getTime() - beginTime
fs.writeFile(resolve(`./_out/stats.json`), Buffer.from(JSON.stringify(stats)), 'utf8', err => {
if (err) {
console.error(err);
} else {
// file written successfully
}
});
stats.totalTime = `${stats.totalTime}ms`
console.log(`Stats: ${JSON.stringify(stats, null, 2).replaceAll('"', '')}`)
});
});