@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
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
|
-
import { exists, remove, walk, writeText } from '../../io/fs.js';
|
|
2
|
+
import { exists, remove, walk, walkPictures, writeBytes, writeText, } from '../../io/fs.js';
|
|
3
3
|
import { flag, has } from '../../lib/args.js';
|
|
4
4
|
import { isGenerated, isThemePath } from '../../lib/theme-files.js';
|
|
5
5
|
import { project, Refused } from '../context.js';
|
|
6
6
|
import { hasSource } from './sync.js';
|
|
7
|
-
/** Take the theme's files from the site into the folder. */
|
|
7
|
+
/** Take the theme's files, and its pictures, from the site into the folder. */
|
|
8
8
|
export async function pull(parsed, io) {
|
|
9
9
|
const { api, config, state, root, saveState } = await project(io);
|
|
10
10
|
const key = flag(parsed, 'theme') ?? config.theme;
|
|
@@ -39,6 +39,28 @@ export async function pull(parsed, io) {
|
|
|
39
39
|
removed += 1;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
// The theme's pictures: only the ones the folder lacks or has differently
|
|
43
|
+
// are fetched, and the ones the site no longer has go.
|
|
44
|
+
if (taken.binaries !== undefined) {
|
|
45
|
+
const local = await walkPictures(root);
|
|
46
|
+
for (const [path, picture] of Object.entries(taken.binaries)) {
|
|
47
|
+
if (local[path]?.hash === picture.hash) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const answer = await io.fetch(new URL(picture.url, config.host));
|
|
51
|
+
if (!answer.ok) {
|
|
52
|
+
throw new Refused(`Couldn't fetch ${path} from the site (${answer.status}).`);
|
|
53
|
+
}
|
|
54
|
+
await writeBytes(join(root, path), new Uint8Array(await answer.arrayBuffer()));
|
|
55
|
+
written += 1;
|
|
56
|
+
}
|
|
57
|
+
for (const path of Object.keys(local)) {
|
|
58
|
+
if (!(path in taken.binaries)) {
|
|
59
|
+
await remove(join(root, path));
|
|
60
|
+
removed += 1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
42
64
|
await saveState({ ...state, versionId: version?.id ?? state.versionId });
|
|
43
65
|
io.out(`Pulled ${written} ${written === 1 ? 'file' : 'files'} of "${key}"${version ? ` at version ${version.number}` : ''}${removed > 0 ? `, and removed ${removed} the site no longer has` : ''}.`);
|
|
44
66
|
if (skipped !== null) {
|
|
@@ -4,8 +4,8 @@ import { problemLines } from '../../lib/problems.js';
|
|
|
4
4
|
import { conflictLine, currentOf } from '../../lib/versions.js';
|
|
5
5
|
import { project } from '../context.js';
|
|
6
6
|
import { check } from './check.js';
|
|
7
|
-
import { buildFirst, filesIn } from './sync.js';
|
|
8
|
-
/** Save the folder's files as a version. */
|
|
7
|
+
import { binariesIn, buildFirst, filesIn } from './sync.js';
|
|
8
|
+
/** Save the folder's files, and its pictures, as a version. */
|
|
9
9
|
export async function push(parsed, io) {
|
|
10
10
|
if (has(parsed, 'check')) {
|
|
11
11
|
return check(parsed, io);
|
|
@@ -14,7 +14,7 @@ export async function push(parsed, io) {
|
|
|
14
14
|
await buildFirst(root, io, has(parsed, 'no-build'));
|
|
15
15
|
const base = has(parsed, 'force') ? null : state.versionId;
|
|
16
16
|
try {
|
|
17
|
-
const theme = await api.save(config.theme, await filesIn(root), flag(parsed, 'note'), base);
|
|
17
|
+
const theme = await api.save(config.theme, await filesIn(root), flag(parsed, 'note'), base, await binariesIn(root, api, config.theme, io));
|
|
18
18
|
await saveState({ ...state, versionId: theme.version?.id ?? null });
|
|
19
19
|
io.out(`Pushed as version ${theme.version?.number ?? '?'} of "${theme.name}".${theme.chosen ? ' The site shows it now.' : ''}`);
|
|
20
20
|
return 0;
|
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
-
import { exists, readText, walk } from '../../io/fs.js';
|
|
3
|
+
import { exists, readText, walk, walkPictures } from '../../io/fs.js';
|
|
4
4
|
import { detectPackageManager, hasScript, runScript, } from '../../lib/scripts.js';
|
|
5
|
-
import { collect } from '../../lib/theme-files.js';
|
|
5
|
+
import { binariesFor, collect, heldBlobs } from '../../lib/theme-files.js';
|
|
6
6
|
import { Refused } from '../context.js';
|
|
7
7
|
/** The theme files a folder holds now. */
|
|
8
8
|
export async function filesIn(root) {
|
|
9
9
|
return collect(await walk(root));
|
|
10
10
|
}
|
|
11
|
+
/** The pictures a folder holds now, by path. */
|
|
12
|
+
export async function picturesIn(root) {
|
|
13
|
+
return walkPictures(root);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What to send for the folder's pictures, given what the site holds for
|
|
17
|
+
* the theme now. Null for a folder with none on a Cabin that takes none,
|
|
18
|
+
* so nothing about pictures is sent to it at all.
|
|
19
|
+
*
|
|
20
|
+
* @returns The pictures to send, and whether the site can take them.
|
|
21
|
+
*/
|
|
22
|
+
export async function binariesIn(root, api, theme, io) {
|
|
23
|
+
const local = await picturesIn(root);
|
|
24
|
+
const held = (await api.theme(theme)).binaries;
|
|
25
|
+
if (held === undefined) {
|
|
26
|
+
if (Object.keys(local).length > 0) {
|
|
27
|
+
io.err("This Cabin doesn't take a theme's pictures yet, so the ones under assets/ weren't sent.");
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return binariesFor(local, heldBlobs(held));
|
|
32
|
+
}
|
|
11
33
|
/** Whether a folder has a script by name, and how to run it. */
|
|
12
34
|
export async function scriptIn(root, name, io) {
|
|
13
35
|
if (!hasScript(await readText(join(root, 'package.json')), name)) {
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
4
4
|
import { Refused } from './commands/context.js';
|
|
5
5
|
import { login, logout, whoami } from './commands/login.js';
|
|
6
6
|
import { check } from './commands/theme/check.js';
|
|
7
|
+
import { context } from './commands/theme/context.js';
|
|
7
8
|
import { dev } from './commands/theme/dev.js';
|
|
8
9
|
import { init } from './commands/theme/init.js';
|
|
9
10
|
import { list } from './commands/theme/list.js';
|
|
@@ -14,7 +15,7 @@ import { push } from './commands/theme/push.js';
|
|
|
14
15
|
import { ApiFailed } from './lib/api.js';
|
|
15
16
|
import { has, parse, USAGE } from './lib/args.js';
|
|
16
17
|
import { problemLines } from './lib/problems.js';
|
|
17
|
-
const VERSION = '0.
|
|
18
|
+
const VERSION = '0.6.0';
|
|
18
19
|
const COMMANDS = {
|
|
19
20
|
login,
|
|
20
21
|
logout,
|
|
@@ -25,6 +26,7 @@ const COMMANDS = {
|
|
|
25
26
|
'theme check': check,
|
|
26
27
|
'theme push': push,
|
|
27
28
|
'theme dev': dev,
|
|
29
|
+
'theme context': context,
|
|
28
30
|
'theme publish': publish,
|
|
29
31
|
'theme open': open,
|
|
30
32
|
};
|
package/dist/io/fs.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { mkdir, readdir, readFile, rm, stat, writeFile, } from 'node:fs/promises';
|
|
2
3
|
import { dirname, join, relative } from 'node:path';
|
|
3
|
-
import { isSkipped, toThemePath } from '../lib/theme-files.js';
|
|
4
|
+
import { isPicturePath, isSkipped, isThemePath, toThemePath, } from '../lib/theme-files.js';
|
|
4
5
|
/** Read a text file, or null when it isn't there. */
|
|
5
6
|
export async function readText(path) {
|
|
6
7
|
try {
|
|
@@ -26,9 +27,49 @@ export async function exists(path) {
|
|
|
26
27
|
export async function remove(path) {
|
|
27
28
|
await rm(path, { force: true });
|
|
28
29
|
}
|
|
30
|
+
/** Write a file that isn't text. */
|
|
31
|
+
export async function writeBytes(path, bytes) {
|
|
32
|
+
await mkdir(dirname(path), { recursive: true });
|
|
33
|
+
await writeFile(path, bytes);
|
|
34
|
+
}
|
|
35
|
+
/** What a file's bytes hash to: SHA-256, in hex, as the site names a picture. */
|
|
36
|
+
export function hashOf(bytes) {
|
|
37
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The pictures under a folder, by path, each with its hash and its bytes
|
|
41
|
+
* in base64, leaving the folders that are never synced.
|
|
42
|
+
*/
|
|
43
|
+
export async function walkPictures(root) {
|
|
44
|
+
const found = {};
|
|
45
|
+
async function visit(folder) {
|
|
46
|
+
for (const entry of await readdir(folder, { withFileTypes: true })) {
|
|
47
|
+
const path = join(folder, entry.name);
|
|
48
|
+
const rel = toThemePath(relative(root, path));
|
|
49
|
+
if (isSkipped(rel)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (entry.isDirectory()) {
|
|
53
|
+
await visit(path);
|
|
54
|
+
}
|
|
55
|
+
else if (entry.isFile() && isPicturePath(rel)) {
|
|
56
|
+
const bytes = await readFile(path);
|
|
57
|
+
found[rel] = {
|
|
58
|
+
hash: hashOf(bytes),
|
|
59
|
+
base64: bytes.toString('base64'),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (await exists(root)) {
|
|
65
|
+
await visit(root);
|
|
66
|
+
}
|
|
67
|
+
return found;
|
|
68
|
+
}
|
|
29
69
|
/**
|
|
30
|
-
* Every text
|
|
31
|
-
* the folders that are never synced
|
|
70
|
+
* Every one of a theme's text files under a folder, as [relative path,
|
|
71
|
+
* contents], leaving the folders that are never synced and anything a
|
|
72
|
+
* theme doesn't hold as text.
|
|
32
73
|
*/
|
|
33
74
|
export async function walk(root) {
|
|
34
75
|
const found = [];
|
|
@@ -43,7 +84,7 @@ export async function walk(root) {
|
|
|
43
84
|
if (entry.isDirectory()) {
|
|
44
85
|
await visit(path);
|
|
45
86
|
}
|
|
46
|
-
else if (entry.isFile()) {
|
|
87
|
+
else if (entry.isFile() && isThemePath(rel)) {
|
|
47
88
|
found.push([rel, await readFile(path, 'utf8')]);
|
|
48
89
|
}
|
|
49
90
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
/**
|
|
3
|
+
* Listen on 127.0.0.1, on a port the system picks, for the browser to come
|
|
4
|
+
* back from signing in. The first visit to `/callback` is answered with the
|
|
5
|
+
* page `answer` makes, and settles `next`; anything else is a 404.
|
|
6
|
+
*/
|
|
7
|
+
export function loopback(answer) {
|
|
8
|
+
let arrived = () => { };
|
|
9
|
+
const next = new Promise((resolve) => {
|
|
10
|
+
arrived = resolve;
|
|
11
|
+
});
|
|
12
|
+
let done = false;
|
|
13
|
+
const server = createServer((request, response) => {
|
|
14
|
+
const url = new URL(request.url ?? '/', 'http://127.0.0.1');
|
|
15
|
+
if (url.pathname !== '/callback' || done) {
|
|
16
|
+
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
17
|
+
response.end('Not found');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
done = true;
|
|
21
|
+
response.writeHead(200, {
|
|
22
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
23
|
+
'Cache-Control': 'no-store',
|
|
24
|
+
Connection: 'close',
|
|
25
|
+
});
|
|
26
|
+
response.end(answer(url));
|
|
27
|
+
arrived(url);
|
|
28
|
+
});
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
server.once('error', reject);
|
|
31
|
+
server.listen(0, '127.0.0.1', () => {
|
|
32
|
+
const { port } = server.address();
|
|
33
|
+
resolve({
|
|
34
|
+
url: `http://127.0.0.1:${port}/callback`,
|
|
35
|
+
next,
|
|
36
|
+
close: () => new Promise((closed) => {
|
|
37
|
+
server.close(() => closed());
|
|
38
|
+
server.closeAllConnections();
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
package/dist/io/world.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn as nodeSpawn } from 'node:child_process';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { createInterface } from 'node:readline/promises';
|
|
4
|
+
import { loopback } from './loopback.js';
|
|
4
5
|
/** The real world, for `cabin` run from a terminal. */
|
|
5
6
|
export function world() {
|
|
6
7
|
let stop = () => { };
|
|
@@ -55,7 +56,14 @@ export function world() {
|
|
|
55
56
|
detached: true,
|
|
56
57
|
}).unref();
|
|
57
58
|
},
|
|
58
|
-
|
|
59
|
+
listen: loopback,
|
|
60
|
+
sleep: (ms, signal) => new Promise((resolve) => {
|
|
61
|
+
const timer = setTimeout(resolve, ms);
|
|
62
|
+
signal?.addEventListener('abort', () => {
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
resolve();
|
|
65
|
+
}, { once: true });
|
|
66
|
+
}),
|
|
59
67
|
now: () => Date.now(),
|
|
60
68
|
stopped,
|
|
61
69
|
};
|
package/dist/lib/api.js
CHANGED
|
@@ -34,16 +34,19 @@ export function failureOf(status, body) {
|
|
|
34
34
|
*/
|
|
35
35
|
export class ApiClient {
|
|
36
36
|
host;
|
|
37
|
-
token;
|
|
38
37
|
fetchFn;
|
|
39
|
-
|
|
38
|
+
auth;
|
|
39
|
+
constructor(host, auth, fetchFn) {
|
|
40
40
|
this.host = host;
|
|
41
|
-
this.token = token;
|
|
42
41
|
this.fetchFn = fetchFn;
|
|
42
|
+
this.auth = typeof auth === 'string' ? { token: auth } : { ...auth };
|
|
43
43
|
}
|
|
44
44
|
token_() {
|
|
45
45
|
return this.call('GET', '/v1/token');
|
|
46
46
|
}
|
|
47
|
+
sites() {
|
|
48
|
+
return this.call('GET', '/v1/sites');
|
|
49
|
+
}
|
|
47
50
|
themes() {
|
|
48
51
|
return this.call('GET', '/v1/themes');
|
|
49
52
|
}
|
|
@@ -56,19 +59,24 @@ export class ApiClient {
|
|
|
56
59
|
create(key, name, from) {
|
|
57
60
|
return this.call('POST', '/v1/themes', { key, name, from });
|
|
58
61
|
}
|
|
59
|
-
save(key, files, note, baseVersion) {
|
|
62
|
+
save(key, files, note, baseVersion, binaries = null) {
|
|
60
63
|
return this.call('PUT', `/v1/themes/${key}`, {
|
|
61
64
|
files,
|
|
65
|
+
...(binaries === null ? {} : { binaries }),
|
|
62
66
|
note,
|
|
63
67
|
base_version: baseVersion,
|
|
64
68
|
});
|
|
65
69
|
}
|
|
66
|
-
check(key, files) {
|
|
67
|
-
return this.call('POST', `/v1/themes/${key}/check`, {
|
|
70
|
+
check(key, files, binaries = null) {
|
|
71
|
+
return this.call('POST', `/v1/themes/${key}/check`, {
|
|
72
|
+
files,
|
|
73
|
+
...(binaries === null ? {} : { binaries }),
|
|
74
|
+
});
|
|
68
75
|
}
|
|
69
|
-
draft(key, files, path, token) {
|
|
76
|
+
draft(key, files, path, token, binaries = null) {
|
|
70
77
|
return this.call('POST', `/v1/themes/${key}/drafts`, {
|
|
71
78
|
files,
|
|
79
|
+
...(binaries === null ? {} : { binaries }),
|
|
72
80
|
path,
|
|
73
81
|
token,
|
|
74
82
|
});
|
|
@@ -79,26 +87,27 @@ export class ApiClient {
|
|
|
79
87
|
version(key, id) {
|
|
80
88
|
return this.call('GET', `/v1/themes/${key}/versions/${id}`);
|
|
81
89
|
}
|
|
90
|
+
context(key, path, token) {
|
|
91
|
+
const query = new URLSearchParams({
|
|
92
|
+
path,
|
|
93
|
+
...(token === null ? {} : { token }),
|
|
94
|
+
});
|
|
95
|
+
return this.call('GET', `/v1/themes/${key}/context?${query.toString()}`);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Send a request and read its `data`. A token that is refused (401) is
|
|
99
|
+
* swapped for a fresh one once, when there is a way to get one: a sign-in
|
|
100
|
+
* outlives its day-long token, and `theme dev` runs for longer than that.
|
|
101
|
+
*/
|
|
82
102
|
async call(method, path, body) {
|
|
83
|
-
let response;
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
...(body === undefined
|
|
91
|
-
? {}
|
|
92
|
-
: { 'Content-Type': 'application/json' }),
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
if (body !== undefined) {
|
|
96
|
-
init.body = JSON.stringify(body);
|
|
103
|
+
let response = await this.send(method, path, body);
|
|
104
|
+
if (response.status === 401 && this.auth.refresh !== undefined) {
|
|
105
|
+
const fresh = await this.auth.refresh();
|
|
106
|
+
if (fresh !== null) {
|
|
107
|
+
this.auth.token = fresh;
|
|
108
|
+
await response.body?.cancel();
|
|
109
|
+
response = await this.send(method, path, body);
|
|
97
110
|
}
|
|
98
|
-
response = await this.fetchFn(`${this.host}${path}`, init);
|
|
99
|
-
}
|
|
100
|
-
catch {
|
|
101
|
-
throw failureOf(0, null);
|
|
102
111
|
}
|
|
103
112
|
const text = await response.text();
|
|
104
113
|
let parsed = null;
|
|
@@ -113,4 +122,30 @@ export class ApiClient {
|
|
|
113
122
|
}
|
|
114
123
|
return parsed.data;
|
|
115
124
|
}
|
|
125
|
+
async send(method, path, body) {
|
|
126
|
+
const init = {
|
|
127
|
+
method,
|
|
128
|
+
headers: {
|
|
129
|
+
Authorization: `Bearer ${this.auth.token}`,
|
|
130
|
+
Accept: 'application/json',
|
|
131
|
+
...(this.auth.site === undefined
|
|
132
|
+
? {}
|
|
133
|
+
: { [SITE_HEADER]: this.auth.site }),
|
|
134
|
+
...(body === undefined
|
|
135
|
+
? {}
|
|
136
|
+
: { 'Content-Type': 'application/json' }),
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
if (body !== undefined) {
|
|
140
|
+
init.body = JSON.stringify(body);
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
return await this.fetchFn(`${this.host}${path}`, init);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
throw failureOf(0, null);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
116
149
|
}
|
|
150
|
+
/** The header a request names its site with, for a person signed in through the browser. */
|
|
151
|
+
export const SITE_HEADER = 'Cabin-Site';
|
package/dist/lib/args.js
CHANGED
|
@@ -14,6 +14,9 @@ const OPTIONS = {
|
|
|
14
14
|
force: { type: 'boolean' },
|
|
15
15
|
check: { type: 'boolean' },
|
|
16
16
|
all: { type: 'boolean' },
|
|
17
|
+
draft: { type: 'boolean' },
|
|
18
|
+
full: { type: 'boolean' },
|
|
19
|
+
only: { type: 'string' },
|
|
17
20
|
help: { type: 'boolean', short: 'h' },
|
|
18
21
|
};
|
|
19
22
|
/** Read the command words and flags from argv. */
|
|
@@ -69,10 +72,11 @@ export const USAGE = `cabin — develop a Cabin site theme locally
|
|
|
69
72
|
cabin theme check [--no-build]
|
|
70
73
|
cabin theme push [--note NOTE] [--force] [--check] [--no-build]
|
|
71
74
|
cabin theme dev [--path /] [--no-build]
|
|
75
|
+
cabin theme context [/path] [--only entry.fields] [--full] [--draft]
|
|
72
76
|
cabin theme publish
|
|
73
77
|
cabin theme open [--site]
|
|
74
78
|
|
|
75
|
-
Signs in to https://app.cabincms.com.
|
|
76
|
-
Set CABIN_HOST to use another install.
|
|
79
|
+
Signs in to https://app.cabincms.com through the browser.
|
|
80
|
+
Set CABIN_HOST to use another install, or --token where there is no browser.
|
|
77
81
|
|
|
78
82
|
Exit codes: 0 done, 1 refused or problems found, 2 usage.`;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** How long a piece of text is printed before it is cut, unless all of it is asked for. */
|
|
2
|
+
export const SHORT = 160;
|
|
3
|
+
/**
|
|
4
|
+
* A value with its long texts cut short, so a page's markup doesn't bury
|
|
5
|
+
* the data around it. What was cut says how long it was.
|
|
6
|
+
*/
|
|
7
|
+
export function shorten(value, max = SHORT) {
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
return value.length > max
|
|
10
|
+
? `${value.slice(0, max)}… (${value.length} characters)`
|
|
11
|
+
: value;
|
|
12
|
+
}
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
return value.map((item) => shorten(item, max));
|
|
15
|
+
}
|
|
16
|
+
if (typeof value === 'object' && value !== null) {
|
|
17
|
+
return Object.fromEntries(Object.entries(value).map(([key, inner]) => [
|
|
18
|
+
key,
|
|
19
|
+
shorten(inner, max),
|
|
20
|
+
]));
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The part of the data a dotted path names, as a template would reach it:
|
|
26
|
+
* `entry.fields.photo`, `list.entries.0.title`. Undefined when nothing is
|
|
27
|
+
* there.
|
|
28
|
+
*/
|
|
29
|
+
export function pick(data, path) {
|
|
30
|
+
let at = data;
|
|
31
|
+
for (const key of path.split('.').filter((part) => part !== '')) {
|
|
32
|
+
if (typeof at !== 'object' || at === null || !(key in at)) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
at = at[key];
|
|
36
|
+
}
|
|
37
|
+
return at;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What to print for a page: its template and what a visitor would be
|
|
41
|
+
* answered, the data it was handed (or the part asked for), then each
|
|
42
|
+
* custom block's template and data.
|
|
43
|
+
*/
|
|
44
|
+
export function contextLines(context, options) {
|
|
45
|
+
const show = (value) => JSON.stringify(options.full ? value : shorten(value), null, 2);
|
|
46
|
+
if (context.location !== null) {
|
|
47
|
+
return [`${context.path} → ${context.location} (${context.status})`];
|
|
48
|
+
}
|
|
49
|
+
if (context.template === null || context.data === null) {
|
|
50
|
+
return [`${context.path} drew no template (${context.status}).`];
|
|
51
|
+
}
|
|
52
|
+
if (options.only !== null) {
|
|
53
|
+
const part = pick(context.data, options.only);
|
|
54
|
+
return part === undefined
|
|
55
|
+
? [
|
|
56
|
+
`${context.template} isn't handed "${options.only}". It is handed: ${Object.keys(context.data).join(', ')}.`,
|
|
57
|
+
]
|
|
58
|
+
: [show(part)];
|
|
59
|
+
}
|
|
60
|
+
const lines = [
|
|
61
|
+
`${context.template} (${context.status})`,
|
|
62
|
+
show(context.data),
|
|
63
|
+
];
|
|
64
|
+
for (const block of context.blocks) {
|
|
65
|
+
lines.push('', block.template, show(block.data));
|
|
66
|
+
}
|
|
67
|
+
return lines;
|
|
68
|
+
}
|
package/dist/lib/credentials.js
CHANGED
|
@@ -9,8 +9,9 @@ export function credentialsDir(env, home) {
|
|
|
9
9
|
return `${home}/.config/cabin`;
|
|
10
10
|
}
|
|
11
11
|
export function parseCredentials(json) {
|
|
12
|
+
const none = { hosts: {}, signIns: {} };
|
|
12
13
|
if (json === null) {
|
|
13
|
-
return
|
|
14
|
+
return none;
|
|
14
15
|
}
|
|
15
16
|
try {
|
|
16
17
|
const parsed = JSON.parse(json);
|
|
@@ -18,12 +19,22 @@ export function parseCredentials(json) {
|
|
|
18
19
|
hosts: typeof parsed.hosts === 'object' && parsed.hosts !== null
|
|
19
20
|
? parsed.hosts
|
|
20
21
|
: {},
|
|
22
|
+
signIns: readSignIns(parsed.signIns),
|
|
21
23
|
};
|
|
22
24
|
}
|
|
23
25
|
catch {
|
|
24
|
-
return
|
|
26
|
+
return none;
|
|
25
27
|
}
|
|
26
28
|
}
|
|
29
|
+
/** The sign-ins a file holds, leaving out any that can't be used. */
|
|
30
|
+
function readSignIns(given) {
|
|
31
|
+
if (typeof given !== 'object' || given === null) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
return Object.fromEntries(Object.entries(given).filter(([, signIn]) => typeof signIn?.accessToken === 'string' &&
|
|
35
|
+
typeof signIn.refreshToken === 'string' &&
|
|
36
|
+
typeof signIn.expiresAt === 'string'));
|
|
37
|
+
}
|
|
27
38
|
export function serializeCredentials(credentials) {
|
|
28
39
|
return `${JSON.stringify(credentials, null, 4)}\n`;
|
|
29
40
|
}
|
|
@@ -32,6 +43,7 @@ export function pick(credentials, host, site) {
|
|
|
32
43
|
}
|
|
33
44
|
export function put(credentials, host, site, credential) {
|
|
34
45
|
return {
|
|
46
|
+
...credentials,
|
|
35
47
|
hosts: {
|
|
36
48
|
...credentials.hosts,
|
|
37
49
|
[host]: { ...(credentials.hosts[host] ?? {}), [site]: credential },
|
|
@@ -48,7 +60,22 @@ export function drop(credentials, host, site) {
|
|
|
48
60
|
else {
|
|
49
61
|
hosts[host] = forHost;
|
|
50
62
|
}
|
|
51
|
-
return { hosts };
|
|
63
|
+
return { ...credentials, hosts };
|
|
64
|
+
}
|
|
65
|
+
/** Who is signed in through the browser on a host, if anyone. */
|
|
66
|
+
export function signInFor(credentials, host) {
|
|
67
|
+
return credentials.signIns[host] ?? null;
|
|
68
|
+
}
|
|
69
|
+
export function keepSignIn(credentials, host, signIn) {
|
|
70
|
+
return {
|
|
71
|
+
...credentials,
|
|
72
|
+
signIns: { ...credentials.signIns, [host]: signIn },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function forgetSignIn(credentials, host) {
|
|
76
|
+
const signIns = { ...credentials.signIns };
|
|
77
|
+
delete signIns[host];
|
|
78
|
+
return { ...credentials, signIns };
|
|
52
79
|
}
|
|
53
80
|
/** Whether a string looks like one of Cabin's tokens. */
|
|
54
81
|
export function looksLikeToken(token) {
|