Created
January 4, 2016 09:34
-
-
Save marcbachmann/b6a35b860e4e131b807a to your computer and use it in GitHub Desktop.
A npm version check that doesn't have any dependencies
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 | |
// Embed semver module to make this script completely independent | |
// minified [email protected] | |
// https://github.com/npm/node-semver | |
var semver={};!function(r){function e(r,e){if(r instanceof i)return r;if("string"!=typeof r)return null;if(r.length>L)return null;var t=e?F[pr]:F[ar];if(!t.test(r))return null;try{return new i(r,e)}catch(n){return null}}function t(r,t){var n=e(r,t);return n?n.version:null}function n(r,t){var n=e(r.trim().replace(/^[=v]+/,""),t);return n?n.version:null}function i(r,e){if(r instanceof i){if(r.loose===e)return r;r=r.version}else if("string"!=typeof r)throw new TypeError("Invalid Version: "+r);if(r.length>L)throw new TypeError("version is longer than "+L+" characters");if(!(this instanceof i))return new i(r,e);B("SemVer",r,e),this.loose=e;var t=r.trim().match(e?F[pr]:F[ar]);if(!t)throw new TypeError("Invalid Version: "+r);if(this.raw=r,this.major=+t[1],this.minor=+t[2],this.patch=+t[3],this.major>U||this.major<0)throw new TypeError("Invalid major version");if(this.minor>U||this.minor<0)throw new TypeError("Invalid minor version");if(this.patch>U||this.patch<0)throw new TypeError("Invalid patch version");t[4]?this.prerelease=t[4].split(".").map(function(r){if(/^[0-9]+$/.test(r)){var e=+r;if(e>=0&&U>e)return e}return r}):this.prerelease=[],this.build=t[5]?t[5].split("."):[],this.format()}function s(r,e,t,n){"string"==typeof t&&(n=t,t=void 0);try{return new i(r,t).inc(e,n).version}catch(s){return null}}function o(r,t){if(d(r,t))return null;var n=e(r),i=e(t);if(n.prerelease.length||i.prerelease.length){for(var s in n)if(("major"===s||"minor"===s||"patch"===s)&&n[s]!==i[s])return"pre"+s;return"prerelease"}for(var s in n)if(("major"===s||"minor"===s||"patch"===s)&&n[s]!==i[s])return s}function a(r,e){var t=Dr.test(r),n=Dr.test(e);return t&&n&&(r=+r,e=+e),t&&!n?-1:n&&!t?1:e>r?-1:r>e?1:0}function c(r,e){return a(e,r)}function u(r,e){return new i(r,e).major}function p(r,e){return new i(r,e).minor}function h(r,e){return new i(r,e).patch}function l(r,e,t){return new i(r,t).compare(e)}function f(r,e){return l(r,e,!0)}function v(r,e,t){return l(e,r,t)}function m(e,t){return e.sort(function(e,n){return r.compare(e,n,t)})}function g(e,t){return e.sort(function(e,n){return r.rcompare(e,n,t)})}function w(r,e,t){return l(r,e,t)>0}function y(r,e,t){return l(r,e,t)<0}function d(r,e,t){return 0===l(r,e,t)}function j(r,e,t){return 0!==l(r,e,t)}function E(r,e,t){return l(r,e,t)>=0}function b(r,e,t){return l(r,e,t)<=0}function $(r,e,t,n){var i;switch(e){case"===":"object"==typeof r&&(r=r.version),"object"==typeof t&&(t=t.version),i=r===t;break;case"!==":"object"==typeof r&&(r=r.version),"object"==typeof t&&(t=t.version),i=r!==t;break;case"":case"=":case"==":i=d(r,t,n);break;case"!=":i=j(r,t,n);break;case">":i=w(r,t,n);break;case">=":i=E(r,t,n);break;case"<":i=y(r,t,n);break;case"<=":i=b(r,t,n);break;default:throw new TypeError("Invalid operator: "+e)}return i}function k(r,e){if(r instanceof k){if(r.loose===e)return r;r=r.value}return this instanceof k?(B("comparator",r,e),this.loose=e,this.parse(r),this.semver===Xr?this.value="":this.value=this.operator+this.semver.version,void B("comp",this)):new k(r,e)}function R(r,e){if(r instanceof R&&r.loose===e)return r;if(!(this instanceof R))return new R(r,e);if(this.loose=e,this.raw=r,this.set=r.split(/\s*\|\|\s*/).map(function(r){return this.parseRange(r.trim())},this).filter(function(r){return r.length}),!this.set.length)throw new TypeError("Invalid SemVer Range: "+r);this.format()}function S(r,e){return new R(r,e).set.map(function(r){return r.map(function(r){return r.value}).join(" ").trim().split(" ")})}function I(r,e){return B("comp",r),r=A(r,e),B("caret",r),r=x(r,e),B("tildes",r),r=C(r,e),B("xrange",r),r=_(r,e),B("stars",r),r}function T(r){return!r||"x"===r.toLowerCase()||"*"===r}function x(r,e){return r.trim().split(/\s+/).map(function(r){return V(r,e)}).join(" ")}function V(r,e){var t=e?F[br]:F[Er];return r.replace(t,function(e,t,n,i,s){B("tilde",r,e,t,n,i,s);var o;return T(t)?o="":T(n)?o=">="+t+".0.0 <"+(+t+1)+".0.0":T(i)?o=">="+t+"."+n+".0 <"+t+"."+(+n+1)+".0":s?(B("replaceTilde pr",s),"-"!==s.charAt(0)&&(s="-"+s),o=">="+t+"."+n+"."+i+s+" <"+t+"."+(+n+1)+".0"):o=">="+t+"."+n+"."+i+" <"+t+"."+(+n+1)+".0",B("tilde return",o),o})}function A(r,e){return r.trim().split(/\s+/).map(function(r){return N(r,e)}).join(" ")}function N(r,e){B("caret",r,e);var t=e?F[Ir]:F[Sr];return r.replace(t,function(e,t,n,i,s){B("caret",r,e,t,n,i,s);var o;return T(t)?o="":T(n)?o=">="+t+".0.0 <"+(+t+1)+".0.0":T(i)?o="0"===t?">="+t+"."+n+".0 <"+t+"."+(+n+1)+".0":">="+t+"."+n+".0 <"+(+t+1)+".0.0":s?(B("replaceCaret pr",s),"-"!==s.charAt(0)&&(s="-"+s),o="0"===t?"0"===n?">="+t+"."+n+"."+i+s+" <"+t+"."+n+"."+(+i+1):">="+t+"."+n+"."+i+s+" <"+t+"."+(+n+1)+".0":">="+t+"."+n+"."+i+s+" <"+(+t+1)+".0.0"):(B("no pr"),o="0"===t?"0"===n?">="+t+"."+n+"."+i+" <"+t+"."+n+"."+(+i+1):">="+t+"."+n+"."+i+" <"+t+"."+(+n+1)+".0":">="+t+"."+n+"."+i+" <"+(+t+1)+".0.0"),B("caret return",o),o})}function C(r,e){return B("replaceXRanges",r,e),r.split(/\s+/).map(function(r){return M(r,e)}).join(" ")}function M(r,e){r=r.trim();var t=e?F[wr]:F[gr];return r.replace(t,function(e,t,n,i,s,o){B("xRange",r,e,t,n,i,s,o);var a=T(n),c=a||T(i),u=c||T(s),p=u;return"="===t&&p&&(t=""),a?e=">"===t||"<"===t?"<0.0.0":"*":t&&p?(c&&(i=0),u&&(s=0),">"===t?(t=">=",c?(n=+n+1,i=0,s=0):u&&(i=+i+1,s=0)):"<="===t&&(t="<",c?n=+n+1:i=+i+1),e=t+n+"."+i+"."+s):c?e=">="+n+".0.0 <"+(+n+1)+".0.0":u&&(e=">="+n+"."+i+".0 <"+n+"."+(+i+1)+".0"),B("xRange return",e),e})}function _(r,e){return B("replaceStars",r,e),r.trim().replace(F[Mr],"")}function D(r,e,t,n,i,s,o,a,c,u,p,h,l){return e=T(t)?"":T(n)?">="+t+".0.0":T(i)?">="+t+"."+n+".0":">="+e,a=T(c)?"":T(u)?"<"+(+c+1)+".0.0":T(p)?"<"+c+"."+(+u+1)+".0":h?"<="+c+"."+u+"."+p+"-"+h:"<="+a,(e+" "+a).trim()}function X(r,e){for(var t=0;t<r.length;t++)if(!r[t].test(e))return!1;if(e.prerelease.length){for(var t=0;t<r.length;t++)if(B(r[t].semver),r[t].semver!==Xr&&r[t].semver.prerelease.length>0){var n=r[t].semver;if(n.major===e.major&&n.minor===e.minor&&n.patch===e.patch)return!0}return!1}return!0}function z(r,e,t){try{e=new R(e,t)}catch(n){return!1}return e.test(r)}function G(r,e,t){return r.filter(function(r){return z(r,e,t)}).sort(function(r,e){return v(r,e,t)})[0]||null}function O(r,e){try{return new R(r,e).range||"*"}catch(t){return null}}function P(r,e,t){return q(r,e,"<",t)}function Z(r,e,t){return q(r,e,">",t)}function q(r,e,t,n){r=new i(r,n),e=new R(e,n);var s,o,a,c,u;switch(t){case">":s=w,o=b,a=y,c=">",u=">=";break;case"<":s=y,o=E,a=w,c="<",u="<=";break;default:throw new TypeError('Must provide a hilo val of "<" or ">"')}if(z(r,e,n))return!1;for(var p=0;p<e.set.length;++p){var h=e.set[p],l=null,f=null;if(h.forEach(function(r){r.semver===Xr&&(r=new k(">=0.0.0")),l=l||r,f=f||r,s(r.semver,l.semver,n)?l=r:a(r.semver,f.semver,n)&&(f=r)}),l.operator===c||l.operator===u)return!1;if((!f.operator||f.operator===c)&&o(r,f.semver))return!1;if(f.operator===u&&a(r,f.semver))return!1}return!0}var B;B="object"==typeof process&&process.env&&process.env.NODE_DEBUG&&/\bsemver\b/i.test(process.env.NODE_DEBUG)?function(){var r=Array.prototype.slice.call(arguments,0);r.unshift("SEMVER"),console.log.apply(console,r)}:function(){},r.SEMVER_SPEC_VERSION="2.0.0";var L=256,U=Number.MAX_SAFE_INTEGER||9007199254740991,F=r.re=[],H=r.src=[],J=0,K=J++;H[K]="0|[1-9]\\d*";var Q=J++;H[Q]="[0-9]+";var W=J++;H[W]="\\d*[a-zA-Z-][a-zA-Z0-9-]*";var Y=J++;H[Y]="("+H[K]+")\\.("+H[K]+")\\.("+H[K]+")";var rr=J++;H[rr]="("+H[Q]+")\\.("+H[Q]+")\\.("+H[Q]+")";var er=J++;H[er]="(?:"+H[K]+"|"+H[W]+")";var tr=J++;H[tr]="(?:"+H[Q]+"|"+H[W]+")";var nr=J++;H[nr]="(?:-("+H[er]+"(?:\\."+H[er]+")*))";var ir=J++;H[ir]="(?:-?("+H[tr]+"(?:\\."+H[tr]+")*))";var sr=J++;H[sr]="[0-9A-Za-z-]+";var or=J++;H[or]="(?:\\+("+H[sr]+"(?:\\."+H[sr]+")*))";var ar=J++,cr="v?"+H[Y]+H[nr]+"?"+H[or]+"?";H[ar]="^"+cr+"$";var ur="[v=\\s]*"+H[rr]+H[ir]+"?"+H[or]+"?",pr=J++;H[pr]="^"+ur+"$";var hr=J++;H[hr]="((?:<|>)?=?)";var lr=J++;H[lr]=H[Q]+"|x|X|\\*";var fr=J++;H[fr]=H[K]+"|x|X|\\*";var vr=J++;H[vr]="[v=\\s]*("+H[fr]+")(?:\\.("+H[fr]+")(?:\\.("+H[fr]+")(?:"+H[nr]+")?"+H[or]+"?)?)?";var mr=J++;H[mr]="[v=\\s]*("+H[lr]+")(?:\\.("+H[lr]+")(?:\\.("+H[lr]+")(?:"+H[ir]+")?"+H[or]+"?)?)?";var gr=J++;H[gr]="^"+H[hr]+"\\s*"+H[vr]+"$";var wr=J++;H[wr]="^"+H[hr]+"\\s*"+H[mr]+"$";var yr=J++;H[yr]="(?:~>?)";var dr=J++;H[dr]="(\\s*)"+H[yr]+"\\s+",F[dr]=new RegExp(H[dr],"g");var jr="$1~",Er=J++;H[Er]="^"+H[yr]+H[vr]+"$";var br=J++;H[br]="^"+H[yr]+H[mr]+"$";var $r=J++;H[$r]="(?:\\^)";var kr=J++;H[kr]="(\\s*)"+H[$r]+"\\s+",F[kr]=new RegExp(H[kr],"g");var Rr="$1^",Sr=J++;H[Sr]="^"+H[$r]+H[vr]+"$";var Ir=J++;H[Ir]="^"+H[$r]+H[mr]+"$";var Tr=J++;H[Tr]="^"+H[hr]+"\\s*("+ur+")$|^$";var xr=J++;H[xr]="^"+H[hr]+"\\s*("+cr+")$|^$";var Vr=J++;H[Vr]="(\\s*)"+H[hr]+"\\s*("+ur+"|"+H[vr]+")",F[Vr]=new RegExp(H[Vr],"g");var Ar="$1$2$3",Nr=J++;H[Nr]="^\\s*("+H[vr]+")\\s+-\\s+("+H[vr]+")\\s*$";var Cr=J++;H[Cr]="^\\s*("+H[mr]+")\\s+-\\s+("+H[mr]+")\\s*$";var Mr=J++;H[Mr]="(<|>)?=?\\s*\\*";for(var _r=0;J>_r;_r++)B(_r,H[_r]),F[_r]||(F[_r]=new RegExp(H[_r]));r.parse=e,r.valid=t,r.clean=n,r.SemVer=i,i.prototype.format=function(){return this.version=this.major+"."+this.minor+"."+this.patch,this.prerelease.length&&(this.version+="-"+this.prerelease.join(".")),this.version},i.prototype.toString=function(){return this.version},i.prototype.compare=function(r){return B("SemVer.compare",this.version,this.loose,r),r instanceof i||(r=new i(r,this.loose)),this.compareMain(r)||this.comparePre(r)},i.prototype.compareMain=function(r){return r instanceof i||(r=new i(r,this.loose)),a(this.major,r.major)||a(this.minor,r.minor)||a(this.patch,r.patch)},i.prototype.comparePre=function(r){if(r instanceof i||(r=new i(r,this.loose)),this.prerelease.length&&!r.prerelease.length)return-1;if(!this.prerelease.length&&r.prerelease.length)return 1;if(!this.prerelease.length&&!r.prerelease.length)return 0;var e=0;do{var t=this.prerelease[e],n=r.prerelease[e];if(B("prerelease compare",e,t,n),void 0===t&&void 0===n)return 0;if(void 0===n)return 1;if(void 0===t)return-1;if(t!==n)return a(t,n)}while(++e)},i.prototype.inc=function(r,e){switch(r){case"premajor":this.prerelease.length=0,this.patch=0,this.minor=0,this.major++,this.inc("pre",e);break;case"preminor":this.prerelease.length=0,this.patch=0,this.minor++,this.inc("pre",e);break;case"prepatch":this.prerelease.length=0,this.inc("patch",e),this.inc("pre",e);break;case"prerelease":0===this.prerelease.length&&this.inc("patch",e),this.inc("pre",e);break;case"major":(0!==this.minor||0!==this.patch||0===this.prerelease.length)&&this.major++,this.minor=0,this.patch=0,this.prerelease=[];break;case"minor":(0!==this.patch||0===this.prerelease.length)&&this.minor++,this.patch=0,this.prerelease=[];break;case"patch":0===this.prerelease.length&&this.patch++,this.prerelease=[];break;case"pre":if(0===this.prerelease.length)this.prerelease=[0];else{for(var t=this.prerelease.length;--t>=0;)"number"==typeof this.prerelease[t]&&(this.prerelease[t]++,t=-2);-1===t&&this.prerelease.push(0)}e&&(this.prerelease[0]===e?isNaN(this.prerelease[1])&&(this.prerelease=[e,0]):this.prerelease=[e,0]);break;default:throw new Error("invalid increment argument: "+r)}return this.format(),this.raw=this.version,this},r.inc=s,r.diff=o,r.compareIdentifiers=a;var Dr=/^[0-9]+$/;r.rcompareIdentifiers=c,r.major=u,r.minor=p,r.patch=h,r.compare=l,r.compareLoose=f,r.rcompare=v,r.sort=m,r.rsort=g,r.gt=w,r.lt=y,r.eq=d,r.neq=j,r.gte=E,r.lte=b,r.cmp=$,r.Comparator=k;var Xr={};k.prototype.parse=function(r){var e=this.loose?F[Tr]:F[xr],t=r.match(e);if(!t)throw new TypeError("Invalid comparator: "+r);this.operator=t[1],"="===this.operator&&(this.operator=""),t[2]?this.semver=new i(t[2],this.loose):this.semver=Xr},k.prototype.toString=function(){return this.value},k.prototype.test=function(r){return B("Comparator.test",r,this.loose),this.semver===Xr?!0:("string"==typeof r&&(r=new i(r,this.loose)),$(r,this.operator,this.semver,this.loose))},r.Range=R,R.prototype.format=function(){return this.range=this.set.map(function(r){return r.join(" ").trim()}).join("||").trim(),this.range},R.prototype.toString=function(){return this.range},R.prototype.parseRange=function(r){var e=this.loose;r=r.trim(),B("range",r,e);var t=e?F[Cr]:F[Nr];r=r.replace(t,D),B("hyphen replace",r),r=r.replace(F[Vr],Ar),B("comparator trim",r,F[Vr]),r=r.replace(F[dr],jr),r=r.replace(F[kr],Rr),r=r.split(/\s+/).join(" ");var n=e?F[Tr]:F[xr],i=r.split(" ").map(function(r){return I(r,e)}).join(" ").split(/\s+/);return this.loose&&(i=i.filter(function(r){return!!r.match(n)})),i=i.map(function(r){return new k(r,e)})},r.toComparators=S,R.prototype.test=function(r){if(!r)return!1;"string"==typeof r&&(r=new i(r,this.loose));for(var e=0;e<this.set.length;e++)if(X(this.set[e],r))return!0;return!1},r.satisfies=z,r.maxSatisfying=G,r.validRange=O,r.ltr=P,r.gtr=Z,r.outside=q}(semver); | |
var npmVersion = process.env.npm_package_engines_npm; | |
var ua = process.env.npm_config_user_agent | |
if (!ua) throw new Error('This script does only work when you run it in a npm hook') | |
// Get current npm version | |
var npmOffset = ua.indexOf('npm/')+4 | |
var lastVersionCharIndex = ua.indexOf(' ', npmOffset) | |
if (lastVersionCharIndex == -1) lastVersionCharIndex = ua.length | |
var currentNpmVersion = ua.substring(npmOffset, lastVersionCharIndex) | |
if (!semver.satisfies(currentNpmVersion, npmVersion)) throwErr(npmVersion, currentNpmVersion) | |
function throwErr (desired, current) { | |
msg = '-------------------------------------------------\n' | |
msg +=' Failed to satisfy expected npm version: :v \n'.replace(':v', desired) | |
msg +=' You used npm version: :v \n'.replace(':v', current) | |
msg +='-------------------------------------------------' | |
console.error(msg) | |
process.exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment