npmci/ts/mod_npm/index.ts

117 lines
3.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 './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 () => {
2018-12-09 01:50:00 +00:00
const config = await configModule.getConfig();
let npmrcFileString: string = '';
plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_TOKEN_NPM*', npmEnvArg => {
const npmRegistryUrl = npmEnvArg.split('|')[0];
const npmToken = npmEnvArg.split('|')[1];
npmrcFileString = `//${npmRegistryUrl}/:_authToken="${npmToken}"\n`;
});
if (npmrcFileString.length > 0) {
logger.log('info', 'found one or more access tokens');
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-12-09 01:50:00 +00:00
2018-04-04 20:25:13 +00:00
plugins.smartfile.memory.toFsSync(npmrcFileString, '/root/.npmrc');
2018-12-09 01:51:03 +00:00
logger.log('info', `setting default npm registry to ${config.npmRegistryUrl}`);
2018-12-09 13:39:24 +00:00
await bash(`npm set registry https://${config.npmRegistryUrl}`);
2018-04-04 20:25:13 +00:00
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 = ``;
2018-12-09 01:50:00 +00:00
let npmRegistryCliString = ``;
2018-04-04 20:25:13 +00:00
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-12-09 01:50:00 +00:00
// -> configure registry url
if (config.npmRegistryUrl) {
2018-12-09 14:22:20 +00:00
npmAccessCliString = `--registry=https://${config.npmRegistryUrl}`;
2018-12-09 01:50:00 +00:00
} else {
logger.log('error', `no registry url specified. Can't publish!`);
process.exit(1);
}
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-12-09 01:50:00 +00:00
await bash(`npm publish ${npmAccessCliString} ${npmRegistryCliString}`);
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
};