-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtool.js
More file actions
executable file
·158 lines (135 loc) · 4.04 KB
/
tool.js
File metadata and controls
executable file
·158 lines (135 loc) · 4.04 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
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
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
import child_process from 'child_process';
import fs from 'fs/promises';
import fsSync from 'fs';
import path from 'path';
import JSZip from 'jszip';
import { isAsyncFunction } from 'util/types';
import tsconfig from './tsconfig.json' with { type: 'json' };
import manifest from './pack/manifest.json' with { type: 'json' };
const packVersion = manifest.header.version;
const packMinEngineVersion = manifest.header.min_engine_version;
const actionTable = {};
/* --- HELP --- */
actionTable['help'] = () => console.error(
`usage: ${process.argv[1]} [task...]\n` +
'Utility script for working with the debug-stick project.\n' +
'Available tasks:\n' +
' help Shows this help\n' +
' build Run: npx tsc --build\n' +
' watch Run: npx tsc --watch\n' +
' clean Remove generated files\n' +
' pack Generate dist packages\n' +
'@vytdev'
);
/* --- BUILD --- */
actionTable['build'] = async () => {
try {
const code = await runProcessAsync('npx', 'tsc', '--build');
console.log('typescript exited with code: ' + code);
return code;
}
catch (e) {
printErr(e);
return -1;
}
};
/* --- WATCH --- */
actionTable['watch'] = async () => {
try {
const code = await runProcessAsync('npx', 'tsc', '--watch');
console.log('typescript dev server exited with code: ' + code);
return code;
}
catch (e) {
printErr(e);
return -1;
}
};
/* --- CLEAN --- */
actionTable['clean'] = async () => {
await fs.rm(tsconfig.compilerOptions.outDir, {
force: true,
recursive: true
});
console.log('cleanup complete');
};
/* --- PACK --- */
actionTable['pack'] = async () => {
const zipName = `debug-stick.${packVersion}.zip`;
const mcName = `debug-stick.${packVersion}.mcpack`;
// name format for github
await zipFolder('pack', zipName);
fs.copyFile(zipName, mcName);
console.log('created distribution packages');
};
/**
* Error printing utility.
* @param msgs The messages.
*/
function printErr(...msgs) {
console.error(`${process.argv[1]}:`, ...msgs);
}
/**
* Asynchronously run a sub-process.
* @param arg0 Name of or path to the executable.
* @param args Arguments to pass to the process.
* @returns A Promise.
*/
async function runProcessAsync(arg0, ...args) {
return new Promise((resolve, reject) => {
const subproc = child_process.spawn(arg0, args, { stdio: 'inherit' });
// redirect sigint temporarily
const sigIntHandler = () => subproc.kill('SIGINT');
process.on('SIGINT', sigIntHandler);
// handle success and failure
subproc.on('close', code => {
process.off('SIGINT', sigIntHandler);
resolve(code || 0);
});
subproc.on('error', err => {
process.off('SIGINT', sigIntHandler);
reject(err)
});
});
}
/**
* Zip an entire directory.
* @param folderPath The folder to zip.
* @param outPath Where to save the zip file.
*/
async function zipFolder(folderPath, outPath) {
const zip = new JSZip();
function addDirToZip(zipObj, folder) {
const items = fsSync.readdirSync(folder);
for (const item of items) {
const fullPath = path.join(folder, item);
const stats = fsSync.statSync(fullPath);
if (stats.isDirectory()) {
const subFolder = zipObj.folder(item);
addDirToZip(subFolder, fullPath);
} else {
const data = fsSync.readFileSync(fullPath);
zipObj.file(item, data);
}
}
}
addDirToZip(zip, folderPath);
const content = await zip.generateAsync({ type: 'nodebuffer' });
fsSync.writeFileSync(outPath, content);
}
// We must run from the repo folder.
process.chdir(path.dirname(process.argv[1]));
// Run each task in given order.
for (const task of process.argv.slice(2)) {
const fn = actionTable[task];
if (typeof fn !== 'function') {
printErr('task does not exist: ' + task);
continue;
}
// run the task synchronously
console.log(`--- ${task} ---`);
let code = isAsyncFunction(fn) ? await fn(task) : fn(task);
code = (code || 0) & 0xff;
if (code != 0) process.exit(code);
}