Skip to content

Commit

Permalink
Merge pull request webosbrew#172 from throwaway96/fix-exec-20240305
Browse files Browse the repository at this point in the history
Fix /exec on webOS 1
  • Loading branch information
throwaway96 authored Mar 5, 2024
2 parents 6298cc3 + acd94df commit c2336b0
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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') {

Check failure on line 624 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

Unnecessary 'else' after 'return'

Check failure on line 624 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["command"] is better written in dot notation
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(),
Expand All @@ -629,7 +652,7 @@ function runService() {
service.register('spawn', (message) => {
const payload = message.payload as ExecPayload;
const respond = (event: string, args: Record<string, any>) => 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(),
Expand Down

0 comments on commit c2336b0

Please sign in to comment.