feat(cli): implement commander lib

This commit is contained in:
Lars Hampe 2024-10-31 22:12:25 +01:00
parent 19fa2121d9
commit 649819c777
5 changed files with 26 additions and 42 deletions

View File

@ -14,5 +14,9 @@
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"commander": "^12.1.0"
}
}

View File

@ -1,7 +1,21 @@
#! /usr/bin/env bun
import { program } from '@commander-js/extra-typings'
import { upload_commits } from './upload_commits'
import { args } from './utils/arguments'
import type { Arguments } from './utils/arguments'
if (args.success) {
upload_commits(args.data)
}
const ENV_CHANGELOG_ID = Bun.env.BT_CHANGELOG_ID
const ENV_ACCESS_TOKEN = Bun.env.BT_ACCESS_TOKEN
program.name('bt-cli').description('boring.tools CLI').version('0.8.0')
const commit = program.command('commit').description('Commits')
commit
.command('upload')
.description('Upload commit messages')
.requiredOption('--changelogId', 'Changelog Id', ENV_CHANGELOG_ID)
.requiredOption('--accessToken', 'Access Token', ENV_ACCESS_TOKEN)
.action((data) => {
upload_commits(data as Arguments)
})
program.parse()

View File

@ -6,7 +6,7 @@ const getLastCommitHash = async (args: Arguments) => {
const result = await fetchAPI(
`/v1/changelog/commit?changelogId=${args.changelogId}&limit=1`,
{},
args.token,
args.accessToken,
)
if (!result) {
@ -44,6 +44,6 @@ export const upload_commits = async (arguemnts: Arguments) => {
method: 'POST',
body: JSON.stringify(mappedCommits),
},
arguemnts.token,
arguemnts.accessToken,
)
}

View File

@ -1,42 +1,8 @@
import { parseArgs } from 'node:util'
import { z } from 'zod'
const ENV_ID = Bun.env.BT_CHANGELOG_ID
const ENV_TOKEN = Bun.env.BT_AUTH_TOKEN
const schema = z.object({
token: z.string(),
accessToken: z.string().startsWith('bt_'),
changelogId: z.string(),
})
export type Arguments = z.infer<typeof schema>
const { values } = parseArgs({
args: Bun.argv,
options: {
token: {
type: 'string',
},
changelogId: {
type: 'string',
},
},
strict: true,
allowPositionals: true,
})
const mappedArguments = {
...values,
token: values.token || ENV_TOKEN,
changelogId: values.changelogId || ENV_ID,
}
export const args = schema.safeParse(mappedArguments)
if (!args.success) {
console.error(
`boring.tools CLI: Missing arguemnts: ${args.error.errors
.map((error) => error.path[0])
.join(', ')}`,
)
process.exit(1)
}

BIN
bun.lockb

Binary file not shown.