UNPKG

@bring-it/npm

Version:
1 lines 6.16 kB
export const __webpack_id__=997;export const __webpack_ids__=[997];export const __webpack_modules__={4997:(n,e,t)=>{t.r(e),t.d(e,{action:()=>x});var r=t(6760),i=t(1455),o=t(8127);const{red:a,cyan:s,green:c,yellow:u,magenta:f}=o.Ay,l=c("[okay]"),p=a("[fail]"),g=u("[warn]"),h=new class{constructor(n){this.name=`[${s("bring-it")}:${f(n)}]`}okay(...n){this.log(l,...n)}fail(...n){this.log(p,...n)}warn(...n){this.log(g,...n)}task(...n){this.log("[task]",...n)}file(...n){this.log("[file]",...n)}info(...n){this.log("[info]",...n)}json(n){this.log("[json]",JSON.stringify(n,"",2))}log(...n){console.log(this.name,...n)}}("npm");var d=t(9795);function m(n,e,t){return(0,d.Ho)(n,e,{cwd:process.cwd(),...t})}function w(n){return(0,i.readFile)(n,"utf8").then((n=>JSON.parse(n)))}function k(...n){return m("git",n,undefined).then((({stdout:n,stderr:e})=>{if(e)throw new Error(e);return n?.trim()}))}async function v(n,e,t){return await n?(h.okay(e),!0):(h.fail(t),!1)}const y=["M .npmrc","MM .npmrc","U .npmrc","R .npmrc","A .npmrc","?? .npmrc"],b=["main","master","release"];function C(){return k("diff","HEAD~1","HEAD","--name-only","--ignore-blank-lines","--ignore-cr-at-eol","--ignore-space-at-eol","--diff-filter=d","package.json","*/package.json")}function _(){return k("ls-files","package.json","*/package.json")}async function j({force:n}){h.task("Checking the working directory...");try{if(!await v(k("--version").then(Boolean),"Git is ready","Git is not installed"))return!1;if(!await v(k("rev-parse","--is-inside-work-tree").then((n=>"true"===n)),"Current directory inside a git repo","Current directory not inside a git repo"))return!1;if(!await v(k("rev-parse","--show-toplevel").then((n=>(0,r.resolve)(n)===(0,r.resolve)(process.cwd()))),"Current directory is the git root","Current directory is not the git root"))return!1;if(!n){if(!await async function(){return v(async function(){const n=await k("rev-parse","--abbrev-ref","HEAD");return"HEAD"===n?(await k("name-rev","--name-only","HEAD")).replace(/^remotes\/origin\//,""):n}().then((n=>b.includes(n))),`Current branch match \`${b.join("/")}\``,`Current branch not match \`${b.join("/")}\``)}())return!1;if(!await v(k("status","--porcelain").then((n=>n.split("\n").map((n=>n.trim())).filter((n=>n&&!y.includes(n))))).then((n=>0===n.length||(console.info(n.join("\n")),!1))),"Current directory is a clean repo","Current directory is not a clean repo"))return!1}}catch(n){return h.fail("Checking failed"),h.fail(n.message),!1}return!0}var A=t(8399),E=t(32),H=t(9631);function D(n){if(n.private)return h.info("[package is private]",n.name),!1;if(!n.name||!H(n.name).validForNewPackages)return h.warn("[pkg.name isn't valid]",n.pkg),!1;if(!n.version||!E.valid(n.version))return h.warn("[pkg.version isn't valid]",n.pkg),!1;if(!n.publishConfig?.registry)return h.info("[Registry undefined]",n.name),!1;if(!A(n.publishConfig.registry))return h.warn("[pkg.publishConfig.registry isn't valid]",n.name),!1;if(n.name.startsWith("@")&&"public"!==n.publishConfig.access)return h.info("[pkg.publishConfig.access isn't public]",n.name),!1;if(n.engines?.node){if(!E.validRange(n.engines.node))return h.warn("[pkg.engines.node isn't valid]",n.pkg),!1;if(!E.satisfies(process.versions.node,n.engines.node))return h.warn("[pkg.engines.node isn't match]",n.pkg),!1}return!0}var $=t(8161),F=t(3533);function N(n){return(0,i.readFile)((0,r.join)(n,".npmrc"),"utf8").then(F.parse)}function P(n){return w(n).then((({name:e,version:t,private:i=!1,engines:o,publishConfig:a})=>({pkg:n,dir:(0,r.dirname)(n),name:e,version:t,private:i,publishConfig:a,engines:o}))).catch((()=>!1))}function S(n,e,t=""){return function(n,e){const t=new AbortController,r=setTimeout((()=>t.abort("timeout")),5e3);return fetch(n,{...e,signal:t.signal}).then((n=>(clearTimeout(r),n))).catch((n=>{throw clearTimeout(r),n}))}(new URL(n,e).href,{headers:t?{Authorization:`Bearer ${t}`}:void 0}).then((async t=>{if(!t.ok){if(401===t.status){const t=await function(n="https://registry.npmjs.org/"){return Promise.allSettled([N((0,$.homedir)()),N(process.cwd())]).then((([n,e])=>({...n.value,...e.value}))).then((e=>{const{host:t,pathname:r}=new URL(n);return e[`//${t+r}:_authToken`]})).catch((()=>{}))}(e);if(t)return S(n,e,t)}return[]}return t.json().then((n=>Object.keys(n.versions)))})).catch((()=>(h.warn("[Fail to list version]",n),[])))}async function x({preview:n=!1,force:e=!1,provenance:t=!1}={}){if(!await j({force:e}))return process.exitCode=1,!1;const i=await async function({force:n}){h.task("Scanning all package.json...");try{const e=await async function({force:n}){const e=k("cat-file","-t","HEAD~1").then(n?_:C,_),t=await e.then((n=>n?n.split("\n"):[]));for(const e of t)h.okay(n?"[Forcing scan]":"[Latest Modified]",e);return t}({force:n}),t=await async function(n){const e=[];for(const t of n){const n=await P(t);n&&D(n)&&(e.push(n),h.okay("[Publish Allowed]",n.name))}return e}(e),r=n?t:await async function(n){const e=[];for(const r of n){const n=await(t=r.pkg,k("show",`HEAD~1:${t}`)).then((n=>JSON.parse(n))).catch((()=>!1));n?n?.version&&n.version===r.version||(e.push(r),h.okay("[Version changed]",r.pkg)):(e.push(r),h.okay("[First commit]",r.pkg))}var t;return e}(t),i=await async function(n){const e=[];for(const t of n){const n=await S(t.name,t.publishConfig.registry);n&&n.includes(t.version)?h.info("[Published, skip]",t.name):(e.push(t),h.okay("[Not publish yet]",t.name))}return e}(r);return h.info(i.length>0?i.length:"No",1===i.length?"package":"packages","need to publish"),i}catch(n){return h.fail("Scanning failed"),h.fail(n.message),[]}}({force:e});if(0===i.length)return!1;n&&h.info("Won't publish in preview mode");const o=await async function(){return w((0,r.resolve)(process.cwd(),"package.json")).then((({packageManager:n="npm"})=>n.split("@")[0]))}().catch((()=>"npm"));h.info("Using",o);for(const{dir:e,name:r}of i){const i=n?"[Preview]":"[Publish]";h.task(i,r),await(a=o,s=["publish",!!n&&"--dry-run",!!t&&"--provenance"].filter(Boolean),c={cwd:e},m(a,s,{stdio:[process.stdin,process.stdout,process.stderr],...c})).then((()=>{h.okay(i,r)})).catch((()=>{h.fail(i,r),process.exitCode=1}))}var a,s,c;return!1}}};