Skip to content

Explainer videos and product demos made by your AI agent. Free and open source: a local voice (Kokoro), word timing (Whisper) and a canvas renderer turn a script into a narrated MP4.

Keep init from leaving a folder behind after a bad setting

Vincent Schmalbach1de4ec9bc44f
diff --git a/src/cli.js b/src/cli.js
index b67a805..6cd4404 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -212,22 +212,31 @@ export async function main(argv) {
}
}
+// A text flag given without a value (for example "--title" at the end of the
+// line) parses as true. Name the flag instead of failing deep in string code.
+function textFlag(flags, name) {
+ if (flags[name] === true) throw new ProjectError(`--${name} needs a value`);
+ return flags[name];
+}
+
function init(dir, flags, print) {
if (!dir) throw new ProjectError('usage: explainroo init <dir> [--theme paper] [--size youtube|tiktok|linkedin|...] [--pace 1.2] [--voice af_heart] [--title "..."]');
const target = path.resolve(dir);
if (fs.existsSync(path.join(target, 'video.json'))) throw new ProjectError(`${target} already has a video.json`);
- const theme = flags.theme || 'paper';
+ const theme = textFlag(flags, 'theme') || 'paper';
if (!THEMES.includes(theme)) throw new ProjectError(`theme must be one of ${THEMES.join(', ')}`);
- const voice = flags.voice || 'af_heart';
+ const voice = textFlag(flags, 'voice') || 'af_heart';
if (!VOICES[voice]) throw new ProjectError(`voice "${voice}" does not exist. Run "explainroo voices".`);
- const title = flags.title || path.basename(target).replace(/[-_]+/g, ' ').replace(/^\w/, (c) => c.toUpperCase());
- fs.mkdirSync(path.join(target, 'assets'), { recursive: true });
+ const title = textFlag(flags, 'title') || path.basename(target).replace(/[-_]+/g, ' ').replace(/^\w/, (c) => c.toUpperCase());
const tpl = path.join(ROOT, 'templates', 'starter');
- const size = flags.size || '16:9';
+ const size = textFlag(flags, 'size') || '16:9';
resolveSize(size);
const config = { title, theme, size, voice, music: true, captions: 'auto' };
if (flags.pace !== undefined) config.pace = num(flags.pace, 'pace');
normalizeConfig(config);
+ // Create the folder only after every setting checks out, so a bad flag
+ // leaves nothing behind.
+ fs.mkdirSync(path.join(target, 'assets'), { recursive: true });
fs.writeFileSync(path.join(target, 'video.json'), JSON.stringify(config, null, 2) + '\n');
fs.writeFileSync(path.join(target, 'script.md'), fs.readFileSync(path.join(tpl, 'script.md'), 'utf8').replace('{{title}}', title));
fs.writeFileSync(path.join(target, 'scenes.js'), fs.readFileSync(path.join(tpl, 'scenes.js'), 'utf8').replace(/\{\{title\}\}/g, title.replace(/'/g, "\\'")));
diff --git a/test/cli.test.js b/test/cli.test.js
new file mode 100644
index 0000000..a13bec1
--- /dev/null
+++ b/test/cli.test.js
@@ -0,0 +1,63 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { main } from '../src/cli.js';
+
+// Run the CLI in-process and collect what it wrote to stderr.
+async function run(args) {
+ const write = process.stderr.write;
+ let err = '';
+ process.stderr.write = (chunk) => { err += chunk; return true; };
+ try {
+ const code = await main(args);
+ return { code, err };
+ } finally {
+ process.stderr.write = write;
+ }
+}
+
+function tempDir() {
+ return fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-cli-'));
+}
+
+test('init rejects an unknown theme and creates no folder', async () => {
+ const base = tempDir();
+ const target = path.join(base, 'video');
+ const r = await run(['init', target, '--theme', 'nope']);
+ assert.equal(r.code, 1);
+ assert.match(r.err, /theme must be one of/);
+ assert.equal(fs.existsSync(target), false);
+});
+
+test('init rejects a bad pace without leaving a folder behind', async () => {
+ const base = tempDir();
+ const target = path.join(base, 'video');
+ const r = await run(['init', target, '--pace', 'fast']);
+ assert.equal(r.code, 1);
+ assert.match(r.err, /--pace must be a number/);
+ assert.equal(fs.existsSync(target), false);
+});
+
+test('init names a text flag that was given without a value', async () => {
+ const base = tempDir();
+ const target = path.join(base, 'video');
+ const r = await run(['init', target, '--title']);
+ assert.equal(r.code, 1);
+ assert.match(r.err, /--title needs a value/);
+ assert.equal(fs.existsSync(target), false);
+});
+
+test('init with valid settings writes video.json and the starter files', async () => {
+ const base = tempDir();
+ const target = path.join(base, 'my-first-video');
+ const r = await run(['init', target, '--theme', 'clean', '--pace', '1.2']);
+ assert.equal(r.code, 0);
+ const config = JSON.parse(fs.readFileSync(path.join(target, 'video.json'), 'utf8'));
+ assert.equal(config.theme, 'clean');
+ assert.equal(config.pace, 1.2);
+ assert.equal(config.title, 'My first video');
+ assert.ok(fs.existsSync(path.join(target, 'assets')));
+ assert.ok(fs.existsSync(path.join(target, 'script.md')));
+});