npmci/ts/manager.git/index.ts

72 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

import { logger } from '../npmci.logging.js';
import * as plugins from './mod.plugins.js';
import { bash, bashNoError } from '../npmci.bash.js';
import { Npmci } from '../npmci.classes.npmci.js';
2019-08-29 18:26:23 +00:00
export class NpmciGitManager {
public npmciRef: Npmci;
constructor(npmciRefArg: Npmci) {
this.npmciRef = npmciRefArg;
}
/**
* handle cli input
* @param argvArg
*/
2021-10-19 01:09:50 +00:00
public handleCli = async (argvArg: any) => {
2019-08-29 18:26:23 +00:00
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
switch (action) {
case 'mirror':
await this.mirror();
break;
default:
logger.log('error', `npmci git -> action >>${action}<< not supported!`);
}
} else {
logger.log('info', `npmci git -> cli arguments invalid! Please read the documentation.`);
}
2019-08-29 18:38:44 +00:00
};
2019-08-29 18:26:23 +00:00
public mirror = async () => {
const githubToken = process.env.NPMCI_GIT_GITHUBTOKEN;
const githubUser = process.env.NPMCI_GIT_GITHUBGROUP || this.npmciRef.npmciEnv.repo.user;
2019-09-01 11:49:11 +00:00
const githubRepo = process.env.NPMCI_GIT_GITHUB || this.npmciRef.npmciEnv.repo.repo;
2019-08-29 18:26:23 +00:00
if (
this.npmciRef.npmciConfig.getConfig().projectInfo.npm.packageJson.private === true ||
this.npmciRef.npmciConfig.getConfig().npmAccessLevel === 'private'
) {
logger.log(
'warn',
`refusing to mirror due to private property use a private mirror location instead`
);
return;
}
if (githubToken) {
logger.log('info', 'found github token.');
logger.log('info', 'attempting the mirror the repository to GitHub');
2019-08-29 18:38:44 +00:00
2019-10-02 12:54:21 +00:00
// remove old mirrors
await bashNoError('git remote rm mirror');
2019-10-02 22:00:20 +00:00
await bash(`git fetch`);
2019-08-29 18:26:23 +00:00
// add the mirror
2019-10-04 13:18:51 +00:00
await bashNoError(
2019-08-29 18:26:23 +00:00
`git remote add mirror https://${githubToken}@github.com/${githubUser}/${githubRepo}.git`
);
2019-10-04 13:18:51 +00:00
await bashNoError(`git push mirror --all`);
await bashNoError(`git checkout origin/master`);
await bashNoError(`git push mirror master`);
2019-08-29 18:26:23 +00:00
logger.log('ok', 'pushed all branches to mirror!');
2019-10-04 13:18:51 +00:00
await bashNoError(`git push mirror --tags`);
2019-08-29 18:26:23 +00:00
logger.log('ok', 'pushed all tags to mirror!');
2019-10-02 22:00:20 +00:00
// remove old mirrors
await bashNoError('git remote rm mirror');
2019-08-29 18:26:23 +00:00
} else {
logger.log('error', `cannot find NPMCI_GIT_GITHUBTOKEN env var!`);
process.exit(1);
}
2019-08-29 18:38:44 +00:00
};
2019-08-29 18:26:23 +00:00
}