203 lines
4.3 KiB
JavaScript
203 lines
4.3 KiB
JavaScript
//
|
|
//#region Imports ...
|
|
const os = require('os');
|
|
const process = require('process');
|
|
const { exec } = require("child_process");
|
|
const XFileTools = require('../tools/x-file.tools');
|
|
//#endregion
|
|
|
|
//
|
|
//#region Constants ...
|
|
//
|
|
const OS = {
|
|
Aix: 'aix',
|
|
Darwin: 'darwin',
|
|
FreeBSD: 'freebsd',
|
|
Linux: 'linux',
|
|
OpenBSD: 'openbsd',
|
|
SnOS: 'sunos',
|
|
Windows: 'win32'
|
|
}
|
|
|
|
//
|
|
const isWindows = process.platform === OS.Windows;
|
|
//#endregion
|
|
|
|
//
|
|
//#region Actions ...
|
|
//
|
|
//#region Pure shell commands ...
|
|
/**
|
|
* execute a command using NodeJS on shell ...
|
|
*
|
|
* @param {string} cmd command to execute ...
|
|
* @param {string} cwd working directory ...
|
|
*
|
|
* @returns Promise<any, errr> instance ...
|
|
*/
|
|
function execute(cmd, cwd) {
|
|
return new Promise((resolve, reject) => {
|
|
//
|
|
if (!cmd || cmd.toString().length === 0 || (cwd && !XFileTools.isDirectoryExists(cwd))) {
|
|
reject('invalid args ...');
|
|
return;
|
|
}
|
|
|
|
//
|
|
exec(cmd, { cwd }, (err, result, stdError) => {
|
|
//
|
|
if (err) {
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
//
|
|
if (stdError) {
|
|
//
|
|
// reject(stdError);
|
|
// return;
|
|
}
|
|
|
|
//
|
|
resolve(result);
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* determines a command exists on host or not ...
|
|
*
|
|
* @param {string} name specific command name ...
|
|
*
|
|
* @returns boolean Promise ...
|
|
*/
|
|
function checkCommandExists(name) {
|
|
return new Promise(resolve => {
|
|
//
|
|
if (!name) {
|
|
resolve(false);
|
|
return;
|
|
}
|
|
|
|
//
|
|
const cmd = isWindows ? `${name} >nul 2>&1` : `type ${name} >/dev/null 2>&1`;
|
|
execute(cmd).then(result => {
|
|
resolve(true);
|
|
})
|
|
.catch(err => {
|
|
resolve(false);
|
|
});
|
|
});
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region required commands state ...
|
|
/**
|
|
* check al required commands exists or not ...
|
|
*
|
|
* @returns
|
|
*/
|
|
async function isRequiredCommandsExists() {
|
|
//
|
|
let result = false;
|
|
|
|
//
|
|
// const isTarExists = await isTarCommandExists();
|
|
// const isCatExists = await isCatCommandExists();
|
|
// const isGrepExists = await isGrepCommandExists();
|
|
// const isSedExists = await isSedCommandExists();
|
|
// const isNpmExists = await isNpmCommandExists();
|
|
const isNgExists = await isNgCommandExists();
|
|
const isIonicExists = await isIonicCommandExists();
|
|
const isCordovaExists = await isCordovaCommandExists();
|
|
|
|
//
|
|
result = isNgExists &&
|
|
isIonicExists &&
|
|
isCordovaExists;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* retrieve required commands state object ...
|
|
*
|
|
* @returns
|
|
*/
|
|
async function getRequiredCommandsStates() {
|
|
//
|
|
const result = {};
|
|
|
|
//
|
|
// const isTarExists = await isTarCommandExists();
|
|
// const isCatExists = await isCatCommandExists();
|
|
// const isGrepExists = await isGrepCommandExists();
|
|
// const isSedExists = await isSedCommandExists();
|
|
// const isNpmExists = await isNpmCommandExists();
|
|
const isNgExists = await isNgCommandExists();
|
|
const isIonicExists = await isIonicCommandExists();
|
|
const isCordovaExists = await isCordovaCommandExists();
|
|
|
|
//
|
|
result['ng'] = isNgExists;
|
|
result['ionic'] = isIonicExists;
|
|
result['cordova'] = isCordovaExists;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region Commonly used Command Checkers ...
|
|
/**
|
|
* determines ng command exists or not ...
|
|
*
|
|
* @returns
|
|
*/
|
|
function isNgCommandExists() {
|
|
return checkCommandExists('ng');
|
|
}
|
|
|
|
/**
|
|
* determines ionic command exists or not ...
|
|
*
|
|
* @returns
|
|
*/
|
|
function isIonicCommandExists() {
|
|
return checkCommandExists('ionic');
|
|
}
|
|
|
|
/**
|
|
* determines cordova command exists or not ...
|
|
*
|
|
* @returns
|
|
*/
|
|
function isCordovaCommandExists() {
|
|
return checkCommandExists('cordova');
|
|
}
|
|
//#endregion
|
|
//#endregion
|
|
|
|
//
|
|
// Module Exports ...
|
|
module.exports = {
|
|
//
|
|
execute,
|
|
checkCommandExists,
|
|
isRequiredCommandsExists,
|
|
getRequiredCommandsStates,
|
|
|
|
//
|
|
// isFxCommandExists,
|
|
// isTarCommandExists,
|
|
// isCatCommandExists,
|
|
// isSedCommandExists,
|
|
// isNpmCommandExists,
|
|
// isGrepCommandExists,
|
|
isNgCommandExists,
|
|
isIonicCommandExists,
|
|
isCordovaCommandExists,
|
|
} |