verso-cms 0.0.0-stage → 0.1.3

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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +168 -2
  3. package/bin/verso.js +16 -0
  4. package/editor/door.js +139 -0
  5. package/editor/editor.js +1880 -0
  6. package/editor/elevate.css +2158 -0
  7. package/editor/ext-attrs.js +87 -0
  8. package/editor/ext-blocks.js +422 -0
  9. package/editor/ext-builder.js +819 -0
  10. package/editor/ext-channels.js +574 -0
  11. package/editor/ext-components.js +160 -0
  12. package/editor/ext-history.js +242 -0
  13. package/editor/ext-palette.js +307 -0
  14. package/editor/ext-people.js +345 -0
  15. package/editor/ext-repeaters.js +143 -0
  16. package/editor/ext-screens.js +94 -0
  17. package/editor/ext-site-settings.js +318 -0
  18. package/editor/ext-site-view.js +632 -0
  19. package/editor/ext-tokens.js +1195 -0
  20. package/editor/new-page.js +252 -0
  21. package/editor/rich.js +23 -0
  22. package/package.json +69 -4
  23. package/src/blocks.js +269 -0
  24. package/src/box.js +339 -0
  25. package/src/build.js +210 -0
  26. package/src/cli.js +161 -0
  27. package/src/color.js +137 -0
  28. package/src/components.js +223 -0
  29. package/src/elevate.js +74 -0
  30. package/src/errors.js +46 -0
  31. package/src/extensions/README.md +70 -0
  32. package/src/extensions/blocks.js +294 -0
  33. package/src/extensions/builder.js +734 -0
  34. package/src/extensions/channels.js +341 -0
  35. package/src/extensions/components.js +141 -0
  36. package/src/extensions/history.js +169 -0
  37. package/src/extensions/palette.js +31 -0
  38. package/src/extensions/people.js +113 -0
  39. package/src/extensions/site-settings.js +251 -0
  40. package/src/extensions/site-view.js +381 -0
  41. package/src/extensions/tags.js +15 -0
  42. package/src/extensions/tokens.js +587 -0
  43. package/src/fonts.js +139 -0
  44. package/src/git.js +419 -0
  45. package/src/html-edit.js +942 -0
  46. package/src/images.js +116 -0
  47. package/src/people.js +286 -0
  48. package/src/rich.js +124 -0
  49. package/src/scaffold.js +185 -0
  50. package/src/screens.js +65 -0
  51. package/src/server.js +693 -0
  52. package/src/site.js +618 -0
  53. package/src/template.js +915 -0
  54. package/src/video.js +29 -0
  55. package/starter/Dockerfile +20 -0
  56. package/starter/README.md +49 -0
  57. package/starter/blocks/call-to-action.html +6 -0
  58. package/starter/blocks/features.html +15 -0
  59. package/starter/blocks/hero.html +8 -0
  60. package/starter/blocks/image-and-text.html +8 -0
  61. package/starter/blocks/latest-posts.html +20 -0
  62. package/starter/blocks/post-index.html +30 -0
  63. package/starter/blocks/quote.html +9 -0
  64. package/starter/blocks/site-footer.html +8 -0
  65. package/starter/blocks/site-nav.html +8 -0
  66. package/starter/blocks/tag-list.html +10 -0
  67. package/starter/blocks/tagged-posts.html +19 -0
  68. package/starter/blocks/team.html +15 -0
  69. package/starter/blocks/text.html +4 -0
  70. package/starter/content/404/page.json +21 -0
  71. package/starter/content/about/page.json +65 -0
  72. package/starter/content/journal/designing-for-the-morning-shift/body.html +22 -0
  73. package/starter/content/journal/designing-for-the-morning-shift/checklist.jpg +0 -0
  74. package/starter/content/journal/designing-for-the-morning-shift/cover.jpg +0 -0
  75. package/starter/content/journal/designing-for-the-morning-shift/page.json +17 -0
  76. package/starter/content/journal/page.json +19 -0
  77. package/starter/content/journal/the-first-week/body.html +17 -0
  78. package/starter/content/journal/the-first-week/cover.jpg +0 -0
  79. package/starter/content/journal/the-first-week/page.json +17 -0
  80. package/starter/content/journal/the-first-week/wall-of-cards.jpg +0 -0
  81. package/starter/content/page.json +94 -0
  82. package/starter/content/site.json +88 -0
  83. package/starter/content/studio-table.jpg +0 -0
  84. package/starter/content/topics/page.json +27 -0
  85. package/starter/dockerignore +5 -0
  86. package/starter/elevate/config/design.ts +30 -0
  87. package/starter/elevate/config/elevate.ts +16 -0
  88. package/starter/elevate/config/syntax.ts +6 -0
  89. package/starter/elevate/design/site.ts +9 -0
  90. package/starter/elevate/design/tokens.json +20 -0
  91. package/starter/elevate/extend/fonts.css +1 -0
  92. package/starter/gitignore +6 -0
  93. package/starter/netlify.toml +5 -0
  94. package/starter/parts/.gitkeep +0 -0
  95. package/starter/public/favicon.svg +1 -0
  96. package/starter/templates/page.html +20 -0
  97. package/starter/templates/page.layout.json +23 -0
  98. package/starter/templates/post.html +65 -0
  99. package/starter/verso.defaults.json +33 -0
  100. package/starter-blank/blocks/site-footer.html +3 -0
  101. package/starter-blank/blocks/site-nav.html +8 -0
  102. package/starter-blank/blocks/text.html +4 -0
  103. package/starter-blank/content/404/page.json +18 -0
  104. package/starter-blank/content/page.json +18 -0
  105. package/starter-blank/content/site.json +36 -0
  106. package/starter-blank/elevate/design/tokens.json +10 -0
  107. package/starter-blank/templates/page.html +17 -0
  108. package/starter-blank/verso.defaults.json +26 -0
