
const { execSync } = require("child_process");
const MSG_CODEGEN_LOG = 'CODEGEN ANGULAR APP: ';
const MSG_NG_RUNTIME_LOG = 'APP NG RUNTIME NODE MODULES: ';
const MSG_APP_RUNTIME_WM_BUILD_LOG = 'APP RUNTIME WM BUILD : ';

const MSG_ANGULAR_CODEGEN_SUCCESS = 'ANGULAR_CODEGEN_SUCCESS';
const MSG_APP_RUNTIME_WM_BUILD_SUCCESS = 'WAVEMAKER_APP_RUNTIME_WM_BUILD_SUCCESS';
const MSG_NG_RUNTIME_SUCCESS = 'APP_NG_RUNTIME_SUCCESS';
/**
 * Read the console arguments and prepare the key value pairs.
 * @returns Object console arguments as key value pairs
 */
const getArgs = (customArgs) => {
    const args = {};
    let arguments = customArgs || process.argv; 
    arguments.slice(2, process.argv.length)
        .forEach(arg => {
            if (arg.slice(0, 2) === '--') {
                const longArg = arg.split('=');
                const longArgFlag = longArg[0].slice(2, longArg[0].length);
                const longArgValue = longArg.length > 2 ? longArg.slice(1, longArg.length).join('=') : longArg[1];
                args[longArgFlag] = longArgValue;
            }
        });
    return args;
}

const args = getArgs();

/**
 * To run the system command via node  child process.
 * @param {*} cmd Command in string format to execute in node environment
 * @param {*} errorCallback callback if anything needs to be handled on command failure 
 */
const executeSyncCmd = (cmd, errorCallback, msg, skipExit, errorMsg) => {
    try {
        console.log(msg + 'Current running cmd: ' + cmd)
        execSync(cmd, { stdio: 'inherit' });
    } catch (err) {
        if (errorCallback) {
            errorCallback(err);
            if(skipExit){
                return;
            }
        }
        if(err && err.status == 134){
            console.log("\x1b[31m", "**********************************************************************************************************************************************");
            console.log("\x1b[31m", "**********************************************************************************************************************************************");
            console.log("\x1b[31m", "*******************************************************" + cmd + " FAILED DUE TO INSUFFICIENT MEMORY! " + errorMsg + " ************************************************************");
            console.log("\x1b[31m", "**********************************************************************************************************************************************");
            console.log("\x1b[31m", "**********************************************************************************************************************************************");
          }
          console.error(msg + 'FAILED command: ' + cmd, err);
        process.exit(err.code || err.pid);
    }
}

/**
 *  Build the string with console argument
 * @returns string format console arguments
 */
const buildArgs = () => {
    let cmdArgs = ' --nodeVMArgs="' + args.nodeVMArgs + '" --ngBuildParams="' + args.ngBuildParams + '" --runtimeUIVersion="' + args.runtimeUIVersion + '"  --appSrc="' + args.appSrc + '" --appTarget="' + args.appTarget + '" --codegenPath="' + args.codegenPath + '"';;
    return cmdArgs;
}

// TO capture the ctrl+C signal
process.on('SIGINT', function (e) {
    console.log("Caught interrupt signal", e);
    process.exit(1);
});




module.exports = {
    executeSyncCmd,
    args,
    getArgs,
    buildArgs,
    MSG_CODEGEN_LOG,
    MSG_NG_RUNTIME_LOG,
    MSG_NG_RUNTIME_SUCCESS,
    MSG_APP_RUNTIME_WM_BUILD_LOG,
    MSG_APP_RUNTIME_WM_BUILD_SUCCESS,
    MSG_ANGULAR_CODEGEN_SUCCESS
    
}
