@kurvball/rehearse 0.0.0-stage → 1.2.1

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 (266) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +102 -2
  3. package/dist/affected/affected.d.ts +22 -0
  4. package/dist/affected/affected.js +162 -0
  5. package/dist/affected/index.d.ts +1 -0
  6. package/dist/affected/index.js +1 -0
  7. package/dist/args.d.ts +12 -0
  8. package/dist/args.js +42 -0
  9. package/dist/cli.d.ts +4 -0
  10. package/dist/cli.js +100 -0
  11. package/dist/commands/check.d.ts +2 -0
  12. package/dist/commands/check.js +42 -0
  13. package/dist/commands/clean.d.ts +2 -0
  14. package/dist/commands/clean.js +37 -0
  15. package/dist/commands/command.d.ts +29 -0
  16. package/dist/commands/command.js +29 -0
  17. package/dist/commands/compare.d.ts +4 -0
  18. package/dist/commands/compare.js +84 -0
  19. package/dist/commands/drive.d.ts +2 -0
  20. package/dist/commands/drive.js +244 -0
  21. package/dist/commands/index.d.ts +3 -0
  22. package/dist/commands/index.js +32 -0
  23. package/dist/commands/init.d.ts +2 -0
  24. package/dist/commands/init.js +19 -0
  25. package/dist/commands/list.d.ts +2 -0
  26. package/dist/commands/list.js +69 -0
  27. package/dist/commands/open.d.ts +2 -0
  28. package/dist/commands/open.js +60 -0
  29. package/dist/commands/people.d.ts +2 -0
  30. package/dist/commands/people.js +49 -0
  31. package/dist/commands/run.d.ts +4 -0
  32. package/dist/commands/run.js +82 -0
  33. package/dist/commands/services.d.ts +28 -0
  34. package/dist/commands/services.js +51 -0
  35. package/dist/commands/stage.d.ts +7 -0
  36. package/dist/commands/stage.js +49 -0
  37. package/dist/commands/up.d.ts +3 -0
  38. package/dist/commands/up.js +77 -0
  39. package/dist/commands/validate.d.ts +2 -0
  40. package/dist/commands/validate.js +35 -0
  41. package/dist/commands/worlds.d.ts +2 -0
  42. package/dist/commands/worlds.js +15 -0
  43. package/dist/compare/announce.d.ts +6 -0
  44. package/dist/compare/announce.js +27 -0
  45. package/dist/compare/compare.d.ts +82 -0
  46. package/dist/compare/compare.js +310 -0
  47. package/dist/compare/diff.d.ts +15 -0
  48. package/dist/compare/diff.js +105 -0
  49. package/dist/compare/index.d.ts +4 -0
  50. package/dist/compare/index.js +4 -0
  51. package/dist/compare/setaside.d.ts +29 -0
  52. package/dist/compare/setaside.js +121 -0
  53. package/dist/compare/verdict.d.ts +21 -0
  54. package/dist/compare/verdict.js +30 -0
  55. package/dist/drive/aria.d.ts +8 -0
  56. package/dist/drive/aria.js +12 -0
  57. package/dist/drive/browser.d.ts +34 -0
  58. package/dist/drive/browser.js +216 -0
  59. package/dist/drive/daemon.d.ts +1 -0
  60. package/dist/drive/daemon.js +171 -0
  61. package/dist/drive/grammar.d.ts +93 -0
  62. package/dist/drive/grammar.js +303 -0
  63. package/dist/drive/index.d.ts +4 -0
  64. package/dist/drive/index.js +4 -0
  65. package/dist/drive/names.d.ts +24 -0
  66. package/dist/drive/names.js +97 -0
  67. package/dist/drive/state.d.ts +13 -0
  68. package/dist/drive/state.js +28 -0
  69. package/dist/errors.d.ts +26 -0
  70. package/dist/errors.js +32 -0
  71. package/dist/exec.d.ts +16 -0
  72. package/dist/exec.js +43 -0
  73. package/dist/git/checkout.d.ts +11 -0
  74. package/dist/git/checkout.js +84 -0
  75. package/dist/git/git.d.ts +7 -0
  76. package/dist/git/git.js +27 -0
  77. package/dist/git/index.d.ts +2 -0
  78. package/dist/git/index.js +2 -0
  79. package/dist/index.d.ts +18 -0
  80. package/dist/index.js +15 -0
  81. package/dist/kinds/all.d.ts +4 -0
  82. package/dist/kinds/all.js +4 -0
  83. package/dist/kinds/call/check.d.ts +29 -0
  84. package/dist/kinds/call/check.js +187 -0
  85. package/dist/kinds/call/definition.d.ts +16 -0
  86. package/dist/kinds/call/definition.js +33 -0
  87. package/dist/kinds/call/hurl.d.ts +65 -0
  88. package/dist/kinds/call/hurl.js +333 -0
  89. package/dist/kinds/call/kind.d.ts +8 -0
  90. package/dist/kinds/call/kind.js +122 -0
  91. package/dist/kinds/call/send.d.ts +32 -0
  92. package/dist/kinds/call/send.js +152 -0
  93. package/dist/kinds/call/words.d.ts +22 -0
  94. package/dist/kinds/call/words.js +24 -0
  95. package/dist/kinds/file/kind.d.ts +44 -0
  96. package/dist/kinds/file/kind.js +331 -0
  97. package/dist/kinds/file/layouts.d.ts +14 -0
  98. package/dist/kinds/file/layouts.js +103 -0
  99. package/dist/kinds/flow/kind.d.ts +21 -0
  100. package/dist/kinds/flow/kind.js +401 -0
  101. package/dist/kinds/flow/literal.d.ts +8 -0
  102. package/dist/kinds/flow/literal.js +148 -0
  103. package/dist/kinds/index.d.ts +10 -0
  104. package/dist/kinds/index.js +8 -0
  105. package/dist/kinds/kind.d.ts +57 -0
  106. package/dist/kinds/kind.js +21 -0
  107. package/dist/kinds/piece/canvas.d.ts +18 -0
  108. package/dist/kinds/piece/canvas.js +165 -0
  109. package/dist/kinds/piece/conventions.d.ts +12 -0
  110. package/dist/kinds/piece/conventions.js +101 -0
  111. package/dist/kinds/piece/csf.d.ts +10 -0
  112. package/dist/kinds/piece/csf.js +304 -0
  113. package/dist/kinds/piece/kind.d.ts +12 -0
  114. package/dist/kinds/piece/kind.js +297 -0
  115. package/dist/kinds/piece/server.d.ts +23 -0
  116. package/dist/kinds/piece/server.js +236 -0
  117. package/dist/manifest/fill.d.ts +4 -0
  118. package/dist/manifest/fill.js +7 -0
  119. package/dist/manifest/index.d.ts +5 -0
  120. package/dist/manifest/index.js +5 -0
  121. package/dist/manifest/init.d.ts +1 -0
  122. package/dist/manifest/init.js +47 -0
  123. package/dist/manifest/load.d.ts +7 -0
  124. package/dist/manifest/load.js +244 -0
  125. package/dist/manifest/types.d.ts +253 -0
  126. package/dist/manifest/types.js +7 -0
  127. package/dist/manifest/yaml.d.ts +5 -0
  128. package/dist/manifest/yaml.js +47 -0
  129. package/dist/people/index.d.ts +1 -0
  130. package/dist/people/index.js +1 -0
  131. package/dist/people/people.d.ts +39 -0
  132. package/dist/people/people.js +139 -0
  133. package/dist/run/cache.d.ts +20 -0
  134. package/dist/run/cache.js +98 -0
  135. package/dist/run/index.d.ts +4 -0
  136. package/dist/run/index.js +3 -0
  137. package/dist/run/observation.d.ts +27 -0
  138. package/dist/run/observation.js +1 -0
  139. package/dist/run/session.d.ts +32 -0
  140. package/dist/run/session.js +258 -0
  141. package/dist/run/vars.d.ts +3 -0
  142. package/dist/run/vars.js +13 -0
  143. package/dist/scenarios/area.d.ts +6 -0
  144. package/dist/scenarios/area.js +37 -0
  145. package/dist/scenarios/files.d.ts +9 -0
  146. package/dist/scenarios/files.js +26 -0
  147. package/dist/scenarios/glob.d.ts +6 -0
  148. package/dist/scenarios/glob.js +106 -0
  149. package/dist/scenarios/index.d.ts +4 -0
  150. package/dist/scenarios/index.js +4 -0
  151. package/dist/scenarios/read.d.ts +18 -0
  152. package/dist/scenarios/read.js +54 -0
  153. package/dist/scenarios/scenario.d.ts +34 -0
  154. package/dist/scenarios/scenario.js +10 -0
  155. package/dist/schema.d.ts +14 -0
  156. package/dist/schema.js +122 -0
  157. package/dist/services/index.d.ts +2 -0
  158. package/dist/services/index.js +2 -0
  159. package/dist/services/registry.d.ts +23 -0
  160. package/dist/services/registry.js +49 -0
  161. package/dist/services/service.d.ts +22 -0
  162. package/dist/services/service.js +284 -0
  163. package/dist/stage/api.d.ts +28 -0
  164. package/dist/stage/api.js +206 -0
  165. package/dist/stage/client/client/api.js +11 -0
  166. package/dist/stage/client/client/index.js +1 -0
  167. package/dist/stage/client/features/canvas/call.js +65 -0
  168. package/dist/stage/client/features/canvas/context.js +1 -0
  169. package/dist/stage/client/features/canvas/file.js +36 -0
  170. package/dist/stage/client/features/canvas/flow.js +59 -0
  171. package/dist/stage/client/features/canvas/index.js +1 -0
  172. package/dist/stage/client/features/canvas/observed.js +8 -0
  173. package/dist/stage/client/features/canvas/parts.js +24 -0
  174. package/dist/stage/client/features/canvas/piece.js +31 -0
  175. package/dist/stage/client/features/canvas/reach.js +6 -0
  176. package/dist/stage/client/features/canvas/views.js +16 -0
  177. package/dist/stage/client/features/list/index.js +1 -0
  178. package/dist/stage/client/features/list/list.js +53 -0
  179. package/dist/stage/client/features/list/reach.js +10 -0
  180. package/dist/stage/client/features/scenario/heading.js +14 -0
  181. package/dist/stage/client/features/scenario/index.js +1 -0
  182. package/dist/stage/client/features/scenario/reach.js +9 -0
  183. package/dist/stage/client/features/scenario/scenario.js +145 -0
  184. package/dist/stage/client/features/try/index.js +1 -0
  185. package/dist/stage/client/features/try/reach.js +12 -0
  186. package/dist/stage/client/features/try/try.js +65 -0
  187. package/dist/stage/client/index.html +16 -0
  188. package/dist/stage/client/main.js +24 -0
  189. package/dist/stage/client/primitives/busy.js +6 -0
  190. package/dist/stage/client/primitives/focus.js +21 -0
  191. package/dist/stage/client/primitives/h.js +25 -0
  192. package/dist/stage/client/primitives/index.js +6 -0
  193. package/dist/stage/client/primitives/keyed.js +16 -0
  194. package/dist/stage/client/primitives/say.js +6 -0
  195. package/dist/stage/client/primitives/state.js +21 -0
  196. package/dist/stage/client/primitives/types.js +1 -0
  197. package/dist/stage/client/primitives/verdicts.js +8 -0
  198. package/dist/stage/client/stage.css +652 -0
  199. package/dist/stage/compared.d.ts +20 -0
  200. package/dist/stage/compared.js +58 -0
  201. package/dist/stage/files.d.ts +4 -0
  202. package/dist/stage/files.js +59 -0
  203. package/dist/stage/guard.d.ts +3 -0
  204. package/dist/stage/guard.js +20 -0
  205. package/dist/stage/http.d.ts +12 -0
  206. package/dist/stage/http.js +34 -0
  207. package/dist/stage/index.d.ts +1 -0
  208. package/dist/stage/index.js +1 -0
  209. package/dist/stage/sending.d.ts +18 -0
  210. package/dist/stage/sending.js +66 -0
  211. package/dist/stage/server.d.ts +31 -0
  212. package/dist/stage/server.js +250 -0
  213. package/dist/version.d.ts +2 -0
  214. package/dist/version.js +5 -0
  215. package/dist/worlds/define.d.ts +16 -0
  216. package/dist/worlds/define.js +45 -0
  217. package/dist/worlds/index.d.ts +2 -0
  218. package/dist/worlds/index.js +2 -0
  219. package/dist/worlds/worlds.d.ts +15 -0
  220. package/dist/worlds/worlds.js +46 -0
  221. package/llms.txt +46 -0
  222. package/package.json +84 -3
  223. package/schema/v1/announce.schema.json +28 -0
  224. package/schema/v1/check.schema.json +67 -0
  225. package/schema/v1/clean.schema.json +19 -0
  226. package/schema/v1/common.schema.json +210 -0
  227. package/schema/v1/comparison.schema.json +298 -0
  228. package/schema/v1/dependents.schema.json +30 -0
  229. package/schema/v1/drive.schema.json +112 -0
  230. package/schema/v1/error.schema.json +26 -0
  231. package/schema/v1/file-scenario.schema.json +175 -0
  232. package/schema/v1/flow.schema.json +216 -0
  233. package/schema/v1/init.schema.json +20 -0
  234. package/schema/v1/list.schema.json +70 -0
  235. package/schema/v1/manifest.schema.json +430 -0
  236. package/schema/v1/observation.schema.json +388 -0
  237. package/schema/v1/open.schema.json +14 -0
  238. package/schema/v1/people.schema.json +88 -0
  239. package/schema/v1/samples/announce.yaml +5 -0
  240. package/schema/v1/samples/check.json +13 -0
  241. package/schema/v1/samples/clean.json +1 -0
  242. package/schema/v1/samples/comparison.json +83 -0
  243. package/schema/v1/samples/dependents.json +7 -0
  244. package/schema/v1/samples/drive.json +10 -0
  245. package/schema/v1/samples/error.json +10 -0
  246. package/schema/v1/samples/file-scenario.yaml +13 -0
  247. package/schema/v1/samples/flow.yaml +13 -0
  248. package/schema/v1/samples/init.json +1 -0
  249. package/schema/v1/samples/list.json +24 -0
  250. package/schema/v1/samples/manifest.yaml +55 -0
  251. package/schema/v1/samples/observation.json +49 -0
  252. package/schema/v1/samples/open.json +6 -0
  253. package/schema/v1/samples/people.json +11 -0
  254. package/schema/v1/samples/services.json +27 -0
  255. package/schema/v1/samples/stage.json +6 -0
  256. package/schema/v1/samples/try.json +13 -0
  257. package/schema/v1/samples/up.json +16 -0
  258. package/schema/v1/samples/validate.json +7 -0
  259. package/schema/v1/samples/worlds.json +21 -0
  260. package/schema/v1/services.schema.json +72 -0
  261. package/schema/v1/stage.schema.json +14 -0
  262. package/schema/v1/try.schema.json +56 -0
  263. package/schema/v1/up.schema.json +55 -0
  264. package/schema/v1/validate.schema.json +40 -0
  265. package/schema/v1/worlds.schema.json +33 -0
  266. package/skill/rehearse/SKILL.md +148 -0
