@hakubase/cli 0.4.0 → 0.4.2

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
@@ -12,7 +12,7 @@ npm i -D @hakubase/cli
12
12
  npx hakubase link https://noesis.hakubase.com
13
13
  ```
14
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.
15
+ The command prints a code and opens the browser on the workspace. There, signed in, write the code the terminal shows, 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
16
 
17
17
  With a key made by hand in the site's settings (tab *Settings*, *Keys*), no browser:
18
18
 
@@ -20,7 +20,7 @@ With a key made by hand in the site's settings (tab *Settings*, *Keys*), no brow
20
20
  npx hakubase link https://noesis.hakubase.com <site-id> --key hb_…
21
21
  ```
22
22
 
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.
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` — and writing a `.gitignore` with that line when the project has none. The key is never printed whole.
24
24
 
25
25
  The key is read from `--key`, then the `HAKUBASE_KEY` variable, then `.env.local`.
26
26
 
package/astro.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { HakubaseViteOptions } from './vite.js'
2
+ /** The Vite plugin of @hakubase/cli, as an Astro integration. */
3
+ export default function hakubase(options?: HakubaseViteOptions): { name: string; hooks: Record<string, (...args: any[]) => void> }
4
+ export { hakubase }
package/lib/commands.js CHANGED
@@ -129,13 +129,31 @@ export function target(ctx, flags) {
129
129
  return { root, api, site, key, siteUrl: project.site_url || '' }
130
130
  }
131
131
 
132
+ /** What opens a page on this system, or null when the address is not a
133
+ * web page. The address comes from the workspace the person named, and
134
+ * the system's opener runs whatever it is handed: a `file:` address is
135
+ * a program to start, and on Windows `cmd /c start` read `|` and `&`
136
+ * in it as commands. Only http and https, rewritten by the URL parser,
137
+ * and never through a shell. */
138
+ export function browserCommand(url, platform = process.platform) {
139
+ let page
140
+ try {
141
+ page = new URL(url)
142
+ } catch {
143
+ return null
144
+ }
145
+ if (page.protocol !== 'https:' && page.protocol !== 'http:') return null
146
+ if (platform === 'darwin') return ['open', [page.href]]
147
+ if (platform === 'win32') return ['rundll32', ['url.dll,FileProtocolHandler', page.href]]
148
+ return ['xdg-open', [page.href]]
149
+ }
150
+
132
151
  /** Opens a page in the person's browser; a failure only means they open it by hand. */
133
152
  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]]
153
+ const opener = browserCommand(url)
154
+ if (!opener) return
137
155
  try {
138
- spawn(command, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref()
156
+ spawn(opener[0], opener[1], { stdio: 'ignore', detached: true }).on('error', () => {}).unref()
139
157
  } catch {
140
158
  // The address is printed: the person opens it.
141
159
  }
@@ -147,7 +165,7 @@ export async function keyFromBrowser(ctx, api, siteId, flags) {
147
165
  json: { computer: ctx.computer || os.hostname(), site_id: siteId || '' },
148
166
  })
149
167
  ctx.out(`Open ${asked.url}`)
150
- ctx.out(`and check that the code is ${asked.code}. Waiting for the approval…`)
168
+ ctx.out(`and write this code there: ${asked.code}. Waiting for the approval…`)
151
169
  if (!flags['no-browser']) (ctx.openBrowser || openInBrowser)(asked.url)
152
170
  const sleep = ctx.sleep || ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
153
171
  const tries = Math.ceil((asked.expires_in || 600) / (asked.interval || 2))
@@ -178,8 +196,9 @@ export async function link(ctx, positional, flags) {
178
196
  const info = await request(ctx, 'GET', `${api}/api/sites/${encodeURIComponent(siteId)}/code-site`, { key })
179
197
  writeProject(root, { api, site_id: info.id || siteId, site_url: info.url || '' })
180
198
  if (given) writeEnvKey(root, given)
181
- ensureGitignored(root)
182
- const where = given ? ` Key ${maskKey(given)} in .env.local.` : ''
199
+ // A key just written is kept out of git even where no .gitignore was.
200
+ const ignored = ensureGitignored(root, { create: Boolean(given) })
201
+ const where = given ? ` Key ${maskKey(given)} in .env.local${ignored ? ', now in .gitignore' : ''}.` : ''
183
202
  ctx.out(`Linked to ${info.name || siteId} (${info.url || api}).${where}`)
184
203
  return 0
185
204
  }
package/lib/project.js CHANGED
@@ -88,14 +88,19 @@ export function writeEnvKey(root, key) {
88
88
  fs.writeFileSync(file, out.join('\n') + '\n', { mode: 0o600 })
89
89
  }
90
90
 
91
- /** Where a .gitignore exists, makes sure it keeps `.env.local` out. Returns true when it added the line. */
92
- export function ensureGitignored(root) {
91
+ /** Makes sure .gitignore keeps `.env.local` out: where the file exists,
92
+ * and — with `create`, when a key was just written — where it does not:
93
+ * a folder with a key and no .gitignore commits the key at the first
94
+ * `git add .`. Returns true when it added the line. */
95
+ export function ensureGitignored(root, { create = false } = {}) {
93
96
  const file = path.join(root, '.gitignore')
94
97
  let text
95
98
  try {
96
99
  text = fs.readFileSync(file, 'utf8')
97
100
  } catch {
98
- return false
101
+ if (!create) return false
102
+ fs.writeFileSync(file, ENV_FILE + '\n')
103
+ return true
99
104
  }
100
105
  const covered = text.split(/\r?\n/).map((l) => l.trim()).some((l) =>
101
106
  l === ENV_FILE || l === '/' + ENV_FILE || l === '.env*' || l === '*.local' || l === '.env.*')
package/next.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export interface HakubaseNextOptions {
2
+ /** The site's address; else HAKUBASE_SITE_URL, else the linked project. */
3
+ siteUrl?: string
4
+ /** The key; else HAKUBASE_KEY, else .env.local. */
5
+ key?: string
6
+ /** Where to look for the linked project. */
7
+ root?: string
8
+ log?(message: string): void
9
+ }
10
+
11
+ type Config = Record<string, any>
12
+ type ConfigFunction = (phase: string, context: { defaultConfig: Config }) => Config | Promise<Config>
13
+
14
+ /**
15
+ * In `next dev`, Hakubase's paths (/_hb, /_data, /_fn, /_m) reach the real
16
+ * site with the key; the build sees the config untouched.
17
+ */
18
+ export declare function withHakubase<T extends Config>(config: T | ConfigFunction, options?: HakubaseNextOptions):
19
+ (phase: string, context: { defaultConfig: Config }) => Promise<T>
20
+ export default withHakubase
package/package.json CHANGED
@@ -1,16 +1,25 @@
1
1
  {
2
2
  "name": "@hakubase/cli",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Link, type, develop and deploy Hakubase code sites; Vite, Astro and Next.js helpers for development with real data.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "hakubase": "./bin/hakubase.js"
8
8
  },
9
9
  "exports": {
10
- "./vite": "./vite.js",
11
- "./astro": "./astro.js",
12
- "./package.json": "./package.json",
13
- "./next": "./next.js"
10
+ "./vite": {
11
+ "types": "./vite.d.ts",
12
+ "default": "./vite.js"
13
+ },
14
+ "./astro": {
15
+ "types": "./astro.d.ts",
16
+ "default": "./astro.js"
17
+ },
18
+ "./next": {
19
+ "types": "./next.d.ts",
20
+ "default": "./next.js"
21
+ },
22
+ "./package.json": "./package.json"
14
23
  },
15
24
  "files": [
16
25
  "bin",
@@ -18,7 +27,10 @@
18
27
  "vite.js",
19
28
  "astro.js",
20
29
  "next.js",
21
- "README.md"
30
+ "README.md",
31
+ "vite.d.ts",
32
+ "astro.d.ts",
33
+ "next.d.ts"
22
34
  ],
23
35
  "engines": {
24
36
  "node": ">=20"
package/vite.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export interface HakubaseViteOptions {
2
+ root?: string
3
+ key?: string
4
+ log?(message: string): void
5
+ }
6
+ /** In development, Hakubase's paths reach the real site with the key. */
7
+ export default function hakubase(options?: HakubaseViteOptions): { name: string; apply: 'serve'; config(): Record<string, unknown> }
8
+ export { hakubase }