Skip to content

Commit

Permalink
Return exit code from spawned process in run scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fobos committed Nov 8, 2016
1 parent f47597c commit 9179b76
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ template/package.json

# Desktop Services Store on macOS
.DS_Store

/test-app/
90 changes: 51 additions & 39 deletions bin/global-cli.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,51 @@
#!/usr/bin/env node

const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const commands = argv._;
const spawn = require('cross-spawn');
const argv = require('minimist')(process.argv.slice(2));
const executablePaths = require('elm/platform').executablePaths;

const version = require('../package.json').version;
const elmPlatformVersion = require('../node_modules/elm/package.json').version;
const paths = require('../config/paths');

function help (version) {
console.log('\nUsage: elm-app <command>\n');
console.log('where <command> is one of:');
console.log(' create, build, start, package, reactor, make, repl\n');
console.log('\nElm ' + elmPlatformVersion + '\n');
console.log('create-elm-app@' + version + ' ' + path.resolve(__dirname, '..'));
}

const commands = argv._;

if (commands.length === 0) {
help(version);
process.exit(1);
}

var script = commands[ 0 ];
const script = commands[0];
const scriptPath = path.resolve(paths.scripts, script);
const scriptArgs = commands.splice(1);

switch (script) {
case 'create':
spawn.sync(
'node',
[ path.resolve(paths.scripts, script), commands.splice(1) ],
{ stdio: 'inherit' }
);
break;
case 'build':
spawn.sync(
'node',
[ path.resolve(paths.scripts, script) ],
{ stdio: 'inherit' }
);
break;

case 'eject':
spawn.sync(
'node',
[ path.resolve(paths.scripts, script) ],
{ stdio: 'inherit' }
);
break;

case 'start':
spawn.sync(
'node',
[ path.resolve(paths.scripts, script) ],
{ stdio: 'inherit' }
);
spawnSyncNode(scriptPath, scriptArgs);
break;

case 'test':
spawn.sync(
case 'test': {
const cp = spawn.sync(
path.resolve(__dirname, '..', 'node_modules/elm-test/bin/elm-test'),
[ '--compiler', path.normalize(executablePaths['elm-make']) ],
{ stdio: 'inherit' }
);

if (cp.status !== 0) {
process.exit(cp.status);
}

break;
}
default:

// Proxy elm-platform cli commands.
if ([ 'package', 'reactor', 'make', 'repl' ].indexOf(script) !== -1) {
var executable = executablePaths[ 'elm-' + script ];
const executable = executablePaths[ 'elm-' + script ];

spawn.sync(
path.normalize(executable),
Expand All @@ -77,7 +55,41 @@ switch (script) {
break;
} else {
help(version);
process.exit(1);
}

break;
}

/**
*Prints help message
*
* @param {string} version [description]
* @return {undefined}
*/
function help (version) {
console.log('\nUsage: elm-app <command>\n');
console.log('where <command> is one of:');
console.log(' create, build, start, package, reactor, make, repl\n');
console.log('\nElm ' + elmPlatformVersion + '\n');
console.log('create-elm-app@' + version + ' ' + path.resolve(__dirname, '..'));
}

/**
* Spawn separate node process with specified script
*
* @param {string} script Path to script
* @param {Arrays} args Script arguments
* @return {undefined}
*/
function spawnSyncNode(script, args) {
const cp = spawn.sync(
'node',
[script].concat(args || []),
{ stdio: 'inherit' }
);

if (cp.status !== 0) {
process.exit(cp.status);
}
}

0 comments on commit 9179b76

Please sign in to comment.