Skip to content

Commit

Permalink
#187 Moved os platform selection to a reusable function
Browse files Browse the repository at this point in the history
  • Loading branch information
czprz committed May 18, 2023
1 parent e10cec8 commit cda50cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
34 changes: 6 additions & 28 deletions bin/common/executor/executions/tye/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {ExecutionInterface} from "../../models.js";
import docker from "../../../helper/docker/index.js";
import platformSelector from "../../../helper/platform-selector.js";

import {exec, execSync} from "child_process";
import path from "path";
import os from "os";

"use strict";
export default new class extends ExecutionInterface {
Expand Down Expand Up @@ -46,7 +46,11 @@ export default new class extends ExecutionInterface {
command = this.#addConfigFile(command, execute);

const workingDir = execute.location;
const shellCommand = this.#makeCommand(workingDir, command);
const shellCommand = platformSelector.get({
windows: `start powershell.exe -command "cd ${workingDir} ; ${command}"`,
darwin: `osascript -e 'tell app "Terminal" to do script "cd ${workingDir} ; ${command}"'`,
linux: `gnome-terminal --working-directory="${workingDir}" -- bash -c "${command}"`,
});

const childProcess = exec(shellCommand, {
detached: true,
Expand All @@ -63,32 +67,6 @@ export default new class extends ExecutionInterface {
}
}

/**
* Constructs shell command
* @param workingDir {string}
* @param command {string}
* @returns {string}
*/
#makeCommand(workingDir, command) {
let shellCommand = '';

switch (os.platform()) {
case 'win32':
shellCommand = `start powershell.exe -command "cd ${workingDir} ; ${command}"`;
break;
case 'darwin':
shellCommand = `osascript -e 'tell app "Terminal" to do script "cd ${workingDir} ; ${command}"'`;
break;
case 'linux':
shellCommand = `gnome-terminal --working-directory="${workingDir}" -- bash -c "${command}"`;
break;
default:
throw new Error(`Unsupported platform: ${os.platform()}`);
}

return shellCommand;
}

/**
* Adds config files to command
* @param command {string}
Expand Down
38 changes: 38 additions & 0 deletions bin/common/helper/platform-selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os from "os";

export default new class {
/**
* Get the platform specific command
* @param {Platforms} platforms
* @returns {string}
*/
get(platforms) {
switch (os.platform()) {
case 'win32':
return platforms.windows;
case 'darwin':
return platforms.darwin;
case 'linux':
return platforms.linux;
default:
throw new Error(`Unsupported platform: ${os.platform()}`);
}
}
}

export class Platforms {
/**
* @type {string} windows
*/
windows;

/**
* @type {string} darwin
*/
darwin;

/**
* @type {string} linux
*/
linux;
}

0 comments on commit cda50cd

Please sign in to comment.