@hakubase/cli 0.4.1 → 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 +2 -2
- package/lib/commands.js +26 -7
- package/lib/project.js +8 -3
- package/package.json +1 -1
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,
|
|
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
|
|
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/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
|
|
135
|
-
|
|
136
|
-
: ['xdg-open', [url]]
|
|
153
|
+
const opener = browserCommand(url)
|
|
154
|
+
if (!opener) return
|
|
137
155
|
try {
|
|
138
|
-
spawn(
|
|
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
|
|
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
|
-
|
|
182
|
-
const
|
|
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
|
-
/**
|
|
92
|
-
|
|
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/package.json
CHANGED