npmci/ts/mod_node/index.ts

78 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-08-27 15:24:17 +02:00
import * as plugins from '../npmci.plugins'
import * as npmciConfig from '../npmci.config'
2017-03-11 14:07:36 +01:00
import {
bash,
bashNoError,
nvmAvailable,
2017-08-27 15:24:17 +02:00
yarnAvailable
} from '../npmci.bash'
/**
* handle cli input
* @param argvArg
*/
export let handleCli = async (argvArg) => {
if (argvArg._.length >= 3) {
let action: string = argvArg._[1]
switch (action) {
case 'install':
await install(argvArg._[2])
break
default:
plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`)
process.exit(1)
2017-08-27 15:24:17 +02:00
}
} else {
plugins.beautylog.error(`>>npmci node ...<< cli arguments invalid... Please read the documentation.`)
process.exit(1)
2017-08-27 15:24:17 +02:00
}
}
2017-03-08 14:50:41 +01:00
/**
* Install a specific version of node
* @param versionArg
*/
export let install = async (versionArg) => {
2017-02-19 14:46:05 +01:00
plugins.beautylog.log(`now installing node version ${versionArg}`)
let version: string
if (versionArg === 'stable') {
version = 'stable'
} else if (versionArg === 'lts') {
version = '6'
} else if (versionArg === 'legacy') {
version = '6'
} else {
version = versionArg
2017-08-27 15:24:17 +02:00
}
2017-03-11 01:10:37 +01:00
if (await nvmAvailable.promise) {
2017-03-08 14:50:41 +01:00
await bash(`nvm install ${version} && nvm alias default ${version}`)
2017-02-19 14:46:05 +01:00
plugins.beautylog.success(`Node version ${version} successfully installed!`)
} else {
plugins.beautylog.warn('Nvm not in path so staying at installed node version!')
2017-08-27 15:24:17 +02:00
}
2017-03-08 14:50:41 +01:00
await bash('node -v')
await bash('npm -v')
2017-02-19 14:46:05 +01:00
// lets look for further config
2017-08-27 15:24:17 +02:00
await npmciConfig.getConfig()
2017-03-08 14:50:41 +01:00
.then(async configArg => {
2017-02-19 14:46:05 +01:00
plugins.beautylog.log('Now checking for needed global npm tools...')
2017-08-28 17:19:31 +02:00
for (let npmTool of configArg.npmGlobalTools) {
2017-02-19 14:46:05 +01:00
plugins.beautylog.info(`Checking for global "${npmTool}"`)
2017-03-08 14:50:41 +01:00
let whichOutput: string = await bashNoError(`which ${npmTool}`)
2017-02-19 14:46:05 +01:00
let toolAvailable: boolean = !((/not\sfound/.test(whichOutput)) || whichOutput === '')
if (toolAvailable) {
plugins.beautylog.log(`Tool ${npmTool} is available`)
} else {
plugins.beautylog.info(`globally installing ${npmTool} from npm`)
2017-03-11 15:17:24 +01:00
if (await yarnAvailable.promise) {
2017-03-11 14:07:36 +01:00
await bash(`yarn global add ${npmTool}`)
} else {
await bash(`npm install ${npmTool} -q -g`)
}
2017-02-19 14:46:05 +01:00
}
}
plugins.beautylog.success('all global npm tools specified in npmextra.json are now available!')
})
2016-11-24 23:21:40 +01:00
}