This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsync.ts
143 lines (130 loc) · 4.28 KB
/
sync.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Runs the given command in each repository, and commits
* files that were added or changed.
*/
import * as cp from 'child_process';
import * as fs from 'fs';
import * as meow from 'meow';
import ora from 'ora';
import Q from 'p-queue';
import * as path from 'path';
import {promisify} from 'util';
import {GetConfig} from './lib/config.js';
import {GitHub} from './lib/github.js';
import * as logger from './lib/logger.js';
const mkdir = promisify(fs.mkdir);
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const spawn = promisify(cp.exec);
function print(res: {stdout: string; stderr: string}) {
if (res.stdout) {
console.log(res.stdout);
}
if (res.stderr) {
console.log(res.stderr);
}
return res;
}
/**
* Clone all repositories into ~/.repo.
* If repo already exists, fetch and reset.
*/
export async function sync(cli: meow.Result<meow.AnyFlags>) {
const repos = await getRepos();
const rootPath = await getRootPath();
const dirs = await readdir(rootPath);
const orb = ora('Synchronizing repositories...').start();
let i = 0;
const concurrency = cli.flags.concurrency
? Number(cli.flags.concurrency)
: 50;
const q = new Q({concurrency});
const proms = repos.map(repo => {
const cloneUrl = repo.getRepository().ssh_url!;
const cwd = path.join(rootPath, repo.name);
return q.add(async () => {
if (dirs.indexOf(repo.name) !== -1) {
await spawn('git fetch --all', {cwd});
await spawn(`git reset --hard origin/${repo.baseBranch}`, {cwd});
await spawn(`git checkout ${repo.baseBranch}`, {cwd});
await spawn('git fetch origin', {cwd});
await spawn(`git reset --hard origin/${repo.baseBranch}`, {cwd});
orb.text = `[${i + 1}/${repos.length}] Synchronized ${repo.name}...`;
} else {
await spawn(`git clone ${cloneUrl}`, {cwd: rootPath});
orb.text = `[${i + 1}/${repos.length}] Cloned ${repo.name}...`;
}
i++;
});
});
await Promise.all(proms);
orb.succeed('Repo sync complete.');
}
export async function exec(cli: meow.Result<meow.AnyFlags>) {
const command = cli.input.slice(1);
const rootPath = await getRootPath();
// get all of the subdirectories in ~/.repo.
const files: string[] = await readdir(rootPath);
const ps = await Promise.all(
files.map(async file => {
file = path.join(rootPath, file);
const stats = await stat(file);
return {file, isDirectory: stats.isDirectory()};
})
);
const dirs = ps.filter(x => x.isDirectory).map(x => x.file);
if (dirs.length === 0) {
// the user likely hasn't run sync yet. Lets be nice and do that for them.
await sync(cli);
}
logger.info(`Executing '${command}' in ${dirs.length} directories.`);
let i = 0;
const concurrency = cli.flags.concurrency
? Number(cli.flags.concurrency)
: 10;
const q = new Q({concurrency});
const proms = dirs.map(dir => {
return q.add(() => {
return spawn(command.join(' '), {cwd: dir})
.then(r => {
i++;
logger.info(`[${i}/${dirs.length}] Executed cmd in ${dir}.`);
print(r);
})
.catch(e => {
i++;
logger.error(dir);
logger.error(e);
});
});
});
await Promise.all(proms);
logger.info('Command execution successful.');
}
async function getRepos() {
const config = await GetConfig.getConfig();
const github = new GitHub(config);
const repos = await github.getRepositories();
return repos.filter(x => !x.repository.archived);
}
async function getRootPath() {
const config = await GetConfig.getConfig();
const repoPath = config.clonePath;
if (!fs.existsSync(repoPath)) {
await mkdir(repoPath);
}
return repoPath;
}