Skip to content

Commit 87419d5

Browse files
committed
chore: improve tooling
1 parent 480265f commit 87419d5

File tree

4 files changed

+174
-47
lines changed

4 files changed

+174
-47
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# exclude files
2-
debug-stick.mcpack
2+
debug-stick*.mcpack
3+
debug-stick*.zip
4+
35
node_modules
46
package-lock.json
7+
58
pack/scripts/*

pack.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

package.json

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
{
22
"name": "debug-stick",
3-
"version": "1.0.0",
4-
"description": "Debug stick for minecraft bedrock",
3+
"type": "module",
54
"scripts": {
65
"test": "npx tsc",
7-
"build": "npx tsc && python pack.py",
8-
"clean": "rm -rf debug-stick.mcpack pack/scripts/*"
6+
"build": "node tool.js build pack",
7+
"clean": "node tool.js clean",
8+
"watch": "node tool.js watch"
99
},
10-
"repository": {
11-
"type": "git",
12-
"url": "git+https://github.com/vytdev/debug-stick.git"
13-
},
14-
"keywords": [
15-
"debug-stick",
16-
"add-on",
17-
"minecraft",
18-
"bedrock"
19-
],
20-
"author": "Vincent Yanzee J. Tan (vytdev)",
21-
"license": "MIT",
22-
"bugs": {
23-
"url": "https://github.com/vytdev/debug-stick/issues"
24-
},
25-
"homepage": "https://github.com/vytdev/debug-stick#readme",
2610
"dependencies": {
27-
"@minecraft/server": "^2.4.0",
11+
"@minecraft/server": "^2.4.0"
12+
},
13+
"devDependencies": {
14+
"jszip": "^3.10.1",
2815
"typescript": "^5.9.3"
2916
}
3017
}

tool.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env node
2+
3+
import child_process from 'child_process';
4+
import fs from 'fs/promises';
5+
import fsSync from 'fs';
6+
import path from 'path';
7+
import JSZip from 'jszip';
8+
import { isAsyncFunction } from 'util/types';
9+
10+
import tsconfig from './tsconfig.json' with { type: 'json' };
11+
import manifest from './pack/manifest.json' with { type: 'json' };
12+
13+
const packVersion = manifest.header.version;
14+
const packMinEngineVersion = manifest.header.min_engine_version.join('.');
15+
const actionTable = {};
16+
17+
18+
/* --- HELP --- */
19+
actionTable['help'] = () => console.error(
20+
`usage: ${process.argv[1]} [task...]\n` +
21+
'Utility script for working with the debug-stick project.\n' +
22+
'Available tasks:\n' +
23+
' help Shows this help\n' +
24+
' build Run: npx tsc --build\n' +
25+
' watch Run: npx tsc --watch\n' +
26+
' clean Remove generated files\n' +
27+
' pack Generate dist packages\n' +
28+
'@vytdev'
29+
);
30+
31+
32+
/* --- BUILD --- */
33+
actionTable['build'] = async () => {
34+
try {
35+
const code = await runProcessAsync('npx', 'tsc', '--build');
36+
console.log('typescript exited with code: ' + code);
37+
return code;
38+
}
39+
catch (e) {
40+
printErr(e);
41+
return -1;
42+
}
43+
};
44+
45+
46+
/* --- WATCH --- */
47+
actionTable['watch'] = async () => {
48+
try {
49+
const code = await runProcessAsync('npx', 'tsc', '--watch');
50+
console.log('typescript dev server exited with code: ' + code);
51+
return code;
52+
}
53+
catch (e) {
54+
printErr(e);
55+
return -1;
56+
}
57+
};
58+
59+
60+
/* --- CLEAN --- */
61+
actionTable['clean'] = async () => {
62+
await fs.rm(tsconfig.compilerOptions.outDir, {
63+
force: true,
64+
recursive: true
65+
});
66+
console.log('cleanup complete');
67+
};
68+
69+
70+
/* --- PACK --- */
71+
actionTable['pack'] = async () => {
72+
// name format for github
73+
await zipFolder('pack', 'debug-stick.zip');
74+
fs.copyFile('debug-stick.zip', 'debug-stick.mcpack');
75+
// name format for archiving
76+
fs.copyFile('debug-stick.zip',
77+
`debug-stick-${packVersion}.mcpack`);
78+
// name format for curseforge
79+
fs.copyFile('debug-stick.zip',
80+
`debug-stick-${packVersion}-r${packMinEngineVersion}.mcpack`);
81+
console.log('created distribution packages');
82+
};
83+
84+
85+
/**
86+
* Error printing utility.
87+
* @param msgs The messages.
88+
*/
89+
function printErr(...msgs) {
90+
console.error(`${process.argv[1]}:`, ...msgs);
91+
}
92+
93+
94+
/**
95+
* Asynchronously run a sub-process.
96+
* @param arg0 Name of or path to the executable.
97+
* @param args Arguments to pass to the process.
98+
* @returns A Promise.
99+
*/
100+
async function runProcessAsync(arg0, ...args) {
101+
return new Promise((resolve, reject) => {
102+
const subproc = child_process.spawn(arg0, args, { stdio: 'inherit' });
103+
// redirect sigint temporarily
104+
const sigIntHandler = () => subproc.kill('SIGINT');
105+
process.on('SIGINT', sigIntHandler);
106+
// handle success and failure
107+
subproc.on('close', code => {
108+
process.off('SIGINT', sigIntHandler);
109+
resolve(code || 0);
110+
});
111+
subproc.on('error', err => {
112+
process.off('SIGINT', sigIntHandler);
113+
reject(err)
114+
});
115+
});
116+
}
117+
118+
119+
/**
120+
* Zip an entire directory.
121+
* @param folderPath The folder to zip.
122+
* @param outPath Where to save the zip file.
123+
*/
124+
async function zipFolder(folderPath, outPath) {
125+
const zip = new JSZip();
126+
function addDirToZip(zipObj, folder) {
127+
const items = fsSync.readdirSync(folder);
128+
for (const item of items) {
129+
const fullPath = path.join(folder, item);
130+
const stats = fsSync.statSync(fullPath);
131+
if (stats.isDirectory()) {
132+
const subFolder = zipObj.folder(item);
133+
addDirToZip(subFolder, fullPath);
134+
} else {
135+
const data = fsSync.readFileSync(fullPath);
136+
zipObj.file(item, data);
137+
}
138+
}
139+
}
140+
addDirToZip(zip, folderPath);
141+
const content = await zip.generateAsync({ type: 'nodebuffer' });
142+
fsSync.writeFileSync(outPath, content);
143+
}
144+
145+
146+
// We must run from the repo folder.
147+
process.chdir(path.dirname(process.argv[1]));
148+
149+
// Run each task in given order.
150+
for (const task of process.argv.slice(2)) {
151+
const fn = actionTable[task];
152+
if (typeof fn !== 'function') {
153+
printErr('task does not exist: ' + task);
154+
continue;
155+
}
156+
157+
// run the task synchronously
158+
console.log(`--- ${task} ---`);
159+
let code = isAsyncFunction(fn) ? await fn(task) : fn(task);
160+
code = (code || 0) & 0xff;
161+
if (code != 0) process.exit(code);
162+
}

0 commit comments

Comments
 (0)