diff --git a/services/service.ts b/services/service.ts index 40b1541..4620517 100644 --- a/services/service.ts +++ b/services/service.ts @@ -39,6 +39,18 @@ const homebrewBaseDir = ((): string | null => { } })(); +const nodeVersion = (() => { + try { + // Just in case there's a build/pre-release suffix. + const core = process.versions.node.split(/[-+]/, 1)[0] as string; + const [major, minor = 0, patch = 0] = core.split('.').map((x) => parseInt(x, 10)); + return { major, minor, patch }; + } catch (err) { + console.warn('getting nodeVersion failed:', err); + return { major: 0, minor: 0, patch: 0 }; + } +})(); + // Maps internal setting field name with filesystem flag name. type FlagName = string; const availableFlags = { @@ -605,9 +617,20 @@ function runService() { * Executes a shell command and responds with exit code, stdout and stderr. */ type ExecPayload = { command: string }; - service.register('exec', (message) => { + service.register('exec', (message: Message) => { + if (!('command' in message.payload)) { + message.respond(makeError('missing "command"')); + return; + } else if (typeof message.payload['command'] !== 'string') { + message.respond(makeError('"command" is not a string')); + return; + } + const payload = message.payload as ExecPayload; - child_process.exec(payload.command, { encoding: 'buffer' }, (error, stdout, stderr) => { + + const encoding = nodeVersion.major === 0 && nodeVersion.minor < 12 ? null : 'buffer'; + + child_process.exec(payload.command, { encoding }, (error, stdout, stderr) => { const response = { error, stdoutString: stdout.toString(), @@ -629,7 +652,7 @@ function runService() { service.register('spawn', (message) => { const payload = message.payload as ExecPayload; const respond = (event: string, args: Record) => message.respond({ event, ...args }); - const proc = child_process.spawn('/bin/sh', ['-c', payload.command]); + const proc = child_process.spawn('/bin/sh', ['-c', '--', payload.command]); proc.stdout.on('data', (data) => respond('stdoutData', { stdoutString: data.toString(),