Skip to content

feat: npx auth ask #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Options:
-h, --help display help for command

Commands:
secret [options] Generate a random string.
ask Ask any question about docs, API, etc.
framework [framework] Clone a framework template.
secret [options] Generate a random string.
help [command] display help for command
```

Expand Down
26 changes: 26 additions & 0 deletions commands/ask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check

import * as y from "yoctocolors"
import { input } from "@inquirer/prompts"

export async function action() {
try {
const query = await input({ message: "What would you like to know: " })

const response = await fetch("https://authjs.dev/api/ask", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query }),
})

const result = await response.json()
if (!response.ok) return console.error(y.red(result))

console.log(result)
} catch (e) {
if (!(e instanceof Error)) return
if (e.message.startsWith("User force closed")) return

console.error(y.red(e.stack ?? e.message))
}
}
2 changes: 1 addition & 1 deletion commands/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const frameworks = {
},
}

export const action = (framework) => {
export function action(framework) {
if (!framework) {
return console.log(`
Supported frameworks are: ${Object.keys(frameworks).join(", ")}`)
Expand Down
3 changes: 3 additions & 0 deletions commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as ask from "./ask.js"
export * as framework from "./framework.js"
export * as secret from "./secret.js"
2 changes: 1 addition & 1 deletion commands/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function randomString(size = 32) {
return Buffer.from(bytes, "base64").toString("base64")
}

export async function action(options) {
export function action(options) {
let value = randomString()
if (options.raw) return console.log(value)

Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { InvalidArgumentError } from "commander"
import { Command } from "commander"
import { frameworks, action as frameworkAction } from "./commands/framework.js"
import { action as secretAction } from "./commands/secret.js"
import { ask, framework, secret } from "./commands/index.js"

// import pkg from "./package.json" assert { type: "json" }

import fs from "fs/promises"
Expand All @@ -27,24 +27,29 @@ const program = new Command()

program.name(name).description(description).version(version)

program
.command("ask")
.description("Ask any question about docs, API, etc.")
.action(ask.action)

program
.command("framework")
.argument("[framework]", "The framework to use.", (value) => {
if (!value) return value
if (Object.keys(frameworks).includes(value)) return value
if (Object.keys(framework.frameworks).includes(value)) return value
throw new InvalidArgumentError(
`Valid frameworks are: ${frameworks.join(", ")}`
`Valid frameworks are: ${framework.frameworks.join(", ")}`
)
})
.description("Clone a framework template.")
.action(frameworkAction)
.action(framework.action)

program
.command("secret")
.option("--raw", "Output the string without any formatting.")
.option("--copy", 'Copy AUTH_SECRET="value"')
.description("Generate a random string.")
.action(secretAction)
.action(secret.action)

program.parse()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cli"
],
"dependencies": {
"@inquirer/prompts": "3.3.2",
"commander": "11.1.0",
"yoctocolors": "1.0.0"
},
Expand Down
Loading