Skip to content

Commit a0c085f

Browse files
feat: npx auth ask (#3)
* feat: `npx auth ask` * tweak readme * lock package version
1 parent de69263 commit a0c085f

File tree

8 files changed

+349
-9
lines changed

8 files changed

+349
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ Options:
4545
-h, --help display help for command
4646

4747
Commands:
48-
secret [options] Generate a random string.
48+
ask Ask any question about docs, API, etc.
4949
framework [framework] Clone a framework template.
50+
secret [options] Generate a random string.
5051
help [command] display help for command
5152
```
5253

commands/ask.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @ts-check
2+
3+
import * as y from "yoctocolors"
4+
import { input } from "@inquirer/prompts"
5+
6+
export async function action() {
7+
try {
8+
const query = await input({ message: "What would you like to know: " })
9+
10+
const response = await fetch("https://authjs.dev/api/ask", {
11+
method: "POST",
12+
headers: { "Content-Type": "application/json" },
13+
body: JSON.stringify({ query }),
14+
})
15+
16+
const result = await response.json()
17+
if (!response.ok) return console.error(y.red(result))
18+
19+
console.log(result)
20+
} catch (e) {
21+
if (!(e instanceof Error)) return
22+
if (e.message.startsWith("User force closed")) return
23+
24+
console.error(y.red(e.stack ?? e.message))
25+
}
26+
}

commands/framework.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const frameworks = {
1616
},
1717
}
1818

19-
export const action = (framework) => {
19+
export function action(framework) {
2020
if (!framework) {
2121
return console.log(`
2222
Supported frameworks are: ${Object.keys(frameworks).join(", ")}`)

commands/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * as ask from "./ask.js"
2+
export * as framework from "./framework.js"
3+
export * as secret from "./secret.js"

commands/secret.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function randomString(size = 32) {
1010
return Buffer.from(bytes, "base64").toString("base64")
1111
}
1212

13-
export async function action(options) {
13+
export function action(options) {
1414
let value = randomString()
1515
if (options.raw) return console.log(value)
1616

index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import { InvalidArgumentError } from "commander"
66
import { Command } from "commander"
7-
import { frameworks, action as frameworkAction } from "./commands/framework.js"
8-
import { action as secretAction } from "./commands/secret.js"
7+
import { ask, framework, secret } from "./commands/index.js"
8+
99
// import pkg from "./package.json" assert { type: "json" }
1010

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

2828
program.name(name).description(description).version(version)
2929

30+
program
31+
.command("ask")
32+
.description("Ask any question about docs, API, etc.")
33+
.action(ask.action)
34+
3035
program
3136
.command("framework")
3237
.argument("[framework]", "The framework to use.", (value) => {
3338
if (!value) return value
34-
if (Object.keys(frameworks).includes(value)) return value
39+
if (Object.keys(framework.frameworks).includes(value)) return value
3540
throw new InvalidArgumentError(
36-
`Valid frameworks are: ${frameworks.join(", ")}`
41+
`Valid frameworks are: ${framework.frameworks.join(", ")}`
3742
)
3843
})
3944
.description("Clone a framework template.")
40-
.action(frameworkAction)
45+
.action(framework.action)
4146

4247
program
4348
.command("secret")
4449
.option("--raw", "Output the string without any formatting.")
4550
.option("--copy", 'Copy AUTH_SECRET="value"')
4651
.description("Generate a random string.")
47-
.action(secretAction)
52+
.action(secret.action)
4853

4954
program.parse()
5055

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"cli"
2929
],
3030
"dependencies": {
31+
"@inquirer/prompts": "3.3.2",
3132
"commander": "11.1.0",
3233
"yoctocolors": "1.0.0"
3334
},

0 commit comments

Comments
 (0)