-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·147 lines (130 loc) · 7.14 KB
/
build.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
const childProcess = require("child_process")
const fs = require("fs")
const path = require("path")
const archiver = require("archiver")
const htmlMinify = require("html-minifier").minify
const sqwish = require("sqwish")
const Terser = require("terser")
const version = require("./package.json").version
async function listFilesRecursively(dir){
let results = []
const list = fs.readdirSync(dir)
for(let file of list){
file = path.resolve(dir, file)
const stat = fs.statSync(file)
if (stat && stat.isDirectory()) results = results.concat(await listFilesRecursively(file))
else results.push(file)
}
return results
}
async function minifyContent(string, language){
var minified
if(language == "js") minified = (await Terser.minify(string)).code
else if(language == "css") minified = sqwish.minify(string)
else if(language == "html") minified = htmlMinify(string, { useShortDoctype: true, removeStyleLinkTypeAttributes: true, removeScriptTypeAttributes: true, removeComments: true, minifyURLs: true, minifyJS: true, minifyCSS: true, caseSensitive: true, preserveLineBreaks: true, collapseWhitespace: true, continueOnParseError: true })
else throw new Error(`Le langage ${language} n'est pas supporté`)
return minified
}
async function main(){
// Si le dossier release-builds n'existe pas, on le crée, sinon on le vide
if(!fs.existsSync(path.join(__dirname, "release-builds"))) fs.mkdirSync(path.join(__dirname, "release-builds"))
else {
var files = fs.readdirSync(path.join(__dirname, "release-builds"))
files.forEach(file => {
fs.rmSync(path.join(__dirname, "release-builds", file), { recursive: true })
})
}
// Build Tailwind CSS
console.log("@@@@ npm run build_tailwindcss")
childProcess.execSync("npm run build_tailwindcss")
// Copier et minifier les fichiers nécessaires dans un dossier temporaire
var filesToCopy = ["index.js", "package.json", "LICENSE", "src/fonts", "src/icons", "src/js", "src/libs", "src/chat.html", "src/notif.html", "src/style.css"]
for(var i = 0; i < filesToCopy.length; i++){
var filePath = path.join(__dirname, filesToCopy[i])
var destPath = path.join(__dirname, "release-builds", "minified-temp", filesToCopy[i])
if(!fs.existsSync(filePath)) throw new Error(`Le fichier ${filePath} n'existe pas`)
if(!fs.existsSync(path.dirname(destPath))) fs.mkdirSync(path.dirname(destPath), { recursive: true })
console.log(`@@@@ Copie: ${filePath} -> ${destPath}`)
if(fs.lstatSync(filePath).isDirectory()){
fs.mkdirSync(destPath, { recursive: true })
var files = fs.readdirSync(filePath)
for(var j = 0; j < files.length; j++){
var fileName = files[j]
if(fileName.endsWith(".html") || fileName.endsWith(".css") || fileName.endsWith(".js")) { // Minifier si c'est un fichier HTML, CSS ou JS
var fileContent = fs.readFileSync(path.join(filePath, fileName), "utf-8")
fileContent = await minifyContent(fileContent, fileName.split(".").pop())
fs.writeFileSync(path.join(destPath, fileName), fileContent)
} else { // Sinon, on copie simplement le fichier
fs.copyFileSync(path.join(filePath, fileName), path.join(destPath, fileName))
}
}
} else {
if(filePath.endsWith(".html") || filePath.endsWith(".css") || filePath.endsWith(".js")) { // Minifier si c'est un fichier HTML, CSS ou JS
var fileContent = fs.readFileSync(filePath, "utf-8")
fileContent = filesToCopy[i].endsWith(".html") || filesToCopy[i].endsWith(".css") || filesToCopy[i].endsWith(".js") ? await minifyContent(fileContent, filesToCopy[i].split(".").pop()) : fileContent
fs.writeFileSync(destPath, fileContent)
} else { // Sinon, on copie simplement le fichier
fs.copyFileSync(path.join(__dirname, filesToCopy[i]), destPath)
}
}
}
// Télécharger les node_modules
console.log("@@@@ npm install --no-audit --progress=false --omit=dev")
childProcess.execSync("npm install --no-audit --progress=false --omit=dev", { cwd: path.join(__dirname, "release-builds", "minified-temp") })
// Supprimer les fichiers inutiles dans les nodes_modules
var nodesDeletionPattern = ["history.md", "readme.md", "changelog.md", "changelog", ".yml", ".lock", ".log", ".ts", ".tsbuildinfo", ".map", ".npmignore", ".jshintrc", ".eslintrc", ".nycrc", "tsconfig.json"]
var nodesModulesPath = path.join(__dirname, "release-builds", "minified-temp", "node_modules")
var nodesModulesFiles = await listFilesRecursively(nodesModulesPath)
nodesModulesFiles.forEach(file => {
if(nodesDeletionPattern.some(e => file.toLowerCase().endsWith(e))) console.log(`@@@@ Suppression: ${file}`) && fs.rmSync(file)
})
// Exécuter quelques scripts
var commands = [
"npm run build-exe-x64",
process.platform == "darwin" ? "npm run build-app-x64" : null,
process.platform == "darwin" ? "npm run build-app-arm64" : null
].filter(e => e != null)
for(var i = 0; i < commands.length; i++){
console.log(`@@@@ ${commands[i]}`)
childProcess.execSync(commands[i])
}
// On build un fichier dmg pour macOS
if(process.platform == "darwin"){
var macOsSupportedArchs = [
"arm64", "x64"
]
for(var i = 0; i < macOsSupportedArchs.length; i++){
// On fait la commande
var command = `npx create-dmg ${path.join(__dirname, "release-builds", `LocalChat-darwin-${macOsSupportedArchs[i]}`, "LocalChat.app")} release-builds`
console.log(`@@@@ ${command}`)
try { childProcess.execSync(command) } catch(e){}
// On renomme le fichier dmg généré
fs.renameSync(
path.join(__dirname, "release-builds", `LocalChat ${version}.dmg`),
path.join(__dirname, "release-builds", `LocalChat-${version}-macos-${macOsSupportedArchs[i]}.dmg`)
)
}
} else console.log("@@@@ Pas de build pour macOS car on est pas sur macOS")
// On compresse les dossiers (pour pouvoir les mettre sur GitHub)
var releaseBuildsFiles = [
{ platform: "win32", arch: "x64", input: path.join(__dirname, "release-builds", "LocalChat-win32-x64"), output: path.join(__dirname, "release-builds", `LocalChat-${version}-win32-x64.zip`) },
process.platform == "darwin" ? { platform: "darwin", arch: "x64", input: path.join(__dirname, "release-builds", "LocalChat-darwin-x64"), output: path.join(__dirname, "release-builds", `LocalChat-${version}-macos-x64.zip`) } : null,
process.platform == "darwin" ? { platform: "darwin", arch: "arm64", input: path.join(__dirname, "release-builds", "LocalChat-darwin-arm64"), output: path.join(__dirname, "release-builds", `LocalChat-${version}-macos-arm64.zip`) } : null
].filter(e => e != null)
for(var i = 0; i < releaseBuildsFiles.length; i++){
console.log(`@@@@ Création du zip pour ${releaseBuildsFiles[i].platform} ${releaseBuildsFiles[i].arch}`)
var archive = archiver("zip", { zlib: { level: 9 } })
archive.pipe(fs.createWriteStream(releaseBuildsFiles[i].output))
archive.directory(releaseBuildsFiles[i].input, false)
await archive.finalize()
console.log(`@@@@ Zip créé pour ${releaseBuildsFiles[i].platform} ${releaseBuildsFiles[i].arch}`)
}
// Nettoyage
console.log("@@@@ Nettoyage")
var files = fs.readdirSync(path.join(__dirname, "release-builds"))
files.forEach(file => {
if(!file.endsWith(".dmg") && !file.endsWith(".zip")) fs.rmSync(path.join(__dirname, "release-builds", file), { recursive: true })
})
console.log("@@@@ Nettoyage terminé")
}
main()