feat(cli): implement commander lib
This commit is contained in:
parent
19fa2121d9
commit
649819c777
@ -14,5 +14,9 @@
|
|||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@commander-js/extra-typings": "^12.1.0",
|
||||||
|
"commander": "^12.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,21 @@
|
|||||||
#! /usr/bin/env bun
|
#! /usr/bin/env bun
|
||||||
|
import { program } from '@commander-js/extra-typings'
|
||||||
import { upload_commits } from './upload_commits'
|
import { upload_commits } from './upload_commits'
|
||||||
import { args } from './utils/arguments'
|
import type { Arguments } from './utils/arguments'
|
||||||
|
|
||||||
if (args.success) {
|
const ENV_CHANGELOG_ID = Bun.env.BT_CHANGELOG_ID
|
||||||
upload_commits(args.data)
|
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()
|
||||||
|
@ -6,7 +6,7 @@ const getLastCommitHash = async (args: Arguments) => {
|
|||||||
const result = await fetchAPI(
|
const result = await fetchAPI(
|
||||||
`/v1/changelog/commit?changelogId=${args.changelogId}&limit=1`,
|
`/v1/changelog/commit?changelogId=${args.changelogId}&limit=1`,
|
||||||
{},
|
{},
|
||||||
args.token,
|
args.accessToken,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
@ -44,6 +44,6 @@ export const upload_commits = async (arguemnts: Arguments) => {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(mappedCommits),
|
body: JSON.stringify(mappedCommits),
|
||||||
},
|
},
|
||||||
arguemnts.token,
|
arguemnts.accessToken,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,42 +1,8 @@
|
|||||||
import { parseArgs } from 'node:util'
|
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
const ENV_ID = Bun.env.BT_CHANGELOG_ID
|
|
||||||
const ENV_TOKEN = Bun.env.BT_AUTH_TOKEN
|
|
||||||
|
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
token: z.string(),
|
accessToken: z.string().startsWith('bt_'),
|
||||||
changelogId: z.string(),
|
changelogId: z.string(),
|
||||||
})
|
})
|
||||||
|
|
||||||
export type Arguments = z.infer<typeof schema>
|
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)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user