@politelabs/cabin-cli 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +60 -2
- package/dist/commands/context.js +45 -0
- package/dist/commands/login.js +77 -0
- package/dist/commands/theme/check.js +24 -0
- package/dist/commands/theme/dev.js +122 -0
- package/dist/commands/theme/init.js +143 -0
- package/dist/commands/theme/list.js +27 -0
- package/dist/commands/theme/open.js +19 -0
- package/dist/commands/theme/publish.js +13 -0
- package/dist/commands/theme/pull.js +48 -0
- package/dist/commands/theme/push.js +35 -0
- package/dist/commands/theme/sync.js +37 -0
- package/dist/index.js +93 -0
- package/dist/io/fs.js +55 -0
- package/dist/io/io.js +1 -0
- package/dist/io/world.js +62 -0
- package/dist/lib/api.js +116 -0
- package/dist/lib/args.js +78 -0
- package/dist/lib/choose.js +9 -0
- package/dist/lib/config.js +75 -0
- package/dist/lib/credentials.js +60 -0
- package/dist/lib/output.js +33 -0
- package/dist/lib/problems.js +25 -0
- package/dist/lib/scaffold.js +72 -0
- package/dist/lib/scripts.js +36 -0
- package/dist/lib/theme-files.js +66 -0
- package/dist/lib/versions.js +34 -0
- package/dist/lib/watch.js +42 -0
- package/package.json +34 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { exists, readText, walk } from '../../io/fs.js';
|
|
4
|
+
import { detectPackageManager, hasScript, runScript, } from '../../lib/scripts.js';
|
|
5
|
+
import { collect } from '../../lib/theme-files.js';
|
|
6
|
+
import { Refused } from '../context.js';
|
|
7
|
+
/** The theme files a folder holds now. */
|
|
8
|
+
export async function filesIn(root) {
|
|
9
|
+
return collect(await walk(root));
|
|
10
|
+
}
|
|
11
|
+
/** Whether a folder has a script by name, and how to run it. */
|
|
12
|
+
export async function scriptIn(root, name, io) {
|
|
13
|
+
if (!hasScript(await readText(join(root, 'package.json')), name)) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const manager = detectPackageManager((p) => existsSync(join(root, p)), io.env.npm_config_user_agent ?? '');
|
|
17
|
+
return runScript(manager, name);
|
|
18
|
+
}
|
|
19
|
+
/** Run the folder's `build` script when it has one, unless told not to. */
|
|
20
|
+
export async function buildFirst(root, io, skip) {
|
|
21
|
+
if (skip) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const script = await scriptIn(root, 'build', io);
|
|
25
|
+
if (script === null) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const [command, args] = script;
|
|
29
|
+
io.out(`Running ${command} ${args.join(' ')}…`);
|
|
30
|
+
const code = await io.run(command, args, root);
|
|
31
|
+
if (code !== 0) {
|
|
32
|
+
throw new Refused('The build failed; nothing was sent.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export async function hasSource(root) {
|
|
36
|
+
return exists(join(root, 'src', 'theme.css'));
|
|
37
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { Refused } from './commands/context.js';
|
|
5
|
+
import { login, logout, whoami } from './commands/login.js';
|
|
6
|
+
import { check } from './commands/theme/check.js';
|
|
7
|
+
import { dev } from './commands/theme/dev.js';
|
|
8
|
+
import { init } from './commands/theme/init.js';
|
|
9
|
+
import { list } from './commands/theme/list.js';
|
|
10
|
+
import { open } from './commands/theme/open.js';
|
|
11
|
+
import { publish } from './commands/theme/publish.js';
|
|
12
|
+
import { pull } from './commands/theme/pull.js';
|
|
13
|
+
import { push } from './commands/theme/push.js';
|
|
14
|
+
import { ApiFailed } from './lib/api.js';
|
|
15
|
+
import { has, parse, USAGE } from './lib/args.js';
|
|
16
|
+
import { problemLines } from './lib/problems.js';
|
|
17
|
+
const VERSION = '0.3.0';
|
|
18
|
+
const COMMANDS = {
|
|
19
|
+
login,
|
|
20
|
+
logout,
|
|
21
|
+
whoami,
|
|
22
|
+
'theme init': init,
|
|
23
|
+
'theme list': list,
|
|
24
|
+
'theme pull': pull,
|
|
25
|
+
'theme check': check,
|
|
26
|
+
'theme push': push,
|
|
27
|
+
'theme dev': dev,
|
|
28
|
+
'theme publish': publish,
|
|
29
|
+
'theme open': open,
|
|
30
|
+
};
|
|
31
|
+
/** Run the CLI with the given arguments against the given world; answers the exit code. */
|
|
32
|
+
export async function main(argv, io) {
|
|
33
|
+
if (argv.includes('--version') || argv.includes('-v')) {
|
|
34
|
+
io.out(VERSION);
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
const parsed = parse(argv);
|
|
38
|
+
if ('error' in parsed) {
|
|
39
|
+
io.err(parsed.error);
|
|
40
|
+
io.err(USAGE);
|
|
41
|
+
return 2;
|
|
42
|
+
}
|
|
43
|
+
const name = parsed.command.join(' ');
|
|
44
|
+
const command = COMMANDS[name];
|
|
45
|
+
if (command === undefined || has(parsed, 'help')) {
|
|
46
|
+
io.out(USAGE);
|
|
47
|
+
return command === undefined && name !== '' && !has(parsed, 'help')
|
|
48
|
+
? 2
|
|
49
|
+
: 0;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return await command(parsed, io);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error instanceof Refused) {
|
|
56
|
+
io.err(error.message);
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
if (error instanceof ApiFailed) {
|
|
60
|
+
io.err(error.message);
|
|
61
|
+
for (const line of problemLines(error.errors)) {
|
|
62
|
+
io.err(line);
|
|
63
|
+
}
|
|
64
|
+
if (error.status === 401) {
|
|
65
|
+
io.err('Run cabin login.');
|
|
66
|
+
}
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
io.err(error instanceof Error ? error.message : String(error));
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether this file is the script Node was asked to run. npm installs `cabin`
|
|
75
|
+
* as a symlink, and Node names the link in argv but the real file in
|
|
76
|
+
* import.meta.url, so both are taken to their real paths before comparing.
|
|
77
|
+
*/
|
|
78
|
+
function isScript(argv1) {
|
|
79
|
+
if (argv1 === undefined) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return (realpathSync(argv1) === realpathSync(fileURLToPath(import.meta.url)));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const runAsScript = isScript(process.argv[1]);
|
|
90
|
+
if (runAsScript) {
|
|
91
|
+
const { world } = await import('./io/world.js');
|
|
92
|
+
process.exitCode = await main(process.argv.slice(2), world());
|
|
93
|
+
}
|
package/dist/io/fs.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, rm, stat, writeFile, } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { isSkipped, toThemePath } from '../lib/theme-files.js';
|
|
4
|
+
/** Read a text file, or null when it isn't there. */
|
|
5
|
+
export async function readText(path) {
|
|
6
|
+
try {
|
|
7
|
+
return await readFile(path, 'utf8');
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export async function writeText(path, contents, mode) {
|
|
14
|
+
await mkdir(dirname(path), { recursive: true });
|
|
15
|
+
await writeFile(path, contents, mode === undefined ? { encoding: 'utf8' } : { encoding: 'utf8', mode });
|
|
16
|
+
}
|
|
17
|
+
export async function exists(path) {
|
|
18
|
+
try {
|
|
19
|
+
await stat(path);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function remove(path) {
|
|
27
|
+
await rm(path, { force: true });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Every text file under a folder, as [relative path, contents], leaving
|
|
31
|
+
* the folders that are never synced.
|
|
32
|
+
*/
|
|
33
|
+
export async function walk(root) {
|
|
34
|
+
const found = [];
|
|
35
|
+
async function visit(folder) {
|
|
36
|
+
const entries = await readdir(folder, { withFileTypes: true });
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const path = join(folder, entry.name);
|
|
39
|
+
const rel = toThemePath(relative(root, path));
|
|
40
|
+
if (isSkipped(rel)) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (entry.isDirectory()) {
|
|
44
|
+
await visit(path);
|
|
45
|
+
}
|
|
46
|
+
else if (entry.isFile()) {
|
|
47
|
+
found.push([rel, await readFile(path, 'utf8')]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (await exists(root)) {
|
|
52
|
+
await visit(root);
|
|
53
|
+
}
|
|
54
|
+
return found;
|
|
55
|
+
}
|
package/dist/io/io.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/io/world.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { createInterface } from 'node:readline/promises';
|
|
4
|
+
/** The real world, for `cabin` run from a terminal. */
|
|
5
|
+
export function world() {
|
|
6
|
+
let stop = () => { };
|
|
7
|
+
const stopped = new Promise((resolve) => {
|
|
8
|
+
stop = resolve;
|
|
9
|
+
});
|
|
10
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
11
|
+
process.once(signal, () => stop());
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
cwd: process.cwd(),
|
|
15
|
+
home: homedir(),
|
|
16
|
+
env: process.env,
|
|
17
|
+
isTTY: process.stdout.isTTY === true,
|
|
18
|
+
out: (line) => process.stdout.write(`${line}\n`),
|
|
19
|
+
err: (line) => process.stderr.write(`${line}\n`),
|
|
20
|
+
fetch: globalThis.fetch,
|
|
21
|
+
prompt: async (question) => {
|
|
22
|
+
const rl = createInterface({
|
|
23
|
+
input: process.stdin,
|
|
24
|
+
output: process.stdout,
|
|
25
|
+
});
|
|
26
|
+
try {
|
|
27
|
+
return (await rl.question(question)).trim();
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
rl.close();
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
spawn: (command, args, cwd) => nodeSpawn(command, args, {
|
|
34
|
+
cwd,
|
|
35
|
+
stdio: 'inherit',
|
|
36
|
+
shell: process.platform === 'win32',
|
|
37
|
+
}),
|
|
38
|
+
run: (command, args, cwd) => new Promise((resolve) => {
|
|
39
|
+
const child = nodeSpawn(command, args, {
|
|
40
|
+
cwd,
|
|
41
|
+
stdio: 'inherit',
|
|
42
|
+
shell: process.platform === 'win32',
|
|
43
|
+
});
|
|
44
|
+
child.on('exit', (code) => resolve(code ?? 1));
|
|
45
|
+
child.on('error', () => resolve(1));
|
|
46
|
+
}),
|
|
47
|
+
open: async (url) => {
|
|
48
|
+
const [command, args] = process.platform === 'darwin'
|
|
49
|
+
? ['open', [url]]
|
|
50
|
+
: process.platform === 'win32'
|
|
51
|
+
? ['cmd', ['/c', 'start', '', url]]
|
|
52
|
+
: ['xdg-open', [url]];
|
|
53
|
+
nodeSpawn(command, args, {
|
|
54
|
+
stdio: 'ignore',
|
|
55
|
+
detached: true,
|
|
56
|
+
}).unref();
|
|
57
|
+
},
|
|
58
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
59
|
+
now: () => Date.now(),
|
|
60
|
+
stopped,
|
|
61
|
+
};
|
|
62
|
+
}
|
package/dist/lib/api.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** What the server refused, in its one shape. */
|
|
2
|
+
export class ApiFailed extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
errors;
|
|
6
|
+
body;
|
|
7
|
+
constructor(status, code, message, errors = {}, body = null) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.errors = errors;
|
|
12
|
+
this.body = body;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** Read a failure from what the server answered, whatever it was. */
|
|
16
|
+
export function failureOf(status, body) {
|
|
17
|
+
const error = body?.error;
|
|
18
|
+
if (typeof error === 'object' && error !== null) {
|
|
19
|
+
const errors = typeof error.errors === 'object' && error.errors !== null
|
|
20
|
+
? error.errors
|
|
21
|
+
: {};
|
|
22
|
+
return new ApiFailed(status, typeof error.code === 'string' ? error.code : 'http_error', typeof error.message === 'string'
|
|
23
|
+
? error.message
|
|
24
|
+
: `The server answered ${status}.`, errors, error);
|
|
25
|
+
}
|
|
26
|
+
if (status === 0) {
|
|
27
|
+
return new ApiFailed(0, 'unreachable', "The server couldn't be reached.");
|
|
28
|
+
}
|
|
29
|
+
return new ApiFailed(status, 'http_error', `The server answered ${status}.`);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The content API as the CLI calls it: one method per endpoint, each
|
|
33
|
+
* answering the `data` of a 2xx and throwing an `ApiFailed` otherwise.
|
|
34
|
+
*/
|
|
35
|
+
export class ApiClient {
|
|
36
|
+
host;
|
|
37
|
+
token;
|
|
38
|
+
fetchFn;
|
|
39
|
+
constructor(host, token, fetchFn) {
|
|
40
|
+
this.host = host;
|
|
41
|
+
this.token = token;
|
|
42
|
+
this.fetchFn = fetchFn;
|
|
43
|
+
}
|
|
44
|
+
token_() {
|
|
45
|
+
return this.call('GET', '/v1/token');
|
|
46
|
+
}
|
|
47
|
+
themes() {
|
|
48
|
+
return this.call('GET', '/v1/themes');
|
|
49
|
+
}
|
|
50
|
+
sources() {
|
|
51
|
+
return this.call('GET', '/v1/themes/sources');
|
|
52
|
+
}
|
|
53
|
+
theme(key) {
|
|
54
|
+
return this.call('GET', `/v1/themes/${key}`);
|
|
55
|
+
}
|
|
56
|
+
create(key, name, from) {
|
|
57
|
+
return this.call('POST', '/v1/themes', { key, name, from });
|
|
58
|
+
}
|
|
59
|
+
save(key, files, note, baseVersion) {
|
|
60
|
+
return this.call('PUT', `/v1/themes/${key}`, {
|
|
61
|
+
files,
|
|
62
|
+
note,
|
|
63
|
+
base_version: baseVersion,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
check(key, files) {
|
|
67
|
+
return this.call('POST', `/v1/themes/${key}/check`, { files });
|
|
68
|
+
}
|
|
69
|
+
draft(key, files, path, token) {
|
|
70
|
+
return this.call('POST', `/v1/themes/${key}/drafts`, {
|
|
71
|
+
files,
|
|
72
|
+
path,
|
|
73
|
+
token,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
choose(key) {
|
|
77
|
+
return this.call('POST', `/v1/themes/${key}/choose`);
|
|
78
|
+
}
|
|
79
|
+
version(key, id) {
|
|
80
|
+
return this.call('GET', `/v1/themes/${key}/versions/${id}`);
|
|
81
|
+
}
|
|
82
|
+
async call(method, path, body) {
|
|
83
|
+
let response;
|
|
84
|
+
try {
|
|
85
|
+
const init = {
|
|
86
|
+
method,
|
|
87
|
+
headers: {
|
|
88
|
+
Authorization: `Bearer ${this.token}`,
|
|
89
|
+
Accept: 'application/json',
|
|
90
|
+
...(body === undefined
|
|
91
|
+
? {}
|
|
92
|
+
: { 'Content-Type': 'application/json' }),
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
if (body !== undefined) {
|
|
96
|
+
init.body = JSON.stringify(body);
|
|
97
|
+
}
|
|
98
|
+
response = await this.fetchFn(`${this.host}${path}`, init);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
throw failureOf(0, null);
|
|
102
|
+
}
|
|
103
|
+
const text = await response.text();
|
|
104
|
+
let parsed = null;
|
|
105
|
+
try {
|
|
106
|
+
parsed = text === '' ? null : JSON.parse(text);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
parsed = null;
|
|
110
|
+
}
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
throw failureOf(response.status, parsed);
|
|
113
|
+
}
|
|
114
|
+
return parsed.data;
|
|
115
|
+
}
|
|
116
|
+
}
|
package/dist/lib/args.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
const OPTIONS = {
|
|
3
|
+
token: { type: 'string' },
|
|
4
|
+
site: { type: 'string' },
|
|
5
|
+
theme: { type: 'string' },
|
|
6
|
+
from: { type: 'string' },
|
|
7
|
+
name: { type: 'string' },
|
|
8
|
+
key: { type: 'string' },
|
|
9
|
+
note: { type: 'string' },
|
|
10
|
+
path: { type: 'string' },
|
|
11
|
+
version: { type: 'string' },
|
|
12
|
+
'no-tailwind': { type: 'boolean' },
|
|
13
|
+
'no-build': { type: 'boolean' },
|
|
14
|
+
force: { type: 'boolean' },
|
|
15
|
+
check: { type: 'boolean' },
|
|
16
|
+
all: { type: 'boolean' },
|
|
17
|
+
help: { type: 'boolean', short: 'h' },
|
|
18
|
+
};
|
|
19
|
+
/** Read the command words and flags from argv. */
|
|
20
|
+
export function parse(argv) {
|
|
21
|
+
try {
|
|
22
|
+
const { values, positionals } = parseArgs({
|
|
23
|
+
args: argv,
|
|
24
|
+
options: OPTIONS,
|
|
25
|
+
allowPositionals: true,
|
|
26
|
+
strict: true,
|
|
27
|
+
});
|
|
28
|
+
const words = [];
|
|
29
|
+
const rest = [];
|
|
30
|
+
for (const positional of positionals) {
|
|
31
|
+
if (rest.length === 0 &&
|
|
32
|
+
words.length < 2 &&
|
|
33
|
+
/^[a-z]+$/.test(positional) &&
|
|
34
|
+
(words.length === 0 || words[0] === 'theme')) {
|
|
35
|
+
words.push(positional);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
rest.push(positional);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
command: words,
|
|
43
|
+
flags: values,
|
|
44
|
+
positionals: rest,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return {
|
|
49
|
+
error: error instanceof Error ? error.message : String(error),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function flag(parsed, name) {
|
|
54
|
+
const value = parsed.flags[name];
|
|
55
|
+
return typeof value === 'string' && value !== '' ? value : null;
|
|
56
|
+
}
|
|
57
|
+
export function has(parsed, name) {
|
|
58
|
+
return parsed.flags[name] === true;
|
|
59
|
+
}
|
|
60
|
+
export const USAGE = `cabin — develop a Cabin site theme locally
|
|
61
|
+
|
|
62
|
+
cabin login [--token cabin_…]
|
|
63
|
+
cabin logout [--site handle]
|
|
64
|
+
cabin whoami
|
|
65
|
+
|
|
66
|
+
cabin theme init [dir] [--from blank|birch] [--name NAME] [--key KEY] [--no-tailwind] [--site handle]
|
|
67
|
+
cabin theme list
|
|
68
|
+
cabin theme pull [--theme KEY] [--version ID] [--all]
|
|
69
|
+
cabin theme check [--no-build]
|
|
70
|
+
cabin theme push [--note NOTE] [--force] [--check] [--no-build]
|
|
71
|
+
cabin theme dev [--path /] [--no-build]
|
|
72
|
+
cabin theme publish
|
|
73
|
+
cabin theme open [--site]
|
|
74
|
+
|
|
75
|
+
Signs in to https://app.cabincms.com.
|
|
76
|
+
Set CABIN_HOST to use another install.
|
|
77
|
+
|
|
78
|
+
Exit codes: 0 done, 1 refused or problems found, 2 usage.`;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The 0-based index a typed answer picks from a numbered list, or null. */
|
|
2
|
+
export function parseChoice(answer, count) {
|
|
3
|
+
const trimmed = answer.trim();
|
|
4
|
+
if (!/^\d+$/.test(trimmed)) {
|
|
5
|
+
return null;
|
|
6
|
+
}
|
|
7
|
+
const index = Number(trimmed) - 1;
|
|
8
|
+
return index >= 0 && index < count ? index : null;
|
|
9
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export const CONFIG_FILE = 'cabin.json';
|
|
2
|
+
export const STATE_FILE = '.cabin/state.json';
|
|
3
|
+
/** Cabin's hosted admin, which `cabin login` signs in to when no host is named. */
|
|
4
|
+
export const DEFAULT_HOST = 'https://app.cabincms.com';
|
|
5
|
+
/** A host as the config keeps it: scheme and host, no path, no trailing slash. */
|
|
6
|
+
export function normalizeHost(host) {
|
|
7
|
+
const trimmed = host.trim();
|
|
8
|
+
if (trimmed === '') {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const url = new URL(/^https?:\/\//.test(trimmed) ? trimmed : `https://${trimmed}`);
|
|
13
|
+
return `${url.protocol}//${url.host}`;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Read a project config, or say what is wrong with it. */
|
|
20
|
+
export function parseConfig(json) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = JSON.parse(json);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return { error: `${CONFIG_FILE} isn't JSON.` };
|
|
27
|
+
}
|
|
28
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
29
|
+
return {
|
|
30
|
+
error: `${CONFIG_FILE} should hold an object with host, site and theme.`,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const config = parsed;
|
|
34
|
+
const host = typeof config.host === 'string' ? normalizeHost(config.host) : null;
|
|
35
|
+
if (host === null) {
|
|
36
|
+
return {
|
|
37
|
+
error: `${CONFIG_FILE} needs a host, such as https://app.cabincms.com.`,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (typeof config.site !== 'string' || config.site === '') {
|
|
41
|
+
return {
|
|
42
|
+
error: `${CONFIG_FILE} needs the site's handle under "site".`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (typeof config.theme !== 'string' || config.theme === '') {
|
|
46
|
+
return { error: `${CONFIG_FILE} needs the theme's key under "theme".` };
|
|
47
|
+
}
|
|
48
|
+
return { host, site: config.site, theme: config.theme };
|
|
49
|
+
}
|
|
50
|
+
export function serializeConfig(config) {
|
|
51
|
+
return `${JSON.stringify(config, null, 4)}\n`;
|
|
52
|
+
}
|
|
53
|
+
/** Read the remembered state; anything unreadable reads as nothing remembered. */
|
|
54
|
+
export function parseState(json) {
|
|
55
|
+
const none = { versionId: null, draftToken: null };
|
|
56
|
+
if (json === null) {
|
|
57
|
+
return none;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(json);
|
|
61
|
+
return {
|
|
62
|
+
versionId: typeof parsed.versionId === 'number' ? parsed.versionId : null,
|
|
63
|
+
draftToken: typeof parsed.draftToken === 'string' &&
|
|
64
|
+
/^[A-Za-z0-9]{32}$/.test(parsed.draftToken)
|
|
65
|
+
? parsed.draftToken
|
|
66
|
+
: null,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return none;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export function serializeState(state) {
|
|
74
|
+
return `${JSON.stringify(state, null, 4)}\n`;
|
|
75
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Where the credentials file lives: the environment decides, else the home folder. */
|
|
2
|
+
export function credentialsDir(env, home) {
|
|
3
|
+
if (env.CABIN_CONFIG_DIR) {
|
|
4
|
+
return env.CABIN_CONFIG_DIR;
|
|
5
|
+
}
|
|
6
|
+
if (env.XDG_CONFIG_HOME) {
|
|
7
|
+
return `${env.XDG_CONFIG_HOME}/cabin`;
|
|
8
|
+
}
|
|
9
|
+
return `${home}/.config/cabin`;
|
|
10
|
+
}
|
|
11
|
+
export function parseCredentials(json) {
|
|
12
|
+
if (json === null) {
|
|
13
|
+
return { hosts: {} };
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(json);
|
|
17
|
+
return {
|
|
18
|
+
hosts: typeof parsed.hosts === 'object' && parsed.hosts !== null
|
|
19
|
+
? parsed.hosts
|
|
20
|
+
: {},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return { hosts: {} };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function serializeCredentials(credentials) {
|
|
28
|
+
return `${JSON.stringify(credentials, null, 4)}\n`;
|
|
29
|
+
}
|
|
30
|
+
export function pick(credentials, host, site) {
|
|
31
|
+
return credentials.hosts[host]?.[site] ?? null;
|
|
32
|
+
}
|
|
33
|
+
export function put(credentials, host, site, credential) {
|
|
34
|
+
return {
|
|
35
|
+
hosts: {
|
|
36
|
+
...credentials.hosts,
|
|
37
|
+
[host]: { ...(credentials.hosts[host] ?? {}), [site]: credential },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export function drop(credentials, host, site) {
|
|
42
|
+
const forHost = { ...(credentials.hosts[host] ?? {}) };
|
|
43
|
+
delete forHost[site];
|
|
44
|
+
const hosts = { ...credentials.hosts };
|
|
45
|
+
if (Object.keys(forHost).length === 0) {
|
|
46
|
+
delete hosts[host];
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
hosts[host] = forHost;
|
|
50
|
+
}
|
|
51
|
+
return { hosts };
|
|
52
|
+
}
|
|
53
|
+
/** Whether a string looks like one of Cabin's tokens. */
|
|
54
|
+
export function looksLikeToken(token) {
|
|
55
|
+
return /^cabin_[A-Za-z0-9]{40}$/.test(token.trim());
|
|
56
|
+
}
|
|
57
|
+
/** The handles of the sites a token is kept for on a host, in order. */
|
|
58
|
+
export function sitesOn(credentials, host) {
|
|
59
|
+
return Object.keys(credentials.hosts[host] ?? {}).sort();
|
|
60
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
/** Colour a line when the output is a terminal that wants it. */
|
|
3
|
+
export function paint(text, color, wants) {
|
|
4
|
+
if (!wants) {
|
|
5
|
+
return text;
|
|
6
|
+
}
|
|
7
|
+
return styleText(color === 'dim' ? 'dim' : color, text);
|
|
8
|
+
}
|
|
9
|
+
/** Whether to colour output: a terminal, and nothing saying not to. */
|
|
10
|
+
export function wantsColor(isTTY, env) {
|
|
11
|
+
return isTTY && !env.NO_COLOR && env.TERM !== 'dumb';
|
|
12
|
+
}
|
|
13
|
+
/** The line that says a sync landed. */
|
|
14
|
+
export function syncedLine(at, count) {
|
|
15
|
+
const time = at.toTimeString().slice(0, 8);
|
|
16
|
+
return `synced ${time} (${count} ${count === 1 ? 'file' : 'files'})`;
|
|
17
|
+
}
|
|
18
|
+
/** A list of themes as `theme list` prints it. */
|
|
19
|
+
export function themeTable(rows) {
|
|
20
|
+
if (rows.length === 0) {
|
|
21
|
+
return [
|
|
22
|
+
"No themes of this site's own yet. Make one with cabin theme init.",
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
const width = Math.max(...rows.map((r) => r.key.length));
|
|
26
|
+
return rows.map((row) => {
|
|
27
|
+
const star = row.chosen ? '*' : ' ';
|
|
28
|
+
const version = row.version === null ? 'no version' : `v${row.version}`;
|
|
29
|
+
const by = row.by === null ? '' : ` by ${row.by}`;
|
|
30
|
+
const at = row.at === null ? '' : ` ${row.at.slice(0, 10)}`;
|
|
31
|
+
return `${star} ${row.key.padEnd(width)} ${row.name} ${version}${by}${at}`;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Problems as the server keys them, said one to a line the way the admin's
|
|
3
|
+
* Theme files screen says them: `templates/home.twig:12 message`, and
|
|
4
|
+
* `theme: message` for a problem with the whole set.
|
|
5
|
+
*/
|
|
6
|
+
export function problemLines(errors) {
|
|
7
|
+
const lines = [];
|
|
8
|
+
for (const [key, messages] of Object.entries(errors)) {
|
|
9
|
+
for (const message of messages) {
|
|
10
|
+
lines.push(problemLine(key, message));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return lines;
|
|
14
|
+
}
|
|
15
|
+
export function problemLine(key, message) {
|
|
16
|
+
if (key === 'files') {
|
|
17
|
+
return `theme: ${message}`;
|
|
18
|
+
}
|
|
19
|
+
if (key.startsWith('files.')) {
|
|
20
|
+
const path = key.slice(6);
|
|
21
|
+
const line = /^Line (\d+): (.*)$/s.exec(message);
|
|
22
|
+
return line ? `${path}:${line[1]} ${line[2]}` : `${path} ${message}`;
|
|
23
|
+
}
|
|
24
|
+
return `${key}: ${message}`;
|
|
25
|
+
}
|