@politelabs/cabin-cli 0.3.0 → 0.6.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 +8 -7
- package/dist/commands/context.js +79 -9
- package/dist/commands/login.js +141 -36
- package/dist/commands/theme/check.js +3 -2
- package/dist/commands/theme/context.js +38 -0
- package/dist/commands/theme/dev.js +27 -9
- package/dist/commands/theme/init.js +49 -27
- package/dist/commands/theme/pull.js +24 -2
- package/dist/commands/theme/push.js +3 -3
- package/dist/commands/theme/sync.js +24 -2
- package/dist/index.js +3 -1
- package/dist/io/fs.js +45 -4
- package/dist/io/loopback.js +43 -0
- package/dist/io/world.js +9 -1
- package/dist/lib/api.js +60 -25
- package/dist/lib/args.js +6 -2
- package/dist/lib/context.js +68 -0
- package/dist/lib/credentials.js +30 -3
- package/dist/lib/oauth.js +180 -0
- package/dist/lib/scaffold.js +9 -3
- package/dist/lib/theme-files.js +45 -4
- package/package.json +38 -36
package/README.md
CHANGED
|
@@ -10,15 +10,15 @@ Node 22.12 or newer. Nothing else is installed with it.
|
|
|
10
10
|
|
|
11
11
|
## Signing in
|
|
12
12
|
|
|
13
|
-
A token is made on the site, under Site settings › API tokens: a *management* token, which acts as you. A self-hosted install can make one with `php artisan cabin:tokens:make`.
|
|
14
|
-
|
|
15
13
|
```
|
|
16
14
|
cabin login
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
That signs in to https://app.cabincms.com
|
|
17
|
+
That opens your browser on Cabin, where you approve Cabin CLI. One sign-in reaches every site you're on, and each theme's folder says which site it's for, so you can work on several sites from one machine. It signs in to https://app.cabincms.com; for a self-hosted install, set `CABIN_HOST=https://cabin.example.com`, and sign in to each install you use. Inside a theme's folder, the host in `cabin.json` is used.
|
|
18
|
+
|
|
19
|
+
The sign-in is kept in `~/.config/cabin/credentials.json`, never in the theme's folder, and is refreshed as you use it. It ends after 30 days unused, or when you disconnect Cabin CLI under Security settings. `cabin whoami` says who you are and lists your sites; `cabin logout` signs out.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
On a machine with no browser, such as a server or CI, use a *management* token instead, made on the site under Site settings › API tokens (or with `php artisan cabin:tokens:make` on a self-hosted install): `cabin login --token cabin_…`, or `CABIN_TOKEN` in the environment. A token reaches its one site.
|
|
22
22
|
|
|
23
23
|
## A theme
|
|
24
24
|
|
|
@@ -31,19 +31,20 @@ cabin theme push --note "Tighter header" # saves a version
|
|
|
31
31
|
cabin theme publish # draws the site with it
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
`cabin theme init` asks which of
|
|
34
|
+
`cabin theme init` asks which of your sites the theme is for (the ones where you may change themes, as an Owner or Admin), and what to call it; the folder is named after the theme unless you give one. `--site` and `--name` skip the questions. It makes the theme on the site and writes `cabin.json` (the host, the site's handle and the theme's key), which is committed; `.cabin/` holds what the CLI remembers and isn't.
|
|
35
35
|
|
|
36
|
-
A theme is `theme.json`, `theme.css`, templates under `templates
|
|
36
|
+
A theme is `theme.json`, `theme.css`, templates under `templates/`, patterns under `patterns/`, and its own scripts and drawings (`.js`, `.svg`) under `assets/`: a template prints one's address with `{{ asset('assets/nav.js') }}`, `theme.css` names one by its path (`url(assets/wave.svg)`), and a script at `assets/blocks/<block key>.js` is loaded only on the pages that block is on. A theme's pictures (`.png`, `.jpg`, `.webp`, `.gif`, `.avif` under `assets/`, 2 MB each at most) are synced too: `push` and `dev` send only the ones the site doesn't hold yet, `pull` fetches only the ones the folder lacks, and `{{ asset('assets/hero.png') }}` or `url(assets/hero.png)` in `theme.css` reaches one. With Tailwind, `src/theme.css` is the source and `theme.css` is built from it by the `dev` and `build` scripts in the folder's `package.json`. A new theme's Tailwind classes carry the prefix `tw:` (`tw:flex`, `tw:md:grid`), because Tailwind scans the templates for class names and a class of your own called `container` or `hidden` would otherwise silently take Tailwind's rules; a theme made with an earlier CLI has no prefix, and gets one by adding `prefix(tw)` to both `@import` lines in `src/theme.css` and to its Tailwind classes. `cabin theme dev` runs `dev` beside the watcher, and `push` runs `build` first; a `push` made while `dev` is running is remembered by both. A folder with no scripts is synced as it is.
|
|
37
37
|
|
|
38
38
|
| Command | What it does |
|
|
39
39
|
| --- | --- |
|
|
40
|
-
| `cabin login [--token]` |
|
|
40
|
+
| `cabin login [--token]` | Sign in through the browser, which reaches every site you're on, or keep a management token for one site. The host defaults to https://app.cabincms.com. `whoami` says who you are and lists your sites; `logout` signs out (`--site` forgets one site's token). |
|
|
41
41
|
| `cabin theme init [dir] [--from blank\|birch] [--site] [--name] [--key] [--no-tailwind]` | Make a theme on a site and write it into a folder. Asks for the site and name when they aren't given. |
|
|
42
42
|
| `cabin theme list` | The site's themes, with a star on the one the site is drawn with. |
|
|
43
43
|
| `cabin theme pull [--theme key] [--version id] [--all]` | Take the current (or a named) version's files. `theme.css` is skipped when `src/theme.css` exists, unless `--all`. |
|
|
44
44
|
| `cabin theme check [--no-build]` | Run every check a save runs, writing nothing. |
|
|
45
45
|
| `cabin theme push [--note] [--force] [--check] [--no-build]` | Save a version. Refused when someone saved since you pulled; `--force` saves over it. |
|
|
46
46
|
| `cabin theme dev [--path /] [--no-build]` | Keep a draft on the site as the folder changes, with a link that reloads itself. |
|
|
47
|
+
| `cabin theme context [/path] [--only entry.fields] [--full] [--draft]` | Print what the theme's templates are handed for a page: the page's template and its data, then each custom block's. Long texts are cut short unless `--full`; `--only` prints one part, by a dotted path; `--draft` looks at the draft `theme dev` keeps instead of the saved version. |
|
|
47
48
|
| `cabin theme publish` | Draw the site with the theme. |
|
|
48
49
|
| `cabin theme open [--site]` | Open the Theme files screen, or the site. |
|
|
49
50
|
|
package/dist/commands/context.js
CHANGED
|
@@ -2,7 +2,8 @@ import { join } from 'node:path';
|
|
|
2
2
|
import { readText, writeText } from '../io/fs.js';
|
|
3
3
|
import { ApiClient } from '../lib/api.js';
|
|
4
4
|
import { CONFIG_FILE, DEFAULT_HOST, normalizeHost, parseConfig, parseState, STATE_FILE, serializeState, } from '../lib/config.js';
|
|
5
|
-
import { credentialsDir, parseCredentials, pick } from '../lib/credentials.js';
|
|
5
|
+
import { credentialsDir, keepSignIn, parseCredentials, pick, serializeCredentials, signInFor, } from '../lib/credentials.js';
|
|
6
|
+
import { isDue, OauthRefused, refreshTokens } from '../lib/oauth.js';
|
|
6
7
|
export class Refused extends Error {
|
|
7
8
|
}
|
|
8
9
|
/** Read `cabin.json` from the working directory, or refuse. */
|
|
@@ -15,28 +16,97 @@ export async function project(io) {
|
|
|
15
16
|
if ('error' in config) {
|
|
16
17
|
throw new Refused(config.error);
|
|
17
18
|
}
|
|
18
|
-
const
|
|
19
|
+
const auth = await authFor(io, config.host, config.site);
|
|
19
20
|
const state = parseState(await readText(join(io.cwd, STATE_FILE)));
|
|
20
21
|
return {
|
|
21
22
|
config,
|
|
22
23
|
state,
|
|
23
|
-
api: new ApiClient(config.host,
|
|
24
|
+
api: new ApiClient(config.host, auth, io.fetch),
|
|
24
25
|
root: io.cwd,
|
|
25
26
|
saveState: (next) => writeText(join(io.cwd, STATE_FILE), serializeState(next)),
|
|
27
|
+
updateState: async (change) => writeText(join(io.cwd, STATE_FILE), serializeState(change(parseState(await readText(join(io.cwd, STATE_FILE)))))),
|
|
26
28
|
};
|
|
27
29
|
}
|
|
28
|
-
/**
|
|
29
|
-
export
|
|
30
|
+
/** Where the credentials file is. */
|
|
31
|
+
export function credentialsFile(io) {
|
|
32
|
+
return join(credentialsDir(io.env, io.home), 'credentials.json');
|
|
33
|
+
}
|
|
34
|
+
export async function readCredentials(io) {
|
|
35
|
+
return parseCredentials(await readText(credentialsFile(io)));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Change the credentials file, starting from what it holds now, since
|
|
39
|
+
* another `cabin` may have written it meanwhile. It is only ever readable
|
|
40
|
+
* by its owner.
|
|
41
|
+
*/
|
|
42
|
+
export async function updateCredentials(io, change) {
|
|
43
|
+
await writeText(credentialsFile(io), serializeCredentials(change(await readCredentials(io))), 0o600);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* How to call a host for one of its sites: `CABIN_TOKEN`, else the person
|
|
47
|
+
* signed in through the browser on the host (refreshed when it is due),
|
|
48
|
+
* else a token kept for that site; or refuse with what to do.
|
|
49
|
+
*/
|
|
50
|
+
export async function authFor(io, host, site) {
|
|
30
51
|
const envToken = io.env.CABIN_TOKEN;
|
|
31
52
|
if (envToken) {
|
|
32
|
-
return envToken;
|
|
53
|
+
return { token: envToken };
|
|
54
|
+
}
|
|
55
|
+
const credentials = await readCredentials(io);
|
|
56
|
+
const signedIn = signInFor(credentials, host);
|
|
57
|
+
if (signedIn !== null) {
|
|
58
|
+
return signedInAuth(io, host, signedIn, site);
|
|
33
59
|
}
|
|
34
|
-
const credentials = parseCredentials(await readText(join(credentialsDir(io.env, io.home), 'credentials.json')));
|
|
35
60
|
const kept = pick(credentials, host, site);
|
|
36
61
|
if (kept === null) {
|
|
37
|
-
throw new Refused(`
|
|
62
|
+
throw new Refused(`You aren't signed in to ${host}. Run cabin login${host === DEFAULT_HOST ? '' : ` with CABIN_HOST=${host}`}.`);
|
|
63
|
+
}
|
|
64
|
+
return { token: kept.token };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Call a host as the person signed in there: for one site, named by
|
|
68
|
+
* `Cabin-Site`, or, with none, to ask about their sites.
|
|
69
|
+
*/
|
|
70
|
+
export async function signedInAuth(io, host, signedIn, site) {
|
|
71
|
+
let held = isDue(signedIn.expiresAt, io.now())
|
|
72
|
+
? await refreshSignIn(io, host, signedIn)
|
|
73
|
+
: signedIn;
|
|
74
|
+
return {
|
|
75
|
+
token: held.accessToken,
|
|
76
|
+
...(site === undefined ? {} : { site }),
|
|
77
|
+
refresh: async () => {
|
|
78
|
+
held = await refreshSignIn(io, host, held);
|
|
79
|
+
return held.accessToken;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Trade a sign-in's refresh token for new tokens, and keep them. Cabin ends
|
|
85
|
+
* a refresh token once it is used, so when two `cabin`s refresh at once
|
|
86
|
+
* (`theme dev` and `theme push`), the one that loses finds the other's new
|
|
87
|
+
* tokens in the file and uses those.
|
|
88
|
+
*/
|
|
89
|
+
async function refreshSignIn(io, host, held) {
|
|
90
|
+
try {
|
|
91
|
+
const tokens = await refreshTokens(host, io.fetch, held.refreshToken, io.now());
|
|
92
|
+
const fresh = {
|
|
93
|
+
...held,
|
|
94
|
+
...tokens,
|
|
95
|
+
savedAt: new Date(io.now()).toISOString(),
|
|
96
|
+
};
|
|
97
|
+
await updateCredentials(io, (current) => keepSignIn(current, host, fresh));
|
|
98
|
+
return fresh;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (!(error instanceof OauthRefused)) {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
const now = signInFor(await readCredentials(io), host);
|
|
105
|
+
if (now !== null && now.refreshToken !== held.refreshToken) {
|
|
106
|
+
return now;
|
|
107
|
+
}
|
|
108
|
+
throw new Refused(`Your sign-in to ${host} has ended. Run cabin login${host === DEFAULT_HOST ? '' : ` with CABIN_HOST=${host}`}.`);
|
|
38
109
|
}
|
|
39
|
-
return kept.token;
|
|
40
110
|
}
|
|
41
111
|
/** The host named by CABIN_HOST, if any. */
|
|
42
112
|
export function hostFrom(io) {
|
package/dist/commands/login.js
CHANGED
|
@@ -1,24 +1,93 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
1
2
|
import { join } from 'node:path';
|
|
2
|
-
import { readText
|
|
3
|
+
import { readText } from '../io/fs.js';
|
|
3
4
|
import { ApiClient, ApiFailed } from '../lib/api.js';
|
|
4
5
|
import { flag } from '../lib/args.js';
|
|
5
6
|
import { CONFIG_FILE, DEFAULT_HOST, parseConfig } from '../lib/config.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
import { drop, forgetSignIn, keepSignIn, looksLikeToken, pick, put, signInFor, } from '../lib/credentials.js';
|
|
8
|
+
import { APPROVE_WITHIN_MS, authorizeUrl, base64url, callbackPage, exchangeCode, pkce, readCallback, takesBrowserSignIn, } from '../lib/oauth.js';
|
|
9
|
+
import { authFor, hostFrom, Refused, readCredentials, signedInAuth, updateCredentials, } from './context.js';
|
|
10
|
+
/** The folder's `cabin.json`, when there is a good one. */
|
|
11
|
+
async function folderConfig(io) {
|
|
12
|
+
const json = await readText(join(io.cwd, CONFIG_FILE));
|
|
13
|
+
const config = json === null ? null : parseConfig(json);
|
|
14
|
+
return config === null || 'error' in config ? null : config;
|
|
14
15
|
}
|
|
15
|
-
/**
|
|
16
|
+
/** The host to work with: CABIN_HOST, then cabin.json, then Cabin's own. */
|
|
17
|
+
async function hostFor(io) {
|
|
18
|
+
return hostFrom(io) ?? (await folderConfig(io))?.host ?? DEFAULT_HOST;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sign in to a host: through the browser, which reaches every site the
|
|
22
|
+
* person is on there, or with a management token given by `--token` or
|
|
23
|
+
* `CABIN_TOKEN`, which reaches one. A Cabin that doesn't take signing in
|
|
24
|
+
* through the browser yet is asked for a token instead.
|
|
25
|
+
*/
|
|
16
26
|
export async function login(parsed, io) {
|
|
17
|
-
const host = await
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
27
|
+
const host = await hostFor(io);
|
|
28
|
+
const given = flag(parsed, 'token') ?? io.env.CABIN_TOKEN ?? '';
|
|
29
|
+
if (given !== '') {
|
|
30
|
+
return keepToken(io, host, given);
|
|
31
|
+
}
|
|
32
|
+
if (!(await takesBrowserSignIn(host, io.fetch))) {
|
|
33
|
+
io.err(`${host} doesn't take signing in through the browser yet, so it needs a token.`);
|
|
34
|
+
return keepToken(io, host, (await io.prompt('Paste a management token from Site settings › API tokens: ', true)).trim());
|
|
35
|
+
}
|
|
36
|
+
return signInThroughBrowser(io, host);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Send the person to Cabin to approve the CLI, and wait at a loopback
|
|
40
|
+
* address for the browser to come back with a code to trade for tokens.
|
|
41
|
+
*/
|
|
42
|
+
async function signInThroughBrowser(io, host) {
|
|
43
|
+
const state = base64url(randomBytes(16));
|
|
44
|
+
const { verifier, challenge } = pkce(randomBytes(32));
|
|
45
|
+
const loopback = await io.listen((url) => callbackPage(readCallback(url, state)));
|
|
46
|
+
const deadline = new AbortController();
|
|
47
|
+
try {
|
|
48
|
+
const url = authorizeUrl(host, {
|
|
49
|
+
redirectUri: loopback.url,
|
|
50
|
+
state,
|
|
51
|
+
challenge,
|
|
52
|
+
});
|
|
53
|
+
io.out(`Opening ${new URL(host).host} in your browser to sign in…`);
|
|
54
|
+
io.out(`If it didn't open, go to: ${url}`);
|
|
55
|
+
await io.open(url);
|
|
56
|
+
const came = await Promise.race([
|
|
57
|
+
loopback.next,
|
|
58
|
+
io.stopped.then(() => 'stopped'),
|
|
59
|
+
io
|
|
60
|
+
.sleep(APPROVE_WITHIN_MS, deadline.signal)
|
|
61
|
+
.then(() => 'late'),
|
|
62
|
+
]);
|
|
63
|
+
if (came === 'stopped') {
|
|
64
|
+
throw new Refused('Stopped before signing in.');
|
|
65
|
+
}
|
|
66
|
+
if (came === 'late') {
|
|
67
|
+
throw new Refused('Nobody approved the sign-in in the browser. Run cabin login again, or use cabin login --token on a machine with no browser.');
|
|
68
|
+
}
|
|
69
|
+
const read = readCallback(came, state);
|
|
70
|
+
if ('refused' in read) {
|
|
71
|
+
throw new Refused(read.refused);
|
|
72
|
+
}
|
|
73
|
+
const tokens = await exchangeCode(host, io.fetch, { code: read.code, verifier, redirectUri: loopback.url }, io.now());
|
|
74
|
+
const answer = await new ApiClient(host, tokens.accessToken, io.fetch).sites();
|
|
75
|
+
await updateCredentials(io, (current) => keepSignIn(current, host, {
|
|
76
|
+
...tokens,
|
|
77
|
+
user: answer.person,
|
|
78
|
+
savedAt: new Date(io.now()).toISOString(),
|
|
79
|
+
}));
|
|
80
|
+
io.out(`Signed in to ${host} as ${answer.person.name}.`);
|
|
81
|
+
printSites(io, answer, null);
|
|
82
|
+
return 0;
|
|
21
83
|
}
|
|
84
|
+
finally {
|
|
85
|
+
deadline.abort();
|
|
86
|
+
await loopback.close();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Keep a management token for the site it names. */
|
|
90
|
+
async function keepToken(io, host, token) {
|
|
22
91
|
if (!looksLikeToken(token)) {
|
|
23
92
|
throw new Refused("That doesn't look like one of Cabin's tokens: they start with cabin_ and are 46 characters long.");
|
|
24
93
|
}
|
|
@@ -35,40 +104,76 @@ export async function login(parsed, io) {
|
|
|
35
104
|
if (info.kind !== 'management') {
|
|
36
105
|
io.err(`This is a ${info.kind} token. The theme commands need a management token, which acts as you.`);
|
|
37
106
|
}
|
|
38
|
-
|
|
39
|
-
const all = put(parseCredentials(await readText(file)), host, info.site.handle, {
|
|
107
|
+
await updateCredentials(io, (current) => put(current, host, info.site.handle, {
|
|
40
108
|
token,
|
|
41
109
|
name: info.user?.name ?? info.name,
|
|
42
110
|
savedAt: new Date(io.now()).toISOString(),
|
|
43
|
-
});
|
|
44
|
-
await writeText(file, serializeCredentials(all), 0o600);
|
|
111
|
+
}));
|
|
45
112
|
io.out(`Signed in to ${info.site.name} (${info.site.handle}) on ${host}${info.user ? ` as ${info.user.name}` : ''}.`);
|
|
46
113
|
return 0;
|
|
47
114
|
}
|
|
48
|
-
/**
|
|
115
|
+
/** List a person's sites, marking the folder's and those whose themes they can't change. */
|
|
116
|
+
function printSites(io, answer, folderSite) {
|
|
117
|
+
if (answer.sites.length === 0) {
|
|
118
|
+
io.out("You aren't on any site there yet.");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
io.out('Your sites:');
|
|
122
|
+
for (const site of answer.sites) {
|
|
123
|
+
const notes = [
|
|
124
|
+
...(site.handle === folderSite ? ['this folder'] : []),
|
|
125
|
+
...(site.canEditThemes ? [] : ["you can't change its themes"]),
|
|
126
|
+
];
|
|
127
|
+
io.out(` ${site.name} (${site.handle})${notes.length > 0 ? `, ${notes.join(', ')}` : ''}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Forget a sign-in: with `--site`, the token kept for that site; else the
|
|
132
|
+
* person signed in through the browser on the host, or, failing that, the
|
|
133
|
+
* token kept for the folder's site.
|
|
134
|
+
*/
|
|
49
135
|
export async function logout(parsed, io) {
|
|
50
|
-
const
|
|
51
|
-
const host = hostFrom(io) ??
|
|
52
|
-
const site = flag(parsed, 'site')
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
136
|
+
const folder = await folderConfig(io);
|
|
137
|
+
const host = hostFrom(io) ?? folder?.host ?? DEFAULT_HOST;
|
|
138
|
+
const site = flag(parsed, 'site');
|
|
139
|
+
const credentials = await readCredentials(io);
|
|
140
|
+
if (site === null && signInFor(credentials, host) !== null) {
|
|
141
|
+
await updateCredentials(io, (current) => forgetSignIn(current, host));
|
|
142
|
+
io.out(`Signed out of ${host}.`);
|
|
143
|
+
io.out('Cabin CLI stays among your connected agents on Security settings until you disconnect it there.');
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
const handle = site ?? folder?.site ?? null;
|
|
147
|
+
if (handle === null || pick(credentials, host, handle) === null) {
|
|
148
|
+
throw new Refused(handle === null
|
|
149
|
+
? `You aren't signed in to ${host}.`
|
|
150
|
+
: `No token is kept for ${handle} on ${host}.`);
|
|
56
151
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
io.out(`Forgot the token for ${site} on ${host}.`);
|
|
152
|
+
await updateCredentials(io, (current) => drop(current, host, handle));
|
|
153
|
+
io.out(`Forgot the token for ${handle} on ${host}.`);
|
|
60
154
|
return 0;
|
|
61
155
|
}
|
|
62
|
-
/**
|
|
156
|
+
/**
|
|
157
|
+
* Say who is signed in: the person and their sites, for a sign-in through
|
|
158
|
+
* the browser; the token for a site, otherwise.
|
|
159
|
+
*/
|
|
63
160
|
export async function whoami(parsed, io) {
|
|
64
|
-
const
|
|
65
|
-
const host = hostFrom(io) ??
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
161
|
+
const folder = await folderConfig(io);
|
|
162
|
+
const host = hostFrom(io) ?? folder?.host ?? DEFAULT_HOST;
|
|
163
|
+
const signedIn = io.env.CABIN_TOKEN
|
|
164
|
+
? null
|
|
165
|
+
: signInFor(await readCredentials(io), host);
|
|
166
|
+
if (signedIn !== null) {
|
|
167
|
+
const answer = await new ApiClient(host, await signedInAuth(io, host, signedIn), io.fetch).sites();
|
|
168
|
+
io.out(`${answer.person.name} <${answer.person.email}> on ${host}, signed in through the browser`);
|
|
169
|
+
printSites(io, answer, folder?.site ?? null);
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
const site = flag(parsed, 'site') ?? folder?.site ?? null;
|
|
173
|
+
if (site === null) {
|
|
174
|
+
throw new Refused(`You aren't signed in to ${host}. Run cabin login, or say which site's token: cabin whoami --site handle.`);
|
|
70
175
|
}
|
|
71
|
-
const info = await new ApiClient(host, await
|
|
176
|
+
const info = await new ApiClient(host, await authFor(io, host, site), io.fetch).token_();
|
|
72
177
|
io.out(`${info.site.name} (${info.site.handle}) on ${host}${info.site.url ? `, at ${info.site.url}` : ''}`);
|
|
73
178
|
io.out(info.user
|
|
74
179
|
? `${info.user.name} <${info.user.email}>, through the ${info.kind} token "${info.name}"`
|
|
@@ -2,13 +2,14 @@ import { ApiFailed } from '../../lib/api.js';
|
|
|
2
2
|
import { has } from '../../lib/args.js';
|
|
3
3
|
import { problemLines } from '../../lib/problems.js';
|
|
4
4
|
import { project } from '../context.js';
|
|
5
|
-
import { buildFirst, filesIn } from './sync.js';
|
|
5
|
+
import { buildFirst, filesIn, picturesIn } from './sync.js';
|
|
6
6
|
/** Run every check a save runs, writing nothing. */
|
|
7
7
|
export async function check(parsed, io) {
|
|
8
8
|
const { api, config, root } = await project(io);
|
|
9
9
|
await buildFirst(root, io, has(parsed, 'no-build'));
|
|
10
10
|
try {
|
|
11
|
-
const
|
|
11
|
+
const pictures = Object.entries(await picturesIn(root)).map(([path, picture]) => [path, { hash: picture.hash }]);
|
|
12
|
+
const answer = await api.check(config.theme, await filesIn(root), pictures.length === 0 ? null : Object.fromEntries(pictures));
|
|
12
13
|
io.out(`No problems in ${answer.files} ${answer.files === 1 ? 'file' : 'files'} of "${answer.name}".`);
|
|
13
14
|
return 0;
|
|
14
15
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ApiFailed } from '../../lib/api.js';
|
|
2
|
+
import { flag, has } from '../../lib/args.js';
|
|
3
|
+
import { contextLines } from '../../lib/context.js';
|
|
4
|
+
import { project, Refused } from '../context.js';
|
|
5
|
+
/**
|
|
6
|
+
* Print what the theme's templates are handed for a page of the site: the
|
|
7
|
+
* page's template and its data, then each custom block's. `dump` isn't
|
|
8
|
+
* allowed in a site theme, so this is how to see what there is to print.
|
|
9
|
+
*/
|
|
10
|
+
export async function context(parsed, io) {
|
|
11
|
+
const { api, config, state } = await project(io);
|
|
12
|
+
const path = parsed.positionals[0] ?? flag(parsed, 'path') ?? '/';
|
|
13
|
+
const draft = has(parsed, 'draft');
|
|
14
|
+
if (draft && state.draftToken === null) {
|
|
15
|
+
throw new Refused('There is no draft to look at. Run cabin theme dev first, or leave --draft off to use the saved version.');
|
|
16
|
+
}
|
|
17
|
+
let answer;
|
|
18
|
+
try {
|
|
19
|
+
answer = await api.context(config.theme, path, draft ? state.draftToken : null);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error instanceof ApiFailed && error.status === 404) {
|
|
23
|
+
throw new Refused(`${config.host} doesn't answer what templates are handed yet, or has no theme "${config.theme}".`);
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
for (const line of contextLines(answer, {
|
|
28
|
+
full: has(parsed, 'full'),
|
|
29
|
+
only: flag(parsed, 'only'),
|
|
30
|
+
})) {
|
|
31
|
+
io.out(line);
|
|
32
|
+
}
|
|
33
|
+
if (answer.problem !== null) {
|
|
34
|
+
io.err(`The theme couldn't draw ${path}: ${answer.problem}`);
|
|
35
|
+
return 1;
|
|
36
|
+
}
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
@@ -3,10 +3,10 @@ import { ApiFailed } from '../../lib/api.js';
|
|
|
3
3
|
import { flag, has } from '../../lib/args.js';
|
|
4
4
|
import { paint, syncedLine, wantsColor } from '../../lib/output.js';
|
|
5
5
|
import { problemLines } from '../../lib/problems.js';
|
|
6
|
-
import { isSkipped,
|
|
6
|
+
import { binariesFor, blobKey, heldBlobs, isSkipped, isSynced, toThemePath, } from '../../lib/theme-files.js';
|
|
7
7
|
import { backoff, Debounce } from '../../lib/watch.js';
|
|
8
|
-
import { project } from '../context.js';
|
|
9
|
-
import { filesIn, scriptIn } from './sync.js';
|
|
8
|
+
import { project, Refused } from '../context.js';
|
|
9
|
+
import { filesIn, picturesIn, scriptIn } from './sync.js';
|
|
10
10
|
/**
|
|
11
11
|
* Keep a draft of the folder on the site as it changes: the theme's `dev`
|
|
12
12
|
* script runs beside the watcher, every burst of changes is sent as one
|
|
@@ -14,19 +14,32 @@ import { filesIn, scriptIn } from './sync.js';
|
|
|
14
14
|
* itself again. Ctrl-C ends both.
|
|
15
15
|
*/
|
|
16
16
|
export async function dev(parsed, io) {
|
|
17
|
-
const { api, config, state, root,
|
|
17
|
+
const { api, config, state, root, updateState } = await project(io);
|
|
18
18
|
const path = flag(parsed, 'path') ?? '/';
|
|
19
19
|
const color = wantsColor(io.isTTY, io.env);
|
|
20
20
|
let token = state.draftToken;
|
|
21
21
|
let failures = 0;
|
|
22
22
|
let stopped = false;
|
|
23
23
|
const child = has(parsed, 'no-build') ? null : await startScript(root, io);
|
|
24
|
+
// The pictures the site holds: the theme's, and every one sent since.
|
|
25
|
+
let held;
|
|
24
26
|
const send = async (first) => {
|
|
25
27
|
try {
|
|
26
|
-
|
|
28
|
+
if (held === undefined) {
|
|
29
|
+
const known = (await api.theme(config.theme)).binaries;
|
|
30
|
+
held = known === undefined ? null : heldBlobs(known);
|
|
31
|
+
}
|
|
32
|
+
const pictures = await picturesIn(root);
|
|
33
|
+
const draft = await api.draft(config.theme, await filesIn(root), path, token, held === null ? null : binariesFor(pictures, held));
|
|
34
|
+
for (const [at, picture] of Object.entries(pictures)) {
|
|
35
|
+
held?.add(blobKey(at, picture.hash));
|
|
36
|
+
}
|
|
27
37
|
const renewed = token !== null && token !== draft.token;
|
|
28
38
|
token = draft.token;
|
|
29
|
-
await
|
|
39
|
+
await updateState((current) => ({
|
|
40
|
+
...current,
|
|
41
|
+
draftToken: token,
|
|
42
|
+
}));
|
|
30
43
|
failures = 0;
|
|
31
44
|
if (first || renewed) {
|
|
32
45
|
io.out(`Preview: ${paint(draft.url, 'cyan', color)}`);
|
|
@@ -48,8 +61,13 @@ export async function dev(parsed, io) {
|
|
|
48
61
|
if (error instanceof ApiFailed &&
|
|
49
62
|
(error.status === 401 || error.status === 403)) {
|
|
50
63
|
io.err(error.status === 401
|
|
51
|
-
?
|
|
52
|
-
: `
|
|
64
|
+
? `${config.host} refused your sign-in. Run cabin login.`
|
|
65
|
+
: `You can't change the themes of ${config.site}.`);
|
|
66
|
+
stopped = true;
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (error instanceof Refused) {
|
|
70
|
+
io.err(error.message);
|
|
53
71
|
stopped = true;
|
|
54
72
|
return false;
|
|
55
73
|
}
|
|
@@ -75,7 +93,7 @@ export async function dev(parsed, io) {
|
|
|
75
93
|
let dirty = false;
|
|
76
94
|
const watcher = watch(root, { recursive: true }, (_event, filename) => {
|
|
77
95
|
const relative = toThemePath(String(filename ?? ''));
|
|
78
|
-
if (relative === '' || isSkipped(relative) || !
|
|
96
|
+
if (relative === '' || isSkipped(relative) || !isSynced(relative)) {
|
|
79
97
|
return;
|
|
80
98
|
}
|
|
81
99
|
debounce.push(io.now());
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { join, resolve } from 'node:path';
|
|
2
|
-
import { exists,
|
|
2
|
+
import { exists, writeText } from '../../io/fs.js';
|
|
3
3
|
import { ApiClient, ApiFailed } from '../../lib/api.js';
|
|
4
4
|
import { flag, has } from '../../lib/args.js';
|
|
5
5
|
import { parseChoice } from '../../lib/choose.js';
|
|
6
6
|
import { CONFIG_FILE, DEFAULT_HOST, serializeState } from '../../lib/config.js';
|
|
7
|
-
import {
|
|
7
|
+
import { signInFor, sitesOn } from '../../lib/credentials.js';
|
|
8
8
|
import { problemLines } from '../../lib/problems.js';
|
|
9
9
|
import { scaffold } from '../../lib/scaffold.js';
|
|
10
|
-
import { hostFrom, Refused,
|
|
10
|
+
import { authFor, hostFrom, Refused, readCredentials, signedInAuth, } from '../context.js';
|
|
11
11
|
/** A key from a name: lowercase letters, digits and hyphens, starting with a letter. */
|
|
12
12
|
export function keyFrom(name) {
|
|
13
13
|
const key = name
|
|
@@ -20,35 +20,20 @@ export function keyFrom(name) {
|
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* The site to make the theme on: the flag or CABIN_SITE, else one of the
|
|
23
|
-
* sites
|
|
24
|
-
*
|
|
25
|
-
* kept
|
|
23
|
+
* person's sites, asked for when there are several. Signed in through the
|
|
24
|
+
* browser, they are every site the person is on where they may change its
|
|
25
|
+
* themes; otherwise, the sites a token is kept for, each asked its name.
|
|
26
26
|
*/
|
|
27
27
|
async function chooseSite(parsed, io, host) {
|
|
28
28
|
const given = flag(parsed, 'site') ?? io.env.CABIN_SITE ?? null;
|
|
29
29
|
if (given !== null) {
|
|
30
30
|
return given;
|
|
31
31
|
}
|
|
32
|
-
const credentials =
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const found = await Promise.all(handles.map(async (handle) => {
|
|
38
|
-
const token = credentials.hosts[host]?.[handle]?.token ?? '';
|
|
39
|
-
try {
|
|
40
|
-
const info = await new ApiClient(host, token, io.fetch).token_();
|
|
41
|
-
return { handle, name: info.site.name };
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
if (error instanceof ApiFailed && error.status === 401) {
|
|
45
|
-
io.err(`The token for ${handle} isn't accepted any more. Run cabin login to replace it.`);
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
}));
|
|
51
|
-
const sites = found.filter((site) => site !== null);
|
|
32
|
+
const credentials = await readCredentials(io);
|
|
33
|
+
const signedIn = io.env.CABIN_TOKEN ? null : signInFor(credentials, host);
|
|
34
|
+
const sites = signedIn === null
|
|
35
|
+
? await keptSites(io, host, credentials)
|
|
36
|
+
: await signedInSites(io, host, signedIn);
|
|
52
37
|
const [only] = sites;
|
|
53
38
|
if (only === undefined) {
|
|
54
39
|
throw new Refused(`No kept token is accepted by ${host}.`);
|
|
@@ -68,6 +53,43 @@ async function chooseSite(parsed, io, host) {
|
|
|
68
53
|
}
|
|
69
54
|
return chosen.handle;
|
|
70
55
|
}
|
|
56
|
+
/** The sites the person signed in through the browser may make a theme on. */
|
|
57
|
+
async function signedInSites(io, host, signedIn) {
|
|
58
|
+
const { sites } = await new ApiClient(host, await signedInAuth(io, host, signedIn), io.fetch).sites();
|
|
59
|
+
const open = sites.filter((site) => site.canEditThemes);
|
|
60
|
+
if (sites.length === 0) {
|
|
61
|
+
throw new Refused(`You aren't on any site at ${host} yet.`);
|
|
62
|
+
}
|
|
63
|
+
if (open.length === 0) {
|
|
64
|
+
throw new Refused(`You can't change the themes of any of your sites at ${host}: that takes an Owner or Admin.`);
|
|
65
|
+
}
|
|
66
|
+
return open;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The sites a token is kept for on the host. The API names a token's site
|
|
70
|
+
* but lists no others, so each token is asked for its site's name.
|
|
71
|
+
*/
|
|
72
|
+
async function keptSites(io, host, credentials) {
|
|
73
|
+
const handles = sitesOn(credentials, host);
|
|
74
|
+
if (handles.length === 0) {
|
|
75
|
+
throw new Refused(`You aren't signed in to a site on ${host}. Run cabin login first.`);
|
|
76
|
+
}
|
|
77
|
+
const found = await Promise.all(handles.map(async (handle) => {
|
|
78
|
+
const token = credentials.hosts[host]?.[handle]?.token ?? '';
|
|
79
|
+
try {
|
|
80
|
+
const info = await new ApiClient(host, token, io.fetch).token_();
|
|
81
|
+
return { handle, name: info.site.name };
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error instanceof ApiFailed && error.status === 401) {
|
|
85
|
+
io.err(`The token for ${handle} isn't accepted any more. Run cabin login to replace it.`);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}));
|
|
91
|
+
return found.filter((site) => site !== null);
|
|
92
|
+
}
|
|
71
93
|
/** The theme's name: the flag, else asked for, offering the folder's name. */
|
|
72
94
|
async function chooseName(parsed, io, dir) {
|
|
73
95
|
const given = flag(parsed, 'name');
|
|
@@ -91,7 +113,7 @@ export async function init(parsed, io) {
|
|
|
91
113
|
}
|
|
92
114
|
const host = hostFrom(io) ?? DEFAULT_HOST;
|
|
93
115
|
const site = await chooseSite(parsed, io, host);
|
|
94
|
-
const api = new ApiClient(host, await
|
|
116
|
+
const api = new ApiClient(host, await authFor(io, host, site), io.fetch);
|
|
95
117
|
const name = await chooseName(parsed, io, given);
|
|
96
118
|
const key = flag(parsed, 'key') ?? keyFrom(name);
|
|
97
119
|
const dir = given ?? key;
|