npmci/ts/mod_npm/index.ts

100 lines
2.7 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 './mod.plugins';
import * as configModule 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 >= 2) {
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();
break;
2017-08-27 13:24:17 +00:00
case 'prepare':
2018-04-04 20:25:13 +00:00
await prepare();
break;
2017-08-27 13:24:17 +00:00
case 'test':
2018-04-04 20:25:13 +00:00
await test();
break;
2017-08-28 11:08:21 +00:00
case 'publish':
2018-04-04 20:25:13 +00:00
await publish();
break;
2017-08-27 13:24:17 +00:00
default:
2018-11-24 14:00:19 +00:00
logger.log('error', `>>npmci npm ...<< 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('info', `>>npmci npm ...<< cli arguments invalid... Please read the documentation.`);
2018-04-04 20:25:13 +00:00
process.exit(1);
2017-08-27 13:24:17 +00:00
}
2018-04-04 20:25:13 +00:00
};
2017-08-27 13:24:17 +00:00
/**
* authenticates npm with token from env var
*/
2018-11-24 14:00:19 +00:00
const prepare = async () => {
const npmrcPrefix: string = '//registry.npmjs.org/:_authToken=';
const npmToken: string = process.env.NPMCI_TOKEN_NPM;
const npmrcFileString: string = npmrcPrefix + npmToken;
2017-08-27 13:24:17 +00:00
if (npmToken) {
2018-11-24 14:00:19 +00:00
logger.log('info', 'found access token');
2017-08-27 13:24:17 +00:00
} else {
2018-11-24 14:00:19 +00:00
logger.log('error', 'no access token found! Exiting!');
2018-04-04 20:25:13 +00:00
process.exit(1);
2017-08-27 13:24:17 +00:00
}
2018-04-04 20:25:13 +00:00
plugins.smartfile.memory.toFsSync(npmrcFileString, '/root/.npmrc');
return;
};
2017-08-27 13:24:17 +00:00
2018-04-04 20:43:15 +00:00
/**
* publish a package to npm
*/
2018-11-24 14:00:19 +00:00
const publish = async () => {
2018-04-04 20:25:13 +00:00
let npmAccessCliString = ``;
const config = await configModule.getConfig();
2018-04-29 14:12:32 +00:00
// -> configure package access level
2018-04-04 20:25:13 +00:00
if (
config.npmAccessLevel &&
(config.npmAccessLevel === 'public' || config.npmAccessLevel === 'private')
) {
npmAccessCliString = `--access=${config.npmAccessLevel}`;
}
2018-04-29 14:12:32 +00:00
2018-05-03 18:40:26 +00:00
// -> preparing
2018-11-24 14:00:19 +00:00
logger.log('info', `now preparing environment:`);
2018-05-03 18:40:26 +00:00
prepare();
await bash(`npm -v`);
2018-04-29 14:12:32 +00:00
// -> build it
await bash(`npm install`);
await bash(`npm run build`);
2018-04-29 14:12:32 +00:00
2018-11-24 14:00:19 +00:00
logger.log('success', `Nice!!! The build for the publication was successfull!`);
logger.log('info', `Lets clean up so we don't publish any packages that don't belong to us:`);
2018-05-03 21:52:51 +00:00
// -> clean up before we publish stuff
2018-05-27 13:41:58 +00:00
await bashNoError(`rm -r ./.npmci_cache`);
await bash(`rm -r ./node_modules`);
2018-05-03 21:52:51 +00:00
2018-11-24 14:00:19 +00:00
logger.log('success', `Cleaned up!:`);
2018-05-03 21:52:51 +00:00
2018-04-29 14:12:32 +00:00
// -> publish it
2018-11-24 14:00:19 +00:00
logger.log('info', `now invoking npm to publish the package!`);
2018-04-04 20:25:13 +00:00
await bash(`npm publish ${npmAccessCliString}`);
2018-11-24 14:00:19 +00:00
logger.log('success', `Package was successfully published!`);
2018-04-04 20:25:13 +00:00
};
2017-08-28 11:08:21 +00:00
2018-11-24 14:00:19 +00:00
const install = async (): Promise<void> => {
logger.log('info', 'now installing dependencies:');
await bash('npm install');
2018-04-04 20:25:13 +00:00
};
2017-08-27 13:24:17 +00:00
export let test = async (): Promise<void> => {
2018-11-24 14:00:19 +00:00
logger.log('info', 'now starting tests:');
await bash('npm test');
2018-04-04 20:25:13 +00:00
};