Skip to content

Instantly share code, notes, and snippets.

@sophistifunk
Created June 6, 2016 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sophistifunk/9ebefcc134f47e310adfd3d077fa8c00 to your computer and use it in GitHub Desktop.
Save sophistifunk/9ebefcc134f47e310adfd3d077fa8c00 to your computer and use it in GitHub Desktop.
Pre-publish POC to block "npm publish" from command line
"use strict";
const npm_config_argv = process.env.npm_config_argv;
if (!npm_config_argv) {
console.error("This script should only be run via npm trigger -- npm_config_argv not found in ENV");
process.exit(1);
}
let isInstall = false;
let isPublish = false;
// We will set the "is_running_publish_script" env in custom publish script
const isTriggeredByPublishScript = !!(process.env.is_running_publish_script);
try {
let command = JSON.parse(npm_config_argv)
.original // An array of invocation commands
.map(function(c){return c.toLowerCase()});
isInstall = command.indexOf("install") >= 0;
isPublish = command.indexOf("publish") >= 0;
} catch (e) {
console.error("This script should only be run via npm trigger -- npm_config_argv is not JSON or command missing");
console.error(e);
process.exit(1);
}
if (isPublish && !isTriggeredByPublishScript) {
console.error("Please run 'scripts/publish.js' instead of 'npm publish'");
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment