UNPKG

@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) 7.09 kB
"use strict";const node_child_process=require("node:child_process"),promises=require("node:fs/promises"),update=require("c12/update"),consola=require("consola"),defu=require("defu"),C=require("ini"),pathe=require("pathe"),utils=require("./basis.D3fInv-P.cjs");function _interopDefaultCompat(e){return e&&typeof e=="object"&&"default"in e?e.default:e}const C__default=_interopDefaultCompat(C);function G(){try{return node_child_process.execSync("git --version",{stdio:"pipe"}),!0}catch{return!1}}async function b(e,i=!1,r=!1){try{return await update.updateConfig({cwd:e,configFile:"basis.config",onUpdate:t=>{t.git&&(i&&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 M=["feat","fix","docs","style","refactor","perf","test","build","ci","chore","revert"];function parseCommitMessage(e){const i=e.trim().split(` `),r=i[0].match(/^(\w+)(\(([^)]+)\))?(!)?:\s*(.+)$/);if(!r)return null;const[,t,,o,n,s]=r,c=i.slice(1).find(l=>l.trim())?.trim(),a=i.slice(-1)[0]?.trim();return{type:t,scope:o,description:s,body:c,footer:a,isBreaking:!!n||e.includes("BREAKING CHANGE:")}}function validateCommitMessage(e,i={}){const r=[],{types:t=M,maxLength:o=72,minLength:n=10,scopeRequired:s=!1,allowedScopes:c=[]}=i,a=parseCommitMessage(e);if(!a)return{valid:!1,errors:["Invalid commit format. Expected: type(scope): description"]};t.includes(a.type)||r.push(`Invalid type '${a.type}'. Allowed: ${t.join(", ")}`);const l=e.split(` `)[0];return l.length>o&&r.push(`Header too long (${l.length}). Max: ${o}`),l.length<n&&r.push(`Header too short (${l.length}). Min: ${n}`),s&&!a.scope&&r.push("Scope is required"),a.scope&&c.length>0&&!c.includes(a.scope)&&r.push(`Invalid scope '${a.scope}'. Allowed: ${c.join(", ")}`),{valid:r.length===0,errors:r}}async function lintCommitMessage(e=process.cwd(),i){if(!G())return consola.consola.warn("Git command not available, skipping commit message linting"),!0;const{config:r}=await utils.loadConfig({cwd:e,overrides:i?{git:{commitMsg:i}}:void 0}),t=r.git?.commitMsg||{};let o;try{const s=pathe.resolve(".git/COMMIT_EDITMSG");await utils.fileExists(s)?o=(await promises.readFile(s)).toString("utf8"):o=node_child_process.execSync("git log -1 --pretty=%B",{encoding:"utf8"}).trim()}catch(s){return consola.consola.error("Failed to read commit message:",s),!1}const n=validateCommitMessage(o,t);return n.valid?!0:(consola.consola.error("Invalid commit message:"),n.errors.forEach(s=>consola.consola.error(` ${s}`)),!1)}async function w(e){const i=pathe.resolve(e,".git/config");if(!await utils.fileExists(i))return null;const r=new Date().toISOString().replace(/[:.]/g,"-"),t=pathe.resolve(e,`.git/config.backup.${r}`);try{return await promises.copyFile(i,t),t}catch(o){return consola.consola.warn("Failed to create Git config backup:",o),null}}async function readGitConfig(e=process.cwd()){const i=pathe.resolve(e,".git/config");if(!await utils.fileExists(i))return{};try{const r=await promises.readFile(i,"utf8");return C__default.parse(r)}catch(r){return consola.consola.warn("Failed to read .git/config:",r),{}}}async function writeGitConfig(e,i=process.cwd()){const r=pathe.resolve(i,".git/config");try{let t=C__default.stringify(e,{whitespace:!0});t=t.split(` `).map(o=>o&&!o.startsWith("[")&&o.includes("=")?` ${o}`:o).join(` `),await promises.writeFile(r,t,"utf8")}catch(t){throw consola.consola.error("Failed to write .git/config:",t),t}}function S(e,i){if(!i)return!0;for(const[r,t]of Object.entries(i)){if(typeof t!="object"||!t)continue;const o=e[r];if(!o||typeof o!="object")return!1;for(const[n,s]of Object.entries(t))if(s!==void 0&&o[n]!==s)return!1}return!0}async function setupGitConfig(e=process.cwd(),i){const{config:r}=await utils.loadConfig({cwd:e,overrides:i?{git:{config:i}}:void 0}),t=r.git?.config||{};if(Object.keys(t).length===0)return!0;try{const o=await readGitConfig(e);if(S(o,t))return!0;await w(e);const n=defu.defu(o,t);return await writeGitConfig(n,e),!0}catch(o){return consola.consola.error("Failed to setup Git configuration:",o),!1}}async function resetGitConfig(e=process.cwd(),i=!0,r={}){try{await w(e);const t=await readGitConfig(e);if(!t||Object.keys(t).length===0)return!0;const o={};if(i&&t.user&&(o.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&&(o.core=s)}return await writeGitConfig(o,e),r.updateConfig?await b(e,!1,!0):!0}catch(t){return consola.consola.error("Failed to reset Git configuration:",t),!1}}async function setupGitHooks(e=process.cwd(),i){const{config:r}=await utils.loadConfig({cwd:e,overrides:i?{git:{hooks:i}}:void 0}),t=r.git?.hooks||{},o=pathe.resolve(e,".git/hooks");if(!await utils.fileExists(o))return consola.consola.error("Git hooks directory not found. Is this a Git repository?"),!1;let n=!0;for(const[s,c]of Object.entries(t)){const a=pathe.resolve(o,s);try{let l=`#!/bin/sh `;if(typeof c=="string")l+=`${c} `;else if(c&&typeof c=="object"&&"commands"in c){const f=c.commands;l+=`${f.join(` `)} `}await promises.writeFile(a,l,{mode:493})}catch(l){consola.consola.error(`Failed to setup ${s} hook:`,l),n=!1}}return n}async function initGitRepo(e=process.cwd()){if(!G())return consola.consola.warn("Git command not available, cannot initialize repository"),!1;try{try{return node_child_process.execSync("git rev-parse --git-dir",{cwd:e,stdio:"pipe"}),!0}catch{return node_child_process.execSync("git init",{cwd:e,stdio:"inherit"}),consola.consola.success("Initialized Git repository"),!0}}catch(i){return consola.consola.error("Failed to initialize Git repository:",i),!1}}async function setupGit(e=process.cwd()){const{config:i}=await utils.loadConfig({cwd:e}),r=i.git||{},t=(await Promise.allSettled([setupGitConfig(e,r.config),setupGitHooks(e,r.hooks)])).filter(o=>o.status==="rejected"||o.status==="fulfilled"&&!o.value);return t.length===0?(consola.consola.success("Git setup completed successfully!"),!0):(consola.consola.error(`${t.length} Git setup step(s) failed`),!1)}async function removeGitHooks(e=process.cwd(),i,r={}){const t=pathe.resolve(e,".git/hooks");if(!await utils.fileExists(t))return consola.consola.warn("Git hooks directory not found. Is this a Git repository?"),!0;let o=!0,n;if(i&&i.length>0)n=i;else{const{config:s}=await utils.loadConfig({cwd:e});n=Object.keys(s.git?.hooks||{})}for(const s of n){const c=pathe.resolve(t,s);if(await utils.fileExists(c))try{await promises.unlink(c)}catch(a){consola.consola.error(`Failed to remove ${s} hook:`,a),o=!1}}if(!i&&r.updateConfig){const s=await b(e,!0,!1);o=o&&s}return o}exports.initGitRepo=initGitRepo,exports.lintCommitMessage=lintCommitMessage,exports.parseCommitMessage=parseCommitMessage,exports.readGitConfig=readGitConfig,exports.removeGitHooks=removeGitHooks,exports.resetGitConfig=resetGitConfig,exports.setupGit=setupGit,exports.setupGitConfig=setupGitConfig,exports.setupGitHooks=setupGitHooks,exports.validateCommitMessage=validateCommitMessage,exports.writeGitConfig=writeGitConfig;