Skip to content

Instantly share code, notes, and snippets.

@cspotcode
Last active July 21, 2018 08:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspotcode/5075500105ac95e9fcdda7f7218880a5 to your computer and use it in GitHub Desktop.
Save cspotcode/5075500105ac95e9fcdda7f7218880a5 to your computer and use it in GitHub Desktop.
Extending npm's built-in init behavior rather than replacing it
// $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