@hakubase/cli 0.2.0 → 0.3.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/README.md CHANGED
@@ -8,13 +8,19 @@ npm i -D @hakubase/cli
8
8
 
9
9
  ## Link
10
10
 
11
- Create a **development key** in the site's settings (tab *Settings*, *Keys*), then:
11
+ ```bash
12
+ npx hakubase link https://noesis.hakubase.com
13
+ ```
14
+
15
+ The command prints a code and opens the browser on the workspace. There, signed in, check that the code is the same, choose the code site and click *Collega*: the command receives a publishing key of that site, named after this computer, and revocable from the site's settings like any other. `--no-browser` only prints the address (on a remote machine, open it from your own). Add the site id to have it chosen already.
16
+
17
+ With a key made by hand in the site's settings (tab *Settings*, *Keys*), no browser:
12
18
 
13
19
  ```bash
14
20
  npx hakubase link https://noesis.hakubase.com <site-id> --key hb_…
15
21
  ```
16
22
 
17
- It writes `.hakubase/project.json` (addresses only, safe to commit) and puts the key in `.env.local` as `HAKUBASE_KEY`, adding `.env.local` to `.gitignore` when the project has one. The key is never printed whole.
23
+ Either way it writes `.hakubase/project.json` (addresses only, safe to commit) and puts the key in `.env.local` as `HAKUBASE_KEY`, adding `.env.local` to `.gitignore` when the project has one. The key is never printed whole.
18
24
 
19
25
  The key is read from `--key`, then the `HAKUBASE_KEY` variable, then `.env.local`.
20
26
 
@@ -91,7 +97,7 @@ jobs:
91
97
 
92
98
  | Command | What it does |
93
99
  |---|---|
94
- | `hakubase link <workspace-url> <site-id> [--key hb_…]` | Links the folder to a code site |
100
+ | `hakubase link <workspace-url> [site-id] [--key hb_…] [--no-browser]` | Links the folder to a code site: approved in the browser, or with a key |
95
101
  | `hakubase types [--out file]` | Writes the TypeScript types of the collections |
96
102
  | `hakubase deploy [dir] [--publish] [--message m] [--commit sha] [--via cli\|github]` | Uploads the built folder as a new version |
97
103
  | `hakubase publish <number>` | Puts a version online |
package/lib/commands.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // The commands of `hakubase`. Each one returns its exit code; output
2
2
  // goes through `io` so the tests can read it.
3
3
 
4
- import { execFileSync } from 'node:child_process'
4
+ import { execFileSync, spawn } from 'node:child_process'
5
5
  import crypto from 'node:crypto'
6
6
  import fs from 'node:fs'
7
+ import os from 'node:os'
7
8
  import path from 'node:path'
8
9
 
9
10
  import {
@@ -23,7 +24,8 @@ export class CliError extends Error {}
23
24
  export const USAGE = `Usage: hakubase <command> [options]
24
25
 
25
26
  Commands:
26
- link <workspace-url> <site-id> [--key hb_…] Link this folder to a code site
27
+ link <workspace-url> [site-id] Link this folder to a code site: the browser
28
+ [--key hb_…] [--no-browser] asks you to approve (with --key, no browser)
27
29
  types [--out hakubase.d.ts] Write the TypeScript types of the collections
28
30
  deploy [dir] [--publish] [--message m] Upload the built folder as a new version
29
31
  [--commit sha] [--via cli|github] (dir: dist, build or out by default)
@@ -50,7 +52,7 @@ export function parseArgs(argv) {
50
52
  } else {
51
53
  const name = arg.slice(2)
52
54
  const next = argv[i + 1]
53
- if (['publish', 'help'].includes(name) || next === undefined || next.startsWith('--')) {
55
+ if (['publish', 'help', 'no-browser'].includes(name) || next === undefined || next.startsWith('--')) {
54
56
  flags[name] = true
55
57
  } else {
56
58
  flags[name] = next
@@ -127,14 +129,52 @@ export function target(ctx, flags) {
127
129
  return { root, api, site, key, siteUrl: project.site_url || '' }
128
130
  }
129
131
 
132
+ /** Opens a page in the person's browser; a failure only means they open it by hand. */
133
+ export function openInBrowser(url) {
134
+ const [command, args] = process.platform === 'darwin' ? ['open', [url]]
135
+ : process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url.replace(/&/g, '^&')]]
136
+ : ['xdg-open', [url]]
137
+ try {
138
+ spawn(command, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref()
139
+ } catch {
140
+ // The address is printed: the person opens it.
141
+ }
142
+ }
143
+
144
+ /** The key, approved by a person in the browser (ADR-0229). */
145
+ export async function keyFromBrowser(ctx, api, siteId, flags) {
146
+ const asked = await request(ctx, 'POST', `${api}/api/public/site-links`, {
147
+ json: { computer: ctx.computer || os.hostname(), site_id: siteId || '' },
148
+ })
149
+ ctx.out(`Open ${asked.url}`)
150
+ ctx.out(`and check that the code is ${asked.code}. Waiting for the approval…`)
151
+ if (!flags['no-browser']) (ctx.openBrowser || openInBrowser)(asked.url)
152
+ const sleep = ctx.sleep || ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
153
+ const tries = Math.ceil((asked.expires_in || 600) / (asked.interval || 2))
154
+ for (let i = 0; i < tries; i++) {
155
+ await sleep((asked.interval || 2) * 1000)
156
+ const got = await request(ctx, 'POST', `${api}/api/public/site-links/collect`, { json: { device: asked.device } })
157
+ if (got.status === 'approved') return got
158
+ if (got.status === 'denied') throw new CliError('The link was refused in the browser.')
159
+ }
160
+ throw new CliError('The request expired: run hakubase link again.')
161
+ }
162
+
130
163
  export async function link(ctx, positional, flags) {
131
- const [workspace, siteId] = positional
132
- if (!workspace || !siteId) throw new CliError('Usage: hakubase link <workspace-url> <site-id> [--key hb_…]')
164
+ const [workspace, siteArg] = positional
165
+ if (!workspace) throw new CliError('Usage: hakubase link <workspace-url> [site-id] [--key hb_…]')
133
166
  const api = workspaceOrigin(workspace)
134
167
  const root = ctx.cwd
135
- const given = typeof flags.key === 'string' ? flags.key : ''
136
- const key = resolveKey(root, given, ctx.env)
137
- if (!key) throw new CliError('No key: pass --key hb_… (a development or publishing key of this site).')
168
+ let given = typeof flags.key === 'string' ? flags.key : ''
169
+ let key = resolveKey(root, given, ctx.env)
170
+ let siteId = siteArg
171
+ if (!key) {
172
+ const got = await keyFromBrowser(ctx, api, siteId, flags)
173
+ key = given = got.token
174
+ siteId = got.site_id
175
+ } else if (!siteId) {
176
+ throw new CliError('Usage: hakubase link <workspace-url> <site-id> --key hb_… (or no key, to approve in the browser)')
177
+ }
138
178
  const info = await request(ctx, 'GET', `${api}/api/sites/${encodeURIComponent(siteId)}/code-site`, { key })
139
179
  writeProject(root, { api, site_id: info.id || siteId, site_url: info.url || '' })
140
180
  if (given) writeEnvKey(root, given)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hakubase/cli",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Link, type, develop and deploy Hakubase code sites; Vite and Astro plugins for development with real data.",
5
5
  "type": "module",
6
6
  "bin": {