-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.js
More file actions
114 lines (95 loc) · 3.15 KB
/
make.js
File metadata and controls
114 lines (95 loc) · 3.15 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
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
const { exec } = require('child_process');
const fs = require('fs');
const IS_PRODUCTION = process.argv.indexOf('--production') !== -1 || process.argv.indexOf('--release') !== -1;
const IS_DEBUG = process.argv.indexOf('--debug') !== -1;
const IS_WATCH = process.argv.indexOf('--watch') !== -1;
const IS_NOCOLORFUL = process.argv.indexOf('--no-color') !== -1;
const outputPath = 'build';
let outputFilename = 'scl';
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i].indexOf('--output-filename=') !== -1) outputFilename = process.argv[i].split('=')[1];
};
const IS_NO_COLLECT_FILE = process.argv.indexOf('--no-collect') !== -1;
const IS_NO_DOWNLOAD_SEAT_TABLE = process.argv.indexOf('--no-download-seat-table') !== -1;
if (!IS_PRODUCTION && !IS_DEBUG) {
console.error('错误:请设置编译版本,--debug或--production参数');
process.exit(0);
};
if (IS_PRODUCTION && IS_DEBUG) {
console.error('错误:不能同时设置--production和--debug');
process.exit(0);
};
var execCommand = '';
try {
fs.mkdirSync(outputPath);
} catch (e) { };
if (IS_DEBUG) execCommand = `gcc -o ${outputPath}/${outputFilename}
./src/index.c
-DDEBUG
-Wall -Wextra
-std=c99
-ggdb
-lcurl
-m32
`;
if (IS_PRODUCTION) execCommand = `gcc -o ${outputPath}/${outputFilename}
./src/index.c
-DRELEASE
-std=c99
-funsigned-char
-lcurl
-Ofast
-m32`;
if (IS_NOCOLORFUL) execCommand += '\n-DNOCOLORFUL';
if (IS_NO_COLLECT_FILE) execCommand += '\n-DNOCOLLECTFILE';
if (IS_NO_DOWNLOAD_SEAT_TABLE) execCommand += '\n-DNODOWNLOADSEATTABLE';
console.log('---Score Calculator Makefile---');
console.log('Mode:', IS_PRODUCTION ? 'Production' : 'Development', '\n');
console.log('Output filename:', outputFilename);
const buildFile = () => new Promise((resolve, reject) => {
const startTime = new Date().getTime();
exec(execCommand.replaceAll('\n', ' '), (err, stdout, stderr) => {
if (stderr.toLowerCase().indexOf('error') !== -1) {
console.error(stderr);
console.error('\n\033[7;31;47m编译错误!\033[0m');
reject(err);
return;
}
else {
const endTime = new Date().getTime();
console.log(stdout);
console.log('\033[7;32;47m构建成功!\033[0m');
console.log('\033[7;33;47m' + `用时 ${(endTime - startTime) / 1000}s` + '\033[0m');
resolve();
};
});
});
((async () => {
try {
await buildFile();
} catch (e) { };
if (IS_WATCH) {
console.log('\n\033[7;36;47m正在监听改变......\033[0m');
fs.watch('./src', {
recursive: true
}, async (curr, prev) => {
if (lock === 0) return;
await getLock();
console.clear();
try {
await buildFile();
} catch (e) { };
await releaseLock();
console.log('\n\033[7;36;47m正在监听改变......\033[0m');
});
};
})());
let lock = 1;
async function getLock() {
while (lock === 0) await sleep(500);
lock = 0;
};
async function releaseLock() {
lock = 1;
};
const sleep = (ts) => new Promise(resolve => setTimeout(resolve, ts));