2016-11-24 22:21:40 +00:00
|
|
|
import * as plugins from './npmci.plugins'
|
2017-03-11 13:07:36 +00:00
|
|
|
import * as paths from './npmci.paths'
|
|
|
|
|
2017-03-11 00:10:37 +00:00
|
|
|
import * as smartq from 'smartq'
|
2016-06-07 01:57:43 +00:00
|
|
|
|
2017-03-11 00:10:37 +00:00
|
|
|
/**
|
|
|
|
* wether nvm is available or not
|
|
|
|
*/
|
|
|
|
export let nvmAvailable = smartq.defer<boolean>()
|
2017-03-11 13:07:36 +00:00
|
|
|
export let yarnAvailable = smartq.defer<boolean>()
|
2017-03-11 00:10:37 +00:00
|
|
|
/**
|
|
|
|
* the smartshell instance for npmci
|
|
|
|
*/
|
|
|
|
let npmciSmartshell = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
|
|
|
sourceFilePaths: []
|
|
|
|
})
|
|
|
|
|
2017-03-11 13:07:36 +00:00
|
|
|
/**
|
|
|
|
* check for tools.
|
|
|
|
*/
|
|
|
|
let checkToolsAvailable = async () => {
|
|
|
|
// check for nvm
|
2017-04-02 21:41:51 +00:00
|
|
|
if (!process.env.NPMTS_TEST) {
|
|
|
|
if (
|
|
|
|
(await plugins.smartshell.execSilent(`bash -c "source /usr/local/nvm/nvm.sh"`)).exitCode === 0
|
|
|
|
) {
|
|
|
|
npmciSmartshell.addSourceFiles([ `/usr/local/nvm/nvm.sh` ])
|
|
|
|
nvmAvailable.resolve(true)
|
|
|
|
} else if (
|
|
|
|
(await plugins.smartshell.execSilent(`bash -c "source ~/.nvm/nvm.sh"`)).exitCode === 0
|
|
|
|
) {
|
|
|
|
npmciSmartshell.addSourceFiles([ `~/.nvm/nvm.sh` ])
|
|
|
|
nvmAvailable.resolve(true)
|
|
|
|
} else {
|
|
|
|
nvmAvailable.resolve(false)
|
|
|
|
};
|
2017-03-11 13:07:36 +00:00
|
|
|
|
2017-04-02 21:41:51 +00:00
|
|
|
// check for yarn
|
|
|
|
await plugins.smartshell.which('yarn').then(
|
2017-05-15 13:54:09 +00:00
|
|
|
async () => {
|
|
|
|
await plugins.smartshell.exec(`yarn config set cache-folder ${plugins.path.join(paths.cwd, '.yarn')}`)
|
2017-04-02 21:41:51 +00:00
|
|
|
yarnAvailable.resolve(true)
|
|
|
|
},
|
|
|
|
() => { yarnAvailable.resolve(false) }
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
nvmAvailable.resolve(true)
|
|
|
|
yarnAvailable.resolve(true)
|
|
|
|
}
|
2016-11-24 22:21:40 +00:00
|
|
|
}
|
2017-03-11 13:07:36 +00:00
|
|
|
checkToolsAvailable()
|
2016-08-02 14:20:32 +00:00
|
|
|
|
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
|
|
|
*/
|
2017-03-08 13:50:58 +00:00
|
|
|
export let bash = async (commandArg: string, retryArg: number = 2, bareArg: boolean = false): Promise<string> => {
|
2017-03-11 00:10:37 +00:00
|
|
|
await nvmAvailable.promise // make sure nvm check has run
|
|
|
|
let execResult: plugins.smartshell.IExecResult
|
|
|
|
|
|
|
|
// determine if we fail
|
2017-02-19 13:46:05 +00:00
|
|
|
let failOnError: boolean = true
|
|
|
|
if (retryArg === -1) {
|
|
|
|
failOnError = false
|
|
|
|
retryArg = 0
|
|
|
|
}
|
2017-03-11 00:10:37 +00:00
|
|
|
|
2017-02-19 13:46:05 +00:00
|
|
|
if (!process.env.NPMTS_TEST) { // NPMTS_TEST is used during testing
|
|
|
|
for (let i = 0; i <= retryArg; i++) {
|
|
|
|
if (!bareArg) {
|
2017-03-11 01:41:18 +00:00
|
|
|
execResult = await npmciSmartshell.exec(commandArg)
|
2017-02-19 13:46:05 +00:00
|
|
|
} else {
|
2017-05-15 14:07:05 +00:00
|
|
|
execResult = await npmciSmartshell.exec(commandArg)
|
2017-02-19 13:46:05 +00:00
|
|
|
}
|
2016-12-10 21:43:14 +00:00
|
|
|
|
2017-02-19 13:46:05 +00:00
|
|
|
// determine how bash reacts to error and success
|
2017-03-11 00:10:37 +00:00
|
|
|
if (execResult.exitCode !== 0 && i === retryArg) { // something went wrong and retries are exhausted
|
2017-02-19 13:46:05 +00:00
|
|
|
if (failOnError) {
|
2017-03-07 17:07:03 +00:00
|
|
|
plugins.beautylog.error('something went wrong and retries are exhausted')
|
2017-02-19 13:46:05 +00:00
|
|
|
process.exit(1)
|
2016-06-05 12:55:08 +00:00
|
|
|
}
|
2017-03-11 00:10:37 +00:00
|
|
|
} else if (execResult.exitCode === 0) { // everything went fine, or no error wanted
|
2017-02-19 13:46:05 +00:00
|
|
|
i = retryArg + 1 // retry +1 breaks for loop, if everything works out ok retrials are not wanted
|
|
|
|
} else {
|
2017-03-11 00:10:37 +00:00
|
|
|
plugins.beautylog.warn('Something went wrong! Exit Code: ' + execResult.exitCode.toString())
|
2017-02-19 13:46:05 +00:00
|
|
|
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)
|
2017-03-11 01:27:48 +00:00
|
|
|
await plugins.smartdelay.delayFor(100)
|
2017-03-11 00:10:37 +00:00
|
|
|
execResult = {
|
|
|
|
exitCode: 0,
|
|
|
|
stdout: 'testOutput'
|
|
|
|
}
|
2017-02-19 13:46:05 +00:00
|
|
|
}
|
2017-03-11 00:10:37 +00:00
|
|
|
return execResult.stdout
|
2016-06-05 12:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 21:43:14 +00:00
|
|
|
/**
|
|
|
|
* bashNoError allows executing stuff without throwing an error
|
|
|
|
*/
|
2017-03-11 00:34:03 +00:00
|
|
|
export let bashNoError = async (commandArg: string): Promise<string> => {
|
|
|
|
return await bash(commandArg, -1)
|
2016-12-10 21:43:14 +00:00
|
|
|
}
|