@funish/basis
Version:
A unified development toolkit with CLI for package management, versioning, publishing, linting, and git hooks management for JavaScript/TypeScript projects.
11 lines (9 loc) • 5.66 kB
JavaScript
import{execSync as p}from"node:child_process";import{readFile as h,unlink as M,writeFile as y,copyFile as O}from"node:fs/promises";import{updateConfig as S}from"c12/update";import{consola as a}from"consola";import{defu as x}from"defu";import v from"ini";import{resolve as u}from"pathe";import{l as d,f as g}from"./basis.BrlVPuDi.mjs";function G(){try{return p("git --version",{stdio:"pipe"}),!0}catch{return!1}}async function k(e,o=!1,r=!1){try{return await S({cwd:e,configFile:"basis.config",onUpdate:t=>{t.git&&(o&&t.git.hooks&&delete t.git.hooks,r&&t.git.config&&delete t.git.config,Object.keys(t.git).length===0&&delete t.git)}}),!0}catch{return!1}}const E=["feat","fix","docs","style","refactor","perf","test","build","ci","chore","revert"];function b(e){const o=e.trim().split(`
`),r=o[0].match(/^(\w+)(\(([^)]+)\))?(!)?:\s*(.+)$/);if(!r)return null;const[,t,,i,n,s]=r,c=o.slice(1).find(l=>l.trim())?.trim(),f=o.slice(-1)[0]?.trim();return{type:t,scope:i,description:s,body:c,footer:f,isBreaking:!!n||e.includes("BREAKING CHANGE:")}}function $(e,o={}){const r=[],{types:t=E,maxLength:i=72,minLength:n=10,scopeRequired:s=!1,allowedScopes:c=[]}=o,f=b(e);if(!f)return{valid:!1,errors:["Invalid commit format. Expected: type(scope): description"]};t.includes(f.type)||r.push(`Invalid type '${f.type}'. Allowed: ${t.join(", ")}`);const l=e.split(`
`)[0];return l.length>i&&r.push(`Header too long (${l.length}). Max: ${i}`),l.length<n&&r.push(`Header too short (${l.length}). Min: ${n}`),s&&!f.scope&&r.push("Scope is required"),f.scope&&c.length>0&&!c.includes(f.scope)&&r.push(`Invalid scope '${f.scope}'. Allowed: ${c.join(", ")}`),{valid:r.length===0,errors:r}}async function H(e=process.cwd(),o){if(!G())return a.warn("Git command not available, skipping commit message linting"),!0;const{config:r}=await d({cwd:e,overrides:o?{git:{commitMsg:o}}:void 0}),t=r.git?.commitMsg||{};let i;try{const s=u(".git/COMMIT_EDITMSG");await g(s)?i=(await h(s)).toString("utf8"):i=p("git log -1 --pretty=%B",{encoding:"utf8"}).trim()}catch(s){return a.error("Failed to read commit message:",s),!1}const n=$(i,t);return n.valid?!0:(a.error("Invalid commit message:"),n.errors.forEach(s=>a.error(` ${s}`)),!1)}async function j(e){const o=u(e,".git/config");if(!await g(o))return null;const r=new Date().toISOString().replace(/[:.]/g,"-"),t=u(e,`.git/config.backup.${r}`);try{return await O(o,t),t}catch(i){return a.warn("Failed to create Git config backup:",i),null}}async function m(e=process.cwd()){const o=u(e,".git/config");if(!await g(o))return{};try{const r=await h(o,"utf8");return v.parse(r)}catch(r){return a.warn("Failed to read .git/config:",r),{}}}async function w(e,o=process.cwd()){const r=u(o,".git/config");try{let t=v.stringify(e,{whitespace:!0});t=t.split(`
`).map(i=>i&&!i.startsWith("[")&&i.includes("=")?` ${i}`:i).join(`
`),await y(r,t,"utf8")}catch(t){throw a.error("Failed to write .git/config:",t),t}}function A(e,o){if(!o)return!0;for(const[r,t]of Object.entries(o)){if(typeof t!="object"||!t)continue;const i=e[r];if(!i||typeof i!="object")return!1;for(const[n,s]of Object.entries(t))if(s!==void 0&&i[n]!==s)return!1}return!0}async function C(e=process.cwd(),o){const{config:r}=await d({cwd:e,overrides:o?{git:{config:o}}:void 0}),t=r.git?.config||{};if(Object.keys(t).length===0)return!0;try{const i=await m(e);if(A(i,t))return!0;await j(e);const n=x(i,t);return await w(n,e),!0}catch(i){return a.error("Failed to setup Git configuration:",i),!1}}async function z(e=process.cwd(),o=!0,r={}){try{await j(e);const t=await m(e);if(!t||Object.keys(t).length===0)return!0;const i={};if(o&&t.user&&(i.user=t.user),t.core){const n=["repositoryformatversion","filemode","bare","logallrefupdates"],s={};n.forEach(c=>{t.core[c]!==void 0&&(s[c]=t.core[c])}),Object.keys(s).length>0&&(i.core=s)}return await w(i,e),r.updateConfig?await k(e,!1,!0):!0}catch(t){return a.error("Failed to reset Git configuration:",t),!1}}async function F(e=process.cwd(),o){const{config:r}=await d({cwd:e,overrides:o?{git:{hooks:o}}:void 0}),t=r.git?.hooks||{},i=u(e,".git/hooks");if(!await g(i))return a.error("Git hooks directory not found. Is this a Git repository?"),!1;let n=!0;for(const[s,c]of Object.entries(t)){const f=u(i,s);try{let l=`#!/bin/sh
`;if(typeof c=="string")l+=`${c}
`;else if(c&&typeof c=="object"&&"commands"in c){const I=c.commands;l+=`${I.join(`
`)}
`}await y(f,l,{mode:493})}catch(l){a.error(`Failed to setup ${s} hook:`,l),n=!1}}return n}async function B(e=process.cwd()){if(!G())return a.warn("Git command not available, cannot initialize repository"),!1;try{try{return p("git rev-parse --git-dir",{cwd:e,stdio:"pipe"}),!0}catch{return p("git init",{cwd:e,stdio:"inherit"}),a.success("Initialized Git repository"),!0}}catch(o){return a.error("Failed to initialize Git repository:",o),!1}}async function R(e=process.cwd()){const{config:o}=await d({cwd:e}),r=o.git||{},t=(await Promise.allSettled([C(e,r.config),F(e,r.hooks)])).filter(i=>i.status==="rejected"||i.status==="fulfilled"&&!i.value);return t.length===0?(a.success("Git setup completed successfully!"),!0):(a.error(`${t.length} Git setup step(s) failed`),!1)}async function q(e=process.cwd(),o,r={}){const t=u(e,".git/hooks");if(!await g(t))return a.warn("Git hooks directory not found. Is this a Git repository?"),!0;let i=!0,n;if(o&&o.length>0)n=o;else{const{config:s}=await d({cwd:e});n=Object.keys(s.git?.hooks||{})}for(const s of n){const c=u(t,s);if(await g(c))try{await M(c)}catch(f){a.error(`Failed to remove ${s} hook:`,f),i=!1}}if(!o&&r.updateConfig){const s=await k(e,!0,!1);i=i&&s}return i}export{C as a,z as b,F as c,q as d,B as i,H as l,b as p,m as r,R as s,$ as v,w};