@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,72 @@
|
|
|
1
|
+
/** What the Tailwind scaffold puts in the theme's `package.json`. */
|
|
2
|
+
export function packageJson(key) {
|
|
3
|
+
return `${JSON.stringify({
|
|
4
|
+
name: `${key}-theme`,
|
|
5
|
+
private: true,
|
|
6
|
+
type: 'module',
|
|
7
|
+
scripts: {
|
|
8
|
+
dev: 'tailwindcss -i src/theme.css -o theme.css --watch',
|
|
9
|
+
build: 'tailwindcss -i src/theme.css -o theme.css --minify',
|
|
10
|
+
},
|
|
11
|
+
devDependencies: {
|
|
12
|
+
'@tailwindcss/cli': '^4.1.0',
|
|
13
|
+
tailwindcss: '^4.1.0',
|
|
14
|
+
},
|
|
15
|
+
}, null, 4)}\n`;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The stylesheet's source with Tailwind: its theme and utilities, not its
|
|
19
|
+
* preflight, so the stylesheet as pulled keeps its look, inside the base
|
|
20
|
+
* layer so utilities written in templates win.
|
|
21
|
+
*/
|
|
22
|
+
export function tailwindSource(pulledCss) {
|
|
23
|
+
return [
|
|
24
|
+
'/* theme.css is built from this file: `cabin theme dev` and `cabin theme push`',
|
|
25
|
+
" run the scripts in package.json, which write it. Tailwind's theme and",
|
|
26
|
+
" utilities are included; its preflight isn't, so the styles below stay as",
|
|
27
|
+
" they were. Cabin sets the site's colours and fonts as CSS variables:",
|
|
28
|
+
' --cabin-accent, --cabin-background, --cabin-text, --cabin-font-body, ...',
|
|
29
|
+
" The built file has to pass Cabin's stylesheet rules: no @import, no url()",
|
|
30
|
+
' to another host, no data: URLs, and at most 256 KB. */',
|
|
31
|
+
'@layer theme, base, components, utilities;',
|
|
32
|
+
'@import "tailwindcss/theme.css" layer(theme);',
|
|
33
|
+
'@import "tailwindcss/utilities.css" layer(utilities);',
|
|
34
|
+
'@source "../templates";',
|
|
35
|
+
'',
|
|
36
|
+
'@layer base {',
|
|
37
|
+
pulledCss.trimEnd(),
|
|
38
|
+
'}',
|
|
39
|
+
'',
|
|
40
|
+
].join('\n');
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The files a new theme's folder gets: what the site answered, then
|
|
44
|
+
* `cabin.json`, `.gitignore`, and, with Tailwind, `package.json` and the
|
|
45
|
+
* stylesheet's source. `theme.css` as pulled stays, so the folder is a
|
|
46
|
+
* whole theme before the first build.
|
|
47
|
+
*/
|
|
48
|
+
export function scaffold(config, files, options) {
|
|
49
|
+
const out = Object.entries(files).map(([path, contents]) => ({
|
|
50
|
+
path,
|
|
51
|
+
contents,
|
|
52
|
+
}));
|
|
53
|
+
out.push({
|
|
54
|
+
path: 'cabin.json',
|
|
55
|
+
contents: `${JSON.stringify(config, null, 4)}\n`,
|
|
56
|
+
});
|
|
57
|
+
if (options.tailwind) {
|
|
58
|
+
out.push({ path: 'package.json', contents: packageJson(config.theme) });
|
|
59
|
+
out.push({
|
|
60
|
+
path: 'src/theme.css',
|
|
61
|
+
contents: tailwindSource(files['theme.css'] ?? ''),
|
|
62
|
+
});
|
|
63
|
+
out.push({
|
|
64
|
+
path: '.gitignore',
|
|
65
|
+
contents: 'node_modules\n.cabin\ntheme.css\n',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
out.push({ path: '.gitignore', contents: '.cabin\n' });
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Which package manager runs a folder's scripts, from what the folder holds. */
|
|
2
|
+
export function detectPackageManager(folderHas, userAgent = '') {
|
|
3
|
+
if (folderHas('pnpm-lock.yaml')) {
|
|
4
|
+
return 'pnpm';
|
|
5
|
+
}
|
|
6
|
+
if (folderHas('yarn.lock')) {
|
|
7
|
+
return 'yarn';
|
|
8
|
+
}
|
|
9
|
+
if (folderHas('bun.lock') || folderHas('bun.lockb')) {
|
|
10
|
+
return 'bun';
|
|
11
|
+
}
|
|
12
|
+
if (folderHas('package-lock.json')) {
|
|
13
|
+
return 'npm';
|
|
14
|
+
}
|
|
15
|
+
const [name] = userAgent.split('/');
|
|
16
|
+
return name === 'pnpm' || name === 'yarn' || name === 'bun' ? name : 'npm';
|
|
17
|
+
}
|
|
18
|
+
/** Whether a folder's package.json has a script, from its contents. */
|
|
19
|
+
export function hasScript(packageJson, name) {
|
|
20
|
+
if (packageJson === null) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const parsed = JSON.parse(packageJson);
|
|
25
|
+
return typeof parsed.scripts?.[name] === 'string';
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The command and arguments that run a script. */
|
|
32
|
+
export function runScript(manager, script) {
|
|
33
|
+
return manager === 'npm'
|
|
34
|
+
? ['npm', ['run', script]]
|
|
35
|
+
: [manager, ['run', script]];
|
|
36
|
+
}
|
|
@@ -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,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@politelabs/cabin-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
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
|
+
}
|
|
6
37
|
}
|