npmci/ts/mod_node/index.ts

73 lines
2.3 KiB
TypeScript
Raw Normal View History

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