npmci/ts/npmci.bash.ts

74 lines
2.4 KiB
TypeScript
Raw Normal View History

2016-11-24 22:21:40 +00:00
import * as plugins from './npmci.plugins'
2016-06-07 01:57:43 +00:00
2016-11-24 22:21:40 +00:00
let nvmSourceString: string = ''
export let nvmAvailable: boolean = false
let checkNvm = () => {
2017-02-19 13:46:05 +00:00
let localExec: any = plugins.shelljs.exec
if (localExec(`bash -c "source /usr/local/nvm/nvm.sh"`, { silent: true }).code === 0) {
nvmSourceString = `source /usr/local/nvm/nvm.sh && `
nvmAvailable = true
} else if (localExec(`bash -c "source ~/.nvm/nvm.sh"`, { silent: true }).code === 0) {
nvmSourceString = `source ~/.nvm/nvm.sh && `
nvmAvailable = true
};
2016-11-24 22:21:40 +00:00
}
checkNvm()
2016-12-10 21:43:14 +00: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 21:43:14 +00:00
*/
export let bash = (commandArg: string, retryArg: number = 2, bareArg: boolean = false): string => {
2017-02-19 13:46:05 +00:00
let exitCode: number
let stdOut: string
let execResult
let failOnError: boolean = true
if (retryArg === -1) {
failOnError = false
retryArg = 0
}
if (!process.env.NPMTS_TEST) { // NPMTS_TEST is used during testing
for (let i = 0; i <= retryArg; i++) {
if (!bareArg) {
execResult = plugins.shelljs.exec(
`bash -c "${nvmSourceString} ${commandArg}"`
)
} else {
execResult = plugins.shelljs.exec(commandArg)
}
exitCode = execResult.code
stdOut = execResult.stdout
2016-12-10 21:43:14 +00:00
2017-02-19 13:46:05 +00:00
// determine how bash reacts to error and success
if (exitCode !== 0 && i === retryArg) { // something went wrong and retries are exhausted
if (failOnError) {
process.exit(1)
2016-06-05 12:55:08 +00:00
}
2017-02-19 13:46:05 +00:00
} else if (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
} else {
plugins.beautylog.warn('Something went wrong! Exit Code: ' + exitCode.toString())
plugins.beautylog.info('Retry ' + (i + 1).toString() + ' of ' + retryArg.toString())
}
2016-05-30 01:43:15 +00:00
}
2017-02-19 13:46:05 +00:00
} else {
plugins.beautylog.log('ShellExec would be: ' + commandArg)
}
return stdOut
}
2016-12-10 21:43:14 +00:00
/**
* bashBare allows usage of bash without sourcing any files like nvm
*/
export let bashBare = (commandArg: string, retryArg: number = 2) => {
2017-02-19 13:46:05 +00:00
return bash(commandArg, retryArg, true)
2016-11-24 22:21:40 +00:00
}
2016-12-10 21:43:14 +00:00
/**
* bashNoError allows executing stuff without throwing an error
*/
2016-12-15 00:08:54 +00:00
export let bashNoError = (commandArg: string): string => {
2017-02-19 13:46:05 +00:00
return bash(commandArg, -1)
2016-12-10 21:43:14 +00:00
}