Skip to content

Commit

Permalink
railsware#800: execute commands in a cross-platform way
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed May 24, 2017
1 parent 2cf7a4d commit 060ce75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/PTY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ export async function linedOutputOf(command: string, args: string[], directory:
export async function executeCommandWithShellConfig(command: string): Promise<string[]> {
const sourceCommands = (await loginShell.existingConfigFiles()).map(fileName => `source ${fileName} &> /dev/null`);

return await linedOutputOf(loginShell.executableName, ["/c", `"${[...sourceCommands, command].join(" && ")}"`], process.env.HOME);
return await linedOutputOf(loginShell.executableName, [...loginShell.interactiveCommandSwitches, loginShell.combineCommands([...sourceCommands, command])], process.env.HOME);
}
27 changes: 23 additions & 4 deletions src/utils/Shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ abstract class Shell {
abstract get executableName(): string;
abstract get configFiles(): string[];
abstract get noConfigSwitches(): string[];
abstract get commandSwitches(): string[];
abstract get executeCommandSwitches(): string[];
abstract get interactiveCommandSwitches(): string[];
abstract get preCommandModifiers(): string[];
abstract get historyFileName(): string;
abstract get commandExecutorPath(): string;
abstract loadAliases(): Promise<string[]>;

abstract combineCommands(commands: string[]): string;

async existingConfigFiles(): Promise<string[]> {
const resolvedConfigFiles = this.configFiles.map(fileName => resolveFile(process.env.HOME, fileName));
return await filterAsync(resolvedConfigFiles, io.fileExists);
Expand All @@ -38,7 +41,11 @@ abstract class Shell {
}

abstract class UnixShell extends Shell {
get commandSwitches() {
get executeCommandSwitches() {
return ["-c"];
}

get interactiveCommandSwitches() {
return ["-i", "-c"];
}

Expand All @@ -49,6 +56,10 @@ abstract class UnixShell extends Shell {
loadAliases() {
return executeCommandWithShellConfig("alias");
}

combineCommands(commands: string[]) {
return `'${commands.join("; ")}'`;
}
}

class Bash extends UnixShell {
Expand Down Expand Up @@ -137,8 +148,12 @@ class Cmd extends Shell {
];
}

get commandSwitches() {
return ["/c"];
get executeCommandSwitches() {
return ["-c"];
}

get interactiveCommandSwitches() {
return ["-c"];
}

get noConfigSwitches() {
Expand All @@ -157,6 +172,10 @@ class Cmd extends Shell {
return Cmd.cmdPath;
}

combineCommands(commands: string[]) {
return `"${commands.join(" && ")}`;
}

async loadAliases() {
return [];
}
Expand Down

0 comments on commit 060ce75

Please sign in to comment.