diff --git a/package.json b/package.json index 35fb95b..d02b076 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "index.js", "license": "MIT", "scripts": { + "lint": "docker run --rm -i hadolint/hadolint < Dockerfile", "start": "ts-node src/index.ts", "bundle": "ncc build src/index.ts -o dist" }, diff --git a/src/index.ts b/src/index.ts index 9940950..f6edc65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,22 +15,24 @@ const token = process.env.GITHUB_TOKEN; const backupLocation = process.env.GITHUB_BACKUP_LOCATION || '/backup'; const mirror = async (target: string, repo: string) => { - const git = simpleGit(); const authRemote = `https://foo:${token}@github.com/${repo}` if (fs.existsSync(target)) { - const backup = path.join(os.tmpdir(), nanoid()); - await fs.move(target, backup); - try { - await git.mirror(authRemote, target); - await rimraf(backup); - } catch(err) { - await rimraf(target); - await fs.move(backup, target); - throw err; + const git = simpleGit(target); + const remotes = await git.getRemotes(); + const origin = remotes.find(r => r.name === 'origin'); + if (origin) { + await git.remote(['set-url', 'origin', authRemote]); + } else { + await git.addRemote('origin', authRemote); } + await git.remote(['update']); + await git.remote(['set-url', 'origin', `https://github.com/${repo}`]); } else { + await fs.mkdirp(target); + const git = simpleGit(target); await git.mirror(authRemote, target); + await git.remote(['set-url', 'origin', `https://github.com/${repo}`]); } }; @@ -43,14 +45,19 @@ const run = async () => { const errors: any[] = []; for await (const repos of github.paginate.iterator(action, { visibility: 'all' })) { for (const repo of repos.data) { - const loader = ora(repo.full_name).start(); + const loader = ora('preparing'); + loader.prefixText = repo.full_name; + loader.start(); try { const repoBackupLocation = path.join(backupLocation, repo.full_name); const infoLocation = path.join(repoBackupLocation, 'info.json'); const gitLocation = path.join(repoBackupLocation, 'git'); await fs.mkdirp(repoBackupLocation); + loader.text = 'fething info'; await fs.writeFile(infoLocation, JSON.stringify(repo, null, ' '), 'utf-8'); + loader.text = 'mirroring'; await mirror(gitLocation, repo.full_name); + loader.text = ''; loader.succeed(); } catch (err) { loader.fail(err.toString());