mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
chore: add yarn start:android-usb for local USB device testing
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn } from 'node:child_process';
|
||||
import net from 'node:net';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
export const repoRoot = path.resolve(__dirname, '..');
|
||||
export const isWindows = process.platform === 'win32';
|
||||
|
||||
function checkPort(port) {
|
||||
return new Promise((resolve) => {
|
||||
const server = net.createServer();
|
||||
|
||||
server.once('error', () => resolve(false));
|
||||
server.once('listening', () => {
|
||||
server.close(() => resolve(true));
|
||||
});
|
||||
server.listen(port);
|
||||
});
|
||||
}
|
||||
|
||||
export async function resolvePort(requestedPort) {
|
||||
let port = requestedPort;
|
||||
|
||||
while (!(await checkPort(port))) {
|
||||
port += 1;
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
export function startVite(host, port) {
|
||||
const child = spawn('corepack', ['yarn', 'exec', 'vite', '--host', host, '--port', String(port), '--strictPort'], {
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
PORTLESS: '0',
|
||||
},
|
||||
stdio: 'inherit',
|
||||
});
|
||||
|
||||
const forwardSignal = (signal) => {
|
||||
if (!child.killed) {
|
||||
child.kill(signal);
|
||||
}
|
||||
};
|
||||
|
||||
const onSigint = () => forwardSignal('SIGINT');
|
||||
const onSigterm = () => forwardSignal('SIGTERM');
|
||||
|
||||
process.on('SIGINT', onSigint);
|
||||
process.on('SIGTERM', onSigterm);
|
||||
|
||||
child.on('exit', (code, signal) => {
|
||||
process.off('SIGINT', onSigint);
|
||||
process.off('SIGTERM', onSigterm);
|
||||
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
|
||||
process.exit(code ?? 0);
|
||||
});
|
||||
|
||||
return child;
|
||||
}
|
||||
Reference in New Issue
Block a user