-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
672 lines (596 loc) · 25.4 KB
/
index.js
File metadata and controls
672 lines (596 loc) · 25.4 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import fs from 'fs-extra'
import shell from 'shelljs'
import { relative, dirname, basename, extname } from 'path'
import fetch from 'node-fetch'
import { extension } from 'mime-types'
import { glob } from 'glob'
shell.config.fatal = true
const perPage = 100
const retryCount = 10
const retryDelayRateLimit = 6 * 60
const retryDelayOthers = 6
const { USERNAME, TOKEN } = process.env
const folder = '/usr/src/backup'
const metadataPath = `${folder}/metadata.json`
const METADATA_VERSION = 3
function delay(seconds) {
return new Promise(resolve => {
console.log(`... delay ${seconds}s`)
setTimeout(() => {
return resolve()
}, seconds * 1000)
})
}
function request(path, options = {}) {
return new Promise(async (resolve, reject) => {
const baseUrl = path.substr(0, 4) !== 'http' ? 'https://api.github.com' : ''
console.log(`Request ${baseUrl}${path}`)
for (let n = 1; n <= retryCount; n++) {
let resp
try {
resp = await fetch(`${baseUrl}${path}`, {
...options,
headers: {
Authorization: `Token ${TOKEN}`,
...options.headers || {}
}
})
} catch {
console.log(`... failed at #${n} attempt`)
if (n < retryCount) {
await delay(retryDelayOthers)
continue
} else {
return reject()
}
}
if (resp.ok) {
return resolve(resp)
} else {
const rateLimitHeader = [ ...resp.headers ].find(obj => obj[0] === 'x-ratelimit-remaining')
const rateLimitLimitHeader = [ ...resp.headers ].find(obj => obj[0] === 'x-ratelimit-limit')
const rateLimitRemaining = rateLimitHeader ? parseInt(rateLimitHeader[1]) : null
const rateLimitLimit = rateLimitLimitHeader ? parseInt(rateLimitLimitHeader[1]) : null
console.log(`... failed at #${n} attempt (status ${resp.status})`)
if (rateLimitRemaining === 0) {
console.log(`... API rate limit of ${rateLimitLimit} requests per hour exceeded`)
if (n < retryCount) await delay(retryDelayRateLimit)
} else {
if (n < retryCount) await delay(retryDelayOthers)
}
}
}
return reject()
})
}
function requestJson(path, options) {
return new Promise(async (resolve, reject) => {
try {
const response = await request(path, options)
const json = await response.json()
return resolve(json)
} catch (err) {
return reject(err)
}
})
}
function requestAll(path, options) {
return new Promise(async (resolve, reject) => {
try {
let items = []
let page = 1
while (page !== null) {
const separator = path.indexOf('?') === -1 ? '?' : '&'
const moreItemsResponse = await request(`${path}${separator}per_page=${perPage}&page=${page}`, options)
const moreItems = await moreItemsResponse.json()
if (moreItems.length) {
items = [...items, ...moreItems]
page = moreItems.length === perPage ? page + 1 : null
} else {
page = null
}
}
return resolve(items)
} catch (err) {
return reject(err)
}
})
}
async function requestAllWithRetry(path, options) {
for (let n = 1; n <= retryCount; n++) {
try {
const items = requestAll(path, options)
return items
} catch (err) {
if (n === 10) return err
console.log('... failed at attempt #' + n)
await delay(retryDelayOthers)
}
}
}
function downloadFile(sourceFileUrl, targetFilePath) {
return new Promise(async (resolve, reject) => {
// Prefer the extension from the source URL (e.g. .zip, .pdf in /files/<id>/<name>.zip)
if (!extname(targetFilePath)) {
let urlExt = ''
try { urlExt = extname(new URL(sourceFileUrl).pathname) } catch {}
if (urlExt) targetFilePath = targetFilePath + urlExt
}
// Skip download if file already exists (check with any extension if none specified)
if (!extname(targetFilePath)) {
const dir = dirname(targetFilePath)
const base = basename(targetFilePath)
if (fs.existsSync(dir)) {
const existing = fs.readdirSync(dir).filter(f => f.startsWith(base + '.'))
if (existing.length > 0) {
return resolve(`${dir}/${existing[0]}`)
}
}
} else if (fs.existsSync(targetFilePath)) {
return resolve(targetFilePath)
}
const isArchive = /\/(zipball|tarball)\//.test(sourceFileUrl)
const requestOptions = isArchive ? {} : { headers: { Accept: 'application/octet-stream' } }
const response = await request(sourceFileUrl, requestOptions)
if (!extname(targetFilePath)) {
const ctHeader = [ ...response.headers ].find(obj => obj[0] === 'content-type')
const ext = ctHeader ? extension(ctHeader[1]) : ''
targetFilePath = targetFilePath + (ext ? '.' + ext : '.bin')
}
fs.ensureDirSync(dirname(targetFilePath))
const fileStream = fs.createWriteStream(targetFilePath)
response.body.pipe(fileStream)
response.body.on('error', () => { return reject() })
fileStream.on('finish', () => {
return resolve(targetFilePath)
})
})
}
function downloadAttachments(body, folder, filename, baseAttachmentPath = './attachments') {
return new Promise(async (resolve, reject) => {
try {
const files = []
const attachments = body?.match(/["(]https:\/\/github\.com\/(.+)\/(assets|files)\/(.+)[)"]/g) || []
for (let n = 0; n < attachments.length; n++) {
const targetFilename = filename.replace('{id}', (n+1).toString().padStart(attachments.length.toString().length, '0'))
const targetPath = folder + '/' + targetFilename
const sourceUrl = attachments[n].replace(/^["(](.+)[)"]$/, '$1')
fs.ensureDirSync(folder)
const realTargetFilename = basename(await downloadFile(sourceUrl, targetPath))
files.push(realTargetFilename)
body = body.replace(`"${sourceUrl}"`, `"${baseAttachmentPath}/${realTargetFilename}"`)
body = body.replace(`(${sourceUrl})`, `(${baseAttachmentPath}/${realTargetFilename})`)
}
return resolve({ body, files })
} catch (err) {
return reject(err)
}
})
}
function migrateLegacyAttachmentPaths(filePath) {
if (!fs.existsSync(filePath)) return false
const content = fs.readFileSync(filePath, 'utf8')
const updated = content.replace(/((?:\.\.?\/)+)images\/(issue|release|markdown)_/g, '$1attachments/$2_')
if (content === updated) return false
fs.writeFileSync(filePath, updated)
return true
}
function readJsonSafe(path, fallback) {
try {
return fs.existsSync(path) ? fs.readJsonSync(path) : fallback
} catch {
return fallback
}
}
function writeJSON(path, json) {
fs.ensureDirSync(dirname(path))
fs.writeJsonSync(path, json, { spaces: 2 })
}
function loadMetadata() {
try {
return fs.existsSync(metadataPath) ? fs.readJsonSync(metadataPath) : {}
} catch {
return {}
}
}
function saveMetadata(metadata) {
writeJSON(metadataPath, { version: METADATA_VERSION, ...metadata })
}
async function backup() {
try {
// Ensure backup folder exists (no longer wiped)
fs.ensureDirSync(folder)
// Load metadata from previous backup; discard if version mismatch to force full re-sync
const metadata = loadMetadata()
const metadataValid = metadata.version === METADATA_VERSION
if (!metadataValid) console.log(`Metadata version mismatch (expected ${METADATA_VERSION}, got ${metadata.version ?? 'none'}) — forcing full re-sync`)
const repoMeta = metadataValid ? (metadata.repositories || {}) : {}
const starredMeta = metadataValid ? (metadata.starred || {}) : {}
// Get repositories
const repositories = await requestAllWithRetry('/user/repos')
// Determine which repos were removed from GitHub
const currentRepoNames = new Set(repositories.map(r => r.name))
const existingRepoNames = new Set(Object.keys(repoMeta))
for (const name of existingRepoNames) {
if (!currentRepoNames.has(name)) {
console.log(`Removing deleted repository: ${name}`)
fs.removeSync(`${folder}/repositories/${name}`)
}
}
// Save repositories
if (fs.existsSync(`${folder}/repositories.json`)) fs.removeSync(`${folder}/repositories.json`)
if (repositories.length > 0) writeJSON(`${folder}/repositories/repositories.json`, repositories)
// Track new metadata
const newRepoMeta = {}
// Loop repositories
for (const repository of repositories) {
const prev = repoMeta[repository.name]
const repoDir = `${folder}/repositories/${repository.name}`
const repoPath = `${repoDir}/repository`
const issuesDir = `${repoDir}/issues`
const releasesDir = `${repoDir}/releases`
const attachmentsDir = `${repoDir}/attachments`
const localExists = fs.existsSync(`${repoPath}/.git`)
const isNew = !prev || !localExists
const codeChanged = isNew || prev.pushed_at !== repository.pushed_at
const defaultBranch = repository.default_branch || 'main'
// Migrate legacy images folder to attachments (markdown attachments)
const legacyImagesDir = `${repoDir}/images`
if (fs.existsSync(legacyImagesDir) && !fs.existsSync(attachmentsDir)) {
console.log(`Migrating images to attachments: ${repository.name}`)
fs.moveSync(legacyImagesDir, attachmentsDir)
}
// Migrate legacy issues.json to issues/issues.json
if (fs.existsSync(`${repoDir}/issues.json`) && !fs.existsSync(`${issuesDir}/issues.json`)) {
console.log(`Migrating issues.json location: ${repository.name}`)
fs.ensureDirSync(issuesDir)
fs.moveSync(`${repoDir}/issues.json`, `${issuesDir}/issues.json`)
}
// Migrate legacy releases.json to releases/releases.json
if (fs.existsSync(`${repoDir}/releases.json`) && !fs.existsSync(`${releasesDir}/releases.json`)) {
console.log(`Migrating releases.json location: ${repository.name}`)
fs.ensureDirSync(releasesDir)
fs.moveSync(`${repoDir}/releases.json`, `${releasesDir}/releases.json`)
}
// Rewrite legacy ./images/ references in stored data so old backups stay valid
if (fs.existsSync(attachmentsDir)) {
let pathsRewritten = false
if (migrateLegacyAttachmentPaths(`${issuesDir}/issues.json`)) pathsRewritten = true
if (migrateLegacyAttachmentPaths(`${releasesDir}/releases.json`)) pathsRewritten = true
if (fs.existsSync(repoPath)) {
for (const markdownFile of await glob(`${repoPath}/**/*.{md,MD}`)) {
if (migrateLegacyAttachmentPaths(markdownFile)) pathsRewritten = true
}
}
if (pathsRewritten) console.log(`Migrated legacy attachment paths: ${repository.name}`)
}
console.log(`Processing repository data: ${repository.name}`)
// Load previously stored issues/releases to reuse data for unchanged items
// Skip stored data on full re-sync so all attachments are re-checked
const storedIssuesById = metadataValid ? new Map(readJsonSafe(`${issuesDir}/issues.json`, []).map(i => [i.id, i])) : new Map()
const storedReleasesById = metadataValid ? new Map(readJsonSafe(`${releasesDir}/releases.json`, []).map(r => [r.id, r])) : new Map()
// Get issues
const issues = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/issues?state=all`)
const currentIssueNumbers = new Set(issues.map(i => String(i.number)))
// Loop issues
for (const issue of issues) {
const issueDir = `${issuesDir}/${issue.number}`
const stored = storedIssuesById.get(issue.id)
const issueChanged = !stored || stored.updated_at !== issue.updated_at
if (!issueChanged) {
issue.body = stored.body
issue.comments = stored.comments
continue
}
// Download issue body attachments into the per-issue folder
const issueResult = await downloadAttachments(
issue.body,
issueDir,
`body_{id}`,
`./${issue.number}`
)
issue.body = issueResult.body
const issueFiles = new Set(issueResult.files)
// Get issue comments
const comments = issue.comments !== 0 ? await requestAllWithRetry(issue.comments_url) : []
issue.comments = comments
for (const comment of comments) {
const commentResult = await downloadAttachments(
comment.body,
issueDir,
`comment_${comment.id}_{id}`,
`./${issue.number}`
)
comment.body = commentResult.body
for (const f of commentResult.files) issueFiles.add(f)
}
// Clean up orphaned attachments in this issue's folder
if (fs.existsSync(issueDir)) {
for (const file of fs.readdirSync(issueDir)) {
if (!issueFiles.has(file)) fs.removeSync(`${issueDir}/${file}`)
}
}
}
if (issues.length > 0) {
writeJSON(`${issuesDir}/issues.json`, issues)
// Remove folders for deleted issues
for (const entry of fs.readdirSync(issuesDir)) {
if (entry === 'issues.json') continue
const entryPath = `${issuesDir}/${entry}`
if (fs.statSync(entryPath).isDirectory() && !currentIssueNumbers.has(entry)) {
console.log(`Removing deleted issue: ${repository.name}#${entry}`)
fs.removeSync(entryPath)
}
}
} else if (fs.existsSync(issuesDir)) {
console.log(`Removing issues folder (no issues): ${repository.name}`)
fs.removeSync(issuesDir)
}
// Get releases
const releases = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/releases`)
const currentReleaseTags = new Set(releases.map(r => r.tag_name))
const releaseExpectedFiles = new Map()
// Loop releases
for (const release of releases) {
const releaseDir = `${releasesDir}/${release.tag_name}`
const safeDownload = (url, target) => downloadFile(url, target).catch(() => {
console.log(`... failed to download ${url}`)
})
// Track exactly which files belong in this release folder
const expectedFiles = new Set()
if (release.zipball_url) expectedFiles.add('Source code.zip')
for (const asset of release.assets) expectedFiles.add(asset.name)
// Download release assets and zip
for (const asset of release.assets) {
safeDownload(asset.url, `${releaseDir}/${asset.name}`)
}
if (release.zipball_url) {
safeDownload(release.zipball_url, `${releaseDir}/Source code.zip`)
}
const stored = storedReleasesById.get(release.id)
const releaseChanged = !stored || stored.updated_at !== release.updated_at
if (!releaseChanged) {
release.body = stored.body
// Preserve existing numeric body attachment files
if (fs.existsSync(releaseDir)) {
for (const file of fs.readdirSync(releaseDir)) {
if (/^\d+\.\w+$/.test(file)) expectedFiles.add(file)
}
}
} else {
// Download release body attachments into the release folder
const releaseResult = await downloadAttachments(
release.body,
releaseDir,
`{id}`,
`./${release.tag_name}`
)
release.body = releaseResult.body
for (const f of releaseResult.files) expectedFiles.add(f)
// Clean up orphaned body attachments (numeric-only filenames, e.g. 01.png)
if (fs.existsSync(releaseDir)) {
for (const file of fs.readdirSync(releaseDir)) {
if (/^\d+\.\w+$/.test(file) && !expectedFiles.has(file)) {
fs.removeSync(`${releaseDir}/${file}`)
}
}
}
}
releaseExpectedFiles.set(release.tag_name, expectedFiles)
}
if (releases.length > 0) {
writeJSON(`${releasesDir}/releases.json`, releases)
// Remove folders for deleted releases
for (const entry of fs.readdirSync(releasesDir)) {
if (entry === 'releases.json') continue
const entryPath = `${releasesDir}/${entry}`
if (fs.statSync(entryPath).isDirectory() && !currentReleaseTags.has(entry)) {
console.log(`Removing deleted release: ${repository.name}/${entry}`)
fs.removeSync(entryPath)
}
}
} else if (fs.existsSync(releasesDir)) {
console.log(`Removing releases folder (no releases): ${repository.name}`)
fs.removeSync(releasesDir)
}
// Clean up legacy issue_* and release_* files from the attachments folder
if (fs.existsSync(attachmentsDir)) {
for (const file of fs.readdirSync(attachmentsDir)) {
if (file.startsWith('issue_') || file.startsWith('release_')) {
fs.removeSync(`${attachmentsDir}/${file}`)
}
}
}
// Clone or update git repository only if code changed
if (codeChanged) {
// Remove macOS AppleDouble files from pack directory to avoid "non-monotonic index" errors
shell.exec(`find "${repoPath}/.git/objects/pack" -name '._*' -delete 2>/dev/null || true`)
if (localExists) {
console.log(`Updating git repository: ${repository.name}`)
shell.exec(`git -C "${repoPath}" fetch --all && git -C "${repoPath}" reset --hard "origin/${defaultBranch}"`)
} else {
console.log(`Cloning git repository: ${repository.name}`)
shell.exec(`git clone "https://${TOKEN}@github.com/${USERNAME}/${repository.name}.git" "${repoPath}"`)
}
// Process markdown attachments into /markdown/{relative-path}/{filename.md}/
const repoFolder = `${repoPath}/`
const markdownBaseDir = `${repoDir}/markdown`
const markdownFiles = await glob(`${repoFolder}**/*.{md,MD}`)
const activeMarkdownDirs = new Set()
for (const markdownFile of markdownFiles) {
const relPath = markdownFile.replace(repoFolder, '')
const markdownAttachmentDir = `${markdownBaseDir}/${relPath}`
const baseAttachmentPath = relative(dirname(markdownFile), markdownAttachmentDir)
const markdownFileContent = fs.readFileSync(markdownFile, { encoding: 'utf8' })
const markdownResult = await downloadAttachments(
markdownFileContent,
markdownAttachmentDir,
`{id}`,
baseAttachmentPath
)
if (markdownResult.files.length > 0) {
activeMarkdownDirs.add(markdownAttachmentDir)
const validFiles = new Set(markdownResult.files)
for (const file of fs.readdirSync(markdownAttachmentDir)) {
if (!validFiles.has(file)) fs.removeSync(`${markdownAttachmentDir}/${file}`)
}
}
fs.writeFileSync(markdownFile, markdownResult.body)
}
// Remove markdown attachment folders for files that no longer exist or have no attachments
const cleanMarkdownTree = (dir) => {
if (!fs.existsSync(dir)) return false
let hasContent = false
for (const entry of [...fs.readdirSync(dir)]) {
const entryPath = `${dir}/${entry}`
if (!fs.statSync(entryPath).isDirectory()) { hasContent = true; continue }
if (/\.(md|MD)$/.test(entry)) {
if (activeMarkdownDirs.has(entryPath)) hasContent = true
else fs.removeSync(entryPath)
} else {
if (cleanMarkdownTree(entryPath)) hasContent = true
else fs.removeSync(entryPath)
}
}
return hasContent
}
if (fs.existsSync(markdownBaseDir)) {
if (!cleanMarkdownTree(markdownBaseDir)) fs.removeSync(markdownBaseDir)
}
// Clean up legacy markdown_* files from the attachments folder
if (fs.existsSync(attachmentsDir)) {
for (const file of fs.readdirSync(attachmentsDir)) {
if (file.startsWith('markdown_')) fs.removeSync(`${attachmentsDir}/${file}`)
}
}
} else {
console.log(`Skipping unchanged git repository: ${repository.name}`)
}
// Remove attachments folder if now empty
if (fs.existsSync(attachmentsDir) && fs.readdirSync(attachmentsDir).length === 0) {
fs.removeSync(attachmentsDir)
}
// Remove any unexpected files or folders from the repo backup folder (recursively)
const cleanupDir = (dir, isAllowed) => {
if (!fs.existsSync(dir)) return
for (const entry of fs.readdirSync(dir)) {
const entryPath = `${dir}/${entry}`
const isDir = fs.statSync(entryPath).isDirectory()
if (!isAllowed(entry, isDir)) {
console.log(`Removing unexpected entry: ${entryPath.replace(`${repoDir}/`, `${repository.name}/`)}`)
fs.removeSync(entryPath)
}
}
}
// Top level: only repository/, issues/, releases/, markdown/
cleanupDir(repoDir, (e) => ['repository', 'issues', 'releases', 'markdown'].includes(e))
// issues/: only issues.json and numeric issue-number folders
cleanupDir(issuesDir, (e, isDir) => e === 'issues.json' || (isDir && /^\d+$/.test(e)))
// issues/{number}/: only files, no subdirectories
if (fs.existsSync(issuesDir)) {
for (const entry of fs.readdirSync(issuesDir).filter(e => e !== 'issues.json')) {
cleanupDir(`${issuesDir}/${entry}`, (_e, isDir) => !isDir)
}
}
// releases/: only releases.json and tag-name folders
cleanupDir(releasesDir, (e, isDir) => e === 'releases.json' || isDir)
// releases/{tag}/: only files, no subdirectories
if (fs.existsSync(releasesDir)) {
for (const entry of fs.readdirSync(releasesDir).filter(e => e !== 'releases.json')) {
cleanupDir(`${releasesDir}/${entry}`, (e, isDir) => !isDir && (releaseExpectedFiles.get(entry) || new Set()).has(e))
}
}
// Store metadata for this repo
newRepoMeta[repository.name] = {
updated_at: repository.updated_at,
pushed_at: repository.pushed_at
}
// Save metadata progressively to allow safe interruptions
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: { ...repoMeta, ...newRepoMeta },
starred: starredMeta
})
}
// Get user details
const user = await requestJson('/user')
if (fs.existsSync(`${folder}/user.json`)) fs.removeSync(`${folder}/user.json`)
writeJSON(`${folder}/user/user.json`, user)
if (user.avatar_url) {
await downloadFile(user.avatar_url, `${folder}/user/avatar`).catch(() => {
console.log('... failed to download user avatar')
})
}
// Get starred repositories
const starred = await requestAllWithRetry('/user/starred')
const starredDir = `${folder}/starred`
// Determine which starred repos were removed
const currentStarredKeys = new Set(starred.map(r => `${r.owner.login}/${r.name}`))
const existingStarredKeys = new Set(Object.keys(starredMeta))
for (const key of existingStarredKeys) {
if (!currentStarredKeys.has(key)) {
const [owner, name] = key.split('/')
console.log(`Removing unstarred repository: ${key}`)
fs.removeSync(`${starredDir}/${owner}/${name}.zip`)
const ownerDir = `${starredDir}/${owner}`
if (fs.existsSync(ownerDir) && fs.readdirSync(ownerDir).length === 0) {
fs.removeSync(ownerDir)
}
}
}
// Download latest source code zip for each starred repository
const newStarredMeta = {}
for (const repo of starred) {
const owner = repo.owner.login
const name = repo.name
const key = `${owner}/${name}`
const ownerDir = `${starredDir}/${owner}`
const targetZip = `${ownerDir}/${name}.zip`
const defaultBranch = repo.default_branch || 'main'
const prev = starredMeta[key]
const isChanged = !metadataValid || !prev || prev.pushed_at !== repo.pushed_at
// Migrate legacy subfolder (git clone or zip-in-subdir) to flat zip
if (fs.existsSync(`${ownerDir}/${name}`)) {
console.log(`Removing legacy subfolder: ${key}`)
fs.removeSync(`${ownerDir}/${name}`)
}
if (isChanged || !fs.existsSync(targetZip)) {
console.log(`Downloading starred repository: ${key}`)
fs.ensureDirSync(ownerDir)
if (fs.existsSync(targetZip)) fs.removeSync(targetZip)
await downloadFile(`https://api.github.com/repos/${owner}/${name}/zipball/${defaultBranch}`, targetZip).catch(() => {
console.log(`... failed to download ${key}`)
})
} else {
console.log(`Skipping unchanged starred repository: ${key}`)
}
newStarredMeta[key] = { pushed_at: repo.pushed_at }
// Save metadata progressively for starred repos
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: newRepoMeta,
starred: { ...starredMeta, ...newStarredMeta }
})
}
// Save starred.json inside the starred folder (only if any starred repos exist)
if (starred.length > 0) {
writeJSON(`${starredDir}/starred.json`, starred)
} else if (fs.existsSync(starredDir)) {
console.log('Removing starred folder (no starred repositories)')
fs.removeSync(starredDir)
}
// Save final complete metadata
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: newRepoMeta,
starred: newStarredMeta
})
// Complete script
console.log('Backup completed!')
shell.exit()
} catch (err) {
throw new Error(err)
}
}
// Run the backup
backup()