Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(os): proper support for windows os (#6)
- Loading branch information
1 parent
dfcf014
commit 2dc9dc9
Showing
5 changed files
with
166 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
const opn = require('opn') | ||
const debug = require('debug')('opn-shell') | ||
|
||
function getLinuxTerminalHandlers(path) { | ||
const terminalApps = [ | ||
// MacOS variations of terminal apps | ||
'Hyper', | ||
'iTerm', | ||
'terminal.app', | ||
// Linux variations of terminal apps | ||
['x-terminal-emulator', '-e'], | ||
['gnome-terminal', '-e'], | ||
['konsole', '-e'], | ||
['xterm', '-e'], | ||
['urxvt', '-e'], | ||
[process.env.COLORTERM, '-e'], | ||
[process.env.XTERM, '-e'] | ||
] | ||
|
||
const handlers = terminalApps.map(appInfo => { | ||
debug('adding terminal configuration: %s', appInfo.toString()) | ||
return () => opn(path, {app: appInfo}) | ||
}) | ||
|
||
handlers.push(() => opn(path)) | ||
return handlers | ||
} | ||
|
||
module.exports = getLinuxTerminalHandlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable security/detect-child-process */ | ||
'use strict' | ||
|
||
const debug = require('debug')('opn-shell') | ||
const path = require('path') | ||
const childProcess = require('child_process') | ||
|
||
function getWindowsTerminalHandlers(scriptPath) { | ||
const extension = path.extname(scriptPath).toLowerCase() | ||
const logAddedTerminal = terminal => debug(`'adding terminal configuration: ${terminal}'`) | ||
|
||
let command | ||
if (getCmdSupportedExtensions().includes(extension)) { | ||
command = `start cmd /c "${scriptPath}"` | ||
logAddedTerminal('cmd') | ||
} else { | ||
command = `start powershell "${scriptPath}"` | ||
logAddedTerminal('powershell') | ||
} | ||
|
||
const handler = () => | ||
new Promise((resolve, reject) => { | ||
const cp = childProcess.exec(command) | ||
|
||
cp.once('error', reject) | ||
cp.once( | ||
'close', | ||
code => (code === 0 ? resolve(cp) : reject(new Error('Exited with code ' + code))) | ||
) | ||
}) | ||
|
||
return [handler] | ||
} | ||
|
||
function getCmdSupportedExtensions() { | ||
// PATHEXT contains semicolon separated list of file extensions which are considered to be executable by Windows | ||
// (runnable by cmd) | ||
const extensions = process.env.PATHEXT.toLowerCase().split(';') | ||
return extensions | ||
} | ||
|
||
module.exports = getWindowsTerminalHandlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
function getTerminals(executablePath) { | ||
const module = /^win/i.test(process.platform) | ||
? './get-terminals-windows' | ||
: './get-terminals-linux' | ||
return require(module)(executablePath) | ||
} | ||
|
||
module.exports = getTerminals |