npmci/ts/mod_npm/index.ts

144 lines
4.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 = '';
2018-12-09 15:48:33 +00:00
await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_TOKEN_NPM*', npmEnvArg => {
2018-12-09 01:50:00 +00:00
const npmRegistryUrl = npmEnvArg.split('|')[0];
const npmToken = npmEnvArg.split('|')[1];
2018-12-24 01:13:04 +00:00
npmrcFileString += `//${npmRegistryUrl}/:_authToken="${plugins.smartstring.base64.decode(
npmToken
)}"\n`;
2018-12-09 01:50:00 +00:00
});
2018-12-09 14:53:38 +00:00
logger.log('info', `setting default npm registry to ${config.npmRegistryUrl}`);
npmrcFileString += `registry=https://${config.npmRegistryUrl}\n`;
2018-12-09 01:50:00 +00:00
2018-12-09 14:53:38 +00:00
// final check
2018-12-09 01:50:00 +00:00
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-12-09 14:53:38 +00:00
// lets save it to disk
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 () => {
2019-07-17 15:04:10 +00:00
const buildPublishCommand = async () => {
let npmAccessCliString = ``;
let npmRegistryCliString = ``;
let publishVerdaccioAsWell = false;
const config = await configModule.getConfig();
const availableRegistries: string[] = [];
await plugins.smartparam.forEachMinimatch(process.env, 'NPMCI_TOKEN_NPM*', npmEnvArg => {
availableRegistries.push(npmEnvArg.split('|')[0]);
});
2018-04-29 14:12:32 +00:00
2019-07-17 15:04:10 +00:00
// -> configure package access level
2019-07-17 15:57:20 +00:00
if (config.npmAccessLevel) {
2019-07-17 15:04:10 +00:00
npmAccessCliString = `--access=${config.npmAccessLevel}`;
if (config.npmAccessLevel === 'public') {
publishVerdaccioAsWell = true;
}
}
// -> configure registry url
if (config.npmRegistryUrl) {
npmRegistryCliString = `--registry=https://${config.npmRegistryUrl}`;
} else {
logger.log('error', `no registry url specified. Can't publish!`);
process.exit(1);
}
2018-04-29 14:12:32 +00:00
2019-07-17 15:04:10 +00:00
let publishCommand = `npm publish ${npmAccessCliString} ${npmRegistryCliString} `;
// publishEverywhere
if (publishVerdaccioAsWell) {
2019-07-17 15:57:20 +00:00
const verdaccioRegistry = availableRegistries.find(registryString =>
registryString.startsWith('verdaccio')
);
2019-07-17 15:04:10 +00:00
if (verdaccioRegistry) {
2019-07-17 15:57:20 +00:00
logger.log(
'info',
`package is public and verdaccio registry is specified. Also publishing to Verdaccio!`
);
2019-07-17 15:04:10 +00:00
publishCommand = `${publishCommand} && npm publish ${npmAccessCliString} --registry=https://${verdaccioRegistry}`;
}
}
return publishCommand;
};
2018-12-09 01:50:00 +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!`);
2019-07-17 15:04:10 +00:00
await bash(await buildPublishCommand());
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
};