Last active
July 21, 2018 08:33
-
-
Save cspotcode/5075500105ac95e9fcdda7f7218880a5 to your computer and use it in GitHub Desktop.
Extending npm's built-in init behavior rather than replacing it
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
// $HOME/.npm-init.js | |
function main() { | |
// Put your customizations here | |
package.private = true; | |
} | |
// Helpers for getting access to modules that are either installed globally or available as dependencies of npm | |
function requireGlobal(name) { | |
return require(require('path').join(config.get('prefix'), 'node_modules', name)); | |
} | |
function requireNpmDep(name) { | |
return requireGlobal(`npm/node_modules/${ name }`); | |
} | |
// Everything below this point is a hack to pull in npm's default `npm init` prompts | |
const fs = require('fs'); | |
const Path = require('path'); | |
const npmDepsPath = Path.join(config.get('prefix'), 'node_modules/npm/node_modules'); | |
const defaultInitPath = Path.join(npmDepsPath, 'init-package-json/default-input.js'); | |
const body = fs.readFileSync(defaultInitPath, 'utf8'); | |
const exp = {}; | |
exports = module.exports = ({require, exports, module}) => ( | |
eval(body) | |
)({ | |
require(path) { | |
try {return require(path)} catch(e) {return require(Path.join(npmDepsPath, path))} | |
}, | |
exports: exp, | |
module: {exports: exp} | |
}); | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment