master
1#!/usr/bin/env node
2
3import fs from 'node:fs'
4import allBadges from '#badges'
5import { presentBadges } from './present-badges.js'
6import { getRepo } from './repo.js'
7import { updateBadges } from './update-badges.js'
8import { updateReadme } from './update-readme.js'
9import { processTasks } from './process-tasks.js'
10import { Data } from './data.js'
11import { createCtx } from './context.js'
12import { log } from './log.js'
13import url from 'node:url'
14
15isMain() &&
16 main()
17 .then(() => process.exit(0))
18 .catch((err) => {
19 log.error(err)
20 process.exit(1)
21 })
22
23export async function main(
24 argv: string[] = process.argv.slice(2),
25 env: Record<string, string | undefined> = process.env,
26) {
27 const ctx = createCtx(argv, env)
28 const repo = getRepo(ctx)
29
30 let data: Data
31 if (fs.existsSync(ctx.dataFile)) {
32 data = JSON.parse(fs.readFileSync(ctx.dataFile, 'utf8')) as Data
33 } else {
34 let ok: boolean
35 ;[ok, data] = await processTasks(ctx)
36
37 if (!ok) {
38 return
39 }
40 }
41
42 repo.pull()
43 let userBadges = repo.getUserBadges()
44 userBadges = presentBadges(
45 allBadges.map((m) => m.default),
46 data,
47 userBadges,
48 ctx.badgesPick,
49 ctx.badgesOmit,
50 ctx.badgesCompact,
51 )
52
53 log.info(JSON.stringify(userBadges, null, 2))
54
55 if (repo.ready) {
56 updateBadges(userBadges, ctx.badgesDir, ctx.badgesDatafile)
57 updateReadme(userBadges, ctx.badgesSize, ctx.gitDir)
58 if (!ctx.dryrun && repo.hasChanges()) {
59 repo.push()
60 }
61 }
62}
63
64function isMain(metaurl = import.meta.url, scriptpath = process.argv[1]) {
65 if (metaurl.startsWith('file:')) {
66 const modulePath = url.fileURLToPath(metaurl).replace(/\.\w+$/, '')
67 const mainPath = fs.realpathSync(scriptpath).replace(/\.\w+$/, '')
68 return mainPath === modulePath
69 }
70
71 return false
72}