Last active
March 24, 2020 04:18
-
-
Save a7ul/a778325dc73f847757060dd5f79353f1 to your computer and use it in GitHub Desktop.
Launch a command in new terminal - just like react native's metro bundler . This snippet is inspired from react-native source code
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
const path = require('path'); | |
const childProcess = require('child_process'); | |
const process = require('process'); | |
const shelljs = require('shelljs'); | |
const startServerInNewWindow = () => { | |
const scriptFile = /^win/.test(process.platform) ? | |
'startCommand.bat' : | |
'startCommand.command'; | |
const scriptsDir = path.resolve(__dirname); | |
const startCommandScript = path.resolve(scriptsDir, scriptFile); | |
const procConfig = {cwd: scriptsDir}; | |
const terminal = process.env.CUSTOM_TERMINAL; | |
shelljs.chmod('-c', '+x', startCommandScript); | |
if (process.platform === 'darwin') { | |
if (terminal) { | |
return childProcess.spawnSync('open', ['-a', terminal, startCommandScript], procConfig); | |
} | |
return childProcess.spawnSync('open', [startCommandScript], procConfig); | |
} else if (process.platform === 'linux') { | |
procConfig.detached = true; | |
if (terminal) { | |
return childProcess.spawn(terminal, ['-e', 'sh ' + startCommandScript], procConfig); | |
} | |
return childProcess.spawn('sh', [startCommandScript], procConfig); | |
} else if (/^win/.test(process.platform)) { | |
procConfig.detached = true; | |
procConfig.stdio = 'ignore'; | |
return childProcess.spawn('cmd.exe', ['/C', startCommandScript], procConfig); | |
} else { | |
console.log(`Cannot start the packager. Unknown platform ${process.platform}`); //eslint-disable-line | |
} | |
}; | |
startServerInNewWindow(); |
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
REM You can run the commands for the terminal in Windows here for example | |
cd /d %~dp0 | |
cd ../../ | |
npm run buildServer |
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
# You can run the commands for the terminal in unix environment here for example | |
cur_dir=`dirname $0` | |
cd $cur_dir/../../ | |
npm run buildServer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node launchTerminal.js
startCommand.bat
(in case of windows) orstartCommand.command
(incase of linux or mac)npm install shelljs