2022-10-09 18:15:37 +02:00
|
|
|
import { logger } from './npmci.logging.js';
|
|
|
|
import * as plugins from './npmci.plugins.js';
|
|
|
|
import * as paths from './npmci.paths.js';
|
2017-03-11 14:07:36 +01:00
|
|
|
|
2017-03-11 01:10:37 +01:00
|
|
|
/**
|
|
|
|
* wether nvm is available or not
|
|
|
|
*/
|
2023-07-12 15:35:38 +02:00
|
|
|
export let nvmAvailable = plugins.smartpromise.defer<boolean>();
|
2017-03-11 01:10:37 +01:00
|
|
|
/**
|
|
|
|
* the smartshell instance for npmci
|
|
|
|
*/
|
2018-11-24 15:00:19 +01:00
|
|
|
const npmciSmartshell = new plugins.smartshell.Smartshell({
|
2017-03-11 01:10:37 +01:00
|
|
|
executor: 'bash',
|
2021-05-14 18:11:12 +00:00
|
|
|
sourceFilePaths: [],
|
2018-04-04 22:25:13 +02:00
|
|
|
});
|
2017-03-11 01:10:37 +01:00
|
|
|
|
2017-03-11 14:07:36 +01:00
|
|
|
/**
|
|
|
|
* check for tools.
|
|
|
|
*/
|
2018-11-24 15:00:19 +01:00
|
|
|
const checkToolsAvailable = async () => {
|
2017-03-11 14:07:36 +01:00
|
|
|
// check for nvm
|
2017-04-02 23:41:51 +02:00
|
|
|
if (!process.env.NPMTS_TEST) {
|
|
|
|
if (
|
2018-09-22 14:13:25 +02:00
|
|
|
(await npmciSmartshell.execSilent(`bash -c "source /usr/local/nvm/nvm.sh"`)).exitCode === 0
|
2017-04-02 23:41:51 +02:00
|
|
|
) {
|
2018-09-22 14:13:25 +02:00
|
|
|
npmciSmartshell.shellEnv.addSourceFiles([`/usr/local/nvm/nvm.sh`]);
|
2018-04-04 22:25:13 +02:00
|
|
|
nvmAvailable.resolve(true);
|
2017-04-02 23:41:51 +02:00
|
|
|
} else if (
|
2018-09-22 14:13:25 +02:00
|
|
|
(await npmciSmartshell.execSilent(`bash -c "source ~/.nvm/nvm.sh"`)).exitCode === 0
|
2017-04-02 23:41:51 +02:00
|
|
|
) {
|
2018-09-22 14:13:25 +02:00
|
|
|
npmciSmartshell.shellEnv.addSourceFiles([`~/.nvm/nvm.sh`]);
|
2018-04-04 22:25:13 +02:00
|
|
|
nvmAvailable.resolve(true);
|
2017-04-02 23:41:51 +02:00
|
|
|
} else {
|
2018-04-04 22:25:13 +02:00
|
|
|
nvmAvailable.resolve(false);
|
2017-05-15 16:35:16 +02:00
|
|
|
}
|
2017-04-02 23:41:51 +02:00
|
|
|
} else {
|
2018-04-04 22:25:13 +02:00
|
|
|
nvmAvailable.resolve(true);
|
2017-04-02 23:41:51 +02:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
};
|
|
|
|
checkToolsAvailable();
|
2016-08-02 16:20:32 +02:00
|
|
|
|
2016-12-10 22:43:14 +01:00
|
|
|
/**
|
|
|
|
* bash() allows using bash with nvm in path
|
2016-12-14 16:45:48 +00:00
|
|
|
* @param commandArg - The command to execute
|
|
|
|
* @param retryArg - The retryArg: 0 to any positive number will retry, -1 will always succeed, -2 will return undefined
|
2016-12-10 22:43:14 +01:00
|
|
|
*/
|
2017-05-15 16:35:16 +02:00
|
|
|
export let bash = async (commandArg: string, retryArg: number = 2): Promise<string> => {
|
2018-04-04 22:25:13 +02:00
|
|
|
await nvmAvailable.promise; // make sure nvm check has run
|
|
|
|
let execResult: plugins.smartshell.IExecResult;
|
2017-03-11 01:10:37 +01:00
|
|
|
|
|
|
|
// determine if we fail
|
2018-04-04 22:25:13 +02:00
|
|
|
let failOnError: boolean = true;
|
2017-02-19 14:46:05 +01:00
|
|
|
if (retryArg === -1) {
|
2018-04-04 22:25:13 +02:00
|
|
|
failOnError = false;
|
|
|
|
retryArg = 0;
|
2017-02-19 14:46:05 +01:00
|
|
|
}
|
2017-03-11 01:10:37 +01:00
|
|
|
|
2018-04-04 22:25:13 +02:00
|
|
|
if (!process.env.NPMTS_TEST) {
|
|
|
|
// NPMTS_TEST is used during testing
|
2017-02-19 14:46:05 +01:00
|
|
|
for (let i = 0; i <= retryArg; i++) {
|
2017-05-15 16:35:16 +02:00
|
|
|
if (process.env.DEBUG_NPMCI === 'true') {
|
2018-04-04 22:25:13 +02:00
|
|
|
console.log(commandArg);
|
2017-02-19 14:46:05 +01:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
execResult = await npmciSmartshell.exec(commandArg);
|
2016-12-10 22:43:14 +01:00
|
|
|
|
2017-02-19 14:46:05 +01:00
|
|
|
// determine how bash reacts to error and success
|
2018-04-04 22:25:13 +02:00
|
|
|
if (execResult.exitCode !== 0 && i === retryArg) {
|
|
|
|
// something went wrong and retries are exhausted
|
2017-02-19 14:46:05 +01:00
|
|
|
if (failOnError) {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('error', 'something went wrong and retries are exhausted');
|
2018-04-04 22:25:13 +02:00
|
|
|
process.exit(1);
|
2016-06-05 14:55:08 +02:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
} else if (execResult.exitCode === 0) {
|
|
|
|
// everything went fine, or no error wanted
|
|
|
|
i = retryArg + 1; // retry +1 breaks for loop, if everything works out ok retrials are not wanted
|
2017-02-19 14:46:05 +01:00
|
|
|
} else {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('warn', 'Something went wrong! Exit Code: ' + execResult.exitCode.toString());
|
|
|
|
logger.log('info', 'Retry ' + (i + 1).toString() + ' of ' + retryArg.toString());
|
2017-02-19 14:46:05 +01:00
|
|
|
}
|
2016-05-30 03:43:15 +02:00
|
|
|
}
|
2017-02-19 14:46:05 +01:00
|
|
|
} else {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('info', 'ShellExec would be: ' + commandArg);
|
2017-03-11 01:10:37 +01:00
|
|
|
execResult = {
|
|
|
|
exitCode: 0,
|
2021-05-14 18:11:12 +00:00
|
|
|
stdout: 'testOutput',
|
2018-04-04 22:25:13 +02:00
|
|
|
};
|
2017-02-19 14:46:05 +01:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
return execResult.stdout;
|
|
|
|
};
|
2016-06-05 14:33:59 +02:00
|
|
|
|
2016-12-10 22:43:14 +01:00
|
|
|
/**
|
|
|
|
* bashNoError allows executing stuff without throwing an error
|
|
|
|
*/
|
2017-03-11 01:34:03 +01:00
|
|
|
export let bashNoError = async (commandArg: string): Promise<string> => {
|
2018-04-04 22:25:13 +02:00
|
|
|
return await bash(commandArg, -1);
|
|
|
|
};
|