@bring-it/npm
Version:
Publish npm packages when needed
1 lines • 5.78 kB
JavaScript
export const __webpack_id__=368;export const __webpack_ids__=[368];export const __webpack_modules__={7368:(n,e,t)=>{t.r(e),t.d(e,{action:()=>H});var r=t(6760),i=t(3846),o=t(1455),a=t(9795);function s(n,e,t){return(0,a.Ho)(n,e,{cwd:process.cwd(),...t})}function c(n,e,t){return s(n,e,{stdio:[process.stdin,process.stdout,process.stderr],...t})}function u(n){return(0,o.readFile)(n,"utf8").then(n=>JSON.parse(n))}function f(...n){return s("git",n,undefined).then(({stdout:n,stderr:e})=>{if(e)throw new Error(e);return n?.trim()})}async function p(n,e,t){return await n?(i.v.okay(e),!0):(i.v.fail(t),!1)}const l=["M .npmrc","MM .npmrc","U .npmrc","R .npmrc","A .npmrc","?? .npmrc"],h=["main","master","release"];function g(n){return f("show",`HEAD~1:${n}`)}function v(){return f("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 d(){return f("ls-files","package.json","*/package.json")}async function m({force:n}){i.v.task("Checking the working directory...");try{if(!await p(f("--version").then(Boolean),"Git is ready","Git is not installed"))return!1;if(!await p(f("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 p(f("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 p(async function(){const n=await f("rev-parse","--abbrev-ref","HEAD");return"HEAD"===n?(await f("name-rev","--name-only","HEAD")).replace(/^remotes\/origin\//,""):n}().then(n=>h.includes(n)),`Current branch match \`${h.join("/")}\``,`Current branch not match \`${h.join("/")}\``)}())return!1;if(!await p(f("status","--porcelain").then(n=>n.split("\n").map(n=>n.trim()).filter(n=>n&&!l.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 i.v.fail("Checking failed"),i.v.fail(n.message),!1}return!0}var w=t(8399),k=t(32),y=t(9631);function b(n){if(n.private)return i.v.info("[package is private]",n.name),!1;if(!n.name||!y(n.name).validForNewPackages)return i.v.warn("[pkg.name isn't valid]",n.pkg),!1;if(!n.version||!k.valid(n.version))return i.v.warn("[pkg.version isn't valid]",n.pkg),!1;if(!n.publishConfig?.registry)return i.v.info("[Registry undefined]",n.name),!1;if(!w(n.publishConfig.registry))return i.v.warn("[pkg.publishConfig.registry isn't valid]",n.name),!1;if(n.name.startsWith("@")&&"public"!==n.publishConfig.access)return i.v.info("[pkg.publishConfig.access isn't public]",n.name),!1;if(n.engines?.node){if(!k.validRange(n.engines.node))return i.v.warn("[pkg.engines.node isn't valid]",n.pkg),!1;if(!k.satisfies(process.versions.node,n.engines.node))return i.v.warn("[pkg.engines.node isn't match]",n.pkg),!1}return!0}var C=t(8161),_=t(3533);function j(n){return(0,o.readFile)((0,r.join)(n,".npmrc"),"utf8").then(_.parse)}function A(n){return u(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 E(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([j((0,C.homedir)()),j(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 E(n,e,t)}return[]}return t.json().then(n=>Object.keys(n.versions))}).catch(()=>(i.v.warn("[Fail to list version]",n),[]))}async function H({preview:n=!1,force:e=!1,provenance:t=!1}={}){if(!await m({force:e}))return process.exitCode=1,!1;const o=await async function({force:n}){i.v.task("Scanning all package.json...");try{const e=await async function({force:n}){const e=f("cat-file","-t","HEAD~1").then(n?d:v,d),t=await e.then(n=>n?n.split("\n"):[]);for(const e of t)i.v.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 A(t);n&&b(n)&&(e.push(n),i.v.okay("[Publish Allowed]",n.name))}return e}(e),r=n?t:await async function(n){const e=[];for(const t of n){const n=await g(t.pkg).then(n=>JSON.parse(n)).catch(()=>!1);n?n?.version&&n.version===t.version||(e.push(t),i.v.okay("[Version changed]",t.pkg)):(e.push(t),i.v.okay("[First commit]",t.pkg))}return e}(t),o=await async function(n){const e=[];for(const t of n){const n=await E(t.name,t.publishConfig.registry);n&&n.includes(t.version)?i.v.info("[Published, skip]",t.name):(e.push(t),i.v.okay("[Not publish yet]",t.name))}return e}(r);return i.v.info(o.length>0?o.length:"No",1===o.length?"package":"packages","need to publish"),o}catch(n){return i.v.fail("Scanning failed"),i.v.fail(n.message),[]}}({force:e});if(0===o.length)return!1;n&&i.v.info("Won't publish in preview mode");const a=await async function(){return u((0,r.resolve)(process.cwd(),"package.json")).then(({packageManager:n="npm"})=>n.split("@")[0]).catch(()=>"npm")}();i.v.info("Using",a);for(const{dir:e,name:r}of o){const o=n?"[Preview]":"[Publish]";i.v.task(o,r),await c(a,["publish",!!n&&"--dry-run",!!t&&"--provenance","pnpm"===a&&"--git-checks=false"].filter(Boolean),{cwd:e}).then(()=>{i.v.okay(o,r)}).catch(()=>{i.v.fail(o,r),process.exitCode=1})}return!1}}};