@@ -0,0 +1,185 @@
1
+ // verso new <folder>: a working site, styled and ready to edit.
2
+
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { spawn } from 'child_process';
6
+ import { fileURLToPath } from 'url';
7
+ import { VersoError } from './errors.js';
8
+ import { humanize } from './template.js';
9
+
10
+ const here = path.dirname(fileURLToPath(import.meta.url));
11
+ const starter = path.join(here, '..', 'starter');
12
+ // --blank: one page type, three plain blocks and a page, in place of the
13
+ // starter's studio site. The rest (Elevate's settings, the favicon) is shared.
14
+ const blankStarter = path.join(here, '..', 'starter-blank');
15
+ const ownPackage = JSON.parse(fs.readFileSync(path.join(here, '..', 'package.json'), 'utf8'));
16
+
17
+ // No shell, so a path with spaces in it (Node in "C:\\Program Files") can't
18
+ // be split in two. Only npm needs a shell, on Windows, and npm() asks for it.
19
+ const run = (cmd, args, cwd, { quiet = false, shell = false } = {}) =>
20
+ new Promise((resolve) => {
21
+ const child = spawn(cmd, args, { cwd, stdio: quiet ? 'pipe' : 'inherit', shell });
22
+ let output = '';
23
+ if (quiet) {
24
+ child.stdout.on('data', (d) => { output += d; });
25
+ child.stderr.on('data', (d) => { output += d; });
26
+ }
27
+ child.on('error', (err) => resolve({ ok: false, output: err.message, missing: err.code === 'ENOENT' }));
28
+ child.on('exit', (code) => resolve({ ok: code === 0, output }));
29
+ });
30
+
31
+ // Under npx or an npm script, npm says where its own code is, and Node runs
32
+ // that directly: no shell, nothing to find on the PATH. Otherwise Windows needs
33
+ // a shell to find npm.cmd, given as one string since every word is Verso's own.
34
+ const npm = (args, cwd) => {
35
+ const told = process.env.npm_execpath || '';
36
+ const cli = /^np[mx]-cli\.js$/.test(path.basename(told)) ? path.join(path.dirname(told), 'npm-cli.js') : '';
37
+ if (cli && fs.existsSync(cli)) return run(process.execPath, [cli, ...args], cwd);
38
+ if (process.platform === 'win32') return run(`npm ${args.join(' ')}`, [], cwd, { shell: true });
39
+ return run('npm', args, cwd);
40
+ };
41
+
42
+ // Until Verso and Elevate are on npm, they come as .tgz files. When the folder
43
+ // "verso new" runs in has them, the new site gets its own copies in vendor/ and
44
+ // installs from those, so it works wherever it goes: moved, cloned or built by
45
+ // a host. Verso's file must be this version; Elevate's is the newest there.
46
+ const versionOf = (file) => (/-(\d+)\.(\d+)\.(\d+)\.tgz$/.exec(file) || []).slice(1).map(Number);
47
+ const newer = (a, b) => {
48
+ const [x, y] = [versionOf(a), versionOf(b)];
49
+ for (let i = 0; i < 3; i++) if ((x[i] || 0) !== (y[i] || 0)) return (x[i] || 0) - (y[i] || 0);
50
+ return 0;
51
+ };
52
+ export function findTarballs(dir) {
53
+ let files = [];
54
+ try { files = fs.readdirSync(dir); } catch { return {}; }
55
+ const verso = `verso-cms-${ownPackage.version}.tgz`;
56
+ const elevate = files.filter((f) => /^elevate-framework-\d+\.\d+\.\d+\.tgz$/.test(f)).sort(newer).at(-1);
57
+ return {
58
+ ...(files.includes(verso) ? { 'verso-cms': path.join(dir, verso) } : {}),
59
+ ...(elevate ? { 'elevate-framework': path.join(dir, elevate) } : {}),
60
+ };
61
+ }
62
+
63
+ const noIdentity = (output) => /Please tell me who you are|empty ident|unable to auto-detect email|auto-detection is disabled/i.test(output);
64
+
65
+ // "field-notes" → "Field Notes", a first guess at the site's name.
66
+ const titleCase = (name) => humanize(name).split(' ').map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(' ');
67
+
68
+ const slug = (name) => name.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '') || 'my-site';
69
+
70
+ export async function createSite(folder, { install = true, blank = false, log = console.log, from = process.cwd() } = {}) {
71
+ if (!folder) {
72
+ throw new VersoError('Name the folder for your new site.', { hint: 'For example: npx verso-cms new my-site' });
73
+ }
74
+ const target = path.resolve(folder);
75
+ if (fs.existsSync(target) && fs.readdirSync(target).length) {
76
+ throw new VersoError(`${folder} already has files in it. Pick a new folder name, so nothing gets overwritten.`);
77
+ }
78
+ const name = path.basename(target);
79
+ log(`\n Creating ${name}…\n`);
80
+
81
+ // 1. The starter site.
82
+ fs.mkdirSync(target, { recursive: true });
83
+ const mine = ['templates', 'blocks', 'content', 'parts'];
84
+ fs.cpSync(starter, target, {
85
+ recursive: true,
86
+ filter: (src) => !src.includes(`${path.sep}elevate${path.sep}`) && !src.endsWith(`${path.sep}elevate`) && path.basename(src) !== 'verso.defaults.json'
87
+ && !(blank && mine.some((d) => path.relative(starter, src).split(path.sep)[0] === d)),
88
+ });
89
+ if (blank) {
90
+ fs.cpSync(blankStarter, target, { recursive: true, filter: (src) => !src.includes(`${path.sep}elevate`) && path.basename(src) !== 'verso.defaults.json' });
91
+ fs.mkdirSync(path.join(target, 'parts'), { recursive: true });
92
+ }
93
+ fs.renameSync(path.join(target, 'gitignore'), path.join(target, '.gitignore'));
94
+ if (fs.existsSync(path.join(target, 'dockerignore'))) fs.renameSync(path.join(target, 'dockerignore'), path.join(target, '.dockerignore'));
95
+ // verso.json: the site's name, plus the starter's locale and rich text classes.
96
+ const defaults = JSON.parse(fs.readFileSync(path.join(blank ? blankStarter : starter, 'verso.defaults.json'), 'utf8'));
97
+ fs.writeFileSync(path.join(target, 'verso.json'), JSON.stringify({ name: titleCase(name), ...defaults }, null, 2) + '\n');
98
+ // The pages say the site's name, not the starter's.
99
+ const said = titleCase(name).replace(/["\\]/g, '');
100
+ ['content/site.json', 'content/page.json'].map((f) => path.join(target, f)).filter((f) => fs.existsSync(f)).forEach((f) => {
101
+ const text = fs.readFileSync(f, 'utf8');
102
+ const next = blank ? text.replace(/__SITE__/g, said) : f.endsWith('site.json') ? text.replace(/Northlight Studio|Northlight/g, said) : text;
103
+ if (next !== text) fs.writeFileSync(f, next);
104
+ });
105
+ const tarballs = findTarballs(from);
106
+ const vendored = Object.keys(tarballs);
107
+ const spec = (name) => (tarballs[name] ? `file:vendor/${path.basename(tarballs[name])}` : null);
108
+ if (vendored.length) {
109
+ fs.mkdirSync(path.join(target, 'vendor'), { recursive: true });
110
+ Object.values(tarballs).forEach((file) => fs.copyFileSync(file, path.join(target, 'vendor', path.basename(file))));
111
+ // The Dockerfile installs before it copies the rest, so it needs vendor/ first.
112
+ const dockerfile = path.join(target, 'Dockerfile');
113
+ if (fs.existsSync(dockerfile)) {
114
+ fs.writeFileSync(dockerfile, fs.readFileSync(dockerfile, 'utf8').replace('COPY package*.json ./\n', 'COPY package*.json ./\nCOPY vendor ./vendor\n'));
115
+ }
116
+ }
117
+ const pkg = {
118
+ name: slug(name),
119
+ private: true,
120
+ type: 'module',
121
+ scripts: { dev: 'verso', build: 'verso build', start: 'verso serve' },
122
+ dependencies: {
123
+ 'elevate-framework': spec('elevate-framework') || '^1.3.2',
124
+ 'verso-cms': process.env.VERSO_SPEC || spec('verso-cms') || `^${ownPackage.version}`,
125
+ },
126
+ };
127
+ fs.writeFileSync(path.join(target, 'package.json'), JSON.stringify(pkg, null, 2) + '\n');
128
+
129
+ // The starter's Elevate settings (its design tokens and config). Elevate's
130
+ // compiler goes in beside them when it's installed; Verso does that itself
131
+ // the first time it runs, if it's missing.
132
+ fs.cpSync(path.join(starter, 'elevate'), path.join(target, 'elevate'), { recursive: true });
133
+ if (blank) fs.cpSync(path.join(blankStarter, 'elevate'), path.join(target, 'elevate'), { recursive: true });
134
+
135
+ if (install) {
136
+ // 2. Dependencies, then Elevate's compiler.
137
+ if (vendored.length) log(` Using ${vendored.map((n) => path.basename(tarballs[n])).join(' and ')} from this folder, copied into ${name}/vendor.`);
138
+ log(' Installing Verso and Elevate (this takes a moment)…\n');
139
+ const installed = await npm(['install', '--no-fund', '--no-audit', '--loglevel=error'], target);
140
+ if (!installed.ok) {
141
+ const hint = installed.missing
142
+ ? 'Install Node.js from nodejs.org, open a new terminal, then try again.'
143
+ : `${vendored.length === 2 ? '' : 'If you have Verso as .tgz files, run "verso new" in the folder that has them. '}When it's fixed, run "npm install" in ${folder}, then "npm run dev".`;
144
+ throw new VersoError(installed.missing ? 'npm isn\'t installed, and Verso needs it.' : 'npm couldn\'t install the packages. The messages above say why.', { hint });
145
+ }
146
+ const prepare = await prepareElevate(target);
147
+ if (!prepare.ok) throw new VersoError(`Elevate couldn't be set up: ${prepare.output.trim()}`);
148
+ }
149
+
150
+ // 3. A first restore point. If git doesn't know who you are yet, this one is
151
+ // signed "Verso", so the site still starts with history.
152
+ let committed = false;
153
+ let unnamed = false;
154
+ const init = await run('git', ['init', '-q'], target, { quiet: true });
155
+ if (init.ok) {
156
+ await run('git', ['checkout', '-q', '-b', 'main'], target, { quiet: true });
157
+ await run('git', ['add', '-A'], target, { quiet: true });
158
+ const first = await run('git', ['commit', '-q', '-m', 'Start a Verso site'], target, { quiet: true });
159
+ committed = first.ok;
160
+ if (!first.ok && noIdentity(first.output)) {
161
+ unnamed = true;
162
+ committed = (await run('git', ['-c', 'user.name=Verso', '-c', 'user.email=verso@localhost', 'commit', '-q', '-m', 'Start a Verso site'], target, { quiet: true })).ok;
163
+ }
164
+ }
165
+
166
+ log(`\n √ ${name} is ready. To start editing:\n`);
167
+ log(` cd ${folder}`);
168
+ if (!install) log(' npm install');
169
+ log(' npm run dev\n');
170
+ if (!init.ok) log(' Git isn\'t installed, so Publish won\'t work yet. Install it from git-scm.com.\n');
171
+ else if (unnamed) log(' Git doesn\'t know your name yet, and Publish signs changes with it. Run these once:\n git config --global user.name "Your Name"\n git config --global user.email "you@example.com"\n');
172
+ else if (!committed) log(' Git couldn\'t save a first restore point. Publish from the editor once it\'s open.\n');
173
+ return { target, committed };
174
+ }
175
+
176
+ // Puts Elevate's compiler into elevate/core, keeping the site's own config
177
+ // and design. Safe to run again: it only upgrades the compiler.
178
+ export async function prepareElevate(target) {
179
+ const script = path.join(target, 'node_modules', 'elevate-framework', 'bin', 'install.js');
180
+ if (!fs.existsSync(script)) return { ok: false, missing: true, output: 'elevate-framework isn\'t installed.' };
181
+ const result = await run(process.execPath, [script], target, { quiet: true });
182
+ ['templates', 'design/example-brandTokens.ts', 'rules/example-brandRules.ts'].forEach((p) =>
183
+ fs.rmSync(path.join(target, 'elevate', p), { recursive: true, force: true }));
184
+ return result;
185
+ }
package/src/screens.js ADDED
@@ -0,0 +1,65 @@
1
+ // Screen sizes: Elevate's breakpoints, in words. Each one is a minimum width
2
+ // (mobile first), kept in elevate/design/tokens.json as "BreakPointToken"
3
+ // once the site changes them. What people call them ("Tablets") is kept in
4
+ // verso.json as "screens", since only Verso uses the words.
5
+
6
+ import fs from 'fs';
7
+ import path from 'path';
8
+
9
+ export const DEFAULT_LABELS = {
10
+ '2xs': 'Small phones', xs: 'Large phones', sm: 'Phones sideways', md: 'Tablets', lg: 'Laptops',
11
+ xl: 'Desktops', '2xl': 'Large desktops', '3xl': 'Full HD', '4xl': 'QHD', '5xl': '4K screens',
12
+ };
13
+
14
+ // A breakpoint name: what goes between the slashes in /md/.
15
+ export const SCREEN_NAME = /^[a-z0-9][a-z0-9-]{0,11}$/;
16
+ export const SCREEN_WIDTH = /^\d+(\.\d+)?(px|rem|em)$/;
17
+
18
+ export const widthPx = (value) => {
19
+ const m = /^(-?[\d.]+)(px|rem|em)?$/.exec(String(value || '').trim());
20
+ if (!m) return null;
21
+ return Math.round(Number(m[1]) * (m[2] === 'rem' || m[2] === 'em' ? 16 : 1));
22
+ };
23
+
24
+ const words = (name) => {
25
+ const w = String(name).replace(/[-_]+/g, ' ').trim();
26
+ return w.charAt(0).toUpperCase() + w.slice(1);
27
+ };
28
+
29
+ export function readScreenLabels(root) {
30
+ try {
31
+ const config = JSON.parse(fs.readFileSync(path.join(root, 'verso.json'), 'utf8'));
32
+ return config.screens && typeof config.screens === 'object' ? config.screens : {};
33
+ } catch {
34
+ return {};
35
+ }
36
+ }
37
+
38
+ export const screenLabel = (name, labels = {}) => labels[name] || DEFAULT_LABELS[name] || words(name);
39
+
40
+ // Elevate's breakpoints with their words and widths in px, narrowest first.
41
+ export function screensFor(breakpoints, labels = {}) {
42
+ return (breakpoints || [])
43
+ .map((b) => ({ name: b.name, minWidth: b.minWidth, px: widthPx(b.minWidth), label: screenLabel(b.name, labels) }))
44
+ .sort((a, b) => (a.px ?? Infinity) - (b.px ?? Infinity));
45
+ }
46
+
47
+ // A marker's group in a class string: from /name/ to the next marker.
48
+ const isMarker = (t) => /^\/[a-zA-Z0-9_-]{1,12}\/$/.test(t);
49
+
50
+ export function usesScreen(classes, name) {
51
+ return String(classes || '').split(/\s+/).includes(`/${name}/`);
52
+ }
53
+
54
+ // The class string without that screen size: its marker and the classes
55
+ // after it, up to the next marker.
56
+ export function dropScreen(classes, name) {
57
+ const out = [];
58
+ let skipping = false;
59
+ String(classes || '').split(/(\s+)/).forEach((part) => {
60
+ if (/^\s+$/.test(part) || part === '') { if (!skipping) out.push(part); return; }
61
+ if (isMarker(part)) skipping = part === `/${name}/`;
62
+ if (!skipping) out.push(part);
63
+ });
64
+ return out.join('').replace(/\s{2,}/g, ' ').trim();
65
+ }