UNPKG

create-defuss

Version:

Checks out git projects from sub-directories. Originally for jump-starting defuss projects from templates.

9 lines (7 loc) 3.32 kB
#!/usr/bin/env node var v=Object.defineProperty;var f=(t,o)=>v(t,"name",{value:o,configurable:!0});import{spawnSync as u}from"node:child_process";import{normalize as C,isAbsolute as U,sep as g,basename as z,resolve as b,join as w}from"node:path";import{existsSync as y,mkdirSync as S,mkdtempSync as G,rmSync as I,readdirSync as D,lstatSync as P,readlinkSync as F,symlinkSync as L,copyFileSync as j}from"node:fs";import{tmpdir as N}from"node:os";const E=/^(https:\/\/(?:github|gitlab|bitbucket)\.com)\/([^\/]+)\/([^\/]+)\/(?:tree|src)\/([^\/]+)\/(.+)$/,R=f((t,o,n=E)=>{try{const r=t.match(n);if(!r)throw new Error("Invalid URL format. Use a subdirectory URL (https) from GitHub, GitLab, or Bitbucket.");const[,i,s,c,a,x]=r;[s,c,a].forEach(k=>{if(!/^[\w\-]+$/.test(k))throw new Error(`Invalid characters in input: ${k}`)});const h=C(x);if(U(h)||h.startsWith("..")||h.includes(`${g}..${g}`))throw new Error("Invalid subdirectory path.");const l=h,m=o||z(l),d=b(process.cwd(),m);if(y(d))throw new Error(`Destination folder "${m}" already exists.`);S(d,{recursive:!0});const e=G(w(N(),"sparse-checkout-"));if(console.log("Cloning repository with sparse checkout into temporary directory..."),u("git",["clone","--no-checkout",`${i}/${s}/${c}.git`,e],{stdio:"inherit"}).status!==0)throw new Error("Git clone failed.");const p=b(e,l);if(!p.startsWith(e+g)&&p!==e)throw new Error("Subdirectory path traversal detected.");if(console.log("Initializing sparse-checkout..."),u("git",["-C",e,"sparse-checkout","init"],{stdio:"inherit"}).status!==0)throw new Error("Git sparse-checkout init failed.");if(console.log(`Setting sparse-checkout to subdirectory: ${l}`),u("git",["-C",e,"sparse-checkout","set",l],{stdio:"inherit"}).status!==0)throw new Error("Git sparse-checkout set failed.");if(console.log(`Checking out branch: ${a}...`),u("git",["-C",e,"checkout",a],{stdio:"inherit"}).status!==0)throw new Error("Git checkout failed.");if(!y(p))throw new Error(`Subdirectory "${l}" does not exist in the repository.`);if(console.log("Copying files to the destination directory..."),$(p,d),console.log("Cleaning up temporary directory..."),I(e,{recursive:!0,force:!0}),console.log("Initializing a new git repository..."),u("git",["init"],{cwd:d,stdio:"inherit"}).status!==0)throw new Error("Initializing new git repository failed.");console.log("\u{1F389} All done! Your new project has been set up!"),console.log(` To get started, run the following commands: cd ${m} `)}catch(r){console.error("Error during sparse checkout:",r.message),process.exit(1)}},"performSparseCheckout");function $(t,o){if(!y(t))throw new Error(`Source directory "${t}" does not exist.`);const n=D(t);for(const r of n){const i=w(t,r),s=w(o,r),c=P(i);if(c.isDirectory())S(s,{recursive:!0}),$(i,s);else if(c.isSymbolicLink()){const a=F(i);L(a,s)}else j(i,s)}}f($,"copyDirectoryContents");const A=f(async()=>{const t=process.argv.slice(2);(t.length<1||t.length>2)&&(console.error(`Usage: create-defuss <repo-url> [destination-folder] Example: create-defuss https://github.com/kyr0/defuss/tree/main/examples/with-astro-ts ./my-new-project`),process.exit(1));const o=t[0],n=t[1];R(o,n)},"main");A().catch(t=>{console.error("Unexpected error:",t),process.exit(1)});export{E as defaultScmHostPattern,R as performSparseCheckout}; //# sourceMappingURL=cli.mjs.map