chalk-talk 0.7.0 → 0.9.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/scripts/build.mjs CHANGED
@@ -6,12 +6,15 @@
6
6
  //
7
7
  // node build.mjs <recipe.json|.chalk|.mmd|.md> [out.html] [--no-review]
8
8
  // node build.mjs <one> <two> … --out course.html [--title "The course"] [--no-review]
9
+ // node build.mjs <recipe> [out.svg] --svg an SVG that draws itself, with no script: it plays inside an <img>,
10
+ // in a README on GitHub too (one SVG per lesson, named by its id)
9
11
  //
10
- // Only a path ending in .html is ever written. Exit codes: 0 drawn and checked (or no browser to check with); 1 a recipe
11
- // breaks the contract (nothing written); 2 bad arguments; 3 drawn, but the reviewer found something to fix (off the
12
- // paper, text over text, a line across a text). Needs Node 18 or later and nothing else; the review uses Chrome,
13
- // Chromium or Edge when one is installed.
14
- import { readFileSync, statSync, writeFileSync } from 'node:fs'
12
+ // Only paths ending in .html or .svg are ever written. Exit codes: 0 drawn and checked (or no browser to check with); 1 a
13
+ // recipe breaks the contract (nothing written); 2 bad arguments; 3 drawn, but the reviewer found something to fix (off
14
+ // the paper, text over text, a line across a text); 4 --svg with no browser to draw it in. Needs Node 18 or later and
15
+ // nothing else; the review and --svg use Chrome, Chromium or Edge.
16
+ import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
17
+ import { tmpdir } from 'node:os'
15
18
  import { basename, dirname, join, resolve } from 'node:path'
16
19
  import { fileURLToPath } from 'node:url'
17
20
  import { runPage } from './browser.mjs'
@@ -26,11 +29,13 @@ const option = (name) => {
26
29
  const valued = new Set(['--out', '--title'])
27
30
  const args = argv.filter((a, i) => !a.startsWith('--') && !valued.has(argv[i - 1]))
28
31
  const noReview = argv.includes('--no-review')
32
+ const asSvg = argv.includes('--svg')
33
+ const ext = asSvg ? '.svg' : '.html'
29
34
  const KINDS = ['.json', '.chalk', '.mmd', '.md']
30
35
  const sources = args.filter((a) => KINDS.some((k) => a.endsWith(k)))
31
- const outArg = option('--out') ?? args.find((a) => a.endsWith('.html'))
32
- const usage = 'usage: node build.mjs <recipe.json> [out.html] [--no-review]\n node build.mjs <one.json> <two.json> … --out course.html [--title "…"] [--no-review]'
33
- if (!sources.length || args.some((a) => !KINDS.some((k) => a.endsWith(k)) && !a.endsWith('.html')) || (outArg && !outArg.endsWith('.html'))) {
36
+ const outArg = option('--out') ?? args.find((a) => a.endsWith(ext))
37
+ const usage = 'usage: node build.mjs <recipe.json> [out.html] [--no-review]\n node build.mjs <one.json> <two.json> … --out course.html [--title "…"] [--no-review]\n node build.mjs <recipe.json> [out.svg] --svg'
38
+ if (!sources.length || args.some((a) => !KINDS.some((k) => a.endsWith(k)) && !a.endsWith(ext)) || (outArg && !outArg.endsWith(ext))) {
34
39
  console.error(usage)
35
40
  process.exit(2)
36
41
  }
@@ -64,8 +69,11 @@ for (const file of sources) {
64
69
  }
65
70
 
66
71
  const course = scenes.length > 1
67
- const slug = String(scenes[0].id || basename(sources[0]).replace(/\.[^.]+$/, '')).replace(/[^a-z0-9-]+/gi, '-').slice(0, 60) || 'drawing'
68
- const out = resolve(outArg ?? join(dirname(resolve(sources[0])), `${slug}.html`))
72
+ const slugOf = (scene, file) => String(scene.id || basename(file).replace(/\.[^.]+$/, '')).replace(/[^a-z0-9-]+/gi, '-').slice(0, 60) || 'drawing'
73
+ const slug = slugOf(scenes[0], sources[0])
74
+ // With --svg the page is only the browser's workbench: it goes to a folder of its own and away at the end.
75
+ const work = asSvg ? mkdtempSync(join(tmpdir(), 'chalk-talk-svg-')) : undefined
76
+ const out = work ? join(work, 'page.html') : resolve(outArg ?? join(dirname(resolve(sources[0])), `${slug}.html`))
69
77
  const content = course ? { course: option('--title') ?? scenes[0].title, lessons: scenes } : scenes[0]
70
78
  // JSON inside a <script> tag: "<" escaped so no text in a recipe can end the tag.
71
79
  const recipe = JSON.stringify(content).replace(/</g, '\\u003c')
@@ -96,5 +104,28 @@ else {
96
104
  }
97
105
  }
98
106
  }
99
- console.log(`→ ${out} (${Math.round(statSync(out).size / 1024)} KB, opens offline${course ? `, ${scenes.length} lessons` : ''})`)
107
+ if (!asSvg) {
108
+ console.log(`→ ${out} (${Math.round(statSync(out).size / 1024)} KB, opens offline${course ? `, ${scenes.length} lessons` : ''})`)
109
+ process.exit(code)
110
+ }
111
+
112
+ // --svg: each lesson drawn by the browser with its timing as CSS; one file, or one per lesson beside the first source.
113
+ const where = outArg && !course ? undefined : dirname(resolve(outArg ?? sources[0]))
114
+ for (let k = 0; k < scenes.length; k++) {
115
+ const r = runPage(out, 'svg', course ? `lesson=${k + 1}` : '')
116
+ if (!r) {
117
+ console.error('✗ svg no Chrome, Chromium or Edge here to draw it (set CHROME_PATH)')
118
+ code = 4
119
+ break
120
+ }
121
+ if (r.error) {
122
+ console.error(`✗ svg ${r.error}`)
123
+ code = 4
124
+ break
125
+ }
126
+ const file = where ? join(where, `${slugOf(scenes[k], sources[0])}.svg`) : resolve(outArg)
127
+ writeFileSync(file, r.svg)
128
+ console.log(`→ ${file} (${Math.round(statSync(file).size / 1024)} KB, draws itself in ${Math.round(r.ms / 1000)} s)`)
129
+ }
130
+ rmSync(work, { recursive: true, force: true })
100
131
  process.exit(code)
@@ -1,4 +1,4 @@
1
- // GENERATED by tools/assets.mjs from the drawing engine v0.243.2. Do not edit.
1
+ // GENERATED by tools/assets.mjs from the drawing engine v0.244.1. Do not edit.
2
2
  var WORDS = ["and", "or", "not"];
3
3
 
4
4
  var PROP_NAMES = [