@politelabs/cabin-cli 0.1.0 → 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/README.md CHANGED
@@ -13,15 +13,17 @@ Node 22.12 or newer. Nothing else is installed with it.
13
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
14
 
15
15
  ```
16
- cabin login --host https://app.cabincms.com
16
+ cabin login
17
17
  ```
18
18
 
19
+ That signs in to https://app.cabincms.com. For a self-hosted install, set `CABIN_HOST=https://cabin.example.com`. Inside a theme's folder, the host in `cabin.json` is used.
20
+
19
21
  The token is kept in `~/.config/cabin/credentials.json`, by host and site, never in the theme's folder.
20
22
 
21
23
  ## A theme
22
24
 
23
25
  ```
24
- cabin theme init riverside --from birch # a copy of Birch; --from blank for the least that compiles
26
+ cabin theme init --from birch # asks which site and what to call the theme
25
27
  cd riverside
26
28
  pnpm install # Tailwind, unless --no-tailwind
27
29
  cabin theme dev # watches the folder, prints the preview link
@@ -29,14 +31,14 @@ cabin theme push --note "Tighter header" # saves a version
29
31
  cabin theme publish # draws the site with it
30
32
  ```
31
33
 
32
- `cabin theme init` 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.
34
+ `cabin theme init` asks which of the sites you're signed in to the theme is for, 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.
33
35
 
34
36
  A theme is `theme.json`, `theme.css`, templates under `templates/` and patterns under `patterns/`. 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`; `cabin theme dev` runs `dev` beside the watcher, and `push` runs `build` first. A folder with no scripts is synced as it is.
35
37
 
36
38
  | Command | What it does |
37
39
  | --- | --- |
38
- | `cabin login [--host] [--token]` | Keep a token for a host and site. `logout` forgets it; `whoami` says who it is. |
39
- | `cabin theme init <dir> [--from blank\|birch] [--name] [--key] [--no-tailwind]` | Make a theme on the site and write it into a folder. |
40
+ | `cabin login [--token]` | Keep a token for a host and site. The host defaults to https://app.cabincms.com. `logout` forgets it; `whoami` says who it is. |
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. |
40
42
  | `cabin theme list` | The site's themes, with a star on the one the site is drawn with. |
41
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`. |
42
44
  | `cabin theme check [--no-build]` | Run every check a save runs, writing nothing. |
@@ -1,8 +1,7 @@
1
1
  import { join } from 'node:path';
2
2
  import { readText, writeText } from '../io/fs.js';
3
3
  import { ApiClient } from '../lib/api.js';
4
- import { flag } from '../lib/args.js';
5
- import { CONFIG_FILE, normalizeHost, parseConfig, parseState, STATE_FILE, serializeState, } from '../lib/config.js';
4
+ import { CONFIG_FILE, DEFAULT_HOST, normalizeHost, parseConfig, parseState, STATE_FILE, serializeState, } from '../lib/config.js';
6
5
  import { credentialsDir, parseCredentials, pick } from '../lib/credentials.js';
7
6
  export class Refused extends Error {
8
7
  }
@@ -35,12 +34,12 @@ export async function tokenFor(io, host, site) {
35
34
  const credentials = parseCredentials(await readText(join(credentialsDir(io.env, io.home), 'credentials.json')));
36
35
  const kept = pick(credentials, host, site);
37
36
  if (kept === null) {
38
- throw new Refused(`No token is kept for ${site} on ${host}. Run cabin login --host ${host}.`);
37
+ throw new Refused(`No token is kept for ${site} on ${host}. Run cabin login${host === DEFAULT_HOST ? '' : ` with CABIN_HOST=${host}`}.`);
39
38
  }
40
39
  return kept.token;
41
40
  }
42
- /** The host a command names, from its flag or the environment. */
43
- export function hostFrom(parsed, io) {
44
- const given = flag(parsed, 'host') ?? io.env.CABIN_HOST ?? null;
41
+ /** The host named by CABIN_HOST, if any. */
42
+ export function hostFrom(io) {
43
+ const given = io.env.CABIN_HOST ?? null;
45
44
  return given === null ? null : normalizeHost(given);
46
45
  }
@@ -2,25 +2,19 @@ import { join } from 'node:path';
2
2
  import { readText, writeText } from '../io/fs.js';
3
3
  import { ApiClient, ApiFailed } from '../lib/api.js';
4
4
  import { flag } from '../lib/args.js';
5
- import { CONFIG_FILE, normalizeHost, parseConfig } from '../lib/config.js';
5
+ import { CONFIG_FILE, DEFAULT_HOST, parseConfig } from '../lib/config.js';
6
6
  import { credentialsDir, drop, looksLikeToken, parseCredentials, put, serializeCredentials, } from '../lib/credentials.js';
7
7
  import { hostFrom, Refused, tokenFor } from './context.js';
8
- async function hostOrAsk(parsed, io) {
8
+ /** The host to sign in to: the flag or CABIN_HOST, then cabin.json, then Cabin's own. */
9
+ async function loginHost(io) {
9
10
  const fromProject = parseConfig((await readText(join(io.cwd, CONFIG_FILE))) ?? '');
10
- const host = hostFrom(parsed, io) ??
11
- ('error' in fromProject ? null : fromProject.host);
12
- if (host !== null) {
13
- return host;
14
- }
15
- const typed = normalizeHost(await io.prompt("The site's admin address (such as https://app.cabincms.com): "));
16
- if (typed === null) {
17
- throw new Refused("Give the admin's address, such as https://app.cabincms.com.");
18
- }
19
- return typed;
11
+ return (hostFrom(io) ??
12
+ ('error' in fromProject ? null : fromProject.host) ??
13
+ DEFAULT_HOST);
20
14
  }
21
15
  /** Keep a token for a host and the site it names. */
22
16
  export async function login(parsed, io) {
23
- const host = await hostOrAsk(parsed, io);
17
+ const host = await loginHost(io);
24
18
  let token = flag(parsed, 'token') ?? io.env.CABIN_TOKEN ?? '';
25
19
  if (token === '') {
26
20
  token = (await io.prompt('Paste a management token from Site settings › API tokens: ', true)).trim();
@@ -54,12 +48,11 @@ export async function login(parsed, io) {
54
48
  /** Forget a token. */
55
49
  export async function logout(parsed, io) {
56
50
  const fromProject = parseConfig((await readText(join(io.cwd, CONFIG_FILE))) ?? '');
57
- const host = hostFrom(parsed, io) ??
58
- ('error' in fromProject ? null : fromProject.host);
51
+ const host = hostFrom(io) ?? ('error' in fromProject ? null : fromProject.host);
59
52
  const site = flag(parsed, 'site') ??
60
53
  ('error' in fromProject ? null : fromProject.site);
61
54
  if (host === null || site === null) {
62
- throw new Refused("Say which to forget: cabin logout --host URL --site handle, or run it inside a theme's folder.");
55
+ throw new Refused("Say which to forget: cabin logout --site handle, or run it inside a theme's folder.");
63
56
  }
64
57
  const file = join(credentialsDir(io.env, io.home), 'credentials.json');
65
58
  await writeText(file, serializeCredentials(drop(parseCredentials(await readText(file)), host, site)), 0o600);
@@ -69,12 +62,11 @@ export async function logout(parsed, io) {
69
62
  /** Say who the kept token is. */
70
63
  export async function whoami(parsed, io) {
71
64
  const fromProject = parseConfig((await readText(join(io.cwd, CONFIG_FILE))) ?? '');
72
- const host = hostFrom(parsed, io) ??
73
- ('error' in fromProject ? null : fromProject.host);
65
+ const host = hostFrom(io) ?? ('error' in fromProject ? null : fromProject.host);
74
66
  const site = flag(parsed, 'site') ??
75
67
  ('error' in fromProject ? null : fromProject.site);
76
68
  if (host === null || site === null) {
77
- throw new Refused("Say which: cabin whoami --host URL --site handle, or run it inside a theme's folder.");
69
+ throw new Refused("Say which: cabin whoami --site handle, or run it inside a theme's folder.");
78
70
  }
79
71
  const info = await new ApiClient(host, await tokenFor(io, host, site), io.fetch).token_();
80
72
  io.out(`${info.site.name} (${info.site.handle}) on ${host}${info.site.url ? `, at ${info.site.url}` : ''}`);
@@ -1,8 +1,10 @@
1
1
  import { join, resolve } from 'node:path';
2
- import { exists, writeText } from '../../io/fs.js';
2
+ import { exists, readText, writeText } from '../../io/fs.js';
3
3
  import { ApiClient, ApiFailed } from '../../lib/api.js';
4
4
  import { flag, has } from '../../lib/args.js';
5
- import { CONFIG_FILE, serializeState } from '../../lib/config.js';
5
+ import { parseChoice } from '../../lib/choose.js';
6
+ import { CONFIG_FILE, DEFAULT_HOST, serializeState } from '../../lib/config.js';
7
+ import { credentialsDir, parseCredentials, sitesOn, } from '../../lib/credentials.js';
6
8
  import { problemLines } from '../../lib/problems.js';
7
9
  import { scaffold } from '../../lib/scaffold.js';
8
10
  import { hostFrom, Refused, tokenFor } from '../context.js';
@@ -16,26 +18,87 @@ export function keyFrom(name) {
16
18
  .slice(0, 63);
17
19
  return /^[a-z]/.test(key) ? key : `theme-${key}`.replace(/-+$/, '');
18
20
  }
21
+ /**
22
+ * The site to make the theme on: the flag or CABIN_SITE, else one of the
23
+ * sites a token is kept for on the host, asked for when there are several.
24
+ * The API names a token's site but lists no others, so the list is the
25
+ * kept tokens, each asked for its site's name.
26
+ */
27
+ async function chooseSite(parsed, io, host) {
28
+ const given = flag(parsed, 'site') ?? io.env.CABIN_SITE ?? null;
29
+ if (given !== null) {
30
+ return given;
31
+ }
32
+ const credentials = parseCredentials(await readText(join(credentialsDir(io.env, io.home), 'credentials.json')));
33
+ const handles = sitesOn(credentials, host);
34
+ if (handles.length === 0) {
35
+ throw new Refused(`You aren't signed in to a site on ${host}. Run cabin login first.`);
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);
52
+ const [only] = sites;
53
+ if (only === undefined) {
54
+ throw new Refused(`No kept token is accepted by ${host}.`);
55
+ }
56
+ if (sites.length === 1) {
57
+ io.out(`Site: ${only.name} (${only.handle})`);
58
+ return only.handle;
59
+ }
60
+ io.out('Which site is the theme for?');
61
+ sites.forEach((site, index) => {
62
+ io.out(` ${index + 1}. ${site.name} (${site.handle})`);
63
+ });
64
+ const index = parseChoice(await io.prompt(`Site (1-${sites.length}): `), sites.length);
65
+ const chosen = index === null ? undefined : sites[index];
66
+ if (chosen === undefined) {
67
+ throw new Refused(`Pick a number from 1 to ${sites.length}.`);
68
+ }
69
+ return chosen.handle;
70
+ }
71
+ /** The theme's name: the flag, else asked for, offering the folder's name. */
72
+ async function chooseName(parsed, io, dir) {
73
+ const given = flag(parsed, 'name');
74
+ if (given !== null) {
75
+ return given;
76
+ }
77
+ const fromDir = dir?.split(/[\\/]/).filter(Boolean).at(-1);
78
+ const typed = (await io.prompt(`What should the theme be called?${fromDir ? ` (${fromDir})` : ''} `)).trim();
79
+ const name = typed === '' ? fromDir : typed;
80
+ if (name === undefined || name === '') {
81
+ throw new Refused('Give the theme a name.');
82
+ }
83
+ return name;
84
+ }
19
85
  /** Make a theme on the site and write it into a folder. */
20
86
  export async function init(parsed, io) {
21
- const dir = parsed.positionals[0];
22
- if (dir === undefined) {
23
- throw new Refused('Say where to put the theme: cabin theme init <dir>.');
87
+ const given = parsed.positionals[0];
88
+ if (given !== undefined &&
89
+ (await exists(join(resolve(io.cwd, given), CONFIG_FILE)))) {
90
+ throw new Refused(`${given} already holds a theme (${CONFIG_FILE}). Use cabin theme pull there instead.`);
24
91
  }
92
+ const host = hostFrom(io) ?? DEFAULT_HOST;
93
+ const site = await chooseSite(parsed, io, host);
94
+ const api = new ApiClient(host, await tokenFor(io, host, site), io.fetch);
95
+ const name = await chooseName(parsed, io, given);
96
+ const key = flag(parsed, 'key') ?? keyFrom(name);
97
+ const dir = given ?? key;
25
98
  const root = resolve(io.cwd, dir);
26
- if (await exists(join(root, CONFIG_FILE))) {
99
+ if (given === undefined && (await exists(join(root, CONFIG_FILE)))) {
27
100
  throw new Refused(`${dir} already holds a theme (${CONFIG_FILE}). Use cabin theme pull there instead.`);
28
101
  }
29
- const host = hostFrom(parsed, io);
30
- const site = flag(parsed, 'site') ?? io.env.CABIN_SITE ?? null;
31
- if (host === null || site === null) {
32
- throw new Refused('Say which site: cabin theme init <dir> --host URL --site handle (or CABIN_HOST and CABIN_SITE).');
33
- }
34
- const api = new ApiClient(host, await tokenFor(io, host, site), io.fetch);
35
- const name = flag(parsed, 'name') ??
36
- dir.split(/[\\/]/).filter(Boolean).at(-1) ??
37
- 'Theme';
38
- const key = flag(parsed, 'key') ?? keyFrom(name);
39
102
  const from = flag(parsed, 'from') ?? 'blank';
40
103
  const sources = await api.sources();
41
104
  if (!sources.some((source) => source.key === from)) {
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env node
2
+ import { realpathSync } from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
2
4
  import { Refused } from './commands/context.js';
3
5
  import { login, logout, whoami } from './commands/login.js';
4
6
  import { check } from './commands/theme/check.js';
@@ -12,7 +14,7 @@ import { push } from './commands/theme/push.js';
12
14
  import { ApiFailed } from './lib/api.js';
13
15
  import { has, parse, USAGE } from './lib/args.js';
14
16
  import { problemLines } from './lib/problems.js';
15
- const VERSION = '0.1.0';
17
+ const VERSION = '0.3.0';
16
18
  const COMMANDS = {
17
19
  login,
18
20
  logout,
@@ -68,9 +70,23 @@ export async function main(argv, io) {
68
70
  return 1;
69
71
  }
70
72
  }
71
- const runAsScript = process.argv[1] !== undefined &&
72
- /index\.js$/.test(process.argv[1]) &&
73
- import.meta.url.endsWith(process.argv[1].replace(/\\/g, '/'));
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]);
74
90
  if (runAsScript) {
75
91
  const { world } = await import('./io/world.js');
76
92
  process.exitCode = await main(process.argv.slice(2), world());
package/dist/lib/args.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { parseArgs } from 'node:util';
2
2
  const OPTIONS = {
3
- host: { type: 'string' },
4
3
  token: { type: 'string' },
5
4
  site: { type: 'string' },
6
5
  theme: { type: 'string' },
@@ -60,11 +59,11 @@ export function has(parsed, name) {
60
59
  }
61
60
  export const USAGE = `cabin — develop a Cabin site theme locally
62
61
 
63
- cabin login [--host URL] [--token cabin_…]
64
- cabin logout [--host URL] [--site handle]
62
+ cabin login [--token cabin_…]
63
+ cabin logout [--site handle]
65
64
  cabin whoami
66
65
 
67
- cabin theme init <dir> [--from blank|birch] [--name NAME] [--key KEY] [--no-tailwind] [--host URL] [--site handle]
66
+ cabin theme init [dir] [--from blank|birch] [--name NAME] [--key KEY] [--no-tailwind] [--site handle]
68
67
  cabin theme list
69
68
  cabin theme pull [--theme KEY] [--version ID] [--all]
70
69
  cabin theme check [--no-build]
@@ -73,4 +72,7 @@ export const USAGE = `cabin — develop a Cabin site theme locally
73
72
  cabin theme publish
74
73
  cabin theme open [--site]
75
74
 
75
+ Signs in to https://app.cabincms.com.
76
+ Set CABIN_HOST to use another install.
77
+
76
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
+ }
@@ -1,5 +1,7 @@
1
1
  export const CONFIG_FILE = 'cabin.json';
2
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';
3
5
  /** A host as the config keeps it: scheme and host, no path, no trailing slash. */
4
6
  export function normalizeHost(host) {
5
7
  const trimmed = host.trim();
@@ -54,3 +54,7 @@ export function drop(credentials, host, site) {
54
54
  export function looksLikeToken(token) {
55
55
  return /^cabin_[A-Za-z0-9]{40}$/.test(token.trim());
56
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
+ }
package/package.json CHANGED
@@ -1,39 +1,37 @@
1
1
  {
2
- "name": "@politelabs/cabin-cli",
3
- "version": "0.1.0",
4
- "description": "Develop a Cabin site theme locally: init, dev, push, pull, check, publish.",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "https://github.com/PoliteLabs/cabin-cli.git"
9
- },
10
- "type": "module",
11
- "bin": {
12
- "cabin": "dist/index.js"
13
- },
14
- "files": [
15
- "dist",
16
- "README.md"
17
- ],
18
- "engines": {
19
- "node": ">=22.12"
20
- },
21
- "publishConfig": {
22
- "access": "public"
23
- },
24
- "scripts": {
25
- "build": "tsc -p tsconfig.build.json",
26
- "check": "biome check .",
27
- "check:fix": "biome check --write .",
28
- "types:check": "tsc --noEmit",
29
- "test": "vitest run",
30
- "prepublishOnly": "pnpm build"
31
- },
32
- "devDependencies": {
33
- "@biomejs/biome": "2.2.4",
34
- "@types/node": "^22.13.5",
35
- "typescript": "^5.9.3",
36
- "vitest": "^3.2.4"
37
- },
38
- "packageManager": "pnpm@10.18.1"
39
- }
2
+ "name": "@politelabs/cabin-cli",
3
+ "version": "0.3.0",
4
+ "description": "Develop a Cabin site theme locally: init, dev, push, pull, check, publish.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/PoliteLabs/cabin-cli.git"
9
+ },
10
+ "type": "module",
11
+ "bin": {
12
+ "cabin": "dist/index.js"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "engines": {
19
+ "node": ">=22.12"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "devDependencies": {
25
+ "@biomejs/biome": "2.2.4",
26
+ "@types/node": "^22.13.5",
27
+ "typescript": "^5.9.3",
28
+ "vitest": "^3.2.4"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc -p tsconfig.build.json",
32
+ "check": "biome check .",
33
+ "check:fix": "biome check --write .",
34
+ "types:check": "tsc --noEmit",
35
+ "test": "vitest run"
36
+ }
37
+ }