layerbase 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src/cli.tsx → dist/cli.js} +36 -35
- package/dist/cli.js.map +1 -0
- package/dist/commands/alias.js +27 -0
- package/dist/commands/alias.js.map +1 -0
- package/dist/commands/connect.js +51 -0
- package/dist/commands/connect.js.map +1 -0
- package/dist/commands/connection-string.js +43 -0
- package/dist/commands/connection-string.js.map +1 -0
- package/dist/commands/interactive.js +125 -0
- package/dist/commands/interactive.js.map +1 -0
- package/dist/commands/login.js +61 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/logout.js +25 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/ls.js +60 -0
- package/dist/commands/ls.js.map +1 -0
- package/dist/commands/whoami.js +49 -0
- package/dist/commands/whoami.js.map +1 -0
- package/dist/lib/alias.js +68 -0
- package/dist/lib/alias.js.map +1 -0
- package/dist/lib/browser-login.js +78 -0
- package/dist/lib/browser-login.js.map +1 -0
- package/dist/lib/cloud-api.js +44 -0
- package/dist/lib/cloud-api.js.map +1 -0
- package/dist/lib/config.js +32 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/engines.js +89 -0
- package/dist/lib/engines.js.map +1 -0
- package/dist/lib/format.js +38 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/launch.js +43 -0
- package/dist/lib/launch.js.map +1 -0
- package/dist/lib/open-browser.js +18 -0
- package/dist/lib/open-browser.js.map +1 -0
- package/dist/lib/run-spindb.js +49 -0
- package/dist/lib/run-spindb.js.map +1 -0
- package/dist/lib/token.js +15 -0
- package/dist/lib/token.js.map +1 -0
- package/dist/ui/app.js +27 -0
- package/dist/ui/app.js.map +1 -0
- package/dist/ui/brand.js +8 -0
- package/dist/ui/brand.js.map +1 -0
- package/dist/ui/connecting.js +7 -0
- package/dist/ui/connecting.js.map +1 -0
- package/dist/ui/menu.js +33 -0
- package/dist/ui/menu.js.map +1 -0
- package/dist/ui/press-enter.js +13 -0
- package/dist/ui/press-enter.js.map +1 -0
- package/dist/ui/unknown.js +12 -0
- package/dist/ui/unknown.js.map +1 -0
- package/package.json +15 -10
- package/bin/layerbase.mjs +0 -15
- package/src/commands/alias.tsx +0 -28
- package/src/commands/connect.tsx +0 -66
- package/src/commands/connection-string.tsx +0 -49
- package/src/commands/interactive.tsx +0 -168
- package/src/commands/login.tsx +0 -103
- package/src/commands/logout.tsx +0 -27
- package/src/commands/ls.tsx +0 -99
- package/src/commands/whoami.tsx +0 -64
- package/src/lib/alias.ts +0 -84
- package/src/lib/browser-login.ts +0 -115
- package/src/lib/cloud-api.ts +0 -96
- package/src/lib/config.ts +0 -45
- package/src/lib/engines.ts +0 -124
- package/src/lib/format.ts +0 -47
- package/src/lib/launch.ts +0 -47
- package/src/lib/open-browser.ts +0 -20
- package/src/lib/run-spindb.ts +0 -51
- package/src/lib/token.ts +0 -19
- package/src/ui/app.tsx +0 -38
- package/src/ui/brand.tsx +0 -15
- package/src/ui/connecting.tsx +0 -13
- package/src/ui/menu.tsx +0 -56
- package/src/ui/press-enter.tsx +0 -11
- package/src/ui/unknown.tsx +0 -17
- package/tsconfig.json +0 -17
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"menu.js","sourceRoot":"","sources":["../../src/ui/menu.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAChC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAInC,0EAA0E;AAC1E,+CAA+C;AAC/C,MAAM,UAAU,IAAI,CAAC,EACnB,KAAK,EACL,QAAQ,GAIT;IACC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IAErC,QAAQ,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACtB,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChB,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QACvD,CAAC;aAAM,IAAI,GAAG,CAAC,SAAS,EAAE,CAAC;YACzB,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QACvD,CAAC;aAAM,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;YACtB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAA;YACzB,IAAI,IAAI;gBAAE,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;aAAM,IAAI,KAAK,KAAK,GAAG,EAAE,CAAC;YACzB,QAAQ,CAAC,MAAM,CAAC,CAAA;QAClB,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,+CAA+C;IAC/C,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAA;IAEpE,OAAO,CACL,MAAC,GAAG,IAAC,aAAa,EAAC,QAAQ,aACxB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE;gBACrB,MAAM,MAAM,GAAG,CAAC,KAAK,KAAK,CAAA;gBAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAA;gBACpE,OAAO,CACL,MAAC,GAAG,eACF,MAAC,IAAI,IAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,MAAM,aACnD,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EACpB,KAAK,IACD,EACN,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,KAAC,IAAI,IAAC,QAAQ,kBAAE,IAAI,CAAC,IAAI,GAAQ,CAAC,CAAC,CAAC,IAAI,KAL7C,IAAI,CAAC,KAAK,CAMd,CACP,CAAA;YACH,CAAC,CAAC,EACF,KAAC,GAAG,IAAC,SAAS,EAAE,CAAC,YACf,MAAC,IAAI,IAAC,QAAQ,uCACK,GAAG,uBAAmB,GAAG,kBACrC,GACH,IACF,CACP,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Text, useApp, useInput } from 'ink';
|
|
3
|
+
// A tiny gate between a hub action and returning to the menu, so the action's
|
|
4
|
+
// output stays readable until the user is ready to move on.
|
|
5
|
+
export function PressEnter() {
|
|
6
|
+
const { exit } = useApp();
|
|
7
|
+
useInput((_input, key) => {
|
|
8
|
+
if (key.return)
|
|
9
|
+
exit();
|
|
10
|
+
});
|
|
11
|
+
return _jsx(Text, { dimColor: true, children: "Press Enter to return to the menu" });
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=press-enter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"press-enter.js","sourceRoot":"","sources":["../../src/ui/press-enter.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAA;AAE5C,8EAA8E;AAC9E,4DAA4D;AAC5D,MAAM,UAAU,UAAU;IACxB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAA;IACzB,QAAQ,CAAC,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE;QACvB,IAAI,GAAG,CAAC,MAAM;YAAE,IAAI,EAAE,CAAA;IACxB,CAAC,CAAC,CAAA;IACF,OAAO,KAAC,IAAI,IAAC,QAAQ,wDAAyC,CAAA;AAChE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { Text, useApp } from 'ink';
|
|
4
|
+
export function Unknown({ command }) {
|
|
5
|
+
const { exit } = useApp();
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
process.exitCode = 1;
|
|
8
|
+
exit();
|
|
9
|
+
}, [exit]);
|
|
10
|
+
return (_jsxs(Text, { color: "red", children: ["Unknown command: ", command, ". Run `layerbase help` to see the commands."] }));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=unknown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unknown.js","sourceRoot":"","sources":["../../src/ui/unknown.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,KAAK,CAAA;AAElC,MAAM,UAAU,OAAO,CAAC,EAAE,OAAO,EAAuB;IACtD,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,EAAE,CAAA;IAEzB,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAA;QACpB,IAAI,EAAE,CAAA;IACR,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;IAEV,OAAO,CACL,MAAC,IAAI,IAAC,KAAK,EAAC,KAAK,kCACG,OAAO,mDACpB,CACR,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,34 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "layerbase",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Layerbase cloud CLI: connect to your managed databases without leaking the connection string into shell history.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
|
-
"layerbase": "
|
|
8
|
-
"lbase": "
|
|
7
|
+
"layerbase": "dist/cli.js",
|
|
8
|
+
"lbase": "dist/cli.js"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
|
-
"
|
|
12
|
-
"src",
|
|
13
|
-
"tsconfig.json"
|
|
11
|
+
"dist"
|
|
14
12
|
],
|
|
15
13
|
"engines": {
|
|
16
14
|
"node": ">=20"
|
|
17
15
|
},
|
|
18
16
|
"scripts": {
|
|
19
17
|
"dev": "tsx src/cli.tsx",
|
|
20
|
-
"start": "node
|
|
18
|
+
"start": "node dist/cli.js",
|
|
19
|
+
"build": "pnpm clean && pnpm build:tsc && pnpm build:alias && pnpm build:shebang",
|
|
20
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
21
|
+
"build:tsc": "tsc -p tsconfig.build.json",
|
|
22
|
+
"build:alias": "tsc-alias -p tsconfig.build.json --resolve-full-paths",
|
|
23
|
+
"build:shebang": "node scripts/shebang.mjs",
|
|
21
24
|
"format": "prettier --write .",
|
|
22
25
|
"lint": "eslint .",
|
|
23
26
|
"check:types": "tsc --noEmit",
|
|
24
|
-
"check": "tsc --noEmit && eslint ."
|
|
27
|
+
"check": "tsc --noEmit && eslint .",
|
|
28
|
+
"prepack": "pnpm build"
|
|
25
29
|
},
|
|
26
30
|
"dependencies": {
|
|
27
31
|
"ink": "^7.1.0",
|
|
28
32
|
"ink-spinner": "^5.0.0",
|
|
29
33
|
"meow": "^14.1.0",
|
|
30
|
-
"react": "^19.2.0"
|
|
31
|
-
"tsx": "^4.22.4"
|
|
34
|
+
"react": "^19.2.0"
|
|
32
35
|
},
|
|
33
36
|
"devDependencies": {
|
|
34
37
|
"@eslint/js": "^9.18.0",
|
|
@@ -36,6 +39,8 @@
|
|
|
36
39
|
"@types/react": "^19.2.0",
|
|
37
40
|
"eslint": "^9.18.0",
|
|
38
41
|
"prettier": "^3.4.2",
|
|
42
|
+
"tsc-alias": "^1.8.10",
|
|
43
|
+
"tsx": "^4.22.4",
|
|
39
44
|
"typescript": "^5.7.2",
|
|
40
45
|
"typescript-eslint": "^8.18.0"
|
|
41
46
|
},
|
package/bin/layerbase.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// No build step: tsx transpiles the TypeScript/JSX entry at runtime.
|
|
3
|
-
// Point tsx at our shipped tsconfig so it uses the automatic JSX runtime even
|
|
4
|
-
// when installed under node_modules (tsx ignores a tsconfig it finds inside
|
|
5
|
-
// node_modules during its normal walk-up, which otherwise defaults JSX to the
|
|
6
|
-
// classic runtime and crashes with "React is not defined").
|
|
7
|
-
import { fileURLToPath, URL } from 'node:url'
|
|
8
|
-
import process from 'node:process'
|
|
9
|
-
import { tsImport } from 'tsx/esm/api'
|
|
10
|
-
|
|
11
|
-
process.env.TSX_TSCONFIG_PATH = fileURLToPath(
|
|
12
|
-
new URL('../tsconfig.json', import.meta.url),
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
await tsImport('../src/cli.tsx', import.meta.url)
|
package/src/commands/alias.tsx
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Text, useApp } from 'ink'
|
|
3
|
-
import { setupLb } from '../lib/alias'
|
|
4
|
-
|
|
5
|
-
type Result = { ok: boolean; text: string }
|
|
6
|
-
|
|
7
|
-
// Sets up the short `lb` command, but only when it is free on this system.
|
|
8
|
-
export function Alias() {
|
|
9
|
-
const { exit } = useApp()
|
|
10
|
-
const [result, setResult] = useState<Result | null>(null)
|
|
11
|
-
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
const outcome = setupLb()
|
|
14
|
-
if (outcome.ok) {
|
|
15
|
-
setResult({ ok: true, text: 'The `lb` shortcut is ready. Try `lb`.' })
|
|
16
|
-
} else {
|
|
17
|
-
process.exitCode = 1
|
|
18
|
-
setResult({ ok: false, text: outcome.reason })
|
|
19
|
-
}
|
|
20
|
-
}, [])
|
|
21
|
-
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
if (result) exit()
|
|
24
|
-
}, [result, exit])
|
|
25
|
-
|
|
26
|
-
if (!result) return null
|
|
27
|
-
return <Text color={result.ok ? 'green' : 'yellow'}>{result.text}</Text>
|
|
28
|
-
}
|
package/src/commands/connect.tsx
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { render } from 'ink'
|
|
2
|
-
import { Connecting } from '../ui/connecting'
|
|
3
|
-
import { getConnectionInfo } from '../lib/cloud-api'
|
|
4
|
-
import { buildLaunchPlan } from '../lib/engines'
|
|
5
|
-
import { runClient } from '../lib/launch'
|
|
6
|
-
import { printConnectionInfo } from '../lib/format'
|
|
7
|
-
import type { CommandFlags } from '../ui/app'
|
|
8
|
-
|
|
9
|
-
type ExecOptions = {
|
|
10
|
-
command: string
|
|
11
|
-
args: string[]
|
|
12
|
-
flags: CommandFlags
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Resolve a database and hand the TTY to its native client. Returns the client's
|
|
16
|
-
// exit code (or 1 on a resolve/launch error) WITHOUT exiting the process, so it
|
|
17
|
-
// is reusable from both the CLI path and the interactive menu. Ink renders only
|
|
18
|
-
// a transient spinner during the cloud lookup, then unmounts before the client
|
|
19
|
-
// takes over the terminal.
|
|
20
|
-
export async function connectToDatabase(options: {
|
|
21
|
-
dbRef: string
|
|
22
|
-
command: string
|
|
23
|
-
print?: boolean
|
|
24
|
-
}): Promise<number> {
|
|
25
|
-
const { dbRef, command, print } = options
|
|
26
|
-
|
|
27
|
-
const spinner = render(<Connecting label={`Resolving ${dbRef}...`} />)
|
|
28
|
-
let info
|
|
29
|
-
try {
|
|
30
|
-
info = await getConnectionInfo(dbRef)
|
|
31
|
-
} catch (error) {
|
|
32
|
-
spinner.unmount()
|
|
33
|
-
process.stderr.write(`${(error as Error).message}\n`)
|
|
34
|
-
return 1
|
|
35
|
-
}
|
|
36
|
-
spinner.unmount()
|
|
37
|
-
|
|
38
|
-
if (print) {
|
|
39
|
-
printConnectionInfo(info)
|
|
40
|
-
return 0
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
let plan
|
|
44
|
-
try {
|
|
45
|
-
plan = buildLaunchPlan({ info, command })
|
|
46
|
-
} catch (error) {
|
|
47
|
-
process.stderr.write(`${(error as Error).message}\n`)
|
|
48
|
-
return 1
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return runClient(plan)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// CLI entrypoint for connect/psql/redis-cli/mysql: resolve + launch, then exit.
|
|
55
|
-
export async function runExec(options: ExecOptions): Promise<void> {
|
|
56
|
-
const { command, flags } = options
|
|
57
|
-
const dbRef = options.args[0]
|
|
58
|
-
|
|
59
|
-
if (!dbRef) {
|
|
60
|
-
process.stderr.write(`Usage: layerbase ${command} <db>\n`)
|
|
61
|
-
process.exit(1)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const code = await connectToDatabase({ dbRef, command, print: flags.print })
|
|
65
|
-
process.exit(code)
|
|
66
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Text, useApp } from 'ink'
|
|
3
|
-
import { getConnectionInfo } from '../lib/cloud-api'
|
|
4
|
-
import { buildConnectionString } from '../lib/format'
|
|
5
|
-
|
|
6
|
-
type State =
|
|
7
|
-
| { kind: 'loading' }
|
|
8
|
-
| { kind: 'error'; message: string }
|
|
9
|
-
| { kind: 'done' }
|
|
10
|
-
|
|
11
|
-
// Opt-in escape hatch: prints the full connection string (with password) to
|
|
12
|
-
// stdout for piping into an app. The secure path is `layerbase connect`.
|
|
13
|
-
export function ConnectionString({ dbRef }: { dbRef: string | undefined }) {
|
|
14
|
-
const { exit } = useApp()
|
|
15
|
-
const [state, setState] = useState<State>({ kind: 'loading' })
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
async function fetchAndPrintConnectionString() {
|
|
19
|
-
if (!dbRef) {
|
|
20
|
-
setState({
|
|
21
|
-
kind: 'error',
|
|
22
|
-
message: 'Usage: layerbase connection-string <db>',
|
|
23
|
-
})
|
|
24
|
-
process.exitCode = 1
|
|
25
|
-
return
|
|
26
|
-
}
|
|
27
|
-
try {
|
|
28
|
-
const info = await getConnectionInfo(dbRef)
|
|
29
|
-
process.stdout.write(`${buildConnectionString(info)}\n`)
|
|
30
|
-
setState({ kind: 'done' })
|
|
31
|
-
} catch (error) {
|
|
32
|
-
setState({ kind: 'error', message: (error as Error).message })
|
|
33
|
-
process.exitCode = 1
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
fetchAndPrintConnectionString()
|
|
37
|
-
}, [dbRef])
|
|
38
|
-
|
|
39
|
-
useEffect(() => {
|
|
40
|
-
if (state.kind !== 'loading') {
|
|
41
|
-
exit()
|
|
42
|
-
}
|
|
43
|
-
}, [state, exit])
|
|
44
|
-
|
|
45
|
-
if (state.kind === 'error') {
|
|
46
|
-
return <Text color="red">{state.message}</Text>
|
|
47
|
-
}
|
|
48
|
-
return null
|
|
49
|
-
}
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { render, Box, Text } from 'ink'
|
|
2
|
-
import { Menu } from '../ui/menu'
|
|
3
|
-
import type { MenuItem } from '../ui/menu'
|
|
4
|
-
import { Header } from '../ui/brand'
|
|
5
|
-
import { Connecting } from '../ui/connecting'
|
|
6
|
-
import { PressEnter } from '../ui/press-enter'
|
|
7
|
-
import { loadCredentials } from '../lib/config'
|
|
8
|
-
import type { StoredCredentials } from '../lib/config'
|
|
9
|
-
import { decodeTokenClaims } from '../lib/token'
|
|
10
|
-
import { listDatabases } from '../lib/cloud-api'
|
|
11
|
-
import type { CloudDatabase } from '../lib/cloud-api'
|
|
12
|
-
import { runSpindb } from '../lib/run-spindb'
|
|
13
|
-
import { connectToDatabase } from './connect'
|
|
14
|
-
import { lbStatus } from '../lib/alias'
|
|
15
|
-
import { App } from '../ui/app'
|
|
16
|
-
import type { CommandFlags } from '../ui/app'
|
|
17
|
-
|
|
18
|
-
const BACK = '__back__'
|
|
19
|
-
|
|
20
|
-
function clearScreen(): void {
|
|
21
|
-
// Clear the visible screen and home the cursor (scrollback is preserved).
|
|
22
|
-
process.stdout.write('[2J[H')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function accountLine(creds: StoredCredentials | null): {
|
|
26
|
-
text: string
|
|
27
|
-
loggedIn: boolean
|
|
28
|
-
} {
|
|
29
|
-
if (!creds) return { text: 'Not signed in', loggedIn: false }
|
|
30
|
-
const email = decodeTokenClaims(creds.token)?.email
|
|
31
|
-
return { text: email ? `Signed in as ${email}` : 'Signed in', loggedIn: true }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function buildItems(loggedIn: boolean, lbAvailable: boolean): MenuItem[] {
|
|
35
|
-
const items: MenuItem[] = []
|
|
36
|
-
if (loggedIn) {
|
|
37
|
-
items.push({ label: 'List cloud databases', value: 'ls' })
|
|
38
|
-
items.push({ label: 'Connect to a database', value: 'connect' })
|
|
39
|
-
} else {
|
|
40
|
-
items.push({ label: 'Log in', value: 'login', hint: 'browser sign-in' })
|
|
41
|
-
}
|
|
42
|
-
items.push({ label: 'Run spindb', value: 'spindb', hint: 'local databases' })
|
|
43
|
-
if (loggedIn) items.push({ label: 'Log out', value: 'logout' })
|
|
44
|
-
if (lbAvailable) {
|
|
45
|
-
items.push({ label: 'Enable the lb shortcut', value: 'alias' })
|
|
46
|
-
}
|
|
47
|
-
items.push({ label: 'Quit', value: 'quit' })
|
|
48
|
-
return items
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function pick(creds: StoredCredentials | null): Promise<string> {
|
|
52
|
-
const loggedIn = Boolean(creds)
|
|
53
|
-
const lbAvailable = lbStatus().state === 'available'
|
|
54
|
-
const items = buildItems(loggedIn, lbAvailable)
|
|
55
|
-
const account = accountLine(creds)
|
|
56
|
-
|
|
57
|
-
return new Promise<string>((resolve) => {
|
|
58
|
-
const app = render(
|
|
59
|
-
<Box flexDirection="column" paddingX={1} paddingY={1}>
|
|
60
|
-
<Header subtitle="cloud + local databases" />
|
|
61
|
-
<Box marginBottom={1}>
|
|
62
|
-
<Text
|
|
63
|
-
color={account.loggedIn ? 'green' : undefined}
|
|
64
|
-
dimColor={!account.loggedIn}
|
|
65
|
-
>
|
|
66
|
-
{account.text}
|
|
67
|
-
</Text>
|
|
68
|
-
</Box>
|
|
69
|
-
<Menu
|
|
70
|
-
items={items}
|
|
71
|
-
onSelect={(value) => {
|
|
72
|
-
app.unmount()
|
|
73
|
-
resolve(value)
|
|
74
|
-
}}
|
|
75
|
-
/>
|
|
76
|
-
</Box>,
|
|
77
|
-
)
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function pickDatabase(databases: CloudDatabase[]): Promise<string | null> {
|
|
82
|
-
const items: MenuItem[] = databases.map((db) => ({
|
|
83
|
-
label: db.name,
|
|
84
|
-
value: db.id,
|
|
85
|
-
hint: `${db.engine} · ${db.status}`,
|
|
86
|
-
}))
|
|
87
|
-
items.push({ label: 'Back', value: BACK })
|
|
88
|
-
|
|
89
|
-
return new Promise<string | null>((resolve) => {
|
|
90
|
-
const app = render(
|
|
91
|
-
<Box flexDirection="column" paddingX={1} paddingY={1}>
|
|
92
|
-
<Text bold>Pick a database to connect to</Text>
|
|
93
|
-
<Box marginTop={1}>
|
|
94
|
-
<Menu
|
|
95
|
-
items={items}
|
|
96
|
-
onSelect={(value) => {
|
|
97
|
-
app.unmount()
|
|
98
|
-
resolve(value === BACK ? null : value)
|
|
99
|
-
}}
|
|
100
|
-
/>
|
|
101
|
-
</Box>
|
|
102
|
-
</Box>,
|
|
103
|
-
)
|
|
104
|
-
})
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Menu action: list the user's databases, let them pick one, and connect with
|
|
108
|
-
// the right client. Returns to the hub when the client exits (or on Back/error).
|
|
109
|
-
async function runConnectFlow(): Promise<void> {
|
|
110
|
-
const spinner = render(<Connecting label="Loading your databases..." />)
|
|
111
|
-
let databases: CloudDatabase[]
|
|
112
|
-
try {
|
|
113
|
-
databases = await listDatabases()
|
|
114
|
-
} catch (error) {
|
|
115
|
-
spinner.unmount()
|
|
116
|
-
process.stderr.write(`${(error as Error).message}\n`)
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
spinner.unmount()
|
|
120
|
-
|
|
121
|
-
if (databases.length === 0) {
|
|
122
|
-
process.stdout.write(
|
|
123
|
-
'No databases yet. Create one at https://layerbase.com.\n',
|
|
124
|
-
)
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const dbRef = await pickDatabase(databases)
|
|
129
|
-
if (!dbRef) return
|
|
130
|
-
|
|
131
|
-
await connectToDatabase({ dbRef, command: 'connect' })
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function waitForEnter(): Promise<void> {
|
|
135
|
-
const instance = render(<PressEnter />)
|
|
136
|
-
await instance.waitUntilExit()
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// The no-command experience: a small hub. Each action runs on a clean screen,
|
|
140
|
-
// then waits for Enter and returns to the menu, so you can sign in, then list,
|
|
141
|
-
// then connect, ... Cloud actions depend on auth; spindb is always offered.
|
|
142
|
-
export async function runInteractive(flags: CommandFlags): Promise<void> {
|
|
143
|
-
for (;;) {
|
|
144
|
-
clearScreen()
|
|
145
|
-
const creds = await loadCredentials()
|
|
146
|
-
const choice = await pick(creds)
|
|
147
|
-
|
|
148
|
-
if (choice === 'quit') {
|
|
149
|
-
clearScreen()
|
|
150
|
-
process.exitCode = 0
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
clearScreen()
|
|
155
|
-
if (choice === 'spindb') {
|
|
156
|
-
await runSpindb([])
|
|
157
|
-
} else if (choice === 'connect') {
|
|
158
|
-
await runConnectFlow()
|
|
159
|
-
} else {
|
|
160
|
-
// login / logout / ls / alias run through the same Ink dispatcher as
|
|
161
|
-
// typed commands; wait for the action to finish.
|
|
162
|
-
const instance = render(<App command={choice} args={[]} flags={flags} />)
|
|
163
|
-
await instance.waitUntilExit()
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
await waitForEnter()
|
|
167
|
-
}
|
|
168
|
-
}
|
package/src/commands/login.tsx
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Box, Text, useApp } from 'ink'
|
|
3
|
-
import Spinner from 'ink-spinner'
|
|
4
|
-
import { runBrowserLogin } from '../lib/browser-login'
|
|
5
|
-
import { DEFAULT_API_URL, whoami } from '../lib/cloud-api'
|
|
6
|
-
import { saveCredentials, credentialsPath } from '../lib/config'
|
|
7
|
-
import type { CommandFlags } from '../ui/app'
|
|
8
|
-
|
|
9
|
-
type Phase =
|
|
10
|
-
| { kind: 'starting' }
|
|
11
|
-
| { kind: 'waiting'; authUrl: string }
|
|
12
|
-
| { kind: 'finishing' }
|
|
13
|
-
| { kind: 'done'; email: string }
|
|
14
|
-
| { kind: 'error'; message: string }
|
|
15
|
-
|
|
16
|
-
export function Login({ flags }: { flags: CommandFlags }) {
|
|
17
|
-
const { exit } = useApp()
|
|
18
|
-
const apiUrl = flags.apiUrl ?? DEFAULT_API_URL
|
|
19
|
-
const [phase, setPhase] = useState<Phase>({ kind: 'starting' })
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
async function runLogin() {
|
|
23
|
-
try {
|
|
24
|
-
const { token } = await runBrowserLogin({
|
|
25
|
-
apiUrl,
|
|
26
|
-
onPhase: setPhase,
|
|
27
|
-
})
|
|
28
|
-
setPhase({ kind: 'finishing' })
|
|
29
|
-
// Save the token first so whoami() can read it, then enrich with the
|
|
30
|
-
// cloud API key (best-effort, mirrors how desktop backfills it).
|
|
31
|
-
await saveCredentials({ apiUrl, token })
|
|
32
|
-
let email = ''
|
|
33
|
-
try {
|
|
34
|
-
const me = await whoami()
|
|
35
|
-
email = me.user.email
|
|
36
|
-
await saveCredentials({ apiUrl, token, cloudApiKey: me.cloudApiKey })
|
|
37
|
-
} catch {
|
|
38
|
-
// Token is already saved; enrichment is non-fatal.
|
|
39
|
-
}
|
|
40
|
-
setPhase({ kind: 'done', email })
|
|
41
|
-
} catch (error) {
|
|
42
|
-
process.exitCode = 1
|
|
43
|
-
setPhase({ kind: 'error', message: (error as Error).message })
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
runLogin()
|
|
47
|
-
}, [apiUrl])
|
|
48
|
-
|
|
49
|
-
// Exit only after the terminal frame has committed.
|
|
50
|
-
useEffect(() => {
|
|
51
|
-
if (phase.kind === 'done' || phase.kind === 'error') {
|
|
52
|
-
exit()
|
|
53
|
-
}
|
|
54
|
-
}, [phase, exit])
|
|
55
|
-
|
|
56
|
-
if (phase.kind === 'starting') {
|
|
57
|
-
return (
|
|
58
|
-
<Box>
|
|
59
|
-
<Text color="cyan">
|
|
60
|
-
<Spinner type="dots" />
|
|
61
|
-
</Text>
|
|
62
|
-
<Text> Starting sign-in...</Text>
|
|
63
|
-
</Box>
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (phase.kind === 'waiting') {
|
|
68
|
-
return (
|
|
69
|
-
<Box flexDirection="column">
|
|
70
|
-
<Box>
|
|
71
|
-
<Text color="cyan">
|
|
72
|
-
<Spinner type="dots" />
|
|
73
|
-
</Text>
|
|
74
|
-
<Text> Waiting for you to finish signing in in your browser...</Text>
|
|
75
|
-
</Box>
|
|
76
|
-
<Text dimColor>If your browser did not open, visit:</Text>
|
|
77
|
-
<Text color="blue">{phase.authUrl}</Text>
|
|
78
|
-
</Box>
|
|
79
|
-
)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (phase.kind === 'finishing') {
|
|
83
|
-
return (
|
|
84
|
-
<Box>
|
|
85
|
-
<Text color="cyan">
|
|
86
|
-
<Spinner type="dots" />
|
|
87
|
-
</Text>
|
|
88
|
-
<Text> Saving credentials...</Text>
|
|
89
|
-
</Box>
|
|
90
|
-
)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (phase.kind === 'done') {
|
|
94
|
-
return (
|
|
95
|
-
<Text color="green">
|
|
96
|
-
Logged in{phase.email ? ` as ${phase.email}` : ''}. Saved to{' '}
|
|
97
|
-
{credentialsPath()}.
|
|
98
|
-
</Text>
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return <Text color="red">{phase.message}</Text>
|
|
103
|
-
}
|
package/src/commands/logout.tsx
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Text, useApp } from 'ink'
|
|
3
|
-
import { clearCredentials } from '../lib/config'
|
|
4
|
-
|
|
5
|
-
export function Logout() {
|
|
6
|
-
const { exit } = useApp()
|
|
7
|
-
const [done, setDone] = useState(false)
|
|
8
|
-
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
async function fetchAndClearCredentials() {
|
|
11
|
-
await clearCredentials()
|
|
12
|
-
setDone(true)
|
|
13
|
-
}
|
|
14
|
-
fetchAndClearCredentials()
|
|
15
|
-
}, [])
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
if (done) {
|
|
19
|
-
exit()
|
|
20
|
-
}
|
|
21
|
-
}, [done, exit])
|
|
22
|
-
|
|
23
|
-
if (!done) {
|
|
24
|
-
return null
|
|
25
|
-
}
|
|
26
|
-
return <Text color="green">Logged out. Stored API key removed.</Text>
|
|
27
|
-
}
|
package/src/commands/ls.tsx
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react'
|
|
2
|
-
import { Box, Text, useApp } from 'ink'
|
|
3
|
-
import Spinner from 'ink-spinner'
|
|
4
|
-
import { listDatabases } from '../lib/cloud-api'
|
|
5
|
-
import type { CloudDatabase } from '../lib/cloud-api'
|
|
6
|
-
|
|
7
|
-
type State =
|
|
8
|
-
| { kind: 'loading' }
|
|
9
|
-
| { kind: 'ready'; databases: CloudDatabase[] }
|
|
10
|
-
| { kind: 'error'; message: string }
|
|
11
|
-
|
|
12
|
-
const STATUS_COLOR: Record<string, string> = {
|
|
13
|
-
running: 'green',
|
|
14
|
-
hibernated: 'yellow',
|
|
15
|
-
stopped: 'gray',
|
|
16
|
-
archived: 'gray',
|
|
17
|
-
provisioning: 'cyan',
|
|
18
|
-
error: 'red',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function pad(value: string, width: number): string {
|
|
22
|
-
return value.length >= width
|
|
23
|
-
? value
|
|
24
|
-
: value + ' '.repeat(width - value.length)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function List({ json }: { json: boolean }) {
|
|
28
|
-
const { exit } = useApp()
|
|
29
|
-
const [state, setState] = useState<State>({ kind: 'loading' })
|
|
30
|
-
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
async function fetchAndSetDatabases() {
|
|
33
|
-
try {
|
|
34
|
-
const databases = await listDatabases()
|
|
35
|
-
if (json) {
|
|
36
|
-
process.stdout.write(`${JSON.stringify(databases, null, 2)}\n`)
|
|
37
|
-
}
|
|
38
|
-
setState({ kind: 'ready', databases })
|
|
39
|
-
} catch (error) {
|
|
40
|
-
setState({ kind: 'error', message: (error as Error).message })
|
|
41
|
-
process.exitCode = 1
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
fetchAndSetDatabases()
|
|
45
|
-
}, [json])
|
|
46
|
-
|
|
47
|
-
// Exit only after the terminal frame has committed, so the table or error is
|
|
48
|
-
// actually rendered before Ink unmounts.
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
if (state.kind !== 'loading') {
|
|
51
|
-
exit()
|
|
52
|
-
}
|
|
53
|
-
}, [state, exit])
|
|
54
|
-
|
|
55
|
-
if (state.kind === 'loading') {
|
|
56
|
-
return (
|
|
57
|
-
<Box>
|
|
58
|
-
<Text color="cyan">
|
|
59
|
-
<Spinner type="dots" />
|
|
60
|
-
</Text>
|
|
61
|
-
<Text> Loading your databases...</Text>
|
|
62
|
-
</Box>
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (state.kind === 'error') {
|
|
67
|
-
return <Text color="red">{state.message}</Text>
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// JSON already written to stdout; render nothing further.
|
|
71
|
-
if (json) {
|
|
72
|
-
return null
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (state.databases.length === 0) {
|
|
76
|
-
return <Text>No databases yet. Create one at https://layerbase.com.</Text>
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
<Box flexDirection="column">
|
|
81
|
-
<Text bold>
|
|
82
|
-
{pad('NAME', 24)}
|
|
83
|
-
{pad('ENGINE', 14)}
|
|
84
|
-
{pad('STATUS', 14)}
|
|
85
|
-
ID
|
|
86
|
-
</Text>
|
|
87
|
-
{state.databases.map((db) => (
|
|
88
|
-
<Text key={db.id}>
|
|
89
|
-
{pad(db.name, 24)}
|
|
90
|
-
{pad(db.engine, 14)}
|
|
91
|
-
<Text color={STATUS_COLOR[db.status] ?? 'white'}>
|
|
92
|
-
{pad(db.status, 14)}
|
|
93
|
-
</Text>
|
|
94
|
-
{db.id}
|
|
95
|
-
</Text>
|
|
96
|
-
))}
|
|
97
|
-
</Box>
|
|
98
|
-
)
|
|
99
|
-
}
|