-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.js
More file actions
75 lines (56 loc) · 2.76 KB
/
Copy pathcompile.js
File metadata and controls
75 lines (56 loc) · 2.76 KB
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
// import fs, path for filesystem operations
const fs = require("node:fs");
const path = require("node:path");
// import Colours from javascript-console-styling, my own library for console styles: https://www.npmjs.com/package/javascript-console-styling
const Colours = require("javascript-console-styling/Colours");
// import project version from package.json
const { version } = require("../package.json");
// import spawn to spawn child sudo process for chmod command
const { spawn } = require('node:child_process');
// Get required version from common
const { common } = require("./common");
const SCRIPT_VERSION = new common(process.env.BUILD_TYPE === "release", process.env.GITHUB_REF_NAME).init().SCRIPT_VERSION;
// check if script is running in bash or zsh to prevent Windows users trying to contribute from experiencing errors with sudo
const isZsh = process.env.SHELL.includes("zsh");
const isBash = process.env.SHELL.includes("bash");
if (!isZsh && !isBash) {
console.log(Colours.brightRed("This script must be run in bash or zsh! Use WSL if you're on Windows."));
process.exit(1);
}
// Get project version with trailing zeros removed
const UTILE_OS_VERSION = version.replace(/(\.0+)+$/, '');
console.log(Colours.yellow(`Compiling bash patches for Utile OS ${UTILE_OS_VERSION} into production script...\n`));
// initiate 'compiled' script with shebang and greeting
let compiledScript = `#!/bin/bash
echo "${Colours.brightGreen("Setting up Utile OS...")}"`;
// read all .sh files in /patches directory
const files = fs.readdirSync(path.join(__dirname, "..", "patches"));
files.forEach(file => {
const content = fs.readFileSync(
path.join(__dirname, "..", "patches", file),
{ encoding: "utf-8" },
);
if (!file.endsWith(".bash") || !content) {
console.log(Colours.yellow(`Skipping ${file}...`));
return;
};
compiledScript += ("\n" + `\n#^ PATCH FILE ${file}\n` + `\necho -e "\\nAPPLYING PATCH: ${file.replace(".sh", "")}\\n"\n\n` + content);
});
// add closing message
compiledScript += `
echo "${Colours.brightGreen("Utile OS has finished setting up! Please reboot your system before using it.")}"`;
fs.mkdirSync(path.join(__dirname, "..", "dist"), { recursive: true });
fs.writeFileSync(
path.join(__dirname, "..", "dist", `utile-os-${SCRIPT_VERSION}.sh`),
compiledScript
)
console.log(Colours.green(`\nSuccessfully compiled script into ${path.join(__dirname, "..", "dist", `utile-os-${SCRIPT_VERSION}.sh`)}!`));
// spawn child sudo process to run chmod
const child = spawn("sudo", ["chmod", "+x", path.join(__dirname, "..", "dist", `utile-os-${SCRIPT_VERSION}.sh`)], {
stdio: "inherit"
});
child.on('close', (code) => {
console.log((code === 0 ? Colours.green : Colours.yellow)(`chmod exited with code ${code}`));
console.log(`utile-os-${SCRIPT_VERSION}.sh`);
process.exit(0);
});