|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawn } from 'child_process' |
| 3 | +import { program } from 'commander' |
| 4 | +import { version } from '../package.json' |
| 5 | +import { registerService } from './register-service' |
| 6 | +import { startServer } from './start-server' |
| 7 | +import { unregisterService } from './unregister-service' |
| 8 | + |
| 9 | +type TSignalMap = { |
| 10 | + [k in NodeJS.Signals]?: number |
| 11 | +} |
| 12 | + |
| 13 | +const signals: TSignalMap = { |
| 14 | + SIGHUP: 1, |
| 15 | + SIGINT: 2, |
| 16 | + SIGTERM: 15, |
| 17 | +} |
| 18 | + |
| 19 | +program.version(version) |
| 20 | + |
| 21 | +program |
| 22 | + .command('register') |
| 23 | + .description('register service through portz server') |
| 24 | + .requiredOption('-n, --name [name]', 'service name') |
| 25 | + .option('-d, --deps <name...>', 'dependency name') |
| 26 | + .action(async ({ name, deps }) => { |
| 27 | + const result = await registerService({ name, deps }) |
| 28 | + const dashDashArgIndex = program.args.findIndex((arg) => arg === '--') |
| 29 | + |
| 30 | + if (dashDashArgIndex === -1) { |
| 31 | + throw new Error('Arguments to spawn a process are required') |
| 32 | + } |
| 33 | + |
| 34 | + const spawnCommand = program.args[dashDashArgIndex + 1] |
| 35 | + const spawnArgs = program.args.slice(dashDashArgIndex + 2) |
| 36 | + |
| 37 | + const depsEnv: { [k: string]: string } = {} |
| 38 | + |
| 39 | + if (result.deps != null) { |
| 40 | + for (const [name, port] of Object.entries(result.deps)) { |
| 41 | + depsEnv[`PORT_${name.toUpperCase()}`] = String(port) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const childProcess = spawn(spawnCommand, spawnArgs, { |
| 46 | + stdio: 'inherit', |
| 47 | + env: { |
| 48 | + ...process.env, |
| 49 | + ...depsEnv, |
| 50 | + PORT: String(result.port), |
| 51 | + }, |
| 52 | + }) |
| 53 | + |
| 54 | + childProcess.on('error', async (err) => { |
| 55 | + console.error(err) |
| 56 | + await unregisterService(name) |
| 57 | + process.exit(1) |
| 58 | + }) |
| 59 | + |
| 60 | + const kill = async (signal: NodeJS.Signals) => { |
| 61 | + await unregisterService(name) |
| 62 | + console.log('\nbye') |
| 63 | + // https://github.com/npm/cli/issues/1591 |
| 64 | + // https://nodejs.org/api/process.html#process_exit_codes |
| 65 | + process.exit(128 + signals[signal]!) |
| 66 | + } |
| 67 | + |
| 68 | + process.on('SIGTERM', kill) |
| 69 | + process.on('SIGINT', kill) |
| 70 | + process.on('SIGHUP', kill) |
| 71 | + }) |
| 72 | + |
| 73 | +program |
| 74 | + .command('unregister') |
| 75 | + .description('unregister service through portz server') |
| 76 | + .requiredOption('-n, --name [name]', 'service name') |
| 77 | + .action(({ name }) => unregisterService(name)) |
| 78 | + |
| 79 | +program |
| 80 | + .command('start') |
| 81 | + .description('start portz server') |
| 82 | + .requiredOption('-r, --range <from:to>', 'port range, separated by :', (value) => value.split(':')) |
| 83 | + .action(async ({ range }) => { |
| 84 | + const stopServer = await startServer({ |
| 85 | + fromPort: range[0], |
| 86 | + toPort: range[1], |
| 87 | + }) |
| 88 | + |
| 89 | + const kill = async (signal: NodeJS.Signals) => { |
| 90 | + await stopServer() |
| 91 | + console.log('\nbye') |
| 92 | + // https://github.com/npm/cli/issues/1591 |
| 93 | + // https://nodejs.org/api/process.html#process_exit_codes |
| 94 | + process.exit(128 + signals[signal]!) |
| 95 | + } |
| 96 | + |
| 97 | + process.on('SIGTERM', kill) |
| 98 | + process.on('SIGINT', kill) |
| 99 | + process.on('SIGHUP', kill) |
| 100 | + |
| 101 | + console.log('portz server is up and ready') |
| 102 | + }) |
| 103 | + |
| 104 | +program |
| 105 | + .parseAsync(process.argv) |
| 106 | + .catch((err) => { |
| 107 | + console.error(err) |
| 108 | + process.exit(1) |
| 109 | + }) |
0 commit comments