I've got a Node.js CLI I've been building using the Commander Library. Here's the code from the main execution file.
#!/usr/bin/env node
var program = require('commander');
var scmStash = require('./lib/hdqc/scmStash');
var command = {};
program
.version('0.0.1')
.option('-P, --Projects', 'List Projects')
.option('-R, --Repositories', 'List All Repositories on Server.')
.parse(process.argv);
function listProjects() {
scmStash.getProjectListing(function (data) {
for (var i = 0; i < data.size; i++) {
var project = data.values[i];
console.log(' ' + i + ' ' + project.name + ' @ ' + project.link.url);
}
})
}
if (program.Projects) {
console.log(' - Projects');
listProjects();
}
I've been building this in WebStorm, and when I call the command using the node.js running everything works perfectly. for example, if I run the WebStorm runner executing the ./strack -P command to output the project, the output looks like this...
node strack -P
- Projects
0 Business Insights @ /projects/BI
1 Platform @ /projects/HDP
2 H @ /projects/H
3 QC Application Code @ /projects/QCCODE
4 QC Design @ /projects/QCDESIGN
5 QC Reports @ /projects/QCREP
6 Sandbox @ /projects/SAN
7 Systemic Automation Tools @ /projects/SAT
8 The Swamp @ /projects/SWAMP
However when I run the same 'strack' command from the standard bash (inside WebStorm or outside of WebStorm in iTerm or such) then the following output is displayed.
23:11 $ node strack -P
- Projects
As I wrote up this question, I - like so often happens when typing up a stackoverflow question - realized the dillemma. The other call that prints out the projects themselves, is an asynchronous call, the actual app shoots off that call and then executes the remaining lines of code and finishes. Before the projects are even returned and can be printed to the console. I'm not sure what WebStorm is doing to keep the console attached to the running process but I'd love to have that work for my CLI. Any ideas, thoughts, or suggestions on how I should redesign this application to actually print out the projects to the command line?
All of the code is available on the github repo here.