Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
68 lines (54 loc) · 1.61 KB
/
Copy pathutils.js
File metadata and controls
68 lines (54 loc) · 1.61 KB
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
import fs from 'fs/promises'
import path from 'path'
import { $ } from 'zx'
import { memoize } from 'lodash-es'
$.verbose = true
$.env = {
...process.env,
LANG: 'C'
}
export async function* walk(dir) {
for await (const d of await fs.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
export async function* findPackageJsonFiles(dir) {
for await (const file of walk(dir)) {
if (path.basename(file) === 'package.json') {
yield file
}
}
}
export async function* findCargoLockFiles(dir) {
for await (const file of walk(dir)) {
if (path.basename(file) === 'Cargo.lock') {
yield file
}
}
}
export async function asArray(asyncIterable) {
const arr = []
for await (const item of asyncIterable) {
arr.push(item)
}
return arr
}
export async function cloneRepo(repo, wsDir) {
const $$ = $({ cwd: wsDir });
let defaultBranch = memoize(async () => {
return (await $$`git remote show origin | sed -n '/HEAD branch/s/.*: //p'`).text().trim()
})
try {
await $$`git clone ${repo} .`
} catch (e) {
console.error(`Failed to clone ${repo} into ${wsDir}`)
await $$`git fetch origin -p`
console.info(`Default branch: ${await defaultBranch()}`)
await $$`git reset --hard origin/${await defaultBranch()}`
}
const latestCommit = (await $$`git rev-parse HEAD`).text().trim()
console.info(`Repository is now ready.`);
return { defaultBranch, latestCommit }
}