Skip to content

Instantly share code, notes, and snippets.

@justnom
Created November 12, 2014 15:32
Show Gist options
  • Save justnom/bf485f5ca02755862e8c to your computer and use it in GitHub Desktop.
Save justnom/bf485f5ca02755862e8c to your computer and use it in GitHub Desktop.
Add compatibility for NPM long path names when using VirtualBox shared folders.
var fs = require('fs');
var path = require('path');
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
console.log('exists');
fs.readdirSync(path).forEach(function(file, index){
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
// Remove any broken symlinks
try {
if (fs.lstatSync(path).isSymbolicLink()) {
fs.unlinkSync(path);
}
} catch (e) { }
}
/**
* Check if the current directory has a `package.json` file. Check if the
* `node_modules` directory is symlinked into `/cached_node_modules/xxxxxxxx`.
* If a symlink doesn't exist - delete `node_modules` and re-create a symlink
* into the cached folder.
*/
function symlink() {
var cwd = process.cwd();
if (!fs.existsSync(path.join(cwd, 'package.json'))) {
return false;
}
var modulesPath = path.join(cwd, 'node_modules');
if (fs.existsSync(modulesPath) && fs.lstatSync(modulesPath).isSymbolicLink()) {
return false;
}
console.log('Symlinking `node_modules` for Windows VirtualBox compatability...');
deleteFolderRecursive(modulesPath);
var cachePath = path.join('/cached_node_modules', (+new Date()).toString(36));
fs.mkdirSync(cachePath);
fs.symlinkSync(cachePath, modulesPath);
return true;
}
module.exports = {
symlink: symlink
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment