@meldscience/meld
Version:
pipeable one-shot prompt scripting toolkit
79 lines (68 loc) • 2.39 kB
text/typescript
import { Command } from 'commander';
import { Meld } from '../src/meld';
import { existsSync } from 'fs';
import { writeFile } from 'fs/promises';
import { createInterface } from 'readline';
const program = new Command();
async function promptOverwrite(file: string): Promise<boolean> {
const rl = createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(`File ${file} already exists. Overwrite? [Y/n] `, (answer) => {
rl.close();
resolve(answer.toLowerCase() !== 'n');
});
});
}
program
.name('meld')
.description('Process a markdown file containing embedded commands')
.argument('<input>', 'Input .meld.md file')
.option('-o, --outfile <path>', 'Output file path')
.option('--dry-run', 'Print commands that would be executed without running them')
.option('--overwrite', 'Overwrite output file without prompting')
.option('--debug', 'Enable debug logging')
.action(async (input, options) => {
try {
const outputFile = options.outfile || input.replace(/\.meld\.md$/, '.md');
// Check if file exists and we need to prompt
if (existsSync(outputFile) && !options.overwrite) {
const shouldOverwrite = await promptOverwrite(outputFile);
if (!shouldOverwrite) {
console.log('Aborted.');
process.exit(0);
}
}
const meld = new Meld({
inputPath: input,
workspacePath: process.cwd(),
outputFile,
dryRun: options.dryRun || false,
debug: options.debug || false
});
const result = await meld.process();
if (result.errors.length > 0) {
console.error('Errors occurred during processing:');
result.errors.forEach(error => console.error(`- ${error}`));
process.exit(1);
}
if (options.dryRun) {
console.log('\nCommands that would be executed:');
console.log(result.content);
} else {
await writeFile(outputFile, result.content, 'utf-8');
console.log(`🦾 ${input} → ${outputFile}`);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Error:', error.message);
} else {
console.error('An unknown error occurred:', error);
}
process.exit(1);
}
});
program.parse();