Last active
December 10, 2019 20:23
-
-
Save risseraka/5da3c42d805c1c09ca84de1d164b02bf 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
#!/usr/bin/env node | |
const { join } = require("path"); | |
const { Module } = require("module"); | |
const { execSync } = require("child_process"); | |
const originalRequire = Module.prototype.require; | |
// heavily inspired by https://github.com/deepal/baapan | |
Module.prototype.require = function(moduleName) { | |
try { | |
return originalRequire.call(this, moduleName); | |
} catch (err) { | |
try { | |
execSync(`npm install --no-save --silent ${moduleName}`); | |
const modulePath = join("./node_modules", moduleName); | |
return originalRequire.call(this, modulePath); | |
} catch (e) { | |
console.error("Could not install module:", e); | |
} | |
} | |
}; | |
const file = process.argv[2]; | |
if (!file) { | |
console.log("usage: runner file[.js]"); | |
process.exit(1); | |
} | |
console.log("Executing file:", file); | |
originalRequire(`./${file.replace(/^\.\//, "")}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment