Created
June 26, 2019 05:25
-
-
Save zubko/6e6b8ddde6fb03b3109416dae8b6a04e to your computer and use it in GitHub Desktop.
Npm preinstall/postinstall script to link and unlink folders
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
/* eslint-disable no-console */ | |
/** | |
* The script which is able to link and unlink folders. | |
* It is used mainly to add absolute imports. | |
*/ | |
/** | |
* Add or change folders here. | |
* - target: relative path from the link to the folder of interest | |
* - destination: relative path from the project's root to the link | |
*/ | |
const namespace = '@app'; | |
const namespaceDir = `./node_modules/${namespace}`; | |
const folders = [ | |
{ | |
target: '../../app/core', | |
destination: `${namespaceDir}/core`, | |
}, | |
{ | |
target: '../../app/features', | |
destination: `${namespaceDir}/features`, | |
}, | |
]; | |
/** | |
* Main script | |
*/ | |
const fs = require('fs'); | |
if ( | |
process.argv[2] === 'link' || | |
process.env.npm_lifecycle_event === 'postinstall' | |
) { | |
console.log('========================================='); | |
checkCreateDir(namespaceDir); | |
console.log('Linking folders:'); | |
for (const { target, destination } of folders) { | |
resetLink(target, destination); | |
} | |
console.log('========================================='); | |
} else if ( | |
process.argv[2] === 'unlink' || | |
process.env.npm_lifecycle_event === 'preinstall' | |
) { | |
console.log('========================================='); | |
console.log('Unlinking folders:'); | |
for (const { destination } of folders) { | |
removeLink(destination); | |
} | |
checkRemoveDir(namespaceDir); | |
console.log('========================================='); | |
} | |
/** | |
* Utils | |
*/ | |
function checkCreateDir(dir) { | |
if (!fs.existsSync(dir)) { | |
console.log(`π Making dir: ${dir}`); | |
fs.mkdirSync(dir); | |
} | |
} | |
function checkRemoveDir(dir) { | |
if (fs.existsSync(dir)) { | |
console.log(`β Removing dir: ${dir}`); | |
fs.rmdirSync(dir); | |
} | |
} | |
function checkLinkSync(destination) { | |
try { | |
return Boolean(fs.readlinkSync(destination)); | |
} catch (e) { | |
if (e.code === 'ENOENT') { | |
return false; | |
} | |
throw e; | |
} | |
} | |
function resetLink(target, destination) { | |
console.log(`π ${target} ==> ${destination}`); | |
try { | |
if (!checkLinkSync(destination)) { | |
fs.symlinkSync(target, destination, 'junction'); | |
} else { | |
console.log(`File ${destination} already exists.`); | |
} | |
} catch (e) { | |
throw e; | |
} | |
} | |
function removeLink(destination) { | |
console.log(`β Removing link: ${destination}`); | |
try { | |
if (fs.readlinkSync(destination)) { | |
fs.unlinkSync(destination); | |
} | |
} catch (e) { | |
if (e.code === 'ENOENT') { | |
console.log('No link yet'); | |
return; | |
} | |
throw e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment