@pyyupsk/nit 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +5 -7
  2. package/package.json +18 -13
package/dist/cli.js CHANGED
@@ -1,10 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import{join as rt}from"path";import{existsSync as T}from"fs";import{readdir as O,readFile as h,stat as A}from"fs/promises";import{join as k}from"path";async function p(e){try{return[null,await e]}catch(n){return[n instanceof Error?n:new Error(String(n)),null]}}var g=`#!/bin/sh
3
- if [ "$SKIP_NIT"`;function m(e){return`./node_modules/.bin/nit exec ${e}`}function d(e,n){return`#!/bin/sh
2
+ import{existsSync as L}from"fs";import{readdir as G,readFile as w,stat as B}from"fs/promises";import{join as E}from"path";async function y(r){try{return[null,await r]}catch(t){return[t instanceof Error?t:new Error(String(t)),null]}}var f=`#!/bin/sh
4
3
  if [ "$SKIP_NIT" = "1" ]; then
5
- exit 0
4
+ `;function d(r){return`./node_modules/.bin/nit exec ${r}`}function k(r,t){let n=typeof r=="string"?r:d(t);return`${f} exit 0
6
5
  fi
7
- ${typeof e=="string"?e:m(n)}
8
- `}async function C(e,n,t){let r=k(t,e);if(!T(r))return[null,!1];let[o,i]=await p(h(r,"utf8"));if(o!==null)return[o,null];let s=typeof n=="string"?n.trim():m(e);return[null,i.split(`
9
- `).some(c=>c.trim()===s)]}async function I(e,n){try{let t=await O(e,{withFileTypes:!0});for(let r of t)if(!(!r.isFile()||n.has(r.name)))try{if((await h(k(e,r.name),"utf8")).startsWith(g))return!0}catch{}}catch{}return!1}async function y(e,n){try{await A(n)}catch{return[new Error(`Hooks directory does not exist: ${n}`),null]}for(let[t,r]of Object.entries(e)){let[o,i]=await C(t,r,n);if(o!==null)return[o,null];if(!i)return[null,!1]}return await I(n,new Set(Object.keys(e)))?[null,!1]:[null,!0]}import{chmod as D,readdir as F,readFile as L,stat as _,unlink as w,writeFile as G}from"fs/promises";import{join as b}from"path";async function E(e,n){try{await _(n)}catch{return[new Error(`Hooks directory does not exist: ${n}`),null]}let t=[];for(let[r,o]of Object.entries(e)){let i=b(n,r);try{await G(i,d(o,r),"utf8"),t.push(i),await D(i,493)}catch(s){return await Promise.all(t.map(a=>w(a).catch(()=>{}))),[s instanceof Error?s:new Error(`Failed to write hook "${r}"`),null]}}try{let r=await F(n,{withFileTypes:!0}),o=new Set(Object.keys(e));await Promise.all(r.filter(i=>i.isFile()&&!o.has(i.name)).map(async i=>{let s=b(n,i.name);try{(await L(s,"utf8")).startsWith(g)&&await w(s)}catch{}}))}catch{}return[null,!0]}import{spawn as $}from"child_process";var v="{staged_files}";function x(e){return e.replaceAll(/[.+^$|()[\]\\]/g,String.raw`\$&`)}function J(e){let n="",t=0;for(;t<e.length;){let r=e.charAt(t);if(r==="*"&&e.charAt(t+1)==="*")n+=".*",t+=2,e.charAt(t)==="/"&&t++;else if(r==="*")n+="[^/]*",t++;else if(r==="?")n+="[^/]",t++;else if(r==="{"){let o=e.indexOf("}",t);if(o<0)n+=String.raw`\{`,t++;else{let i=e.slice(t+1,o).split(",").map(x).join("|");n+=`(?:${i})`,t=o+1}}else n+=x(r),t++}return new RegExp(`^${n}$`)}function q(e){let n=[],t="",r=0;for(;r<e.length;){let o=e.charAt(r);if(o==='"'||o==="'"){let i=o;for(r++;r<e.length&&e.charAt(r)!==i;)t+=e.charAt(r),r++;r++}else/\s/.test(o)?(t.length>0&&(n.push(t),t=""),r++):(t+=o,r++)}return t.length>0&&n.push(t),n}function z(e,n,t){return new Promise(r=>{let o=[];for(let c of q(e))c===v?o.push(...n):o.push(c);let[i,...s]=o;if(!i)return r(new Error(`Empty command in stage: "${e}"`));let a=$(i,s,{cwd:t,stdio:"inherit"});a.on("close",c=>r(c===0?null:new Error(`Command "${e}" exited with code ${c}`))),a.on("error",r)})}function B(e){return new Promise(n=>{let t=$("git",["diff","--cached","--name-only","--diff-filter=ACMR"],{cwd:e,stdio:["ignore","pipe","ignore"]}),r="";t.stdout.on("data",o=>{r+=o.toString()}),t.on("close",()=>n([null,r.split(`
10
- `).filter(Boolean)])),t.on("error",o=>n([o,null]))})}async function j(e,n){let[t,r]=await B(n);if(t!==null)return[t,null];if(r.length===0)return[null,!0];let o=r.map(i=>{let s=i.replaceAll("\\","/");return{file:s,basename:s.split("/").at(-1)??s}});for(let[i,s]of Object.entries(e)){let a=J(i),c=o.filter(({file:u,basename:f})=>a.test(u)||a.test(f)).map(({file:u})=>u);if(c.length===0)continue;let l=await z(s,c,n);if(l!==null)return[l,null]}return[null,!0]}import{existsSync as K}from"fs";import{join as M}from"path";function W(e){let n=process.platform==="win32"?";":":",t=process.platform==="win32"?[".exe",".cmd",".bat",""]:[""];return(process.env.PATH??"").split(n).some(o=>t.some(i=>K(M(o,e+i))))}function S(e,n){let t=n.trim().split(/\s+/).at(0);return t===void 0||t===""?new Error(`Hook "${e}" has an empty command`):W(t)?null:new Error(`Hook "${e}": command "${t}" not found in PATH`)}function R(e){for(let[n,t]of Object.entries(e))if(typeof t=="string"){let r=S(n,t);if(r!==null)return r}else for(let[r,o]of Object.entries(t.stages)){let i=S(`${n}[${r}]`,o);if(i!==null)return i}return null}import{readFile as U}from"fs/promises";import{join as V}from"path";function Q(e,n){if(typeof n=="string")return[null,n];if(typeof n=="object"&&n!==null&&!Array.isArray(n)&&Object.hasOwn(n,"stages")){let t=n.stages;if(typeof t!="object"||t===null||Array.isArray(t))return[new Error(`Hook "${e}" has invalid stages: stages must be a plain object`),null];let r=t;for(let[o,i]of Object.entries(r))if(typeof i!="string")return[new Error(`Hook "${e}" stage "${o}" must have a string command, got ${typeof i}`),null];return[null,{stages:r}]}return[new Error(`Hook "${e}" must be a string or a stages object, got ${typeof n}`),null]}async function H(e){let n=V(e,"package.json"),t;try{t=await U(n,"utf8")}catch{return[new Error(`Cannot find package.json at ${n}`),null]}let r;try{r=JSON.parse(t)}catch{return[new Error(`package.json contains invalid JSON at ${n}`),null]}if(typeof r!="object"||r===null)return[new Error("package.json must be a JSON object"),null];let o=r.nit;if(o===void 0)return[null,{hooks:{}}];if(typeof o!="object"||o===null)return[new Error('"nit" in package.json must be an object'),null];let s=o.hooks;if(s===void 0)return[null,{hooks:{}}];if(typeof s!="object"||s===null||Array.isArray(s))return[new Error("hooks must be an object in nit config"),null];let a=s,c={};for(let[l,u]of Object.entries(a)){let[f,N]=Q(l,u);if(f!==null)return[f,null];c[l]=N}return[null,{hooks:c}]}import{existsSync as X}from"fs";import{stat as Y}from"fs/promises";import{dirname as Z,join as tt,parse as nt}from"path";async function P(e,n){try{await Y(e)}catch{return[new Error(`Path does not exist: ${e}`),null]}let t=e,{root:r}=nt(t);for(;t!==r;){let o=tt(t,".git");if(X(o))return[null,o];if(n!==void 0&&t===n)break;let i=Z(t);if(i===t)break;t=i}return[new Error(`Not a git repository (searched from ${e})`),null]}var et={log:()=>{},error:()=>{}};async function ot(e,n,t,r){let o=e.at(1);if(!o)return r.error("nit: exec requires a hook name"),1;let i=n.hooks[o];if(i===void 0||typeof i=="string")return r.error(`nit: "${o}" is not a staged hook`),1;let[s]=await j(i.stages,t);return s!==null?(r.error(`nit: ${s.message}`),1):0}async function it(e,n,t){let[r,o]=await y(e.hooks,n);return r!==null?(t.error(`nit: ${r.message}`),1):o?(t.log("nit: hooks are up to date"),0):(t.error("nit: hooks are out of sync \u2014 run `nit install` to fix"),1)}async function st(e,n,t){let r=R(e.hooks);if(r!==null)return t.error(`nit: ${r.message}`),1;let[o]=await E(e.hooks,n);if(o!==null)return t.error(`nit: ${o.message}`),1;let i=Object.keys(e.hooks);return t.log(i.length===0?"nit: no hooks configured":`nit: installed ${i.join(", ")}`),0}async function ct(e,n,t=et){let r=e.at(0)??"install";if(!["install","sync","check","exec"].includes(r))return t.error(`nit: unknown command "${r}"`),t.error("Usage: nit [install|sync|check|exec]"),1;let[o,i]=await H(n);if(o!==null)return t.error(`nit: ${o.message}`),1;if(r==="exec")return ot(e,i,n,t);let[s,a]=await P(n);if(s!==null)return r!=="check"?0:(t.error(`nit: ${s.message}`),1);let c=rt(a,"hooks");return r==="check"?it(i,c,t):st(i,c,t)}var at=typeof process<"u"&&/[/\\](?:cli\.[jt]s|nit)$/.test(process.argv[1]??"");if(at){let e=process.argv.slice(2),n=await ct(e,process.cwd(),console);process.exit(n)}export{ct as run};
6
+ ${n}
7
+ `}async function K(r,t,n){let e=E(n,r);if(!L(e))return[null,!1];let[o,i]=await y(w(e,"utf8"));if(o!==null)return[o,null];let s=typeof t=="string"?t.trim():d(r);return[null,i.split(`
8
+ `).some(a=>a.trim()===s)]}async function z(r,t){try{let n=await G(r,{withFileTypes:!0});for(let e of n)if(!(!e.isFile()||t.has(e.name)))try{if((await w(E(r,e.name),"utf8")).startsWith(f))return!0}catch{}}catch{}return!1}async function b(r,t){try{await B(t)}catch{return[new Error(`Hooks directory does not exist: ${t}`),null]}for(let[n,e]of Object.entries(r)){let[o,i]=await K(n,e,t);if(o!==null)return[o,null];if(!i)return[null,!1]}return await z(t,new Set(Object.keys(r)))?[null,!1]:[null,!0]}import{chmod as J,mkdir as U,readdir as W,readFile as M,unlink as x,writeFile as q}from"fs/promises";import{join as $}from"path";async function S(r,t){try{await U(t,{recursive:!0})}catch(e){return[e instanceof Error?e:new Error(`Cannot create ${t}`),null]}let n=[];for(let[e,o]of Object.entries(r)){let i=$(t,e);try{await q(i,k(o,e),"utf8"),n.push(i),await J(i,493)}catch(s){return await Promise.all(n.map(l=>x(l).catch(()=>{}))),[s instanceof Error?s:new Error(`Failed to write hook "${e}"`),null]}}try{let e=await W(t,{withFileTypes:!0}),o=new Set(Object.keys(r));await Promise.all(e.filter(i=>i.isFile()&&!o.has(i.name)).map(async i=>{let s=$(t,i.name);try{(await M(s,"utf8")).startsWith(f)&&await x(s)}catch{}}))}catch{}return[null,!0]}import{spawn as R}from"child_process";import{delimiter as V,join as Q}from"path";var j="{staged_files}";function H(r){return r.replaceAll(/[.+^$|()[\]\\]/g,String.raw`\$&`)}function X(r){let t="",n=0;for(;n<r.length;){let e=r.charAt(n);if(e==="*"&&r.charAt(n+1)==="*")t+=".*",n+=2,r.charAt(n)==="/"&&n++;else if(e==="*")t+="[^/]*",n++;else if(e==="?")t+="[^/]",n++;else if(e==="{"){let o=r.indexOf("}",n);if(o<0)t+=String.raw`\{`,n++;else{let i=r.slice(n+1,o).split(",").map(H).join("|");t+=`(?:${i})`,n=o+1}}else t+=H(e),n++}return new RegExp(`^${t}$`)}function Y(r){return process.platform==="win32"?`"${r.replaceAll("^","^^").replaceAll("&","^&").replaceAll("|","^|").replaceAll("<","^<").replaceAll(">","^>").replaceAll("(","^(").replaceAll(")","^)").replaceAll("%","%%").replaceAll("!","^!")}"`:`'${r.replaceAll("'",`'"'"'`)}'`}function Z(r,t){if(!r.includes(j))return r;let n=t.map(Y).join(" ");return r.replaceAll(j,n)}function h(r){return Q(r,"node_modules",".bin")}function rr(r,t,n){return new Promise(e=>{let o=Z(r,t);if(o.trim().length===0)return e(new Error(`Empty command in stage: "${r}"`));let i=process.platform==="win32"?Object.keys(process.env).find(l=>l.toUpperCase()==="PATH")??"Path":"PATH",s=R(o,{cwd:n,env:{...process.env,[i]:`${h(n)}${V}${process.env[i]??""}`},shell:!0,stdio:"inherit"});s.on("close",l=>e(l===0?null:new Error(`Command "${r}" exited with code ${l}`))),s.on("error",e)})}function tr(r){return new Promise(t=>{let n=R("git",["diff","--cached","--name-only","-z","--diff-filter=ACMR"],{cwd:r,stdio:["ignore","pipe","ignore"]}),e=[];n.stdout.on("data",o=>{e.push(o)}),n.on("close",()=>t([null,Buffer.concat(e).toString("utf8").split("\0").filter(Boolean)])),n.on("error",o=>t([o,null]))})}async function A(r,t){let[n,e]=await tr(t);if(n!==null)return[n,null];if(e.length===0)return[null,!0];let o=e.map(i=>{let s=i.replaceAll("\\","/");return{file:s,basename:s.split("/").at(-1)??s}});for(let[i,s]of Object.entries(r)){let l=X(i),a=o.filter(({file:u,basename:m})=>l.test(u)||l.test(m)).map(({file:u})=>u);if(a.length===0)continue;let c=await rr(s,a,t);if(c!==null)return[c,null]}return[null,!0]}import{existsSync as nr}from"fs";import{delimiter as er,join as or}from"path";function ir(r,t){let n=process.platform==="win32"?[".exe",".cmd",".bat",""]:[""];return t.some(e=>n.some(o=>nr(or(e,r+o))))}function P(r,t,n){let e=t.trim().split(/\s+/).at(0);return e===void 0||e===""?new Error(`Hook "${r}" has an empty command`):ir(e,n)?null:new Error(`Hook "${r}": command "${e}" not found in PATH`)}function D(r,t=process.cwd()){let n=(process.env.PATH??"").split(er),e=[h(t),...n];for(let[o,i]of Object.entries(r))if(typeof i=="string"){let s=P(o,i,n);if(s!==null)return s}else for(let[s,l]of Object.entries(i.stages)){let a=P(`${o}[${s}]`,l,e);if(a!==null)return a}return null}import{readFile as sr}from"fs/promises";import{join as lr}from"path";function ar(r,t){if(typeof t=="string")return[null,t];if(typeof t=="object"&&t!==null&&!Array.isArray(t)&&Object.hasOwn(t,"stages")){let n=t.stages;if(typeof n!="object"||n===null||Array.isArray(n))return[new Error(`Hook "${r}" has invalid stages: stages must be a plain object`),null];let e=n;for(let[o,i]of Object.entries(e))if(typeof i!="string")return[new Error(`Hook "${r}" stage "${o}" must have a string command, got ${typeof i}`),null];return[null,{stages:e}]}return[new Error(`Hook "${r}" must be a string or a stages object, got ${typeof t}`),null]}async function C(r){let t=lr(r,"package.json"),n;try{n=await sr(t,"utf8")}catch{return[new Error(`Cannot find package.json at ${t}`),null]}let e;try{e=JSON.parse(n)}catch{return[new Error(`package.json contains invalid JSON at ${t}`),null]}if(typeof e!="object"||e===null)return[new Error("package.json must be a JSON object"),null];let o=e.nit;if(o===void 0)return[null,{hooks:{}}];if(typeof o!="object"||o===null)return[new Error('"nit" in package.json must be an object'),null];let s=o.hooks;if(s===void 0)return[null,{hooks:{}}];if(typeof s!="object"||s===null||Array.isArray(s))return[new Error("hooks must be an object in nit config"),null];let l=s,a={};for(let[c,u]of Object.entries(l)){let[m,_]=ar(c,u);if(m!==null)return[m,null];a[c]=_}return[null,{hooks:a}]}import{existsSync as cr}from"fs";import{readFile as T,stat as F}from"fs/promises";import{dirname as p,isAbsolute as ur,join as g,normalize as fr,parse as mr,resolve as N}from"path";function O(r){return[new Error(`Invalid gitdir file: ${r}`),null]}async function gr(r){let t=await F(r);if(t.isDirectory())return[null,r];if(!t.isFile())return[new Error(`Unsupported .git entry: ${r}`),null];let e=(await T(r,"utf8")).split(/\r?\n/u).find(s=>s.startsWith("gitdir:"));if(e===void 0)return O(r);let o=e.slice(7).trim();return o.length===0?O(r):[null,ur(o)?fr(o):N(p(r),o)]}async function v(r,t){let n=await pr(r);if(n instanceof Error)return[n,null];let e=n.isDirectory()?r:p(r),{root:o}=mr(e);for(;!dr(e,o,t);){let i=await hr(e);if(i)return i;e=p(e)}return[new Error(`Not a git repository (searched from ${r})`),null]}async function pr(r){try{return await F(r)}catch{return new Error(`Path does not exist: ${r}`)}}function dr(r,t,n){return r===t||n!==void 0&&r===n||p(r)===r}async function hr(r){let t=g(r,".git");if(!cr(t))return null;try{return await gr(t)}catch(n){return[n instanceof Error?n:new Error(`Failed to resolve git directory from ${t}`),null]}}async function I(r){try{let t=await T(g(r,"commondir"),"utf8");return g(N(r,t.trim()),"hooks")}catch{return g(r,"hooks")}}var yr={log:()=>{},error:()=>{}};async function kr(r,t,n,e){let o=r.at(1);if(!o)return e.error("nit: exec requires a hook name"),1;let i=t.hooks[o];if(i===void 0||typeof i=="string")return e.error(`nit: "${o}" is not a staged hook`),1;let[s]=await A(i.stages,n);return s!==null?(e.error(`nit: ${s.message}`),1):0}async function wr(r,t,n){let[e,o]=await b(r.hooks,t);return e!==null?(n.error(`nit: ${e.message}`),1):o?(n.log("nit: hooks are up to date"),0):(n.error("nit: hooks are out of sync \u2014 run `nit install` to fix"),1)}async function Er(r,t,n,e){let o=D(r.hooks,n);if(o!==null)return e.error(`nit: ${o.message}`),1;let[i]=await S(r.hooks,t);if(i!==null)return e.error(`nit: ${i.message}`),1;let s=Object.keys(r.hooks);return e.log(s.length===0?"nit: no hooks configured":`nit: installed ${s.join(", ")}`),0}async function br(r,t,n=yr){let e=r.at(0)??"install";if(!["install","sync","check","exec"].includes(e))return n.error(`nit: unknown command "${e}"`),n.error("Usage: nit [install|sync|check|exec]"),1;let[o,i]=await C(t);if(o!==null)return n.error(`nit: ${o.message}`),1;if(e==="exec")return kr(r,i,t,n);let[s,l]=await v(t);if(s!==null)return e!=="check"?0:(n.error(`nit: ${s.message}`),1);let a=await I(l);return e==="check"?wr(i,a,n):Er(i,a,t,n)}var xr=typeof process<"u"&&/[/\\](?:cli\.[jt]s|nit)$/.test(process.argv[1]??"");if(xr){let r=process.argv.slice(2),t=await br(r,process.cwd(),console);process.exit(t)}export{br as run};
package/package.json CHANGED
@@ -1,8 +1,18 @@
1
1
  {
2
2
  "name": "@pyyupsk/nit",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Lightweight zero-dependency Git hooks manager for JavaScript/Node.js projects",
5
5
  "license": "MIT",
6
+ "author": "Pongsakorn Thipayanate <pthipayanate@gmail.com>",
7
+ "homepage": "https://github.com/pyyupsk/nit#readme",
8
+ "bugs": {
9
+ "url": "https://github.com/pyyupsk/nit/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/pyyupsk/nit.git",
14
+ "directory": "packages/nit"
15
+ },
6
16
  "packageManager": "bun@1.3.11",
7
17
  "type": "module",
8
18
  "main": "dist/cli.js",
@@ -15,34 +25,29 @@
15
25
  "files": [
16
26
  "dist"
17
27
  ],
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "provenance": true
31
+ },
18
32
  "scripts": {
19
33
  "build": "tsup",
20
34
  "check": "biome check .",
35
+ "check:fix": "biome check --write .",
21
36
  "dev": "tsup --watch",
22
37
  "format": "biome format --write .",
23
38
  "knip": "knip",
24
- "prepare": "bun run build && ln -sf ../../dist/cli.js node_modules/.bin/nit && bun run ./src/cli.ts install",
39
+ "prepare": "bun run ./scripts/prepare.ts",
25
40
  "test": "vitest run",
26
41
  "test:watch": "vitest",
27
42
  "typecheck": "tsc --noEmit"
28
43
  },
29
44
  "devDependencies": {
30
- "@biomejs/biome": "^2.4.11",
31
45
  "@types/bun": "^1.3.12",
46
+ "@types/node": "^25.6.0",
32
47
  "@vitest/coverage-v8": "^4.1.4",
33
48
  "knip": "^6.4.0",
34
49
  "tsup": "^8.5.1",
35
50
  "typescript": "^6.0.2",
36
51
  "vitest": "^4.1.4"
37
- },
38
- "nit": {
39
- "hooks": {
40
- "pre-commit": {
41
- "stages": {
42
- "**/*.{ts,json}": "biome check --write {staged_files}"
43
- }
44
- },
45
- "pre-push": "bun typecheck && bun test && bun run build"
46
- }
47
52
  }
48
53
  }