|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// Define an object that maps module names to their paths |
| 5 | +const localLibraries = { |
| 6 | + "@nance/nance-editor": "../nance-sdk/packages/editor", |
| 7 | + "@nance/nance-sdk": "../nance-sdk/packages/core", |
| 8 | +}; |
| 9 | + |
| 10 | +// Function to find the module path |
| 11 | +function findLocalLibraryPath(moduleName) { |
| 12 | + return localLibraries[moduleName]; |
| 13 | +} |
| 14 | + |
| 15 | +// Function to remove the symbolic link if it already exists |
| 16 | +function removeExistingSymlink(linkPath) { |
| 17 | + try { |
| 18 | + const stats = fs.lstatSync(linkPath); |
| 19 | + if (stats.isSymbolicLink()) { |
| 20 | + fs.unlinkSync(linkPath); |
| 21 | + console.log(`Removed existing symlink: ${linkPath}`); |
| 22 | + } |
| 23 | + } catch (err) { |
| 24 | + // If the file doesn't exist or there's an error, it's safe to ignore |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Function to create the symbolic link |
| 29 | +function createSymlink(module) { |
| 30 | + let localLibraryPath = findLocalLibraryPath(module); |
| 31 | + // turn local path into an absolute path |
| 32 | + localLibraryPath = localLibraryPath ? path.resolve(__dirname, localLibraryPath) : null; |
| 33 | + if (!localLibraryPath) { |
| 34 | + console.error(`Module "${module}" not found in the specified paths.`); |
| 35 | + return; |
| 36 | + } |
| 37 | + console.log(`Module path: ${localLibraryPath}`); |
| 38 | + if (!localLibraryPath) { |
| 39 | + console.error(`Module "${module}" not found in the specified paths.`); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const nodeModulesPath = path.join(process.cwd(), 'node_modules'); |
| 44 | + const thisNodeModulesDirPath = path.join(nodeModulesPath, module); |
| 45 | + |
| 46 | + // rename the existing local node_module to <node_module>.bak |
| 47 | + try { |
| 48 | + fs.renameSync(thisNodeModulesDirPath, `${thisNodeModulesDirPath}.bak`); |
| 49 | + console.log(`Renamed existing node_module to ${module}.bak`); |
| 50 | + } catch (err) { |
| 51 | + console.error(`Error renaming existing node_module: ${err.message}`); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + fs.symlinkSync(localLibraryPath, thisNodeModulesDirPath, "dir"); |
| 56 | + console.log(`Symlink created: ${thisNodeModulesDirPath} -> ${localLibraryPath}`); |
| 57 | + } catch (err) { |
| 58 | + console.error(`Error creating symlink: ${err.message}`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Function to remove the symlink and reinstall the module from npm |
| 63 | +function restoreOriginalModule(moduleName) { |
| 64 | + const nodeModulesPath = path.join(process.cwd(), 'node_modules'); |
| 65 | + const linkPath = path.join(nodeModulesPath, moduleName); |
| 66 | + |
| 67 | + // Remove the existing symlink if it exists |
| 68 | + removeExistingSymlink(linkPath); |
| 69 | + |
| 70 | + try { |
| 71 | + // Restore the original module by renaming the .bak directory back to the original name |
| 72 | + fs.renameSync(`${linkPath}.bak`, linkPath); |
| 73 | + console.log(`Module "${moduleName}" restored from .bak`); |
| 74 | + } catch (err) { |
| 75 | + console.error(`Error reinstalling module: ${err.message}`); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Main function |
| 80 | +function main() { |
| 81 | + const args = process.argv.slice(2); |
| 82 | + if (args.length !== 2) { |
| 83 | + console.error('Usage: node symlink <module-name> [create|remove]'); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const moduleName = args[0]; |
| 88 | + const action = args[1]; |
| 89 | + |
| 90 | + if (action === 'create') { |
| 91 | + createSymlink(moduleName); |
| 92 | + } else if (action === 'remove') { |
| 93 | + restoreOriginalModule(moduleName); |
| 94 | + } else { |
| 95 | + console.error('Invalid action. Use "create" or "remove".'); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +main(); |
0 commit comments