npmci/ts/mod_npm/index.ts

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-04-04 20:25:13 +00:00
import * as plugins from './mod.plugins';
import * as configModule from '../npmci.config';
import { bash, bashNoError, nvmAvailable, yarnAvailable } 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-04-04 20:25:13 +00:00
let 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-04-04 20:25:13 +00:00
plugins.beautylog.error(`>>npmci npm ...<< action >>${action}<< not supported`);
process.exit(1);
2017-08-27 13:24:17 +00:00
}
} else {
2018-04-04 20:25:13 +00:00
plugins.beautylog.log(
`>>npmci npm ...<< 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-08-27 13:24:17 +00:00
/**
* authenticates npm with token from env var
*/
let prepare = async () => {
2018-04-04 20:25:13 +00:00
let npmrcPrefix: string = '//registry.npmjs.org/:_authToken=';
let npmToken: string = process.env.NPMCI_TOKEN_NPM;
let npmrcFileString: string = npmrcPrefix + npmToken;
2017-08-27 13:24:17 +00:00
if (npmToken) {
2018-04-04 20:25:13 +00:00
plugins.beautylog.info('found access token');
2017-08-27 13:24:17 +00:00
} else {
2018-04-04 20:25:13 +00:00
plugins.beautylog.error('no access token found! Exiting!');
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
*/
2017-08-28 11:08:21 +00:00
let 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
// -> build it
2018-04-29 14:32:29 +00:00
await bash(`yarn install`);
2018-04-29 14:12:32 +00:00
await bash(`yarn run build`);
// -> make sure npm is authenticated
prepare();
// -> publish it
2018-04-04 20:25:13 +00:00
await bash(`npm publish ${npmAccessCliString}`);
};
2017-08-28 11:08:21 +00:00
2017-08-27 13:24:17 +00:00
let install = async (): Promise<void> => {
2018-04-04 20:25:13 +00:00
plugins.beautylog.info('now installing dependencies:');
2017-08-27 13:24:17 +00:00
if (await yarnAvailable.promise) {
2018-04-04 20:25:13 +00:00
await bash('yarn install');
2017-08-27 13:24:17 +00:00
} else {
2018-04-04 20:25:13 +00:00
await bash('npm install');
2017-08-27 13:24:17 +00:00
}
2018-04-04 20:25:13 +00:00
};
2017-08-27 13:24:17 +00:00
export let test = async (): Promise<void> => {
2018-04-04 20:25:13 +00:00
plugins.beautylog.info('now starting tests:');
await bash('yarn test');
};