Created
May 26, 2023 08:41
-
-
Save vzakharov/8452479f49345d2536cdd7e4f147c13a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Replaces symbolic links at the given directory with the files/directories they point to. Useful e.g. for git repos because git can't commit symlinks. | |
const fs = require('fs'); | |
const path = require('path'); | |
function unsymlink(dir) { | |
if (!fs.existsSync(dir)) { | |
console.error(`Directory ${dir} does not exist.`); | |
return; | |
} | |
const items = fs.readdirSync(dir); | |
items.forEach((item) => { | |
const itemPath = path.join(dir, item); | |
const stats = fs.lstatSync(itemPath); | |
if (stats.isSymbolicLink()) { | |
const realPath = fs.realpathSync(itemPath); | |
fs.unlinkSync(itemPath); | |
if (stats.isFile()) { | |
fs.copyFileSync(realPath, itemPath); | |
} else if (stats.isDirectory()) { | |
fs.mkdirSync(itemPath); | |
unsymlink(realPath); | |
} | |
console.log(`Unsymlinked ${itemPath}`); | |
} else if (stats.isDirectory()) { | |
unsymlink(itemPath); | |
} | |
}); | |
} | |
if (process.argv.length < 3) { | |
console.error('Please provide a directory path as an argument.'); | |
process.exit(1); | |
} | |
const targetDir = process.argv[2]; | |
unsymlink(targetDir); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment