Last active
September 25, 2019 15:19
-
-
Save deepal/739ccb68d60a8675f46848b02a3b4c44 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
| const workspacePath = '/some/workspace/path'; | |
| const workspaceModulesDir = path.join(workspacePath, 'node_modules'); | |
| function installModule(moduleName) { | |
| execSync(`npm install --silent ${moduleName}`, { cwd: workspacePath }); | |
| } | |
| function wrapRequire() { | |
| const originalRequire = Module.prototype.require; | |
| Module.prototype.require = function (moduleName) { | |
| // Inject workspace node_modules directory | |
| this.paths.unshift(workspaceModulesDir); | |
| try { | |
| return originalRequire.apply(this, [moduleName]); | |
| } catch (err) { | |
| installModule(moduleInfo); | |
| return originalRequire.apply(this, [ | |
| path.join(workspacePath, 'node_modules', moduleName), | |
| ]); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment