Skip to content

Instantly share code, notes, and snippets.

@jaandrle
Last active January 28, 2020 20:13
Show Gist options
  • Save jaandrle/f98a7f8145e7ee8695f0c43e265dbe14 to your computer and use it in GitHub Desktop.
Save jaandrle/f98a7f8145e7ee8695f0c43e265dbe14 to your computer and use it in GitHub Desktop.
node /bin/bg cordova run
/* jshint esversion: 6,-W097, -W040, node: true, expr: true, undef: true */
const /* utils */
{ max, round }= Math,
{ spawn }= require('child_process'),
{ writeFileSync, readFileSync }= require("fs");
const /* runtime arguments and cwd */
[ cmd, ...cmd_arguments ]= process.argv.slice(2),
cwd= process.cwd();
let iteration= -1;
const /* animation */
ani_array= [ "|", "/", "–", "\\" ],
ani_array_length= ani_array.length,
incAni= function(){ iteration+= 1; if(iteration===ani_array_length){ iteration= 0; } },
nextAni= ()=> ": "+ani_array[iteration]+(prev_data_length ? ` (${round(100*data_buffer.length/prev_data_length)}% – based on previous log file)` : "");
let mesage= [ "\x1Bc\033c", (new Date()).toString(),`Running '${cmd}' with '${cmd_arguments.join(" ")}' arguments.` ];
let data_buffer= mesage.join("\n")+"\n\n === LOG === \n\n";
const /* output file */
log_file= `${cmd}.log`,
prev_data_length= getPrevLogLength(log_file);
mesage.push("executing…");
log();
const run_cmd= spawn(cmd+(cmd==="cordova"?".cmd":""), cmd_arguments, { cwd });
mesage.push("running");
const interval= setInterval(logAni, 500);
run_cmd.stdout.on("data", function(data){ data_buffer+= data; });
run_cmd.on("error", function(err){
data_buffer+= err&&err.toString ? err.toString() : "";
if(interval) clearInterval(interval);
mesage.push("error detected");
});
run_cmd.on("close", function(){
if(interval) clearInterval(interval);
mesage.push("done");
try{
writeFileSync(log_file, data_buffer);
} catch(e) {
mesage.push(`unable to create '${cmd}.log', see ${e}.`);
}
log();
});
function logAni(){
incAni();
log(nextAni());
}
function log(text= ""){
console.log(mesage.join("\n")+text);
}
function getPrevLogLength(log_file){
let data_length;
try{
data_length= readFileSync(log_file).toString().length;
if(!max(data_length-data_buffer.length, 0)) return 0;
return data_length;
} catch (e) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment