UNPKG

@cmtlyt/git-down

Version:
3 lines (2 loc) 2.96 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const node_path=require("node:path"),node_child_process=require("node:child_process"),node_fs=require("node:fs"),promises=require("node:fs/promises"),node_util=require("node:util"),exec=node_util.promisify(node_child_process.exec);async function createOutput(t){if(!node_fs.existsSync(t))return promises.mkdir(t,{recursive:!0})}async function rmdir(t){if(node_fs.existsSync(t))return promises.rm(t,{recursive:!0,force:!0}).catch(()=>{})}function buildOption(t){return{output:"./git-down",branch:"main",...t}}function buildCallback(t){return e=>{if(e){if(!t)throw e;t(e)}t&&t(null)}}function parseGitUrl(t){const e=t.replace(/(^https?:\/\/github\.com\/)|(^git@github\.com:)/,"/"),[,n,o,,i="",...r]=e.split("/"),c=r.join("/"),u=e.includes("blob")||!!node_path.extname(c);return{href:t,owner:n,project:o.replace(/\.git$/,""),isRepo:e.endsWith(".git")||!e.includes("tree")&&!e.includes("blob"),sourceType:u?"file":"dir",branch:i,pathname:c}}async function moveOrCopyAndCleanup(t,e){return node_fs.existsSync(e)?promises.cp(t,e,{recursive:!0}):promises.rename(t,e)}function downloadMain(t){const{gitInfo:e,option:n,callback:o}=t,{branch:i,output:r}=n,{owner:c,project:u}=e,a={cwd:node_path.resolve(r)};return exec("git init --quiet",a).then(()=>exec(`git remote add origin https://github.com/${c}/${u}`,a),o).then(()=>exec(`git pull origin --quiet ${i} --depth 1`,a),o).then(()=>rmdir(node_path.resolve(r,".git")),o)}async function downloadPartial(t){const{gitInfo:e,callback:n,option:o}=t,{output:i}=o,{pathname:r,sourceType:c,branch:u,owner:a,project:h}=e,s=node_path.resolve(i,`./.git-down-temp-folder-${Date.now()}-${Math.random().toString(36).slice(2)}`);node_fs.existsSync(s)&&rmdir(s),await createOutput(s).catch(n);const p=node_path.resolve(s,".git/info/sparse-checkout"),g=c==="file"?r:`${r}/*`,f=`${node_fs.existsSync(p)?await promises.readFile(p,{encoding:"utf-8"}):""} ${g}`,m=node_path.resolve(i,r.split("/").pop()),l={cwd:s},d=`remote-${Date.now()}-${Math.random().toString(36).slice(2)}`;return exec("git init --quiet",l).then(()=>exec(`git remote add ${d} https://github.com/${a}/${h}`,l),n).then(async()=>{try{if((await exec("git config --get core.sparseCheckout",l)).stdout.trim()==="true")return}catch{}return exec("git config core.sparseCheckout true",l)},n).then(()=>promises.writeFile(p,f),n).then(()=>exec(`git pull ${d} --quiet ${u} --depth 1`,l),n).then(()=>moveOrCopyAndCleanup(node_path.resolve(s,r),m),n).then(()=>rmdir(s),n)}function gitDownWithCallback(t,e,n){const o=buildOption(e),i=buildCallback(n),r=parseGitUrl(t),c={option:o,callback:i,gitInfo:r};createOutput(o.output).then(()=>(r.isRepo?downloadMain:downloadPartial)(c),i).then(()=>i(null),i)}function gitDown(t,e){return new Promise((n,o)=>{gitDownWithCallback(t,e,i=>{i?o(i):n()})})}exports.default=gitDown,exports.gitDown=gitDown,exports.gitDownWithCallback=gitDownWithCallback,exports.parseGitUrl=parseGitUrl;