|
| 1 | +// repl.js |
| 2 | +import repl from 'node:repl'; |
| 3 | +import util from 'node:util'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +// Helper to get __dirname equivalent in ES Modules |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | +const DATA_STRUCTURES_DIR = path.join(__dirname, 'data-structures'); |
| 12 | + |
| 13 | +// Function to dynamically import all specified modules and add to context |
| 14 | +async function loadDataStructures(context) { |
| 15 | + const filesToImport = [ |
| 16 | + '/data-structures/dijkstra/Dijkstras.js', |
| 17 | + '/data-structures/graph/Graph.js', |
| 18 | + '/data-structures/hashmap/HashMap.js', |
| 19 | + '/data-structures/heap/MinHeap.js', |
| 20 | + '/data-structures/heap/MaxHeap.js', |
| 21 | + '/data-structures/linked-lists/singly-linked-list/LinkedList.js', |
| 22 | + '/data-structures/priority-queue/PriorityQueue.js', |
| 23 | + '/data-structures/queue/Queue.js', |
| 24 | + '/data-structures/stack/Stack.js', |
| 25 | + '/data-structures/tree/TreeNode.js', |
| 26 | + '/data-structures/tree/binaryTree.js', |
| 27 | + '/data-structures/sorting-algos/bubbleSort.js', |
| 28 | + '/data-structures/sorting-algos/getX.js', |
| 29 | + '/data-structures/sorting-algos/mergeSort.js', |
| 30 | + '/data-structures/sorting-algos/quickSort.js' |
| 31 | + ]; |
| 32 | + |
| 33 | + // Named exports handled separately |
| 34 | + const namedExportsFile = '/data-structures/sorting-algos/binarySearch.js'; |
| 35 | + |
| 36 | + // Track loaded module names |
| 37 | + const loadedModules = []; |
| 38 | + |
| 39 | + for (const relativePath of filesToImport) { |
| 40 | + const fullPath = path.join(__dirname, relativePath); |
| 41 | + // Ensure path uses file:// protocol for dynamic import |
| 42 | + const modulePath = `file://${fullPath}`; |
| 43 | + |
| 44 | + try { |
| 45 | + const module = await import(modulePath); |
| 46 | + // Extract the default export |
| 47 | + // Use the filename (without extension) as the context key (e.g., 'Dijkstras') |
| 48 | + const fileName = path.basename(relativePath, path.extname(relativePath)); |
| 49 | + context[fileName] = module.default; |
| 50 | + loadedModules.push(fileName); |
| 51 | + } catch (error) { |
| 52 | + console.error(`Failed to import ${relativePath}: ${error.message}`); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Handle named exports for binarySearch.js |
| 57 | + const binarySearchPath = `file://${path.join(__dirname, namedExportsFile)}`; |
| 58 | + try { |
| 59 | + const binarySearchModule = await import(binarySearchPath); |
| 60 | + context.binarySearchIterative = binarySearchModule.binarySearchIterative; |
| 61 | + context.binarySearchRecursive = binarySearchModule.binarySearchRecursive; |
| 62 | + loadedModules.push('binarySearchIterative', 'binarySearchRecursive'); |
| 63 | + } catch (error) { |
| 64 | + console.error(`Failed to import named exports from ${namedExportsFile}: ${error.message}`); |
| 65 | + } |
| 66 | + |
| 67 | + console.log(`\nSuccessfully loaded modules: ${loadedModules.join(', ')}\n`); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +// --- Start REPL and Expose Context --- |
| 72 | + |
| 73 | +const replServer = repl.start({ |
| 74 | + prompt: '\x1b[38;2;255;0;255m▶ \x1b[0m', |
| 75 | + writer: (output) => { |
| 76 | + return util.inspect(output, { compact: true, breakLength: Infinity, colors: true }); |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | +// Load data structures into the REPL context asynchronously |
| 81 | +await loadDataStructures(replServer.context); |
| 82 | + |
| 83 | +console.log("Welcome to @tjupps REPL! Data structures loaded."); |
| 84 | + |
| 85 | +// Example usage might look like: |
| 86 | +// const list = new LinkedList(); |
| 87 | +// list.add(5); |
| 88 | +// list; // to see the object structure |
| 89 | + |
0 commit comments