UNPKG

anl

Version:
5 lines 4.41 kB
const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../utils/logger.cjs`);require(`../utils/index.cjs`);const n=require(`./utils.cjs`);let r=require(`child_process`),i=require(`fs`);i=e.__toESM(i);let a=require(`path`);a=e.__toESM(a);const o=e=>{try{return(0,r.execSync)(`git rev-parse --is-inside-work-tree`,{cwd:e,stdio:`ignore`}),!0}catch{return!1}},s=(e,n,i)=>{try{return(0,r.execFileSync)(`git`,e,{cwd:n,stdio:`ignore`}),!0}catch(e){return console.log(e),t.log.error(i),!1}},c=e=>{let t=process.platform===`win32`?`where`:`command -v`;try{return(0,r.execSync)(`${t} ${e}`,{stdio:`ignore`}),!0}catch{return!1}},l=e=>i.default.existsSync(a.default.join(e,`pnpm-lock.yaml`))?`pnpm`:i.default.existsSync(a.default.join(e,`yarn.lock`))?`yarn`:`npm`,u=e=>{let t=`@commitlint/cli @commitlint/config-conventional lint-staged`;return e===`pnpm`?`pnpm add -D ${t}`:e===`yarn`?`yarn add -D ${t}`:`npm install -D ${t}`},d=(e,n)=>{let r=[];if(e.includes(`gitflow`)&&!c(`jq`)&&(r.push(`Missing dependency: jq (required by git nb branch generator).`),r.push(`Quick fix (macOS): brew install jq`)),e.includes(`commitSubject`)){let e=i.default.existsSync(a.default.join(n,`node_modules`,`.bin`,process.platform===`win32`?`commitlint.cmd`:`commitlint`))||c(`commitlint`),t=i.default.existsSync(a.default.join(n,`node_modules`,`.bin`,process.platform===`win32`?`lint-staged.cmd`:`lint-staged`))||c(`lint-staged`);if(!e||!t){let e=l(n);r.push(`Missing dependencies for hooks: commitlint and/or lint-staged.`),r.push(`Quick fix (${e}): ${u(e)}`)}}if(r.length>0){console.log(` `);for(let e of r)t.log.warning(e);console.log(` `)}},f=async(e,r)=>{let i=a.default.join(e,`.commit-type.cjs`),o=a.default.join(r,`.commit-type.cjs`);if(!await n.pathExists(i)){t.log.error(`source file ${i} does not exist`);return}await n.copyFileIfMissing(i,o)},p=async()=>{let e=__dirname,r=process.cwd(),o=a.default.join(e,`.gitscripts`),c=a.default.join(r,`.gitscripts`);await n.pathExists(o)?(await n.copyDirectoryRecursive(o,c),t.log.success(`.gitscripts create done.`)):t.log.error(`source directory ${o} does not exist`);for(let i of[`.gitconfig`]){let o=a.default.join(e,i),s=a.default.join(r,i);await n.pathExists(o)?await n.copyFileIfMissing(o,s):t.log.error(`source file ${o} does not exist`)}await f(e,r);try{i.default.chmodSync(a.default.join(c,`random-branch.sh`),493),t.log.success(`random-branch.sh Raise power`)}catch(e){console.log(e),t.log.error(`Set .gitscripts/random-branch.sh executable permission failed: ${e instanceof Error?e.message:String(e)}`)}let l=a.default.resolve(r,`.gitconfig`);s([`config`,`--local`,`--replace-all`,`include.path`,l],r,`Execute [git config --local --replace-all include.path ${l}] failed, please execute the command manually`)&&t.log.success(`.gitconfig git set`)},m=async()=>{let e=a.default.join(__dirname,`.githooks`),r=process.cwd(),o=a.default.join(r,`.githooks`);if(!await n.pathExists(e)){t.log.error(`source directory ${e} does not exist`);return}await n.copyDirectoryRecursive(e,o,!0),t.log.success(`.githooks create done.`),await f(__dirname,r);try{i.default.chmodSync(a.default.join(o,`commit-msg`),493),i.default.chmodSync(a.default.join(o,`pre-commit`),493),t.log.success(`commit-msg/pre-commit raise power done.`)}catch(e){console.log(e),t.log.error(`Set .githooks hooks executable permission failed: ${e instanceof Error?e.message:String(e)}`)}s([`config`,`--local`,`--replace-all`,`core.hooksPath`,`.githooks`],r,`Execute [git config --local --replace-all core.hooksPath .githooks] failed, please execute the command manually`)&&t.log.success(`git set .githooks done.`)},h=async()=>{let e=__dirname,r=process.cwd(),i=a.default.join(e,`.gitattributes`),o=a.default.join(r,`.gitattributes`);if(!await n.pathExists(i)){t.log.error(`source file ${i} does not exist`);return}await n.copyFileIfMissing(i,o)},g=async(e=[])=>{if(e.length===0){t.log.warning(`No Git feature selected, nothing to initialize.`);return}let n=process.cwd();if((e.includes(`gitflow`)||e.includes(`commitSubject`))&&!o(n)){t.log.error(`Current directory is not a Git repository. Please run this command in a Git project.`);return}d(e,n),e.includes(`gitflow`)&&await p(),e.includes(`commitSubject`)&&await m(),e.includes(`customGitCommand`)&&await h(),e.includes(`gitflow`)&&(console.log(` `),t.log.warning(`please run [git nb] command to create a new branch.`),console.log(` `))};exports.gitHandle=g;