Created
April 8, 2024 08:50
-
-
Save VadimGush/7368c72baec4711d1510c53b349be024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { exec, spawn } from 'child_process'; | |
const args = process.argv; | |
// If false, it fails with the following error: | |
// 1 | import { exec, spawn } from 'child_process'; | |
// 2 | | |
// 3 | const args = process.argv; | |
// 4 | | |
// 5 | if (args.length < 4) { | |
// 6 | fail("Too few arguments are provided"); | |
// ^ | |
// ReferenceError: Cannot access uninitialized variable. | |
// at /Users/vadim/Workspace/game/build.js:6:3 | |
// if true, works normally 🤔 | |
if (args.length < 4) { | |
fail("Too few arguments are provided"); | |
} | |
build({ | |
"client": { | |
"compile": [ Client.compile ], | |
"run": [ Client.compile, Client.run ] | |
}, | |
}); | |
async function build(data) { | |
const target = data[args[2]]; | |
if (!target) fail(`Unsupported target '${args[2]}'`); | |
const operation = target[args[3]]; | |
if (!operation) fail(`Unsupported operation '${args[3]}'`); | |
for (const stage of operation) { | |
await stage(); | |
} | |
console.log("\n👍 Build completed"); | |
} | |
const Client = { | |
async compile() { | |
console.log("Compiling the client"); | |
await compile("client"); | |
await bundle("client", args[4]); | |
}, | |
async run() { | |
console.log("Running the client locally"); | |
const { error, stdout, stderr } = await runLive("client", "bun http-server . -c-1"); | |
if (error) fail(`Failed to run the client locally: ${error}`); | |
} | |
} | |
async function compile(project) { | |
const { error, stdout, stderr } = await run(project, "rm -rf build && bunx tsc"); | |
if (error) fail(`Failed to compile the project '${project}': ${error}`); | |
} | |
async function bundle(project, mode="development") { | |
const { error, stdout, stderr } = await run(project, `bunx webpack --mode=${mode}`); | |
if (error) fail(`Failed to bundle the project '${project}': ${error}`); | |
} | |
function run(dir, cmd) { | |
console.log(`Executing '${cmd}' in the directory '${dir}'`) | |
return new Promise((resolve, reject) => { | |
exec(`${cmd}`, { cwd: dir }, (error, stdout, stderr) => { | |
resolve({error, stdout, stderr}); | |
}); | |
}); | |
} | |
function runLive(dir, cmd) { | |
const promise = new Promise((resolve, reject) => { | |
console.log(`Executing '${cmd}' in the directoy '${dir}'`) | |
const process = spawn(cmd, { shell: true, cwd: dir }); | |
process.stdout.setEncoding("utf-8"); | |
process.stdout.on('data', (d) => console.log(d)); | |
process.stderr.setEncoding("utf-8"); | |
process.stderr.on('data', (d) => console.log(d)); | |
process.on('close', () => resolve()); | |
}); | |
return promise; | |
} | |
function fail(message) { | |
console.error(message); | |
process.exit(1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment