|
| 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