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 { bash } from '../npmci.bash';
|
|
|
|
import { repo } from '../npmci.env';
|
2017-08-28 15:11:47 +00:00
|
|
|
|
2018-12-23 16:29:25 +00:00
|
|
|
import { configObject } from '../npmci.config';
|
|
|
|
|
2017-08-28 15:11:47 +00:00
|
|
|
/**
|
|
|
|
* handle cli input
|
|
|
|
* @param argvArg
|
|
|
|
*/
|
2018-04-04 20:25:13 +00:00
|
|
|
export let handleCli = async argvArg => {
|
2017-08-28 15:11:47 +00:00
|
|
|
if (argvArg._.length >= 2) {
|
2018-11-24 14:00:19 +00:00
|
|
|
const action: string = argvArg._[1];
|
2017-08-28 15:11:47 +00:00
|
|
|
switch (action) {
|
|
|
|
case 'mirror':
|
2018-04-04 20:25:13 +00:00
|
|
|
await mirror();
|
|
|
|
break;
|
2017-08-28 15:11:47 +00:00
|
|
|
default:
|
2018-12-23 16:29:25 +00:00
|
|
|
logger.log('error', `npmci git -> action >>${action}<< not supported!`);
|
2017-08-28 15:11:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-23 16:29:25 +00:00
|
|
|
logger.log('info', `npmci git -> cli arguments invalid! Please read the documentation.`);
|
2017-08-28 15:11:47 +00:00
|
|
|
}
|
2018-04-04 20:25:13 +00:00
|
|
|
};
|
2017-08-28 15:11:47 +00:00
|
|
|
|
|
|
|
export let mirror = async () => {
|
2018-11-24 14:00:19 +00: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;
|
2018-12-23 17:57:15 +00:00
|
|
|
if(
|
|
|
|
configObject.projectInfo.npm.packageJson.private === true ||
|
|
|
|
configObject.npmAccessLevel === 'private'
|
|
|
|
) {
|
|
|
|
logger.log('warn', `refusing to mirror due to private property use a private mirror location instead`);
|
2018-12-23 16:29:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 12:58:44 +00:00
|
|
|
if (githubToken) {
|
2018-11-24 14:00:19 +00:00
|
|
|
logger.log('info', 'found github token.');
|
|
|
|
logger.log('info', 'attempting the mirror the repository to GitHub');
|
2017-09-08 12:58:44 +00:00
|
|
|
// add the mirror
|
2018-04-04 20:25:13 +00:00
|
|
|
await bash(
|
|
|
|
`git remote add mirror https://${githubToken}@github.com/${githubUser}/${githubRepo}.git`
|
|
|
|
);
|
|
|
|
await bash(`git push mirror --all`);
|
2018-11-24 14:00:19 +00:00
|
|
|
logger.log('ok', 'pushed all branches to mirror!');
|
2018-04-04 20:25:13 +00:00
|
|
|
await bash(`git push mirror --tags`);
|
2018-11-24 14:00:19 +00:00
|
|
|
logger.log('ok', 'pushed all tags to mirror!');
|
2017-09-08 13:00:43 +00:00
|
|
|
} else {
|
2018-11-24 14:00:19 +00:00
|
|
|
logger.log('error', `cannot find NPMCI_GIT_GITHUBTOKEN env var!`);
|
2018-04-04 20:25:13 +00:00
|
|
|
process.exit(1);
|
2017-09-08 12:58:44 +00:00
|
|
|
}
|
2018-04-04 20:25:13 +00:00
|
|
|
};
|