@navipartner/demo-video 0.0.0-stage → 0.4.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/README.md CHANGED
@@ -1,3 +1,109 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @navipartner/demo-video
2
+
3
+ Marketing demo videos of a React app, filmed by Playwright on the app's own
4
+ e2e mocks, in the NaviPartner house look, with any customer's brand. For
5
+ pnpm repos on GitHub Actions.
6
+
7
+ ## Setup
8
+
9
+ The package is public on npm, so nothing needs a token or registry setup.
10
+
11
+ ### Once per project repo
12
+
13
+ From the repo root:
14
+
15
+ npx --ignore-scripts @navipartner/demo-video init
16
+
17
+ It sets up `.gitignore`, the skill, the `demo video` label and
18
+ `.github/workflows/demo-video.yml`. Safe to re-run; `--dry-run` previews.
19
+ (`--ignore-scripts` skips downloading ffmpeg, which `init` doesn't need.)
20
+
21
+ ### Per app
22
+
23
+ npx --ignore-scripts @navipartner/demo-video init --app apps/<name>
24
+ pnpm install
25
+
26
+ Then ask Claude for a demo video of the app. It finishes
27
+ `demo/demo.config.ts` from the e2e tests and writes the app's primary story.
28
+ The app needs `@playwright/test` 1.59 or later.
29
+
30
+ ## Getting a video
31
+
32
+ | Way | Who | Films | Where the video lands |
33
+ |---|---|---|---|
34
+ | **Run workflow form:** Actions → Demo video → Run workflow; fill in App, Story, Brand (each may be empty) | anyone who can write to the repo | an existing story with a brand, from the branch chosen | the run's Summary page |
35
+ | **PR comment:** `/demo-video checkout in acme branding` | members, on any PR | an existing story with a brand, from the PR's branch | a reply on the PR |
36
+ | **The `demo video` label** on a PR | whoever labels it | every story the PR adds or changes, on every push | a comment on the PR |
37
+ | **Locally:** `pnpm --dir apps/<name> exec demo-video render [--story <name>] [--brand <name>]` | a developer | any story | `demo/out/` |
38
+
39
+ - An empty story means the app's primary story; an empty brand, the app's
40
+ default. A request that doesn't name one existing story, or names a brand
41
+ that doesn't exist, gets the list of stories and brands instead (the form
42
+ run is then marked failed, so a typo is noticed).
43
+ - The form and the comment only choose: they never write or change a story.
44
+ New stories are written by a developer, locally (ask Claude, which uses
45
+ the skill), and reviewed through the label. A PR has no default video.
46
+
47
+ ## Stories and brands
48
+
49
+ - **Stories** are `<app>/demo/stories/<name>.json`, built only from what the
50
+ e2e mocks return. One per app is `"primary": true`: the journey the app
51
+ exists for.
52
+ - **Variants** (`checkout.acme.json`) are a story in a customer's words:
53
+ the base story's steps, with its own title, closing and card text, filmed
54
+ with one brand. Asking for the story in that brand films the variant.
55
+ - **Brands** are `<app>/demo/brands/<name>.json` plus a folder of assets;
56
+ the engine ships `navipartner`, the default.
57
+
58
+ Claude writes both with the skill (`skills/np-demo-video/`), which also holds
59
+ the look and every field.
60
+
61
+ ## Commands
62
+
63
+ Run in the app: `pnpm --dir apps/<name> exec demo-video <command>`.
64
+
65
+ | Command | |
66
+ |---|---|
67
+ | `list` | The stories, primary first. |
68
+ | `check` | Dry run in seconds. Fails on anything that would film badly (a zoom on the wrong area, a click whose result is off-screen, code or unproven numbers on a card) and names the step. |
69
+ | `render` | Films the story frame by frame (60 fps, about 5× its length) to `demo/out/<story>-<brand>-demo.mp4`, with a contact sheet (`review/sheet.jpg`, one frame a second) and ten stills. |
70
+ | `record`, `encode`, `stills` | `render` in parts: record only, encode the last take, redo the stills. |
71
+
72
+ `--story <name>` (default: primary), `--brand <name>` (default:
73
+ `navipartner`), `--draft` (`render` at 30 fps in about half the time, while
74
+ tuning), `--json` (machine-readable output).
75
+
76
+ ## Developing and releasing
77
+
78
+ pnpm install
79
+ pnpm test && pnpm typecheck && pnpm build
80
+
81
+ **Release:** bump `version` in `package.json` on `main`, then
82
+ `git tag vX.Y.Z && git push origin vX.Y.Z`. That publishes it to npm as
83
+ `latest` and moves the `v0` tag, which every project's workflow follows.
84
+ Running `release.yml` by hand publishes a `next` prerelease.
85
+
86
+ **The first release, once, by an npm owner of the `navipartner` org:**
87
+ trusted publishing can only be set up for a package that exists.
88
+
89
+ 1. On main at the release commit: `pnpm install && pnpm pack`, then
90
+ `node scripts/check-public.mjs navipartner-demo-video-*.tgz`.
91
+ 2. `npm login`, then `npm publish navipartner-demo-video-*.tgz --access public`.
92
+ 3. On npmjs.com, the package → Settings → Trusted publisher: GitHub Actions,
93
+ repository `navipartner/demo-video`, workflow `release.yml`. Then set
94
+ "Publishing access" to require trusted publishing (no tokens).
95
+ 4. Push the tag as usual: `release.yml` sees the version is already on npm,
96
+ skips publishing and moves `v0`.
97
+
98
+ The package is public; this repo is not. `release.yml` publishes through npm
99
+ trusted publishing (no npm token is stored), and only after
100
+ `scripts/check-public.mjs` has checked the tarball: only the allowed files,
101
+ no source maps, nothing shaped like a secret, an email or a local path. CI
102
+ runs the same check on every PR. Names it can't catch: keep customers and
103
+ people out of the skill and the README.
104
+
105
+ Projects get a new CLI only with `pnpm up -r --latest @navipartner/demo-video`
106
+ (their lockfile pins it), but their workflow follows `v0` at once. So
107
+ `render.yml` and `request.yml` must work with every CLI
108
+ from 0.4 (older ones get a reply asking for the update); a change that
109
+ can't is `v1`.
package/bin/args.d.mts ADDED
@@ -0,0 +1,30 @@
1
+ export interface DemoArgs {
2
+ command: "check" | "render" | "record" | "encode" | "stills" | "list";
3
+ appDir: string;
4
+ demoDir: string;
5
+ adapterPath: string;
6
+ workDir: string;
7
+ runnerDir: string;
8
+ configPath: string;
9
+ outDir: string;
10
+ storyboardPath: string | null;
11
+ brand: string | null;
12
+ json: boolean;
13
+ draft: boolean;
14
+ }
15
+ export const COMMANDS: readonly string[];
16
+ export function parseArgs(argv: string[], cwd: string): DemoArgs;
17
+ export interface StoryEntry {
18
+ name: string;
19
+ description: string | null;
20
+ primary: boolean;
21
+ /** A variant: the story it extends, and the brand it is for. */
22
+ extends?: string;
23
+ brand?: string | null;
24
+ }
25
+ export function listStories(demoDir: string): StoryEntry[];
26
+ export function resolveStoryboard(
27
+ value: string,
28
+ cwd: string,
29
+ demoDir: string,
30
+ ): string;
package/bin/args.mjs ADDED
@@ -0,0 +1,153 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ export const COMMANDS = [
5
+ "check",
6
+ "render",
7
+ "record",
8
+ "encode",
9
+ "stills",
10
+ "list",
11
+ ];
12
+ const NEEDS_STORYBOARD = new Set(["check", "render", "record"]);
13
+ const USAGE = `Usage: demo-video <${COMMANDS.join("|")}> [--app <dir>] [--story <name|file.json>] [--brand <name>] [--draft] [--json]
14
+ Without --story, check, render and record use the app's primary story.
15
+ --draft renders at 30 fps (about half the time) to iterate on a story.
16
+ Setup: demo-video init [--app <dir>] [--dry-run]
17
+ For a /demo-video request: demo-video pick --request "<text>" [--json] <app dir>...`;
18
+
19
+ export const resolveStoryboard = (value, cwd, demoDir) => {
20
+ if (value.endsWith(".json")) {
21
+ const file = path.resolve(cwd, value);
22
+ if (!fs.existsSync(file)) {
23
+ throw new Error(`Story file ${file} does not exist`);
24
+ }
25
+ return file;
26
+ }
27
+ const dir = path.join(demoDir, "stories");
28
+ const file = path.join(dir, `${value}.json`);
29
+ if (!fs.existsSync(file)) {
30
+ const known = fs.existsSync(dir)
31
+ ? fs
32
+ .readdirSync(dir)
33
+ .filter((f) => f.endsWith(".json"))
34
+ .map((f) => f.slice(0, -".json".length))
35
+ : [];
36
+ throw new Error(
37
+ `No story "${value}" in ${dir}. Known: ${known.join(", ") || "(none)"}`,
38
+ );
39
+ }
40
+ return file;
41
+ };
42
+
43
+ /**
44
+ * The app's stories, primary first, then by name. Reads only `description`
45
+ * and `primary`: the full check needs the adapter and runs in `check`.
46
+ */
47
+ export const listStories = (demoDir) => {
48
+ const dir = path.join(demoDir, "stories");
49
+ if (!fs.existsSync(dir)) {
50
+ return [];
51
+ }
52
+ const stories = fs
53
+ .readdirSync(dir)
54
+ .filter((f) => f.endsWith(".json"))
55
+ .map((f) => {
56
+ const story = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8"));
57
+ return {
58
+ name: f.slice(0, -".json".length),
59
+ description: story.description ?? null,
60
+ primary: story.primary === true,
61
+ // A variant: the base story's flow, for one brand.
62
+ ...(story.extends
63
+ ? { extends: story.extends, brand: story.brand ?? null }
64
+ : {}),
65
+ };
66
+ })
67
+ .sort(
68
+ (a, b) =>
69
+ Number(b.primary) - Number(a.primary) || a.name.localeCompare(b.name),
70
+ );
71
+ const primaries = stories.filter((s) => s.primary).map((s) => s.name);
72
+ if (primaries.length > 1) {
73
+ throw new Error(
74
+ `Only one story can be primary; ${primaries.join(", ")} all are (in ${dir})`,
75
+ );
76
+ }
77
+ return stories;
78
+ };
79
+
80
+ export const parseArgs = (argv, cwd) => {
81
+ const [command, ...rest] = argv;
82
+ if (!COMMANDS.includes(command)) {
83
+ throw new Error(USAGE);
84
+ }
85
+ const opts = {
86
+ app: ".",
87
+ story: undefined,
88
+ brand: undefined,
89
+ json: false,
90
+ draft: false,
91
+ };
92
+ for (let i = 0; i < rest.length; i++) {
93
+ const arg = rest[i];
94
+ if (arg === "--json") {
95
+ opts.json = true;
96
+ } else if (arg === "--draft") {
97
+ opts.draft = true;
98
+ } else if (["--app", "--story", "--brand"].includes(arg)) {
99
+ const value = rest[++i];
100
+ if (!value) {
101
+ throw new Error(`${arg} needs a value`);
102
+ }
103
+ opts[arg.slice(2)] = value;
104
+ } else {
105
+ throw new Error(`Unknown option ${arg}\n${USAGE}`);
106
+ }
107
+ }
108
+ const appDir = path.resolve(cwd, opts.app);
109
+ const demoDir = path.join(appDir, "demo");
110
+ const adapterPath = path.join(demoDir, "demo.config.ts");
111
+ if (!fs.existsSync(adapterPath)) {
112
+ throw new Error(`No demo adapter: expected ${adapterPath}`);
113
+ }
114
+ // Everything a run needs only while it runs (the generated Playwright
115
+ // config and spec, the check report, the raw recording, Playwright's
116
+ // results) goes here, inside demo/out/ (gitignored), and is deleted when
117
+ // the command ends. Only a failure's results are kept, in demo/out/failed/.
118
+ // `check` has its own folder, so it never touches the take that `record`
119
+ // leaves for `encode`.
120
+ const workDir = path.join(
121
+ demoDir,
122
+ "out",
123
+ ".work",
124
+ command === "check" ? "check" : "take",
125
+ );
126
+ const runnerDir = path.join(workDir, "runner");
127
+ let storyboardPath = null;
128
+ if (NEEDS_STORYBOARD.has(command)) {
129
+ // No --story: the app's primary story, its main journey.
130
+ const story =
131
+ opts.story ?? listStories(demoDir).find((s) => s.primary)?.name;
132
+ if (!story) {
133
+ throw new Error(
134
+ `No --story given and no primary story in ${path.join(demoDir, "stories")}. Name one with --story, or mark the app's main journey "primary": true.`,
135
+ );
136
+ }
137
+ storyboardPath = resolveStoryboard(story, cwd, demoDir);
138
+ }
139
+ return {
140
+ command,
141
+ appDir,
142
+ demoDir,
143
+ adapterPath,
144
+ workDir,
145
+ runnerDir,
146
+ configPath: path.join(runnerDir, "playwright.config.mjs"),
147
+ outDir: path.join(demoDir, "out"),
148
+ storyboardPath,
149
+ brand: opts.brand ?? null,
150
+ json: opts.json,
151
+ draft: opts.draft,
152
+ };
153
+ };
@@ -0,0 +1,272 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console -- a CLI; the console is its output */
3
+ import { spawnSync } from "node:child_process";
4
+ import fs from "node:fs";
5
+ import { createRequire } from "node:module";
6
+ import path from "node:path";
7
+
8
+ import { listStories, parseArgs } from "./args.mjs";
9
+ import { encodeRecording, extractStillsFrom } from "./encode.mjs";
10
+ import { initApp, initRepo } from "./init.mjs";
11
+ import { appMenu, menuMarkdown, pick, pickExact } from "./pick.mjs";
12
+ import { writeRunner } from "./runner.mjs";
13
+
14
+ // Runs the app's own Playwright (resolved from the app, never from this
15
+ // package) against the config this CLI writes into demo/out/.work/runner/.
16
+
17
+ // A run's work folder goes when the command ends. A failure first keeps
18
+ // Playwright's results (its error context) in demo/out/failed/; a success
19
+ // clears an older failure, so what's there is always the latest.
20
+ const finish = (args, ok) => {
21
+ const failed = path.join(args.outDir, "failed");
22
+ fs.rmSync(failed, { recursive: true, force: true });
23
+ const results = path.join(args.workDir, "test-results");
24
+ if (!ok && fs.existsSync(results)) {
25
+ fs.cpSync(results, failed, { recursive: true });
26
+ console.error(`Playwright's output for this failure: ${failed}`);
27
+ }
28
+ fs.rmSync(args.workDir, { recursive: true, force: true });
29
+ const work = path.dirname(args.workDir);
30
+ if (fs.existsSync(work) && fs.readdirSync(work).length === 0) {
31
+ fs.rmdirSync(work);
32
+ }
33
+ };
34
+
35
+ const runPlaywright = (args, mode) => {
36
+ const require = createRequire(path.join(args.appDir, "package.json"));
37
+ const cli = require.resolve("@playwright/test/cli");
38
+ fs.rmSync(args.workDir, { recursive: true, force: true });
39
+ fs.mkdirSync(args.workDir, { recursive: true });
40
+ writeRunner(args);
41
+ const result = spawnSync(
42
+ process.execPath,
43
+ [cli, "test", "-c", args.configPath],
44
+ {
45
+ cwd: args.appDir,
46
+ // With --json, Playwright's own output goes to stderr so stdout is pure JSON.
47
+ stdio: ["inherit", args.json ? 2 : "inherit", "inherit"],
48
+ env: {
49
+ ...process.env,
50
+ DEMO_MODE: mode,
51
+ DEMO_STORYBOARD: args.storyboardPath ?? "",
52
+ DEMO_BRAND: args.brand ?? "",
53
+ DEMO_OUT: args.outDir,
54
+ DEMO_WORK: args.workDir,
55
+ DEMO_DRAFT: args.draft ? "1" : "",
56
+ },
57
+ },
58
+ );
59
+ return result.status ?? 1;
60
+ };
61
+
62
+ const printReport = (report) => {
63
+ if (report.ok) {
64
+ console.log(`✓ story passes (${report.steps} steps)`);
65
+ return;
66
+ }
67
+ const where = report.step
68
+ ? `step ${report.step} (${report.do}${report.target ? ` ${report.target}` : ""})`
69
+ : "before the first step";
70
+ console.log(`✗ ${where}: ${report.error}`);
71
+ if (report.visible?.length) {
72
+ console.log(` visible testids: ${report.visible.join(", ")}`);
73
+ }
74
+ };
75
+
76
+ // demo-video init [--app <dir>] [--dry-run]: needs no demo/ yet, so it skips
77
+ // parseArgs.
78
+ const runInit = (argv) => {
79
+ const dryRun = argv.includes("--dry-run");
80
+ const at = argv.indexOf("--app");
81
+ const value = at === -1 ? null : argv[at + 1];
82
+ // A flag after --app (--app --dry-run) is not its directory.
83
+ const appMissing = at !== -1 && (!value || value.startsWith("-"));
84
+ const app = appMissing ? null : value;
85
+ const unknown = argv.filter(
86
+ (a, i) =>
87
+ a !== "--dry-run" && a !== "--app" && !(app && argv[i - 1] === "--app"),
88
+ );
89
+ if (appMissing || unknown.length) {
90
+ // Name what was wrong: "-app" for "--app", or a second command on the line.
91
+ const problem = appMissing
92
+ ? "--app needs a directory right after it"
93
+ : `Unknown argument${unknown.length > 1 ? "s" : ""}: ${unknown.join(" ")}`;
94
+ console.error(
95
+ `${problem}\nUsage: demo-video init [--app <dir>] [--dry-run]`,
96
+ );
97
+ return 2;
98
+ }
99
+ const root = process.cwd();
100
+ const result = app
101
+ ? initApp(path.resolve(root, app), { dryRun })
102
+ : initRepo(root, { dryRun });
103
+ console.log(
104
+ `demo-video init${dryRun ? " (dry run: nothing written)" : ""}: ${app ?? "repo"}`,
105
+ );
106
+ const width = Math.max(...result.changes.map(([, what]) => what.length));
107
+ for (const [status, what, detail] of result.changes) {
108
+ console.log(` ${status.padEnd(8)} ${what.padEnd(width)} ${detail}`);
109
+ }
110
+ if (result.todo.length) {
111
+ console.log("\nBy hand:");
112
+ for (const item of result.todo) {
113
+ console.log(` - ${item}`);
114
+ }
115
+ }
116
+ return 0;
117
+ };
118
+
119
+ // demo-video pick [--request "<text>" | --app <a> --story <s> --brand <b>]
120
+ // [--json] <app dir>...: which existing story and brand a request means
121
+ // (see pick.mjs). --request reads a sentence (a /demo-video comment); the
122
+ // flags are exact names (the Run workflow form).
123
+ const runPick = (argv) => {
124
+ const flags = {};
125
+ const dirs = [];
126
+ let json = false;
127
+ for (let i = 0; i < argv.length; i++) {
128
+ const arg = argv[i];
129
+ if (arg === "--json") {
130
+ json = true;
131
+ } else if (["--request", "--app", "--story", "--brand"].includes(arg)) {
132
+ flags[arg.slice(2)] = argv[++i] ?? "";
133
+ } else {
134
+ dirs.push(arg);
135
+ }
136
+ }
137
+ if (dirs.length === 0) {
138
+ console.error(
139
+ 'Usage: demo-video pick [--request "<text>" | --app <a> --story <s> --brand <b>] [--json] <app dir>...',
140
+ );
141
+ return 2;
142
+ }
143
+ const apps = dirs.map((d) => appMenu(path.resolve(d)));
144
+ const result =
145
+ flags.request !== undefined
146
+ ? pick(flags.request, apps)
147
+ : pickExact(apps, flags);
148
+ const out = result.menu
149
+ ? {
150
+ menu: true,
151
+ reason: result.reason,
152
+ markdown: menuMarkdown(result.apps ?? apps, result.reason),
153
+ }
154
+ : result;
155
+ if (json) {
156
+ console.log(JSON.stringify(out));
157
+ } else if (out.menu) {
158
+ console.log(out.markdown);
159
+ } else {
160
+ console.log(
161
+ `${out.app}: story ${out.story}, brand ${out.brand ?? "(the app's default)"}`,
162
+ );
163
+ }
164
+ return 0;
165
+ };
166
+
167
+ const run = () => {
168
+ if (process.argv[2] === "init") {
169
+ return runInit(process.argv.slice(3));
170
+ }
171
+ if (process.argv[2] === "pick") {
172
+ return runPick(process.argv.slice(3));
173
+ }
174
+ let args;
175
+ try {
176
+ args = parseArgs(process.argv.slice(2), process.cwd());
177
+ } catch (error) {
178
+ console.error(error.message);
179
+ return 2;
180
+ }
181
+ switch (args.command) {
182
+ case "check": {
183
+ const status = runPlaywright(args, "check");
184
+ // The report comes back through the work folder and goes with it.
185
+ const reportPath = path.join(args.workDir, "check.json");
186
+ const report = fs.existsSync(reportPath)
187
+ ? JSON.parse(fs.readFileSync(reportPath, "utf8"))
188
+ : {
189
+ ok: false,
190
+ error:
191
+ "the dry run produced no report (Playwright did not run the demo test — see its output above)",
192
+ };
193
+ const ok = report.ok && status === 0;
194
+ finish(args, ok);
195
+ if (args.json) {
196
+ console.log(JSON.stringify(report, null, 2));
197
+ } else {
198
+ printReport(report);
199
+ }
200
+ return ok ? 0 : 1;
201
+ }
202
+ case "record": {
203
+ // The recording stays in the work folder for `encode`.
204
+ const status = runPlaywright(args, "record");
205
+ if (status !== 0) {
206
+ finish(args, false);
207
+ }
208
+ return status;
209
+ }
210
+ case "render": {
211
+ const status = runPlaywright(args, "record");
212
+ if (status !== 0) {
213
+ finish(args, false);
214
+ return status;
215
+ }
216
+ fs.mkdirSync(args.outDir, { recursive: true });
217
+ const result = encodeRecording(args.workDir, args.outDir);
218
+ finish(args, true);
219
+ if (args.json) {
220
+ // For CI: { output, duration, stills, sheet } on stdout, nothing else.
221
+ console.log(JSON.stringify(result));
222
+ } else {
223
+ console.log(`Wrote ${result.output} (${result.duration.toFixed(1)} s)`);
224
+ console.log(`Review: ${result.sheet} (one frame a second)`);
225
+ }
226
+ return 0;
227
+ }
228
+ case "encode": {
229
+ fs.mkdirSync(args.outDir, { recursive: true });
230
+ const { output, duration } = encodeRecording(args.workDir, args.outDir);
231
+ finish(args, true);
232
+ console.log(`Wrote ${output} (${duration.toFixed(1)} s)`);
233
+ return 0;
234
+ }
235
+ case "list": {
236
+ // For people and the Linear agent: which stories exist, which is primary.
237
+ const stories = listStories(args.demoDir);
238
+ if (args.json) {
239
+ console.log(JSON.stringify(stories));
240
+ } else if (stories.length === 0) {
241
+ console.log("No stories yet in demo/stories/.");
242
+ } else {
243
+ for (const s of stories) {
244
+ console.log(
245
+ `${s.primary ? "* " : " "}${s.name}${s.description ? `: ${s.description}` : ""}${s.extends ? ` (${s.extends}, for ${s.brand})` : ""}`,
246
+ );
247
+ }
248
+ }
249
+ return 0;
250
+ }
251
+ case "stills":
252
+ console.log(`Stills from ${extractStillsFrom(args.outDir)}`);
253
+ return 0;
254
+ }
255
+ return 2;
256
+ };
257
+
258
+ const main = () => {
259
+ try {
260
+ return run();
261
+ } catch (error) {
262
+ const message = error instanceof Error ? error.message : String(error);
263
+ if (process.argv.includes("--json")) {
264
+ console.log(JSON.stringify({ ok: false, error: message }));
265
+ } else {
266
+ console.error(message);
267
+ }
268
+ return 1;
269
+ }
270
+ };
271
+
272
+ process.exitCode = main();