-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
66 lines (57 loc) · 2.83 KB
/
Copy pathexport.js
File metadata and controls
66 lines (57 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// export.js
// Save a finished pre-seed prompt to a file the user can paste into their AI account.
//
// Supports two formats:
// • .txt — just the prompt text, nothing else. Cleanest for pasting.
// • .md — the prompt plus a short header comment (a title + a one-line note on what it
// is and where to paste it). Nicer for keeping a small library of prompts.
//
// Files are written into an `exports/` folder next to the program so they're easy to find
// and don't clutter the project root. The chosen filename is sanitised to stay safe.
const fs = require('fs');
const path = require('path');
const { ask, askRaw, choose } = require('./prompt');
const ui = require('./ui');
const EXPORT_DIR = path.join(__dirname, 'exports');
// Strip anything risky from a user-supplied filename: keep letters, numbers, dash,
// underscore and dot; collapse the rest to dashes. Prevents path tricks like "../".
function safeName(name) {
const base = path.basename(name || '').trim();
const cleaned = base.replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
return cleaned || 'pre-seed-prompt';
}
// Build the file contents for the chosen format.
function render(text, format, label) {
if (format === 'md') {
return '# Pre-seed prompt' + (label ? ' — ' + label : '') + '\n\n' +
'<!-- Paste the text below into your AI account\'s custom instructions / project ' +
'settings. Generated with judge-dredd. -->\n\n' + text + '\n';
}
return text + '\n'; // .txt — prompt only
}
// Interactive export: ask format + filename, write the file, return the path written.
// `text` is the prompt; `label` is an optional friendly name for the .md header.
async function exportPrompt(text, label) {
if (!text || !text.trim()) {
console.log(ui.red('Nothing to export — there is no prompt text.'));
return null;
}
const format = await choose(ui.yellow('Format? [md / txt] '), ['md', 'txt']);
if (format === null) { console.log(ui.dim('Export cancelled.')); return null; } // EOF / closed stdin
const def = 'pre-seed-prompt';
const raw = await askRaw(ui.dim('Filename (without extension) [' + def + ']: '));
const name = safeName(raw || def);
fs.mkdirSync(EXPORT_DIR, { recursive: true });
const out = path.join(EXPORT_DIR, name + '.' + format);
// Don't silently clobber an existing file — confirm first.
if (fs.existsSync(out)) {
const ok = await ask(ui.yellow(name + '.' + format + ' already exists. Overwrite? [y/n] '));
if (ok !== 'y') { console.log(ui.dim('Export cancelled.')); return null; }
}
fs.writeFileSync(out, render(text, format, label));
console.log(ui.green('Saved ') + out);
console.log(ui.dim('Open it, copy the text, and paste it into your AI account\'s custom ' +
'instructions or project settings.'));
return out;
}
module.exports = { EXPORT_DIR, safeName, render, exportPrompt };