Skip to content

Instantly share code, notes, and snippets.

@a7ul
Last active March 24, 2020 04:18
Show Gist options
  • Save a7ul/a778325dc73f847757060dd5f79353f1 to your computer and use it in GitHub Desktop.
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
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();
REM You can run the commands for the terminal in Windows here for example
cd /d %~dp0
cd ../../
npm run buildServer
# You can run the commands for the terminal in unix environment here for example
cur_dir=`dirname $0`
cd $cur_dir/../../
npm run buildServer
@a7ul
Copy link
Author

a7ul commented Jan 21, 2018

  • To run you just need to do node launchTerminal.js
  • This will launch a new terminal with the commands specified in startCommand.bat(in case of windows) or startCommand.command(incase of linux or mac)
    • needs shelljs as dependency - install it via npm install shelljs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment