Skip to content

Instantly share code, notes, and snippets.

@Vadorequest
Last active June 24, 2021 13:23
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Vadorequest/f72fa1c152ec55357839 to your computer and use it in GitHub Desktop.
Save Vadorequest/f72fa1c152ec55357839 to your computer and use it in GitHub Desktop.
define(["require", "exports"], function (require, exports) {
/**
* Helper to use the Command Line Interface (CLI) easily with both Windows and Unix environments.
* Requires underscore or lodash as global through "_".
*/
var Cli = (function () {
function Cli() {
}
/**
* Execute a CLI command.
* Manage Windows and Unix environment and try to execute the command on both env if fails.
* Order: Windows -> Unix.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success.
* @param callbackErrorWindows Failure on Windows env.
* @param callbackErrorUnix Failure on Unix env.
*/
Cli.execute = function (command, args, callback, callbackErrorWindows, callbackErrorUnix) {
if (args === void 0) { args = []; }
Cli.windows(command, args, callback, function () {
callbackErrorWindows();
try {
Cli.unix(command, args, callback, callbackErrorUnix);
}
catch (e) {
console.log('------------- Failed to perform the command: "' + command + '" on all environments. -------------');
}
});
};
/**
* Execute a command on Windows environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
Cli.windows = function (command, args, callback, callbackError) {
if (args === void 0) { args = []; }
try {
Cli._execute(process.env.comspec, _.union(['/c', command], args));
callback(command, args, 'Windows');
}
catch (e) {
callbackError(command, args, 'Windows');
}
};
/**
* Execute a command on Unix environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
Cli.unix = function (command, args, callback, callbackError) {
if (args === void 0) { args = []; }
try {
Cli._execute(command, args);
callback(command, args, 'Unix');
}
catch (e) {
callbackError(command, args, 'Unix');
}
};
/**
* Execute a command no matters what's the environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @private
*/
Cli._execute = function (command, args) {
var spawn = require('child_process').spawn;
var childProcess = spawn(command, args);
childProcess.stdout.on("data", function (data) {
console.log(data.toString());
});
childProcess.stderr.on("data", function (data) {
console.error(data.toString());
});
};
return Cli;
})();
exports.Cli = Cli;
});
/**
* Helper to use the Command Line Interface (CLI) easily with both Windows and Unix environments.
* Requires underscore or lodash as global through "_".
*/
var Cli = (function () {
function Cli() {
}
/**
* Execute a CLI command.
* Manage Windows and Unix environment and try to execute the command on both env if fails.
* Order: Windows -> Unix.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success.
* @param callbackErrorWindows Failure on Windows env.
* @param callbackErrorUnix Failure on Unix env.
*/
Cli.execute = function (command, args, callback, callbackErrorWindows, callbackErrorUnix) {
if (args === void 0) { args = []; }
Cli.windows(command, args, callback, function () {
callbackErrorWindows();
try {
Cli.unix(command, args, callback, callbackErrorUnix);
}
catch (e) {
console.log('------------- Failed to perform the command: "' + command + '" on all environments. -------------');
}
});
};
/**
* Execute a command on Windows environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
Cli.windows = function (command, args, callback, callbackError) {
if (args === void 0) { args = []; }
try {
Cli._execute(process.env.comspec, _.union(['/c', command], args));
callback(command, args, 'Windows');
}
catch (e) {
callbackError(command, args, 'Windows');
}
};
/**
* Execute a command on Unix environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
Cli.unix = function (command, args, callback, callbackError) {
if (args === void 0) { args = []; }
try {
Cli._execute(command, args);
callback(command, args, 'Unix');
}
catch (e) {
callbackError(command, args, 'Unix');
}
};
/**
* Execute a command no matters what's the environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @private
*/
Cli._execute = function (command, args) {
var spawn = require('child_process').spawn;
var childProcess = spawn(command, args);
childProcess.stdout.on("data", function (data) {
console.log(data.toString());
});
childProcess.stderr.on("data", function (data) {
console.error(data.toString());
});
};
return Cli;
})();
exports.Cli = Cli;
/**
* Helper to use the Command Line Interface (CLI) easily with both Windows and Unix environments.
* Requires underscore or lodash as global through "_".
*/
export class Cli {
/**
* Execute a CLI command.
* Manage Windows and Unix environment and try to execute the command on both env if fails.
* Order: Windows -> Unix.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success.
* @param callbackErrorWindows Failure on Windows env.
* @param callbackErrorUnix Failure on Unix env.
*/
public static execute(command: string, args: string[] = [], callback?: any, callbackErrorWindows?: any, callbackErrorUnix?: any){
Cli.windows(command, args, callback, function(){
callbackErrorWindows();
try{
Cli.unix(command, args, callback, callbackErrorUnix);
}catch(e){
console.log('------------- Failed to perform the command: "' + command + '" on all environments. -------------');
}
});
}
/**
* Execute a command on Windows environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
public static windows(command: string, args: string[] = [], callback?: any, callbackError?: any){
try{
Cli._execute(process.env.comspec, _.union(['/c', command], args));
callback(command, args, 'Windows');
}catch(e){
callbackError(command, args, 'Windows');
}
}
/**
* Execute a command on Unix environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @param callback Success callback.
* @param callbackError Failure callback.
*/
public static unix(command: string, args: string[] = [], callback?: any, callbackError?: any){
try{
Cli._execute(command, args);
callback(command, args, 'Unix');
}catch(e){
callbackError(command, args, 'Unix');
}
}
/**
* Execute a command no matters what's the environment.
*
* @param command Command to execute. ('grunt')
* @param args Args of the command. ('watch')
* @private
*/
private static _execute(command, args){
var spawn = require('child_process').spawn;
var childProcess = spawn(command, args);
childProcess.stdout.on("data", function(data) {
console.log(data.toString());
});
childProcess.stderr.on("data", function(data) {
console.error(data.toString());
});
}
}
define(["require", "exports", './Cli'], function (require, exports, cli) {
var Cli = cli.Cli;
/**
* Tool to regenerate the generated files from Node servers.
*/
var Grunt = (function () {
function Grunt() {
}
/**
* Execute a grunt command.
* @param args Args of the command. ('watch')
*/
Grunt.grunt = function (args) {
if (args === void 0) { args = []; }
Cli.execute(Grunt._command, args, function (command, args, env) {
console.log('Grunt has been automatically executed. (' + env + ')');
}, function (command, args, env) {
console.error('------------- Windows "' + command + '" command failed, trying Unix... ---------------');
}, function (command, args, env) {
console.error('------------- Unix "' + command + '" command failed too. ---------------');
});
};
/**
* Command name to execute.
* @type {string}
* @private
*/
Grunt._command = 'grunt';
return Grunt;
})();
exports.Grunt = Grunt;
});
var cli = require('./Cli');
var Cli = cli.Cli;
/**
* Tool to regenerate the generated files from Node servers.
*/
var Grunt = (function () {
function Grunt() {
}
/**
* Execute a grunt command.
* @param args Args of the command. ('watch')
*/
Grunt.grunt = function (args) {
if (args === void 0) { args = []; }
Cli.execute(Grunt._command, args, function (command, args, env) {
console.log('Grunt has been automatically executed. (' + env + ')');
}, function (command, args, env) {
console.error('------------- Windows "' + command + '" command failed, trying Unix... ---------------');
}, function (command, args, env) {
console.error('------------- Unix "' + command + '" command failed too. ---------------');
});
};
/**
* Command name to execute.
* @type {string}
* @private
*/
Grunt._command = 'grunt';
return Grunt;
})();
exports.Grunt = Grunt;
import cli = require('./Cli');
var Cli = cli.Cli;
/**
* Tool to regenerate the generated files from Node servers.
*/
export class Grunt {
/**
* Command name to execute.
* @type {string}
* @private
*/
private static _command: string = 'grunt';
/**
* Execute a grunt command.
* @param args Args of the command. ('watch')
*/
public static grunt(args: string[] = []){
Cli.execute(Grunt._command, args, function(command, args, env){
console.log('Grunt has been automatically executed. ('+env+')');
}, function(command, args, env){
console.error('------------- Windows "' + command + '" command failed, trying Unix... ---------------');
}, function(command, args, env){
console.error('------------- Unix "' + command + '" command failed too. ---------------');
});
}
}
@Vadorequest
Copy link
Author

@galinyc1
Copy link

Added CLI and Grunt to my Angular project but I'm getting this error message: "ERROR in src/app/CLI.ts(40,26): error TS2304: Cannot find name 'process'."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment