-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
99 lines (85 loc) · 3.02 KB
/
link.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs');
const path = require('path');
// Define an object that maps module names to their paths
const localLibraries = {
"@nance/nance-editor": "../nance-sdk/packages/editor",
"@nance/nance-sdk": "../nance-sdk/packages/core",
};
// Function to find the module path
function findLocalLibraryPath(moduleName) {
return localLibraries[moduleName];
}
// Function to remove the symbolic link if it already exists
function removeExistingSymlink(linkPath) {
try {
const stats = fs.lstatSync(linkPath);
if (stats.isSymbolicLink()) {
fs.unlinkSync(linkPath);
console.log(`Removed existing symlink: ${linkPath}`);
}
} catch (err) {
// If the file doesn't exist or there's an error, it's safe to ignore
}
}
// Function to create the symbolic link
function createSymlink(module) {
let localLibraryPath = findLocalLibraryPath(module);
// turn local path into an absolute path
localLibraryPath = localLibraryPath ? path.resolve(__dirname, localLibraryPath) : null;
if (!localLibraryPath) {
console.error(`Module "${module}" not found in the specified paths.`);
return;
}
console.log(`Module path: ${localLibraryPath}`);
if (!localLibraryPath) {
console.error(`Module "${module}" not found in the specified paths.`);
return;
}
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
const thisNodeModulesDirPath = path.join(nodeModulesPath, module);
// rename the existing local node_module to <node_module>.bak
try {
fs.renameSync(thisNodeModulesDirPath, `${thisNodeModulesDirPath}.bak`);
console.log(`Renamed existing node_module to ${module}.bak`);
} catch (err) {
console.error(`Error renaming existing node_module: ${err.message}`);
}
try {
fs.symlinkSync(localLibraryPath, thisNodeModulesDirPath, "dir");
console.log(`Symlink created: ${thisNodeModulesDirPath} -> ${localLibraryPath}`);
} catch (err) {
console.error(`Error creating symlink: ${err.message}`);
}
}
// Function to remove the symlink and reinstall the module from npm
function restoreOriginalModule(moduleName) {
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
const linkPath = path.join(nodeModulesPath, moduleName);
// Remove the existing symlink if it exists
removeExistingSymlink(linkPath);
try {
// Restore the original module by renaming the .bak directory back to the original name
fs.renameSync(`${linkPath}.bak`, linkPath);
console.log(`Module "${moduleName}" restored from .bak`);
} catch (err) {
console.error(`Error reinstalling module: ${err.message}`);
}
}
// Main function
function main() {
const args = process.argv.slice(2);
if (args.length !== 2) {
console.error('Usage: node symlink <module-name> [create|remove]');
return;
}
const moduleName = args[0];
const action = args[1];
if (action === 'create') {
createSymlink(moduleName);
} else if (action === 'remove') {
restoreOriginalModule(moduleName);
} else {
console.error('Invalid action. Use "create" or "remove".');
}
}
main();