Commit d858048
Changed files (4)
src/main.ts
@@ -14,7 +14,8 @@ import { updateBadges } from './update-badges.js'
void (async function main() {
const { env } = process
const argv = minimist(process.argv.slice(2), {
- string: ['data', 'repo', 'token', 'size', 'user', 'dryrun'],
+ string: ['data', 'repo', 'token', 'size', 'user'],
+ boolean: ['dryrun'],
})
const {
token = env.GITHUB_TOKEN,
src/update-badges.ts
@@ -9,7 +9,7 @@ export async function updateBadges(
badges: Badge[],
oldJson: string | undefined,
jsonSha: string | undefined,
- dryrun: string | undefined,
+ dryrun: boolean,
) {
const myBadgesPath = 'my-badges/my-badges.json'
@@ -29,7 +29,7 @@ export async function updateBadges(
name: 'My Badges',
email: 'my-badges@github.com',
},
- content: Buffer.from(newJson, 'utf8').toString('base64'),
+ content: newJson,
sha: jsonSha,
},
dryrun,
@@ -82,7 +82,7 @@ export async function updateBadges(
name: 'My Badges',
email: 'my-badges@github.com',
},
- content: Buffer.from(content, 'utf8').toString('base64'),
+ content,
sha: sha,
},
dryrun,
src/update-readme.ts
@@ -8,7 +8,7 @@ export async function updateReadme(
repo: string,
badges: Badge[],
size: number | string = 64,
- dryrun: string | undefined,
+ dryrun: boolean,
) {
console.log('Loading README.md')
const readme = await octokit.request<'readme'>(
@@ -61,7 +61,7 @@ export async function updateReadme(
name: 'My Badges',
email: 'my-badges@github.com',
},
- content: Buffer.from(content, 'utf8').toString('base64'),
+ content,
sha: readme.data.sha,
},
dryrun,
src/utils.ts
@@ -1,4 +1,5 @@
import fs from 'node:fs/promises'
+import path from 'node:path'
import { Octokit } from 'octokit'
import { Commit, Pull } from './collect/collect.js'
@@ -29,13 +30,19 @@ export const upload = async (
octokit: Octokit,
route: Parameters<Octokit['request']>[0],
data: Parameters<Octokit['request']>[1],
- dryrun?: string,
+ dryrun?: boolean,
) => {
if (dryrun) {
console.log(`Skipped pushing ${data?.path} (dryrun)`)
- return fs.writeFile(data?.path as string, data?.content as string)
+ const filepath = path.join(process.cwd(), data?.path as string)
+
+ await fs.mkdir(path.dirname(filepath), { recursive: true })
+ await fs.writeFile(filepath, data?.content as string)
}
console.log(`Uploading ${data?.path}`)
- return octokit.request(route, data)
+ return octokit.request(route, {
+ ...data,
+ content: Buffer.from(data?.content as string, 'utf8').toString('base64'),
+ })
}