Created
October 25, 2020 07:07
-
-
Save wisetc/a0a4d8523bee4a13bc91dfb4fcd72593 to your computer and use it in GitHub Desktop.
npm link local modules nodejs scripts.
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
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