@politelabs/cabin-cli 0.0.0-stage → 0.1.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.
@@ -0,0 +1,66 @@
1
+ /**
2
+ * What a theme may hold, as Cabin's `ThemeFileRules` says it: the manifest,
3
+ * the stylesheet, templates under `templates/` at any depth, and patterns
4
+ * under `patterns/`, named in lower case. A copy of the server's rule: the
5
+ * server is the judge, and this only says what is worth sending.
6
+ */
7
+ export const THEME_PATH = /^(theme\.json|theme\.css|templates\/[a-z0-9_-]+(\/[a-z0-9_-]+)*\.twig|patterns\/[a-z0-9-]+\.json)$/;
8
+ /** The files every theme has to hold. */
9
+ export const REQUIRED = [
10
+ 'theme.json',
11
+ 'theme.css',
12
+ 'templates/layout.twig',
13
+ 'templates/home.twig',
14
+ 'templates/entry.twig',
15
+ 'templates/page.twig',
16
+ 'templates/list.twig',
17
+ 'templates/not-found.twig',
18
+ ];
19
+ /** Whether a path, relative to the theme's folder, is one a theme may hold. */
20
+ export function isThemePath(path) {
21
+ return THEME_PATH.test(path);
22
+ }
23
+ /** A relative path as the theme names it: forward slashes, no leading `./`. */
24
+ export function toThemePath(relative) {
25
+ return relative.replace(/\\/g, '/').replace(/^\.\//, '');
26
+ }
27
+ /**
28
+ * The theme files among what a folder holds: what the rule allows, with
29
+ * everything else (sources, `node_modules`, `.cabin`, dotfiles) left out.
30
+ */
31
+ export function collect(entries) {
32
+ const files = {};
33
+ for (const [relative, contents] of entries) {
34
+ const path = toThemePath(relative);
35
+ if (isThemePath(path)) {
36
+ files[path] = contents;
37
+ }
38
+ }
39
+ return Object.fromEntries(Object.entries(files).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
40
+ }
41
+ /** The required files a set lacks. */
42
+ export function missingRequired(files) {
43
+ return REQUIRED.filter((path) => !(path in files));
44
+ }
45
+ /**
46
+ * Whether a file is built from a source in the folder, so a pull
47
+ * shouldn't overwrite it: `theme.css` when `src/theme.css` exists.
48
+ */
49
+ export function isGenerated(path, folderHas) {
50
+ return path === 'theme.css' && folderHas('src/theme.css');
51
+ }
52
+ /** The folders a watcher and a walk never look inside. */
53
+ export const SKIPPED_FOLDERS = [
54
+ 'node_modules',
55
+ '.git',
56
+ '.cabin',
57
+ 'src',
58
+ 'dist',
59
+ ];
60
+ /** Whether a relative path is inside a folder that is never synced. */
61
+ export function isSkipped(relative) {
62
+ const [first] = toThemePath(relative).split('/');
63
+ return (first === undefined ||
64
+ SKIPPED_FOLDERS.includes(first) ||
65
+ first.startsWith('.'));
66
+ }
@@ -0,0 +1,34 @@
1
+ export function standing(rememberedId, currentId) {
2
+ if (rememberedId === null || currentId === null) {
3
+ return 'unknown';
4
+ }
5
+ return rememberedId === currentId ? 'same' : 'behind';
6
+ }
7
+ /** What to say when a push was refused because someone saved first. */
8
+ export function conflictLine(current) {
9
+ if (current === null) {
10
+ return 'The theme was saved on the site after the version you pulled. Run cabin theme pull, or push --force to replace it.';
11
+ }
12
+ const who = current.by === null ? 'Someone' : current.by;
13
+ const when = current.createdAt === null ? '' : ` at ${current.createdAt}`;
14
+ return `${who} saved version ${current.number}${when}, after the one you pulled. Run cabin theme pull, or push --force to replace it.`;
15
+ }
16
+ /** The version a refused save says is current, from the failure's body. */
17
+ export function currentOf(body) {
18
+ const current = body?.current;
19
+ if (typeof current !== 'object' || current === null) {
20
+ return null;
21
+ }
22
+ const version = current;
23
+ return typeof version.id === 'number' && typeof version.number === 'number'
24
+ ? {
25
+ id: version.id,
26
+ number: version.number,
27
+ note: typeof version.note === 'string' ? version.note : null,
28
+ by: typeof version.by === 'string' ? version.by : null,
29
+ createdAt: typeof version.createdAt === 'string'
30
+ ? version.createdAt
31
+ : null,
32
+ }
33
+ : null;
34
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * When a burst of file events should be sent as one sync: a short wait
3
+ * after the last event, and no longer than a longer one after the first,
4
+ * so a build that writes many files is sent once, and a folder that never
5
+ * goes quiet is still synced.
6
+ */
7
+ export class Debounce {
8
+ quiet;
9
+ most;
10
+ first = null;
11
+ last = null;
12
+ constructor(quiet = 150, most = 1000) {
13
+ this.quiet = quiet;
14
+ this.most = most;
15
+ }
16
+ /** Note an event at a time. */
17
+ push(now) {
18
+ this.first ??= now;
19
+ this.last = now;
20
+ }
21
+ /** When to fire, given the time now: null for nothing pending. */
22
+ due(now) {
23
+ if (this.first === null || this.last === null) {
24
+ return null;
25
+ }
26
+ return Math.min(this.last + this.quiet, this.first + this.most) - now;
27
+ }
28
+ /** Whether it is time to fire, and forget what was pending if so. */
29
+ fire(now) {
30
+ const due = this.due(now);
31
+ if (due === null || due > 0) {
32
+ return false;
33
+ }
34
+ this.first = null;
35
+ this.last = null;
36
+ return true;
37
+ }
38
+ }
39
+ /** The next wait on a failure, doubling from a second to half a minute. */
40
+ export function backoff(failures) {
41
+ return Math.min(30000, 1000 * 2 ** Math.max(0, failures - 1));
42
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
- "name": "@politelabs/cabin-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
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
+ }