Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created October 25, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisetc/a0a4d8523bee4a13bc91dfb4fcd72593 to your computer and use it in GitHub Desktop.
Save wisetc/a0a4d8523bee4a13bc91dfb4fcd72593 to your computer and use it in GitHub Desktop.
npm link local modules nodejs scripts.
const assert = require('assert');
const {promisify} = require('util');
const fs = require('fs');
const {resolve} = require('path');
//
const symlink = promisify(fs.symlink);
const stat = promisify(fs.stat);
const mkdir = promisify(fs.mkdir);
// Is path exists
async function isExists(p) {
try {
await stat(p);
return true;
} catch (error) {
return false;
}
}
// ensure folder exists.
async function ensureFolder(folder) {
if (!(await isExists(folder))) {
await mkdir(folder);
console.log({folder});
}
}
// ---
const rootDir = resolve(__dirname, '..');
const NODE_MODULES = resolve(rootDir, 'node_modules');
const modulesMap = {
'@wisetc/styledComponents': resolve(rootDir, '../testproject/dist'),
}
async function linkModules() {
assert(process.platform === 'win32', 'Only windows platform support now.');
const dirType = 'dir'; // 'junction'
const promises = [];
for (const module in modulesMap) {
const target = modulesMap[module];
const linkPath = resolve(NODE_MODULES, module);
const folderName = resolve(linkPath, '..');
await ensureFolder(folderName);
console.log({linkPath, target});
promises.push(symlink(target, linkPath, dirType));
}
const results = await Promise.all(promises);
return results;
}
async function main() {
try {
await linkModules();
console.log('done');
} catch (error) {
console.error(error.message);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment