#!/usr/bin/env node // SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 /** * Start a new exercise whose Mojo you write in a plain .mojo file. * * pixi run new-exercise my-thing * node scripts/new-exercise.mjs my-thing --title "My thing" --group "Loop forms" # with a title and group * * If library/mojo/local/my-thing.mojo already exists it is kept as written and only the * exercise is made, so you can start from a .mojo file. Otherwise it creates * library/mojo/local/my-thing.mojo a runnable skeleton with the header verify.py reads * src/exercises/my-thing.ts an exercise that names it (`mojoProgram`) * and registers the exercise in src/exercises/index.ts (before the demo playground, * which stays last). Then `pixi run play my-thing` runs it, checks it on godbolt, * builds it into the app, and tests everything. */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = fileURLToPath(new URL('..', import.meta.url)); const argv = process.argv.slice(2); const flag = (name) => { const i = argv.indexOf(`--${name}`); return i >= 0 ? argv[i + 1] : undefined; }; const id = argv.find((a, i) => !a.startsWith('--') && !(i > 0 && argv[i - 1].startsWith('--'))); function die(msg) { console.error(`new: ${msg}`); process.exit(1); } if (!id) die('usage: pixi run new-exercise \n or: node scripts/new-exercise.mjs [--title "Title"] [--group "Home-page section"]\n is lowercase words joined by hyphens, e.g. rolling-sum'); if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(id)) die(`"${id}" is not a valid id: lowercase letters and digits, joined by single hyphens`); const mojoPath = path.join(ROOT, 'library/mojo/local', `${id}.mojo`); const tsPath = path.join(ROOT, 'src/exercises', `${id}.ts`); const indexPath = path.join(ROOT, 'src/exercises/index.ts'); // A .mojo file you already wrote is adopted as it is: only the exercise is made. const adopt = fs.existsSync(mojoPath); if (fs.existsSync(tsPath)) die(`${path.relative(ROOT, tsPath)} already exists; nothing was changed`); const title = flag('title') ?? id.charAt(0).toUpperCase() + id.slice(1).replace(/-/g, ' '); const group = flag('group') ?? 'Your own'; const ident = (/^\d/.test(id) ? 'x-' : '') + id; const camel = ident.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase()); let index = fs.readFileSync(indexPath, 'utf8'); if (new RegExp(`\\bimport ${camel} from`).test(index)) die(`src/exercises/index.ts already imports something called ${camel}; pick another id`); const anchorImport = "import blank from './demo.js';"; const anchorList = ' blank,\n];'; if (!index.includes(anchorImport) || !index.includes(anchorList)) die('src/exercises/index.ts no longer has the demo playground as its last entry, so I do not know where to register the exercise. Add the import and list entry by hand.'); if (!adopt) fs.writeFileSync(mojoPath, `# origin: Programming+ exercise ${id} # status: unverified; \`corpus.py verify\` checks it on the pinned compiler # probe: what this program shows, in one line (edit me) # mode: both # args: -O0 --debug-level=line-tables # expect-stdout: Hello from ${id} def main(): print("Hello from ${id}") `); fs.writeFileSync(tsPath, `import { exercise } from './types.js'; export default exercise({ id: '${id}', mojoProgram: '${id}', // library/mojo/local/${id}.mojo; \`pixi run play ${id}\` runs, verifies and builds it title: ${JSON.stringify(title)}, group: ${JSON.stringify(group)}, blurb: 'One or two sentences: what the student does here and what they will notice.', meta: { level: 'core' }, sources: {}, lang: 'mojo', }); `); index = index.replace(anchorImport, `import ${camel} from './${id}.js';\n${anchorImport}`).replace(anchorList, ` ${camel},\n blank,\n];`); fs.writeFileSync(indexPath, index); console.log(`${adopt ? 'adopted ' : 'created '} library/mojo/local/${id}.mojo\ncreated src/exercises/${id}.ts\nupdated src/exercises/index.ts\n\nnext: pixi run play ${id}`);