summary refs log tree commit diff
path: root/scripts/build.js
blob: 2c0d73285dc9717f70b390e7d69441f0b01aee79 (plain) (blame)
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
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const { argv, stdout, exit } = require("process");
const {  execIn, parts } = require('./utils');

if(argv.includes("help")) {
	console.log(`Fosscord build script help:
Arguments:
  clean			Cleans up previous builds
  verbose		Enable verbose logging
  logerrors		Log build errors to console
  pretty-errors		Pretty-print build errors
  silent		No output to console or files.`);
	exit(0);
}

let steps = 1, i = 0;
if (argv.includes("clean")) steps++;

const verbose = argv.includes("verbose") || argv.includes("v");
const logerr = argv.includes("logerrors");
const pretty = argv.includes("pretty-errors");
const silent = argv.includes("silent");

if(silent) console.error = console.log = function(){}

if (argv.includes("clean")) {
	console.log(`[${++i}/${steps}] Cleaning...`);
	let d = "dist";
	if (fs.existsSync(d)) {
		fs.rmSync(d, { recursive: true });
		if (verbose) console.log(`Deleted ${d}!`);
	}
}

console.log(`[${++i}/${steps}] Compiling src files ...`);

let buildFlags = ''
if(pretty) buildFlags += '--pretty '

try {
	execSync(
		'node "' +
			path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
			'" -p "' +
			path.join(__dirname, "..") +
			'" ' + buildFlags,
		{
			cwd: path.join(__dirname, ".."),
			shell: true,
			env: process.env,
			encoding: "utf8"
		}
	)
} catch (error) {
	if(verbose || logerr) {
		error.stdout.split(/\r?\n/).forEach((line) => {
			let _line = line.replace('dist/','',1);
			if(!pretty && _line.includes('.ts(')) {
				//reformat file path for easy jumping
				_line = _line.replace('(',':',1).replace(',',':',1).replace(')','',1)
			}
			console.error(_line);
		})
	}
	console.error(`Build failed! Please check build.log for info!`);
	if(!silent){
		if(pretty) fs.writeFileSync("build.log.ansi",  error.stdout);
		fs.writeFileSync("build.log",  error.stdout.replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''));
	}
}