-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-utils.js
executable file
·203 lines (166 loc) · 5.94 KB
/
run-utils.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
import { normalize } from 'path';
import { exec, spawn, execSync } from 'child_process';
// To import spawnargs, we have to do some heavier lifting, because it is a "native" module.
import { createRequire } from 'module';
const require = createRequire( import.meta.url );
const spawnargs = require( 'spawn-args' );
//=========== run_command_sync_to_console: run one command and let output immediately flow to console ============
export const run_command_sync_to_console = function ( cmd ) {
execSync( cmd, { stdio: [ 0, 1, 2 ]}, function ( error, stdout, stderr ) {
if ( error ) {
console.log( '======= RUN ERROR =======' );
console.log( error );
// throw error;
}
} );
}
//=========== run_command_async_to_console: async run one command and dump output to console when complete ============
export const run_command_async_to_console = function ( cmd ) {
exec( cmd, function ( error, stdout, stderr ) {
if ( stdout.length > 0 ) console.log( stdout );
if ( stderr.length > 0 ) console.log( stderr );
} );
}
//=========== run_command_quietly: runs without output unless error ============
export const run_command_quietly = function ( cmd ) {
execSync( cmd, function ( error, stdout, stderr ) {
if ( error ) {
console.log( '======= RUN ERROR =======' );
console.log( error );
}
} );
}
// =========== run_command: run one command and get output ============
// Run a command asynchronously and get the output when it finishes in a callback.
// Usage:
// run_command( "ls -l", function(err,text) { console.log (text) });
export const run_command = function ( cmd, callBack ) {
// TODO convert to exec()
var fullargs = spawnargs( cmd );
cmd = fullargs[ 0 ];
var args = fullargs.slice( 1 );
// DEBUG this works!
// var args = ["status"];
// cmd = "git";
// DEBUG this works!
// cmd = "git";
// var args = ["commit","-a","-m",'"synctrouble"'];
// DEBUG:
// console.log('cmd args: ' + cmd + ' ----- ' + args);
var child = spawn( cmd, args );
// var child = spawn(cmd, args, {stdio: "inherit"});
var resp = "";
var errr = "";
child.stdout.on( 'data', function ( buffer ) { resp += buffer.toString() } );
child.stderr.on( 'data', function ( buffer ) { errr += buffer.toString() } );
child.stdout.on( 'end', function () { callBack( errr, resp ) } );
}
// =========== run_command_sync: run one command and get output ============
// Run a command synchronously and get the output when it finishes.
// Usage:
// var lsout = run_command_sync("ls -l");
export const run_command_sync = function ( cmd ) {
return execSync( cmd, function ( error, stdout, stderr ) {
if ( error ) {
console.log( '======= RUN ERROR =======' );
console.log( error );
}
if ( stderr ) {
console.log( '======= RUN stderr =======' );
console.log( stderr );
}
} ).toString();
}
// =========== runsteps: run a specific set of commands in specific directories ============
// Given: steps, an array of {name,folder,cmd} objects, eg:
//
// var my_steps =
// [
// { name: 'update Project' , folder: '.' , cmd: 'git pull' },
// { name: 'build Project' , folder: '.' , cmd: 'make' },
// ];
//
// This function runs the requested steps.
// Verbosity can be:
// "quiet" no output
// undefined normal output - the command output is logged to console
// "verbose" more output - step name, folder, command output
// Define async to get asynchronous execution.
export const runsteps = function ( steps, verbosity, async ) {
steps.forEach( function ( step, i ) {
try {
process.chdir( normalize( step.folder ) );
if ( async ) {
run_command( step.cmd, function ( text ) {
if ( verbosity == "verbose" ) {
console.log( '------------------------------' );
console.log( 'step: ' + step.name );
console.log( '------------------------------' );
console.log( step.folder + '> ' + step.cmd );
}
console.log( text )
} );
} else if ( verbosity != "quiet" ) {
if ( verbosity == "verbose" ) {
console.log( '------------------------------' );
console.log( 'step: ' + step.name );
console.log( '------------------------------' );
console.log( step.folder + '> ' + step.cmd );
}
run_command_sync_to_console( step.cmd );
} else {
run_command_quietly( step.cmd );
}
}
catch ( err ) {
if ( verbosity == "verbose" ) {
console.log( err );
}
}
} );
}
export const hasArg = ( args, a ) => args?.some( ar => ar === a );
export const combine_params = function ( params, separator = " " ) {
var result = "";
if ( params?.length > 0 ) {
params.forEach( function ( val, index, array ) {
if ( index != 0 ) { result += separator; }
result += val;
} );
}
return result;
}
export const distro = function () {
var distroname = run_command_sync( 'lsb_release -i' ).split( ":" )[ 1 ].trim().toLowerCase();
return distroname
}
export const hostname = function () {
var hostname = run_command_sync( 'hostname' ).trim().toLowerCase()
return hostname
}
export const ping = host => {
try {
run_command_sync( `ping -q -w 1 -c 1 ${host} > /dev/null && echo true || echo false` );
} catch ( e ) {
return false;
}
return true;
}
export const ping_google = () => {
return ping( 'google.com' );
}
export const ping_gw = () => {
try {
run_command_sync( "ping -q -w 1 -c 1 `ip r | grep default | head -n 1 | cut -d ' ' -f 3` > /dev/null && echo true || echo false" );
} catch ( e ) {
return false;
}
return true;
}
// This is a sleep for when you are in an async function, called eg:
// await sleep(500);
// See: https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
export const sleep = function ( ms ) {
return new Promise( resolve => setTimeout( resolve, ms ) );
}