now fully async
This commit is contained in:
@ -1,61 +1,83 @@
|
||||
import * as plugins from './npmci.plugins'
|
||||
import * as smartq from 'smartq'
|
||||
|
||||
let nvmSourceString: string = ''
|
||||
export let nvmAvailable: boolean = false
|
||||
let checkNvm = () => {
|
||||
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
|
||||
/**
|
||||
* wether nvm is available or not
|
||||
*/
|
||||
export let nvmAvailable = smartq.defer<boolean>()
|
||||
|
||||
/**
|
||||
* the smartshell instance for npmci
|
||||
*/
|
||||
let npmciSmartshell = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash',
|
||||
sourceFilePaths: []
|
||||
})
|
||||
|
||||
let checkNvm = async () => {
|
||||
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)
|
||||
};
|
||||
}
|
||||
checkNvm()
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* bash() allows using bash with nvm in path
|
||||
* @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
|
||||
*/
|
||||
export let bash = async (commandArg: string, retryArg: number = 2, bareArg: boolean = false): Promise<string> => {
|
||||
let exitCode: number
|
||||
let stdOut: string
|
||||
let execResult
|
||||
await nvmAvailable.promise // make sure nvm check has run
|
||||
let execResult: plugins.smartshell.IExecResult
|
||||
|
||||
// determine if we fail
|
||||
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}"`
|
||||
)
|
||||
execResult = await npmciSmartshell.execSilent(commandArg)
|
||||
} else {
|
||||
execResult = plugins.shelljs.exec(commandArg)
|
||||
execResult = await plugins.smartshell.exec(commandArg)
|
||||
}
|
||||
exitCode = execResult.code
|
||||
stdOut = execResult.stdout
|
||||
|
||||
// determine how bash reacts to error and success
|
||||
if (exitCode !== 0 && i === retryArg) { // something went wrong and retries are exhausted
|
||||
if (execResult.exitCode !== 0 && i === retryArg) { // something went wrong and retries are exhausted
|
||||
if (failOnError) {
|
||||
plugins.beautylog.error('something went wrong and retries are exhausted')
|
||||
process.exit(1)
|
||||
}
|
||||
} else if (exitCode === 0) { // everything went fine, or no error wanted
|
||||
} 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
|
||||
} else {
|
||||
plugins.beautylog.warn('Something went wrong! Exit Code: ' + exitCode.toString())
|
||||
plugins.beautylog.warn('Something went wrong! Exit Code: ' + execResult.exitCode.toString())
|
||||
plugins.beautylog.info('Retry ' + (i + 1).toString() + ' of ' + retryArg.toString())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
plugins.beautylog.log('ShellExec would be: ' + commandArg)
|
||||
execResult = {
|
||||
exitCode: 0,
|
||||
stdout: 'testOutput'
|
||||
}
|
||||
}
|
||||
return execResult.stdout
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +40,7 @@ export let readDockerfiles = async (): Promise<Dockerfile[]> => {
|
||||
* @param sortableArrayArg an array of instances of class Dockerfile
|
||||
* @returns Promise<Dockerfile[]>
|
||||
*/
|
||||
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): plugins.q.Promise<Dockerfile[]> => {
|
||||
export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): Promise<Dockerfile[]> => {
|
||||
let done = plugins.q.defer<Dockerfile[]>()
|
||||
let sortedArray: Dockerfile[] = []
|
||||
let cleanTagsOriginal = cleanTagsArrayFunction(sortableArrayArg, sortedArray)
|
||||
@ -69,7 +69,7 @@ export let sortDockerfiles = (sortableArrayArg: Dockerfile[]): plugins.q.Promise
|
||||
/**
|
||||
* maps local Dockerfiles dependencies to the correspoding Dockerfile class instances
|
||||
*/
|
||||
export let mapDockerfiles = async (sortedArray: Dockerfile[]): plugins.q.Promise<Dockerfile[]> => {
|
||||
export let mapDockerfiles = async (sortedArray: Dockerfile[]): Promise<Dockerfile[]> => {
|
||||
sortedArray.forEach((dockerfileArg) => {
|
||||
if (dockerfileArg.localBaseImageDependent) {
|
||||
sortedArray.forEach((dockfile2: Dockerfile) => {
|
||||
|
@ -19,7 +19,7 @@ export let install = async (versionArg) => {
|
||||
} else {
|
||||
version = versionArg
|
||||
};
|
||||
if (nvmAvailable) {
|
||||
if (await nvmAvailable.promise) {
|
||||
await bash(`nvm install ${version} && nvm alias default ${version}`)
|
||||
plugins.beautylog.success(`Node version ${version} successfully installed!`)
|
||||
} else {
|
||||
|
@ -1,16 +1,18 @@
|
||||
export import beautylog = require('beautylog')
|
||||
export let gulp = require('gulp')
|
||||
export import gulpFunction = require('gulp-function')
|
||||
export import lodash = require('lodash')
|
||||
export let lodash = require('lodash')
|
||||
export import npmextra = require('npmextra')
|
||||
export import path = require('path')
|
||||
export import projectinfo = require('projectinfo')
|
||||
export import q = require('smartq')
|
||||
export let request = require('request')
|
||||
export import shelljs = require('shelljs')
|
||||
export import smartcli = require('smartcli')
|
||||
export import smartfile = require('smartfile')
|
||||
export import shelljs = require('shelljs')
|
||||
export import smartparam = require('smartparam')
|
||||
export import smartq = require('smartq')
|
||||
export import smartshell = require('smartshell')
|
||||
export import smartsocket = require('smartsocket')
|
||||
export import smartssh = require('smartssh')
|
||||
export import smartstring = require('smartstring')
|
||||
|
Reference in New Issue
Block a user