npmci/ts/mod_git/index.ts

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2018-11-24 15:00:19 +01:00
import { logger } from '../npmci.logging';
2018-04-04 22:25:13 +02:00
import * as plugins from './mod.plugins';
import { bash } from '../npmci.bash';
import { repo } from '../npmci.env';
2017-08-28 17:11:47 +02:00
/**
* handle cli input
* @param argvArg
*/
2018-04-04 22:25:13 +02:00
export let handleCli = async argvArg => {
2017-08-28 17:11:47 +02:00
if (argvArg._.length >= 2) {
2018-11-24 15:00:19 +01:00
const action: string = argvArg._[1];
2017-08-28 17:11:47 +02:00
switch (action) {
case 'mirror':
2018-04-04 22:25:13 +02:00
await mirror();
break;
2017-08-28 17:11:47 +02:00
default:
2018-11-24 15:00:19 +01:00
logger.log('error', `>>npmci git ...<< action >>${action}<< not supported`);
2017-08-28 17:11:47 +02:00
}
} else {
2018-11-24 15:00:19 +01:00
logger.log('info', `>>npmci git ...<< cli arguments invalid... Please read the documentation.`);
2017-08-28 17:11:47 +02:00
}
2018-04-04 22:25:13 +02:00
};
2017-08-28 17:11:47 +02:00
export let mirror = async () => {
2018-11-24 15:00:19 +01:00
const githubToken = process.env.NPMCI_GIT_GITHUBTOKEN;
const githubUser = process.env.NPMCI_GIT_GITHUBGROUP || repo.user;
const githubRepo = process.env.NPMCI_GIT_GITHUB || repo.repo;
2017-09-08 14:58:44 +02:00
if (githubToken) {
2018-11-24 15:00:19 +01:00
logger.log('info', 'found github token.');
logger.log('info', 'attempting the mirror the repository to GitHub');
2017-09-08 14:58:44 +02:00
// add the mirror
2018-04-04 22:25:13 +02:00
await bash(
`git remote add mirror https://${githubToken}@github.com/${githubUser}/${githubRepo}.git`
);
await bash(`git push mirror --all`);
2018-11-24 15:00:19 +01:00
logger.log('ok', 'pushed all branches to mirror!');
2018-04-04 22:25:13 +02:00
await bash(`git push mirror --tags`);
2018-11-24 15:00:19 +01:00
logger.log('ok', 'pushed all tags to mirror!');
2017-09-08 15:00:43 +02:00
} else {
2018-11-24 15:00:19 +01:00
logger.log('error', `cannot find NPMCI_GIT_GITHUBTOKEN env var!`);
2018-04-04 22:25:13 +02:00
process.exit(1);
2017-09-08 14:58:44 +02:00
}
2018-04-04 22:25:13 +02:00
};