@@ -0,0 +1,165 @@
1
+ export const CANVAS_PATH = '/canvas.html';
2
+ export const CANVAS_PREFIX = '/@rehearse-canvas/';
3
+ export const CANVAS_MODULE = '\0rehearse-canvas:';
4
+ export const ROOT_ID = 'storybook-root';
5
+ export const THREW = 'rehearse-piece-threw';
6
+ const escapeHtml = (text) => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
7
+ export const canvasPath = (file, exportName) => `${CANVAS_PATH}?file=${encodeURIComponent(file)}&export=${encodeURIComponent(exportName)}`;
8
+ export const canvasScript = (file, exportName) => `${CANVAS_PREFIX}${encodeURIComponent(JSON.stringify([file, exportName]))}.js`;
9
+ export function canvasKeyOf(source) {
10
+ if (!source.startsWith(CANVAS_PREFIX))
11
+ return null;
12
+ const tail = source.slice(CANVAS_PREFIX.length).replace(/[?#].*$/, '');
13
+ if (!tail.endsWith('.js'))
14
+ return null;
15
+ try {
16
+ const key = JSON.parse(decodeURIComponent(tail.slice(0, -3)));
17
+ return Array.isArray(key) && key.length === 2 && key.every((x) => typeof x === 'string')
18
+ ? key
19
+ : null;
20
+ }
21
+ catch {
22
+ return null;
23
+ }
24
+ }
25
+ export function canvasHtml(file, exportName, title) {
26
+ return [
27
+ '<!doctype html>',
28
+ '<html lang="en">',
29
+ '<head>',
30
+ '<meta charset="utf-8">',
31
+ '<meta name="viewport" content="width=device-width, initial-scale=1">',
32
+ `<title>${escapeHtml(title)}</title>`,
33
+ '</head>',
34
+ '<body>',
35
+ `<div id="${ROOT_ID}"></div>`,
36
+ `<script type="module" src="${escapeHtml(canvasScript(file, exportName))}"></script>`,
37
+ '</body>',
38
+ '</html>',
39
+ '',
40
+ ].join('\n');
41
+ }
42
+ export function missingHtml(said) {
43
+ return `<!doctype html>\n<html lang="en"><head><meta charset="utf-8"><title>No such piece</title></head><body><p role="alert">${escapeHtml(said)}</p></body></html>\n`;
44
+ }
45
+ export function canvasModule(piece, styles, preview) {
46
+ return [
47
+ `import * as React from 'react';`,
48
+ `import { createRoot } from 'react-dom/client';`,
49
+ ...(styles ? [`import ${JSON.stringify(styles)};`] : []),
50
+ preview ? `import * as previewModule from ${JSON.stringify(preview)};` : `const previewModule = {};`,
51
+ `import * as file from ${JSON.stringify(piece.viteUrl)};`,
52
+ ``,
53
+ `const list = (d) => (d == null ? [] : Array.isArray(d) ? d : [d]);`,
54
+ `const preview = { ...previewModule, ...(previewModule.default ?? {}) };`,
55
+ `const meta = file.default ?? {};`,
56
+ `const raw = file[${JSON.stringify(piece.exportName)}];`,
57
+ `const story = typeof raw === 'function' ? { render: raw } : (raw ?? {});`,
58
+ `const initialArgs = { ...preview.args, ...meta.args, ...story.args };`,
59
+ `const title = meta.title ?? ${JSON.stringify(piece.title)};`,
60
+ `const name = story.name ?? story.storyName ?? ${JSON.stringify(piece.exportName.replace(/([a-z0-9])([A-Z])/g, '$1 $2'))};`,
61
+ `const sanitize = (t) => String(t).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');`,
62
+ `const context = {`,
63
+ ` id: meta.id ? sanitize(meta.id) + '--' + sanitize(name) : sanitize(title) + '--' + sanitize(name),`,
64
+ ` title,`,
65
+ ` name,`,
66
+ ` kind: title,`,
67
+ ` story: name,`,
68
+ ` args: initialArgs,`,
69
+ ` initialArgs,`,
70
+ ` argTypes: { ...preview.argTypes, ...meta.argTypes, ...story.argTypes },`,
71
+ ` parameters: { ...preview.parameters, ...meta.parameters, ...story.parameters },`,
72
+ ` globals: { ...(preview.initialGlobals ?? preview.globals ?? {}) },`,
73
+ ` loaded: {},`,
74
+ ` viewMode: 'story',`,
75
+ ` component: meta.component,`,
76
+ ` canvasElement: document.getElementById(${JSON.stringify(ROOT_ID)}),`,
77
+ `};`,
78
+ `const render = story.render ?? meta.render ?? preview.render ?? ((a) => {`,
79
+ ` if (!meta.component) throw new Error('This piece has no render, and its default export names no component.');`,
80
+ ` return React.createElement(meta.component, a);`,
81
+ `});`,
82
+ `let Current = function Piece() { return render(context.args, context); };`,
83
+ `for (const decorator of [...list(story.decorators), ...list(meta.decorators), ...list(preview.decorators)]) {`,
84
+ ` const Inner = Current;`,
85
+ ` Current = function Decorated() { return decorator(Inner, context); };`,
86
+ `}`,
87
+ `function Root() {`,
88
+ ` const [args, setArgs] = React.useState(initialArgs);`,
89
+ ` React.useEffect(() => { document.body.dataset.rehearseMounted = 'yes'; }, []);`,
90
+ ` context.args = args;`,
91
+ ` globalThis.__rehearseStory = {`,
92
+ ` context,`,
93
+ ` updateArgs: (update) => setArgs((a) => ({ ...a, ...update })),`,
94
+ ` resetArgs: () => setArgs(initialArgs),`,
95
+ ` };`,
96
+ ` return React.createElement(Current);`,
97
+ `}`,
98
+ `class Caught extends React.Component {`,
99
+ ` constructor(props) { super(props); this.state = { error: null }; }`,
100
+ ` static getDerivedStateFromError(error) { return { error }; }`,
101
+ ` componentDidCatch(error) {`,
102
+ ` const said = String(error?.message ?? error).split('\\n')[0] || String(error?.name ?? 'Error');`,
103
+ ` console.error(${JSON.stringify(THREW)} + ': ' + said);`,
104
+ ` }`,
105
+ ` render() {`,
106
+ ` if (!this.state.error) return this.props.children;`,
107
+ ` return React.createElement('pre', { role: 'alert', 'data-rehearse-piece': 'failed' }, String(this.state.error?.message ?? this.state.error));`,
108
+ ` }`,
109
+ `}`,
110
+ `const loaders = [...list(preview.loaders), ...list(meta.loaders), ...list(story.loaders)];`,
111
+ `Promise.all(loaders.map((l) => l(context))).then((results) => {`,
112
+ ` for (const r of results) Object.assign(context.loaded, r ?? {});`,
113
+ ` createRoot(document.getElementById(${JSON.stringify(ROOT_ID)})).render(`,
114
+ ` React.createElement(Caught, null, React.createElement(Root)),`,
115
+ ` );`,
116
+ `}, (error) => {`,
117
+ ` document.getElementById(${JSON.stringify(ROOT_ID)}).innerHTML = '<pre role="alert" data-rehearse-piece="failed"></pre>';`,
118
+ ` document.querySelector('[data-rehearse-piece]').textContent = 'A loader failed: ' + String(error?.message ?? error);`,
119
+ `});`,
120
+ ``,
121
+ ].join('\n');
122
+ }
123
+ export const SHIMS = {
124
+ 'preview-api': [
125
+ `import * as React from 'react';`,
126
+ `const story = () => globalThis.__rehearseStory ?? { context: { args: {}, globals: {}, parameters: {} }, updateArgs() {}, resetArgs() {} };`,
127
+ `export function useArgs() { const s = story(); return [s.context.args, s.updateArgs, s.resetArgs]; }`,
128
+ `export function useGlobals() { const s = story(); return [s.context.globals, () => {}]; }`,
129
+ `export function useParameter(name, fallback) { return story().context.parameters?.[name] ?? fallback; }`,
130
+ `export function useStoryContext() { return story().context; }`,
131
+ `export function useChannel() { return () => {}; }`,
132
+ `export const useState = React.useState, useEffect = React.useEffect, useMemo = React.useMemo, useCallback = React.useCallback, useRef = React.useRef, useReducer = React.useReducer;`,
133
+ `const channel = { emit() {}, on() {}, off() {}, once() {} };`,
134
+ `export const addons = { getChannel: () => channel, getConfig: () => ({}) };`,
135
+ `export function makeDecorator({ wrapper }) { return (options) => (storyFn, context) => wrapper(storyFn, context, { options, parameters: {} }); }`,
136
+ `export function definePreview(p) { return p; }`,
137
+ `export function composeStory(s) { return s; }`,
138
+ `export function setProjectAnnotations() {}`,
139
+ ``,
140
+ ].join('\n'),
141
+ test: [
142
+ `export function fn(impl = () => undefined) { const calls = []; const f = (...a) => { calls.push(a); return impl(...a); }; f.mock = { calls }; f.mockName = () => f; f.mockImplementation = (i) => { impl = i; return f; }; f.mockReturnValue = (v) => { impl = () => v; return f; }; f.mockResolvedValue = (v) => { impl = () => Promise.resolve(v); return f; }; f.getMockName = () => 'fn'; f._isMockFunction = true; return f; }`,
143
+ `const later = () => Promise.resolve();`,
144
+ `const any = new Proxy(function () {}, { get: (_t, k) => (k === 'then' ? undefined : any), apply: () => any });`,
145
+ `export const expect = any, userEvent = any, screen = any, fireEvent = any;`,
146
+ `export const within = () => any, waitFor = later, waitForElementToBeRemoved = later, spyOn = () => fn(), clearAllMocks = () => {}, resetAllMocks = () => {}, restoreAllMocks = () => {}, mocked = (x) => x, isMockFunction = (f) => !!f?._isMockFunction, onMockCall = () => () => {}, sb = any;`,
147
+ ``,
148
+ ].join('\n'),
149
+ actions: [
150
+ `export function action() { return () => {}; }`,
151
+ `export function actions(...names) { return Object.fromEntries(names.flat().map((n) => [n, () => {}])); }`,
152
+ ``,
153
+ ].join('\n'),
154
+ };
155
+ export const SHIMMED = {
156
+ 'storybook/preview-api': 'preview-api',
157
+ '@storybook/preview-api': 'preview-api',
158
+ '@storybook/client-api': 'preview-api',
159
+ '@storybook/addons': 'preview-api',
160
+ '@storybook/manager-api': 'preview-api',
161
+ 'storybook/test': 'test',
162
+ '@storybook/test': 'test',
163
+ 'storybook/actions': 'actions',
164
+ '@storybook/addon-actions': 'actions',
165
+ };
@@ -0,0 +1,12 @@
1
+ export type FoundBy = 'named' | 'convention';
2
+ export interface Found {
3
+ file: string;
4
+ by: FoundBy;
5
+ }
6
+ export declare const CONFIG_CONVENTIONS: readonly string[];
7
+ export declare const STYLES_CONVENTIONS: readonly string[];
8
+ export declare const cacheDirOf: (dir: string, under?: string) => string;
9
+ export declare const SKIPPED_FOLDERS: ReadonlySet<string>;
10
+ export declare function findConfig(dir: string, given?: string | null): Found | null;
11
+ export declare function findStyles(dir: string, given?: string | null): Found | null;
12
+ export declare function findPieces(dir: string): string[];
@@ -0,0 +1,101 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import path from 'node:path';
5
+ import { PIECE_FILE } from './csf.js';
6
+ const EXTENSIONS = ['ts', 'mts', 'js', 'mjs', 'cts', 'cjs'];
7
+ export const CONFIG_CONVENTIONS = ['vite', 'vitest'].flatMap((name) => EXTENSIONS.map((ext) => `${name}.config.${ext}`));
8
+ export const STYLES_CONVENTIONS = ['src/styles.css', 'src/index.css'];
9
+ export const cacheDirOf = (dir, under = tmpdir()) => path.join(under, 'rehearse-pieces', createHash('sha256').update(path.resolve(dir)).digest('hex').slice(0, 16));
10
+ export const SKIPPED_FOLDERS = new Set(['node_modules', 'dist', 'out']);
11
+ const isFile = (file) => {
12
+ try {
13
+ return statSync(file).isFile();
14
+ }
15
+ catch {
16
+ return false;
17
+ }
18
+ };
19
+ const named = (dir, file) => ({ file: path.resolve(dir, file), by: 'named' });
20
+ export function findConfig(dir, given) {
21
+ if (given)
22
+ return named(dir, given);
23
+ for (const name of CONFIG_CONVENTIONS) {
24
+ const file = path.join(dir, name);
25
+ if (isFile(file))
26
+ return { file, by: 'convention' };
27
+ }
28
+ return null;
29
+ }
30
+ function exportedStyles(dir) {
31
+ let manifest;
32
+ try {
33
+ manifest = JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
34
+ }
35
+ catch {
36
+ return null;
37
+ }
38
+ const exports = manifest?.exports;
39
+ if (!exports || typeof exports !== 'object' || Array.isArray(exports))
40
+ return null;
41
+ const entries = Object.entries(exports);
42
+ const target = (value) => {
43
+ if (typeof value === 'string')
44
+ return value;
45
+ if (value && typeof value === 'object') {
46
+ const v = value;
47
+ return target(v.style ?? v.import ?? v.default);
48
+ }
49
+ return null;
50
+ };
51
+ const preferred = ['./styles.css', './style.css', './styles', './style'];
52
+ const ordered = [
53
+ ...preferred.flatMap((key) => entries.filter(([k]) => k === key)),
54
+ ...entries.filter(([k]) => !preferred.includes(k) && k.endsWith('.css')),
55
+ ];
56
+ for (const [, value] of ordered) {
57
+ const file = target(value);
58
+ if (file && file.endsWith('.css') && !file.includes('*') && isFile(path.join(dir, file)))
59
+ return path.join(dir, file);
60
+ }
61
+ return null;
62
+ }
63
+ export function findStyles(dir, given) {
64
+ if (given)
65
+ return named(dir, given);
66
+ const exported = exportedStyles(dir);
67
+ if (exported)
68
+ return { file: exported, by: 'convention' };
69
+ for (const name of STYLES_CONVENTIONS) {
70
+ const file = path.join(dir, name);
71
+ if (isFile(file))
72
+ return { file, by: 'convention' };
73
+ }
74
+ return null;
75
+ }
76
+ export function findPieces(dir) {
77
+ const found = [];
78
+ const walk = (relative) => {
79
+ let entries;
80
+ try {
81
+ entries = readdirSync(path.join(dir, relative), { withFileTypes: true });
82
+ }
83
+ catch {
84
+ return;
85
+ }
86
+ for (const entry of entries) {
87
+ if (entry.name.startsWith('.'))
88
+ continue;
89
+ const child = relative ? `${relative}/${entry.name}` : entry.name;
90
+ if (entry.isDirectory()) {
91
+ if (!SKIPPED_FOLDERS.has(entry.name))
92
+ walk(child);
93
+ }
94
+ else if (entry.isFile() && PIECE_FILE.test(entry.name))
95
+ found.push(child);
96
+ }
97
+ };
98
+ if (existsSync(dir))
99
+ walk('');
100
+ return found.sort();
101
+ }
@@ -0,0 +1,10 @@
1
+ export declare const PIECE_FILE: RegExp;
2
+ export declare const sanitize: (text: string) => string;
3
+ export declare function toId(title: string, name: string): string;
4
+ export declare const storyNameFromExport: (key: string) => string;
5
+ export declare const storyExports: (names: readonly string[]) => string[];
6
+ export declare function titleFromPath(relative: string): string;
7
+ export declare function propertyString(code: string, start: number, key: string): string | null;
8
+ export declare function metaTitle(code: string): string | null;
9
+ export declare function storyExportsOf(code: string): string[];
10
+ export declare function statementAt(code: string, index: number): string;
@@ -0,0 +1,304 @@
1
+ export const PIECE_FILE = /\.stories\.(tsx|jsx|ts|js)$/;
2
+ export const sanitize = (text) => text
3
+ .toLowerCase()
4
+ .replace(/[ ’–—―′¿'`~!@#$%^&*()_|+\-=?;:'",.<>{}[\]\\/]/gi, '-')
5
+ .replace(/-+/g, '-')
6
+ .replace(/^-+/, '')
7
+ .replace(/-+$/, '');
8
+ export function toId(title, name) {
9
+ const kind = sanitize(title);
10
+ const story = sanitize(name);
11
+ if (kind === '')
12
+ throw new Error(`The title "${title}" gives an empty id.`);
13
+ if (story === '')
14
+ throw new Error(`The story "${name}" gives an empty id.`);
15
+ return `${kind}--${story}`;
16
+ }
17
+ export const storyNameFromExport = (key) => key
18
+ .replace(/_/g, ' ')
19
+ .replace(/-/g, ' ')
20
+ .replace(/\./g, ' ')
21
+ .replace(/([^\n])([A-Z])([a-z])/g, (_, a, b, c) => `${a} ${b}${c}`)
22
+ .replace(/([a-z])([A-Z])/g, (_, a, b) => `${a} ${b}`)
23
+ .replace(/([a-z])([0-9])/gi, (_, a, b) => `${a} ${b}`)
24
+ .replace(/([0-9])([a-z])/gi, (_, a, b) => `${a} ${b}`)
25
+ .replace(/(\s|^)(\w)/g, (_, a, b) => `${a}${b.toUpperCase()}`)
26
+ .replace(/ +/g, ' ')
27
+ .trim();
28
+ export const storyExports = (names) => names.filter((name, i) => name !== 'default' && !name.startsWith('__') && names.indexOf(name) === i);
29
+ export function titleFromPath(relative) {
30
+ const parts = relative
31
+ .replace(/\\/g, '/')
32
+ .replace(/^\.\//, '')
33
+ .replace(/^src\//, '')
34
+ .replace(PIECE_FILE, '')
35
+ .split('/')
36
+ .filter(Boolean);
37
+ const last = parts.at(-1);
38
+ const before = parts.at(-2);
39
+ if (parts.length > 1 && last !== undefined) {
40
+ if (last === 'index' || (before !== undefined && last.toLowerCase() === before.toLowerCase()))
41
+ parts.pop();
42
+ }
43
+ return parts.join('/');
44
+ }
45
+ const IDENT = /[A-Za-z_$][\w$]*/y;
46
+ function skipSpace(code, i) {
47
+ for (;;) {
48
+ while (i < code.length && /\s/.test(code[i] ?? ''))
49
+ i += 1;
50
+ if (code.startsWith('//', i)) {
51
+ const end = code.indexOf('\n', i);
52
+ i = end < 0 ? code.length : end + 1;
53
+ }
54
+ else if (code.startsWith('/*', i)) {
55
+ const end = code.indexOf('*/', i + 2);
56
+ i = end < 0 ? code.length : end + 2;
57
+ }
58
+ else
59
+ return i;
60
+ }
61
+ }
62
+ function skipQuoted(code, i) {
63
+ const open = code[i];
64
+ if (open === '"' || open === "'" || open === '`') {
65
+ let j = i + 1;
66
+ while (j < code.length && code[j] !== open)
67
+ j += code[j] === '\\' ? 2 : 1;
68
+ return j + 1;
69
+ }
70
+ return i;
71
+ }
72
+ function stringAt(code, i) {
73
+ const open = code[i];
74
+ if (open !== '"' && open !== "'" && open !== '`')
75
+ return null;
76
+ let out = '';
77
+ let j = i + 1;
78
+ while (j < code.length && code[j] !== open) {
79
+ if (code[j] === '\\') {
80
+ const next = code[j + 1] ?? '';
81
+ out += next === 'n' ? '\n' : next === 't' ? '\t' : next;
82
+ j += 2;
83
+ }
84
+ else {
85
+ if (open === '`' && code.startsWith('${', j))
86
+ return null;
87
+ out += code[j];
88
+ j += 1;
89
+ }
90
+ }
91
+ return j < code.length ? out : null;
92
+ }
93
+ export function propertyString(code, start, key) {
94
+ if (code[start] !== '{')
95
+ return null;
96
+ let depth = 0;
97
+ let expectKey = true;
98
+ for (let i = start; i < code.length;) {
99
+ const skipped = skipQuoted(code, i);
100
+ if (skipped !== i) {
101
+ if (depth === 1 && expectKey) {
102
+ const quoted = stringAt(code, i);
103
+ const after = skipSpace(code, skipped);
104
+ if (quoted === key && code[after] === ':')
105
+ return stringAt(code, skipSpace(code, after + 1));
106
+ }
107
+ expectKey = false;
108
+ i = skipped;
109
+ continue;
110
+ }
111
+ const c = code[i] ?? '';
112
+ if (c === '{' || c === '[' || c === '(') {
113
+ depth += 1;
114
+ expectKey = depth === 1;
115
+ i += 1;
116
+ continue;
117
+ }
118
+ if (c === '}' || c === ']' || c === ')') {
119
+ depth -= 1;
120
+ if (depth === 0)
121
+ return null;
122
+ i += 1;
123
+ continue;
124
+ }
125
+ if (c === ',') {
126
+ expectKey = depth === 1;
127
+ i += 1;
128
+ continue;
129
+ }
130
+ if (depth === 1 && expectKey) {
131
+ IDENT.lastIndex = i;
132
+ const word = IDENT.exec(code);
133
+ if (word) {
134
+ const after = skipSpace(code, i + word[0].length);
135
+ if (word[0] === key && code[after] === ':')
136
+ return stringAt(code, skipSpace(code, after + 1));
137
+ i += word[0].length;
138
+ expectKey = false;
139
+ continue;
140
+ }
141
+ }
142
+ if (!/\s/.test(c))
143
+ expectKey = false;
144
+ i += 1;
145
+ }
146
+ return null;
147
+ }
148
+ function objectOf(code, name) {
149
+ const declared = new RegExp(`\\b(?:const|let|var)\\s+${name.replace(/\$/g, '\\$')}\\s*(?::[^=]*)?=\\s*`, 'g');
150
+ const found = declared.exec(code);
151
+ if (!found)
152
+ return null;
153
+ let i = found.index + found[0].length;
154
+ while (code[i] === '(')
155
+ i = skipSpace(code, i + 1);
156
+ return code[i] === '{' ? i : null;
157
+ }
158
+ export function metaTitle(code) {
159
+ const direct = /\bexport\s+default\s+/g.exec(code);
160
+ if (direct) {
161
+ let i = skipSpace(code, direct.index + direct[0].length);
162
+ while (code[i] === '(')
163
+ i = skipSpace(code, i + 1);
164
+ if (code[i] === '{')
165
+ return propertyString(code, i, 'title');
166
+ IDENT.lastIndex = i;
167
+ const name = IDENT.exec(code)?.[0];
168
+ if (name) {
169
+ const at = objectOf(code, name);
170
+ return at === null ? null : propertyString(code, at, 'title');
171
+ }
172
+ return null;
173
+ }
174
+ const renamed = /\bexport\s*\{[^}]*?\b([A-Za-z_$][\w$]*)\s+as\s+default\b/.exec(code);
175
+ if (renamed?.[1]) {
176
+ const at = objectOf(code, renamed[1]);
177
+ return at === null ? null : propertyString(code, at, 'title');
178
+ }
179
+ return null;
180
+ }
181
+ const uncommented = (code) => code.replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, ' ')).replace(/(^|[^:'"`])\/\/[^\n]*/g, '$1');
182
+ export function storyExportsOf(code) {
183
+ const text = uncommented(code);
184
+ const names = [];
185
+ for (const m of text.matchAll(/\bexport\s+(?:const|let|var)\s+([A-Za-z_$][\w$]*)/g)) {
186
+ names.push(m[1]);
187
+ const statement = statementAt(text, m.index);
188
+ for (const more of topLevelDeclarators(statement))
189
+ names.push(more);
190
+ }
191
+ for (const m of text.matchAll(/\bexport\s+(?:async\s+function\*?|function\*?|class)\s+([A-Za-z_$][\w$]*)/g))
192
+ names.push(m[1]);
193
+ for (const m of text.matchAll(/\bexport\s*\{([^}]*)\}/g))
194
+ for (const part of m[1].split(',')) {
195
+ const entry = part.trim();
196
+ if (!entry || /^type\s/.test(entry))
197
+ continue;
198
+ const [orig, alias] = entry.split(/\s+as\s+/);
199
+ const name = (alias ?? orig ?? '').trim();
200
+ if (name && name !== 'default')
201
+ names.push(name);
202
+ }
203
+ const unique = [...new Set(names)].filter((n) => n !== 'default' && !n.startsWith('__'));
204
+ return storiesOnly(unique, text);
205
+ }
206
+ function topLevelDeclarators(statement) {
207
+ const out = [];
208
+ let depth = 0;
209
+ for (let i = 0; i < statement.length; i += 1) {
210
+ const c = statement[i];
211
+ if (c === '"' || c === "'" || c === '`') {
212
+ for (i += 1; i < statement.length && statement[i] !== c; i += statement[i] === '\\' ? 2 : 1)
213
+ ;
214
+ continue;
215
+ }
216
+ if ('([{'.includes(c))
217
+ depth += 1;
218
+ else if (')]}'.includes(c))
219
+ depth -= 1;
220
+ else if (c === ',' && depth === 0) {
221
+ const next = /^\s*([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=/.exec(statement.slice(i + 1));
222
+ if (next)
223
+ out.push(next[1]);
224
+ }
225
+ }
226
+ return out;
227
+ }
228
+ function storiesOnly(names, code) {
229
+ const rule = (key) => {
230
+ const m = new RegExp(`\\b${key}\\s*:\\s*(\\[[^\\]]*\\]|\\/(?:\\\\.|[^/\\n])+\\/[a-z]*)`).exec(code);
231
+ if (!m)
232
+ return null;
233
+ const v = m[1];
234
+ if (v.startsWith('[')) {
235
+ const listed = [...v.matchAll(/['"`]([^'"`]+)['"`]/g)].map((x) => x[1]);
236
+ return (n) => listed.includes(n);
237
+ }
238
+ const end = v.lastIndexOf('/');
239
+ try {
240
+ const re = new RegExp(v.slice(1, end), v.slice(end + 1));
241
+ return (n) => re.test(n);
242
+ }
243
+ catch {
244
+ return null;
245
+ }
246
+ };
247
+ const include = rule('includeStories');
248
+ const exclude = rule('excludeStories');
249
+ return names.filter((n) => (!include || include(n)) && !(exclude && exclude(n)));
250
+ }
251
+ export function statementAt(code, index) {
252
+ let depth = 0;
253
+ let last = '';
254
+ for (let i = index; i < code.length; i += 1) {
255
+ const c = code[i];
256
+ if ((c === '"' || c === "'" || c === '`') &&
257
+ (!/[\w)\]]/.test(last) ||
258
+ /\b(?:return|case|typeof|in|of|yield|await|void|else)$/.test(code.slice(Math.max(index, i - 8), i).trimEnd()))) {
259
+ for (i += 1; i < code.length && code[i] !== c; i += code[i] === '\\' ? 2 : 1)
260
+ ;
261
+ last = c;
262
+ continue;
263
+ }
264
+ if (code.startsWith('//', i)) {
265
+ i = code.indexOf('\n', i);
266
+ if (i < 0)
267
+ return code.slice(index);
268
+ i -= 1;
269
+ continue;
270
+ }
271
+ if (code.startsWith('/*', i)) {
272
+ i = code.indexOf('*/', i + 2) + 1;
273
+ if (i <= 0)
274
+ return code.slice(index);
275
+ continue;
276
+ }
277
+ if (c === '/' && /^$|[=(,:;!&|?{}[\]+\-*%~^]/.test(last)) {
278
+ let inClass = false;
279
+ for (i += 1; i < code.length && code[i] !== '\n'; i += 1) {
280
+ if (code[i] === '\\')
281
+ i += 1;
282
+ else if (code[i] === '[')
283
+ inClass = true;
284
+ else if (code[i] === ']')
285
+ inClass = false;
286
+ else if (code[i] === '/' && !inClass)
287
+ break;
288
+ }
289
+ last = 'a';
290
+ continue;
291
+ }
292
+ if ('([{'.includes(c))
293
+ depth += 1;
294
+ else if (')]}'.includes(c))
295
+ depth -= 1;
296
+ else if (depth === 0 && c === ';')
297
+ return code.slice(index, i + 1);
298
+ else if (depth === 0 && c === '\n' && /^[A-Za-z@]/.test(code.slice(i + 1, i + 2)))
299
+ return code.slice(index, i);
300
+ if (!/\s/.test(c))
301
+ last = c;
302
+ }
303
+ return code.slice(index);
304
+ }
@@ -0,0 +1,12 @@
1
+ import type { Browser } from 'playwright-core';
2
+ import { type Scenario } from '../../scenarios/index.ts';
3
+ import { type Ran, type RunContext } from '../kind.ts';
4
+ export interface PieceSpec {
5
+ exportName: string;
6
+ title: string | null;
7
+ storyId: string | null;
8
+ packageDir: string | null;
9
+ inPackage: string;
10
+ }
11
+ export declare function packageOf(checkout: string, abs: string): string | null;
12
+ export declare function runPieces(scenarios: readonly Scenario[], ctx: RunContext, browser?: Browser): Promise<Map<string, Ran>>;