|
| 1 | +/** |
| 2 | + * Windows-safe rename/move engine template for Codex BUILD work. |
| 3 | + * Uses Node.js path APIs only. No shell interpolation. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * node tools/codex/node_rename_engine_template.js mapping.json |
| 7 | + * |
| 8 | + * mapping.json format: |
| 9 | + * { |
| 10 | + * "projectRoot": "C:/Users/you/Documents/GitHub/HTML-JavaScript-Gaming", |
| 11 | + * "moves": [ |
| 12 | + * { "from": "samples/Phase 13 - Network Concepts, Latency & Simulation (1301-1315)/1316_network_sample_a", "to": "samples/phase13/1316" } |
| 13 | + * ] |
| 14 | + * } |
| 15 | + */ |
| 16 | + |
| 17 | +const fs = require("fs"); |
| 18 | +const path = require("path"); |
| 19 | + |
| 20 | +function readJson(filePath) { |
| 21 | + return JSON.parse(fs.readFileSync(filePath, "utf8")); |
| 22 | +} |
| 23 | + |
| 24 | +function exists(p) { |
| 25 | + return fs.existsSync(p); |
| 26 | +} |
| 27 | + |
| 28 | +function ensureDir(p) { |
| 29 | + fs.mkdirSync(p, { recursive: true }); |
| 30 | +} |
| 31 | + |
| 32 | +function assert(condition, message) { |
| 33 | + if (!condition) { |
| 34 | + throw new Error(message); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function normalizeRel(relPath) { |
| 39 | + return relPath.replace(/[\\/]+/g, "/").replace(/^\/+|\/+$/g, ""); |
| 40 | +} |
| 41 | + |
| 42 | +function validateMapping(projectRoot, moves) { |
| 43 | + assert(Array.isArray(moves) && moves.length > 0, "Mapping must contain at least one move."); |
| 44 | + const targets = new Set(); |
| 45 | + |
| 46 | + for (const move of moves) { |
| 47 | + assert(move.from && move.to, `Invalid move entry: ${JSON.stringify(move)}`); |
| 48 | + |
| 49 | + const fromRel = normalizeRel(move.from); |
| 50 | + const toRel = normalizeRel(move.to); |
| 51 | + |
| 52 | + assert(/^samples\/phase\d{2}\/\d{4}$/.test(toRel.replace(/\//g, "/")) || |
| 53 | + /^samples\/phase\d{2}\/\d{4}$/.test(toRel.replace(/\//g, "/")) === false, |
| 54 | + "Internal regex guard failure."); |
| 55 | + |
| 56 | + assert(/^samples\/phase\d{2}\/\d{4}$/.test(toRel.replace(/\//g, "/")) || |
| 57 | + /^samples\/phase\d{2}\/\d{4}$/.test(toRel.replace(/\//g, "/")) === false, |
| 58 | + "Internal regex guard failure."); |
| 59 | + } |
| 60 | + |
| 61 | + for (const move of moves) { |
| 62 | + const fromRel = normalizeRel(move.from); |
| 63 | + const toRel = normalizeRel(move.to); |
| 64 | + |
| 65 | + assert(/^samples\/phase\d{2}\/\d{4}$/.test(toRel), `Target path must match samples/phasexx/xxyy: ${toRel}`); |
| 66 | + |
| 67 | + const fromAbs = path.resolve(projectRoot, fromRel); |
| 68 | + const toAbs = path.resolve(projectRoot, toRel); |
| 69 | + |
| 70 | + assert(exists(fromAbs), `Source path missing: ${fromRel}`); |
| 71 | + assert(!targets.has(toRel), `Duplicate target path: ${toRel}`); |
| 72 | + targets.add(toRel); |
| 73 | + |
| 74 | + if (exists(toAbs)) { |
| 75 | + throw new Error(`Target already exists: ${toRel}`); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function movePath(projectRoot, fromRel, toRel) { |
| 81 | + const fromAbs = path.resolve(projectRoot, normalizeRel(fromRel)); |
| 82 | + const toAbs = path.resolve(projectRoot, normalizeRel(toRel)); |
| 83 | + ensureDir(path.dirname(toAbs)); |
| 84 | + fs.renameSync(fromAbs, toAbs); |
| 85 | + console.log(`MOVED: ${fromRel} -> ${toRel}`); |
| 86 | +} |
| 87 | + |
| 88 | +function main() { |
| 89 | + const mappingFile = process.argv[2]; |
| 90 | + assert(mappingFile, "Usage: node tools/codex/node_rename_engine_template.js mapping.json"); |
| 91 | + |
| 92 | + const config = readJson(mappingFile); |
| 93 | + const projectRoot = config.projectRoot; |
| 94 | + const moves = config.moves; |
| 95 | + |
| 96 | + assert(projectRoot, "mapping.json must include projectRoot"); |
| 97 | + validateMapping(projectRoot, moves); |
| 98 | + |
| 99 | + for (const move of moves) { |
| 100 | + movePath(projectRoot, move.from, move.to); |
| 101 | + } |
| 102 | + |
| 103 | + console.log("SUCCESS: All moves completed."); |
| 104 | +} |
| 105 | + |
| 106 | +try { |
| 107 | + main(); |
| 108 | +} catch (err) { |
| 109 | + console.error(`FAIL: ${err.message}`); |
| 110 | + process.exit(1); |
| 111 | +} |
0 commit comments