forked from CleverStack/cleverstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
72 lines (60 loc) · 1.64 KB
/
command.js
File metadata and controls
72 lines (60 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var spawn = require( 'child_process' ).spawn
, path = require( 'path' )
, fs = require( 'fs' );
/**
* Launches the main clever bin program
*
* @param {String} _path_
* @param {Commander} program
* @param {String} cmd
* @param {String[]} args
* @api public
*/
module.exports = function ( _path_, program, cmd, args ) {
if (arguments.length < 4) {
args = cmd;
cmd = program;
program = require( path.join( __dirname, 'program' ) );
}
if (arguments.length < 2) {
cmd = 'clever';
}
if (!cmd) {
program.help();
}
// executable name
var bin = 'clever-' + cmd;
if (cmd === "clever") {
bin = 'clever';
}
// local or resolve to absolute executable path
var local = path.join( _path_, bin );
if (fs.existsSync( local )) {
bin = local;
} else {
bin = process.env.PATH.split(':').reduce( function (binary, p) {
p = path.resolve( p, bin );
return fs.existsSync( p ) && fs.statSync( p ).isFile() ? p : binary;
}, bin );
}
// if the bin doesn't exist within the cleverstack binary
// try finding it within the project/module folder...
if (!fs.existsSync( bin )) {
bin = path.join( __dirname, cmd );
// if bin/cmd doesn't exist, then try clever- prefix
if (!fs.existsSync( bin )) {
bin = path.join( __dirname, 'clever-' + cmd );
}
}
// display help if bin does not exist
if (!fs.existsSync( bin )) {
console.error( '\n %s command does not exist', bin );
program.help( );
process.exit( 0 );
}
// spawn
var proc = spawn( bin, args, { stdio: 'inherit' } );
proc.on( 'close', function ( code ) {
process.exit( code );
} );
}