ccommand 1.0.37 → 1.0.38

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.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var ge=require("module"),X=require("fs"),Q=require("path"),p=require("lazy-js-utils"),Z=require("fast-glob"),ee=require("@simon_he/colorize"),re=require("os"),te=require("tty");function _(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var q=_(X),V=_(Q),ne=_(Z),a=_(ee),se=_(re),oe=_(te),ue=require;const i="\x1B[",v="\x1B]",b="\x07",j=";",G=process.env.TERM_PROGRAM==="Apple_Terminal",u={};u.cursorTo=(e,t)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");return typeof t!="number"?i+(e+1)+"G":i+(t+1)+";"+(e+1)+"H"},u.cursorMove=(e,t)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");let r="";return e<0?r+=i+-e+"D":e>0&&(r+=i+e+"C"),t<0?r+=i+-t+"A":t>0&&(r+=i+t+"B"),r},u.cursorUp=(e=1)=>i+e+"A",u.cursorDown=(e=1)=>i+e+"B",u.cursorForward=(e=1)=>i+e+"C",u.cursorBackward=(e=1)=>i+e+"D",u.cursorLeft=i+"G",u.cursorSavePosition=G?"\x1B7":i+"s",u.cursorRestorePosition=G?"\x1B8":i+"u",u.cursorGetPosition=i+"6n",u.cursorNextLine=i+"E",u.cursorPrevLine=i+"F",u.cursorHide=i+"?25l",u.cursorShow=i+"?25h",u.eraseLines=e=>{let t="";for(let r=0;r<e;r++)t+=u.eraseLine+(r<e-1?u.cursorUp():"");return e&&(t+=u.cursorLeft),t},u.eraseEndLine=i+"K",u.eraseStartLine=i+"1K",u.eraseLine=i+"2K",u.eraseDown=i+"J",u.eraseUp=i+"1J",u.eraseScreen=i+"2J",u.scrollUp=i+"S",u.scrollDown=i+"T",u.clearScreen="\x1Bc",u.clearTerminal=process.platform==="win32"?`${u.eraseScreen}${i}0f`:`${u.eraseScreen}${i}3J${i}H`,u.beep=b,u.link=(e,t)=>[v,"8",j,j,t,b,e,v,"8",j,j,b].join(""),u.image=(e,t={})=>{let r=`${v}1337;File=inline=1`;return t.width&&(r+=`;width=${t.width}`),t.height&&(r+=`;height=${t.height}`),t.preserveAspectRatio===!1&&(r+=";preserveAspectRatio=0"),r+":"+e.toString("base64")+b},u.iTerm={setCwd:(e=process.cwd())=>`${v}50;CurrentDir=${e}${b}`,annotation:(e,t={})=>{let r=`${v}1337;`;const n=typeof t.x<"u",c=typeof t.y<"u";if((n||c)&&!(n&&c&&typeof t.length<"u"))throw new Error("`x`, `y` and `length` must be defined when `x` or `y` is defined");return e=e.replace(/\|/g,""),r+=t.isHidden?"AddHiddenAnnotation=":"AddAnnotation=",t.length>0?r+=(n?[e,t.length,t.x,t.y]:[t.length,e]).join("|"):r+=e,r+b}};var H=(e,t=process.argv)=>{const r=e.startsWith("-")?"":e.length===1?"-":"--",n=t.indexOf(r+e),c=t.indexOf("--");return n!==-1&&(c===-1||n<c)};const ae=se.default,Y=oe.default,y=H,{env:f}=process;let R;y("no-color")||y("no-colors")||y("color=false")||y("color=never")?R=0:(y("color")||y("colors")||y("color=true")||y("color=always"))&&(R=1),"FORCE_COLOR"in f&&(f.FORCE_COLOR==="true"?R=1:f.FORCE_COLOR==="false"?R=0:R=f.FORCE_COLOR.length===0?1:Math.min(parseInt(f.FORCE_COLOR,10),3));function x(e){return e===0?!1:{level:e,hasBasic:!0,has256:e>=2,has16m:e>=3}}function L(e,t){if(R===0)return 0;if(y("color=16m")||y("color=full")||y("color=truecolor"))return 3;if(y("color=256"))return 2;if(e&&!t&&R===void 0)return 0;const r=R||0;if(f.TERM==="dumb")return r;if(process.platform==="win32"){const n=ae.release().split(".");return Number(n[0])>=10&&Number(n[2])>=10586?Number(n[2])>=14931?3:2:1}if("CI"in f)return["TRAVIS","CIRCLECI","APPVEYOR","GITLAB_CI","GITHUB_ACTIONS","BUILDKITE"].some(n=>n in f)||f.CI_NAME==="codeship"?1:r;if("TEAMCITY_VERSION"in f)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(f.TEAMCITY_VERSION)?1:0;if(f.COLORTERM==="truecolor")return 3;if("TERM_PROGRAM"in f){const n=parseInt((f.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(f.TERM_PROGRAM){case"iTerm.app":return n>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(f.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(f.TERM)||"COLORTERM"in f?1:r}function ie(e){const t=L(e,e&&e.isTTY);return x(t)}var ce={supportsColor:ie,stdout:x(L(!0,Y.isatty(1))),stderr:x(L(!0,Y.isatty(2)))};const le=ce,C=H;function z(e){if(/^\d{3,4}$/.test(e)){const r=/(\d{1,2})(\d{2})/.exec(e);return{major:0,minor:parseInt(r[1],10),patch:parseInt(r[2],10)}}const t=(e||"").split(".").map(r=>parseInt(r,10));return{major:t[0],minor:t[1],patch:t[2]}}function A(e){const{env:t}=process;if("FORCE_HYPERLINK"in t)return!(t.FORCE_HYPERLINK.length>0&&parseInt(t.FORCE_HYPERLINK,10)===0);if(C("no-hyperlink")||C("no-hyperlinks")||C("hyperlink=false")||C("hyperlink=never"))return!1;if(C("hyperlink=true")||C("hyperlink=always")||"NETLIFY"in t)return!0;if(!le.supportsColor(e)||e&&!e.isTTY||process.platform==="win32"||"CI"in t||"TEAMCITY_VERSION"in t)return!1;if("TERM_PROGRAM"in t){const r=z(t.TERM_PROGRAM_VERSION);switch(t.TERM_PROGRAM){case"iTerm.app":return r.major===3?r.minor>=1:r.major>3;case"WezTerm":return r.major>=20200620;case"vscode":return r.major>1||r.major===1&&r.minor>=72}}if("VTE_VERSION"in t){if(t.VTE_VERSION==="0.50.0")return!1;const r=z(t.VTE_VERSION);return r.major>0||r.minor>=50}return!1}var M={supportsHyperlink:A,stdout:A(process.stdout),stderr:A(process.stderr)};function k(e,t,{target:r="stdout",...n}={}){return M[r]?u.link(e,t):n.fallback===!1?e:typeof n.fallback=="function"?n.fallback(e,t):`${e} (\u200B${t}\u200B)`}k.isSupported=M.stdout,k.stderr=(e,t,r={})=>k(e,t,{target:"stderr",...r}),k.stderr.isSupported=M.stderr;var fe="1.0.37";const de=ue("yamljs");let T=[],E=null;const l=console.log;async function U(){const e="__ccommand__split",{status:t}=p.jsShell("gum -v","pipe");if(t!==0){l(a.default({color:"blue",text:"install gum..."}));const{status:s}=p.jsShell("brew install gum");if(s!==0){const{status:o}=p.jsShell(`sudo mkdir -p /etc/apt/keyrings
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var $e=require("module"),X=require("fs/promises"),Q=require("path"),d=require("lazy-js-utils"),Z=require("fast-glob"),ee=require("@simon_he/colorize"),re=require("os"),te=require("tty");function k(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var B=k(X),q=k(Q),ne=k(Z),a=k(ee),se=k(re),oe=k(te),ue=require;const i="\x1B[",C="\x1B]",_="\x07",v=";",V=process.env.TERM_PROGRAM==="Apple_Terminal",u={};u.cursorTo=(e,r)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");return typeof r!="number"?i+(e+1)+"G":i+(r+1)+";"+(e+1)+"H"},u.cursorMove=(e,r)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");let t="";return e<0?t+=i+-e+"D":e>0&&(t+=i+e+"C"),r<0?t+=i+-r+"A":r>0&&(t+=i+r+"B"),t},u.cursorUp=(e=1)=>i+e+"A",u.cursorDown=(e=1)=>i+e+"B",u.cursorForward=(e=1)=>i+e+"C",u.cursorBackward=(e=1)=>i+e+"D",u.cursorLeft=i+"G",u.cursorSavePosition=V?"\x1B7":i+"s",u.cursorRestorePosition=V?"\x1B8":i+"u",u.cursorGetPosition=i+"6n",u.cursorNextLine=i+"E",u.cursorPrevLine=i+"F",u.cursorHide=i+"?25l",u.cursorShow=i+"?25h",u.eraseLines=e=>{let r="";for(let t=0;t<e;t++)r+=u.eraseLine+(t<e-1?u.cursorUp():"");return e&&(r+=u.cursorLeft),r},u.eraseEndLine=i+"K",u.eraseStartLine=i+"1K",u.eraseLine=i+"2K",u.eraseDown=i+"J",u.eraseUp=i+"1J",u.eraseScreen=i+"2J",u.scrollUp=i+"S",u.scrollDown=i+"T",u.clearScreen="\x1Bc",u.clearTerminal=process.platform==="win32"?`${u.eraseScreen}${i}0f`:`${u.eraseScreen}${i}3J${i}H`,u.beep=_,u.link=(e,r)=>[C,"8",v,v,r,_,e,C,"8",v,v,_].join(""),u.image=(e,r={})=>{let t=`${C}1337;File=inline=1`;return r.width&&(t+=`;width=${r.width}`),r.height&&(t+=`;height=${r.height}`),r.preserveAspectRatio===!1&&(t+=";preserveAspectRatio=0"),t+":"+e.toString("base64")+_},u.iTerm={setCwd:(e=process.cwd())=>`${C}50;CurrentDir=${e}${_}`,annotation:(e,r={})=>{let t=`${C}1337;`;const n=typeof r.x<"u",l=typeof r.y<"u";if((n||l)&&!(n&&l&&typeof r.length<"u"))throw new Error("`x`, `y` and `length` must be defined when `x` or `y` is defined");return e=e.replace(/\|/g,""),t+=r.isHidden?"AddHiddenAnnotation=":"AddAnnotation=",r.length>0?t+=(n?[e,r.length,r.x,r.y]:[r.length,e]).join("|"):t+=e,t+_}};var G=(e,r=process.argv)=>{const t=e.startsWith("-")?"":e.length===1?"-":"--",n=r.indexOf(t+e),l=r.indexOf("--");return n!==-1&&(l===-1||n<l)};const ae=se.default,H=oe.default,$=G,{env:f}=process;let w;$("no-color")||$("no-colors")||$("color=false")||$("color=never")?w=0:($("color")||$("colors")||$("color=true")||$("color=always"))&&(w=1),"FORCE_COLOR"in f&&(f.FORCE_COLOR==="true"?w=1:f.FORCE_COLOR==="false"?w=0:w=f.FORCE_COLOR.length===0?1:Math.min(parseInt(f.FORCE_COLOR,10),3));function F(e){return e===0?!1:{level:e,hasBasic:!0,has256:e>=2,has16m:e>=3}}function I(e,r){if(w===0)return 0;if($("color=16m")||$("color=full")||$("color=truecolor"))return 3;if($("color=256"))return 2;if(e&&!r&&w===void 0)return 0;const t=w||0;if(f.TERM==="dumb")return t;if(process.platform==="win32"){const n=ae.release().split(".");return Number(n[0])>=10&&Number(n[2])>=10586?Number(n[2])>=14931?3:2:1}if("CI"in f)return["TRAVIS","CIRCLECI","APPVEYOR","GITLAB_CI","GITHUB_ACTIONS","BUILDKITE"].some(n=>n in f)||f.CI_NAME==="codeship"?1:t;if("TEAMCITY_VERSION"in f)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(f.TEAMCITY_VERSION)?1:0;if(f.COLORTERM==="truecolor")return 3;if("TERM_PROGRAM"in f){const n=parseInt((f.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(f.TERM_PROGRAM){case"iTerm.app":return n>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(f.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(f.TERM)||"COLORTERM"in f?1:t}function ie(e){const r=I(e,e&&e.isTTY);return F(r)}var le={supportsColor:ie,stdout:F(I(!0,H.isatty(1))),stderr:F(I(!0,H.isatty(2)))};const ce=le,b=G;function Y(e){if(/^\d{3,4}$/.test(e)){const t=/(\d{1,2})(\d{2})/.exec(e);return{major:0,minor:parseInt(t[1],10),patch:parseInt(t[2],10)}}const r=(e||"").split(".").map(t=>parseInt(t,10));return{major:r[0],minor:r[1],patch:r[2]}}function x(e){const{env:r}=process;if("FORCE_HYPERLINK"in r)return!(r.FORCE_HYPERLINK.length>0&&parseInt(r.FORCE_HYPERLINK,10)===0);if(b("no-hyperlink")||b("no-hyperlinks")||b("hyperlink=false")||b("hyperlink=never"))return!1;if(b("hyperlink=true")||b("hyperlink=always")||"NETLIFY"in r)return!0;if(!ce.supportsColor(e)||e&&!e.isTTY||process.platform==="win32"||"CI"in r||"TEAMCITY_VERSION"in r)return!1;if("TERM_PROGRAM"in r){const t=Y(r.TERM_PROGRAM_VERSION);switch(r.TERM_PROGRAM){case"iTerm.app":return t.major===3?t.minor>=1:t.major>3;case"WezTerm":return t.major>=20200620;case"vscode":return t.major>1||t.major===1&&t.minor>=72}}if("VTE_VERSION"in r){if(r.VTE_VERSION==="0.50.0")return!1;const t=Y(r.VTE_VERSION);return t.major>0||t.minor>=50}return!1}var L={supportsHyperlink:x,stdout:x(process.stdout),stderr:x(process.stderr)};function R(e,r,{target:t="stdout",...n}={}){return L[t]?u.link(e,r):n.fallback===!1?e:typeof n.fallback=="function"?n.fallback(e,r):`${e} (\u200B${r}\u200B)`}R.isSupported=L.stdout,R.stderr=(e,r,t={})=>R(e,r,{target:"stderr",...t}),R.stderr.isSupported=L.stderr;var fe="1.0.38";const A=console.log;function de(){const{status:e}=d.jsShell("gum -v","pipe");if(e!==0){A(a.default({color:"blue",text:"install gum..."}));const{status:r}=d.jsShell("brew install gum");if(r!==0){const{status:t}=d.jsShell(`sudo mkdir -p /etc/apt/keyrings
2
2
  curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg
3
3
  echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | sudo tee /etc/apt/sources.list.d/charm.list
4
- sudo apt update && sudo apt install gum`);if(o!==0){const h=k("the official website of gum","https://github.com/charmbracelet/gum#installation");return l(a.default({color:"red",text:`gum install error, you can install it yourself through ${a.default({color:"yellow",text:h,bold:!0})}`}))}}l(a.default({color:"green",text:"gum install successfully \u{1F389}"}))}const r=process.argv.slice(2);if(r[0]==="-v"||r[0]==="--version")return l(a.default({text:`ccommand Version: ${fe}`,color:"green"}));if(r[0]==="-h"||r[0]==="--help"){const s=k("open an issue","https://github.com/Simon-He95/ccommand/issues"),o=k("\u2728star it","https://github.com/Simon-He95/ccommand");return l(a.default({color:"white",text:`
4
+ sudo apt update && sudo apt install gum`);if(t!==0){const n=R("the official website of gum","https://github.com/charmbracelet/gum#installation");return A(a.default({color:"red",text:`gum install error, you can install it yourself through ${a.default({color:"yellow",text:n,bold:!0})}`}))}}A(a.default({color:"green",text:"gum install successfully \u{1F389}"}))}}const pe=ue("yamljs");let j=[],y=null;const p=console.log,z="__ccommand__split";async function U(){de();const e=process.argv.slice(2);if(e[0]==="-v"||e[0]==="--version")return p(a.default({text:`ccommand Version: ${fe}`,color:"green"}));if(e[0]==="-h"||e[0]==="--help"){const s=R("open an issue","https://github.com/Simon-He95/ccommand/issues"),o=R("\u2728star it","https://github.com/Simon-He95/ccommand");return p(a.default({color:"white",text:`
5
5
  ${a.default({bold:!0,text:"Common Commands:",bgColor:"blue"})}
6
6
  ${a.default({text:`- ccommand -v \u67E5\u770B\u5F53\u524D\u7248\u672C
7
7
  - ccommand -help \u67E5\u770B\u5E2E\u52A9
@@ -9,9 +9,9 @@
9
9
  - ccommand find \u67E5\u627E\u5F53\u524Dworkspace\u7684\u6240\u6709\u76EE\u5F55
10
10
  `,color:"cyan"})}
11
11
  If you encounter any problems, you can ${a.default({color:"magenta",text:s})}.
12
- If you like it, please ${a.default({text:o,bold:!0,color:"cyan"})} `}))}const n=p.getPkgTool(),[c,g,w]=pe(r);let d=c,O;if(r[0]==="find"){if(g)await S(n),d=T.filter(s=>s.includes(g))[0];else if(n==="yarn"){await S(n);const{result:s}=p.jsShell(`echo ${T.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(d=s,!d)return l(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else if(n==="pnpm"){await S(n);const{result:s}=p.jsShell(`echo ${T.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(d=s.trim(),!d)return l(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else return l(a.default({color:"red",text:"find command only support yarn or pnpm"}));O=await D()}else{if(O=await D(),c&&E&&!E[c]||!E)try{const s=(await p.getPkg("./package.json")||{})?.scripts;if(s&&s[r[0]])return l(a.default({text:`ccommand is executing ${a.default({color:"cyan",text:`'${r[0]}'`})} \u{1F914} `,color:"yellow"})),N(r[0],r.slice(1).join(" "));if(s&&c){const o=he(s,r[0]),h=r.slice(1).join(" ");return N(o,h)}}catch{}E&&!E[r[0]]&&(l(a.default({color:"red",text:`"${r[0]}" is not found in workspace, current directory
13
- or current scripts, please check`})),process.exit())}if(!O)return l(a.default({color:"red",text:"No scripts found"}));const F=[];let $="";if(!g||r[0]==="find"&&(!r[2]||r[2].startsWith("--"))){const s=Object.keys(O).reduce((h,m)=>{const I=O[m];return F.push(m),h+=`"${m}: ${I.replace(/\"/g,"'")}"${e}`,h},""),{result:o}=p.jsShell(`echo ${s} | sed "s/${e}/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u6307\u4EE4"`,"pipe");$=o.substring(0,o.indexOf(": "))}if(!g&&!$)return l(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"})),process.exit();const{status:W}=await p.jsShell(K());if(W===0)return l(a.default({color:"green",text:`
14
- command '${(r[0]==="find"?r[2]:r[1])||$}' run successfully \u{1F389}`})),process.exit();l(a.default({color:"red",text:`
15
- command '${$}' run error \u274C`}));function P(s){return F.find(o=>o===s)??F.find(o=>s.startsWith(o))}function K(){let s="",o="";const h=n!=="yarn";n==="npm"?(o=w?` -- ${w}`:"",s=d?` --prefix ${d} `:" "):n==="pnpm"?(o=w?` ${w}`:"",s=d?` --filter ${d} `:" "):n==="yarn"?(o=w?` ${w}`:"",s=d?` workspace ${d} `:" "):n==="bun"&&(o=w?` ${w}`:"",s="");let m="";if(o&&!o.startsWith(" --")){const B=o.split(" ").filter(Boolean);m=B[0],o=B.slice(1).join(" ")}const I=`${n}${h?" run":" "}${s} ${m||($?P($)||$:g)} ${J(o)?`-- ${o}`:o}`;return $=`${m||($?P($):g)}`,r[0]==="find"?l(a.default({text:`tips: pfind ${d} ${$} ${o}`.replace(/\s+/g," "),color:"blue",bold:!0})):l(a.default({text:`tips: prun ${$} ${o}`.replace(/\s+/g," "),color:"blue",bold:!0})),I}async function D(){try{if(!d||n==="bun"||n==="npm")return(await p.getPkg("./package.json"))?.scripts;if(n==="pnpm")return(await S(n))[d]||(await p.getPkg(`${d}/package.json`))?.scripts;if(n==="yarn")return(await S(n))[d]||(await p.getPkg(`${d}/package.json`))?.scripts}catch{}}function J(s){return r[0]==="find",r[1]&&s}async function N(s,o){let h;switch(s&&r[0]!==s&&l(a.default({text:`\u{1F914} ${a.default({text:`'${r[0]}'`,color:"cyan"})} automatically match for you to ${a.default({text:`'${s}${o?` ${o}`:""}'`,color:"cyan"})} `,color:"yellow"})),n){case"npm":{const{status:m}=p.jsShell(`npm run ${s}${o?` -- ${o}`:""}`);h=m;break}case"pnpm":{const{status:m}=p.jsShell(`pnpm run ${s}${o?` ${o}`:""}`);h=m;break}case"yarn":{const{status:m}=p.jsShell(`yarn ${s}${o?` ${o}`:""}`);h=m;break}case"bun":{const{status:m}=p.jsShell(`bun ${s} ${o}`);h=m;break}}return l(h===0?a.default({color:"green",text:`
12
+ If you like it, please ${a.default({text:o,bold:!0,color:"cyan"})} `}))}const r=d.getPkgTool(),[t,n,l]=me(e);let c=t,E;if(e[0]==="find"){if(n)await O(r),c=j.filter(s=>s.includes(n))[0];else if(r==="yarn"){await O(r);const{result:s}=d.jsShell(`echo ${j.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(c=s,!c)return p(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else if(r==="pnpm"){await O(r);const{result:s}=d.jsShell(`echo ${j.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(c=s.trim(),!c)return p(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else return p(a.default({color:"red",text:"find command only support yarn or pnpm"}));E=await P()}else{if(E=await P(),t&&y&&!y[t]||!y)try{const s=(await d.getPkg("./package.json")||{})?.scripts;if(s&&s[e[0]])return p(a.default({text:`ccommand is executing ${a.default({color:"cyan",text:`'${e[0]}'`})} \u{1F914} `,color:"yellow"})),D(e[0],e.slice(1).join(" "));if(s&&t){const o=ge(s,e[0]),g=e.slice(1).join(" ");return D(o,g)}}catch{}y&&!y[e[0]]&&(p(a.default({color:"red",text:`"${e[0]}" is not found in workspace, current directory
13
+ or current scripts, please check`})),process.exit())}if(!E)return p(a.default({color:"red",text:"No scripts found"}));const T=[];let h="";if(!n||e[0]==="find"&&(!e[2]||e[2].startsWith("--"))){const s=Object.keys(E).reduce((g,m)=>{const S=E[m];return T.push(m),g+=`"${m}: ${S.replace(/\"/g,"'")}"${z}`,g},""),{result:o}=d.jsShell(`echo ${s} | sed "s/${z}/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u6307\u4EE4"`,"pipe");h=o.substring(0,o.indexOf(": "))}if(!n&&!h)return p(a.default({color:"yellow",text:"\u5DF2\u53D6\u6D88"})),process.exit();const{status:W}=await d.jsShell(K());if(W===0)return p(a.default({color:"green",text:`
14
+ command '${(e[0]==="find"?e[2]:e[1])||h}' run successfully \u{1F389}`})),process.exit();p(a.default({color:"red",text:`
15
+ command '${h}' run error \u274C`}));function M(s){return T.find(o=>o===s)??T.find(o=>s.startsWith(o))}function K(){let s="",o="";const g=r!=="yarn";r==="npm"?(o=l?` -- ${l}`:"",s=c?` --prefix ${c} `:" "):r==="pnpm"?(o=l?` ${l}`:"",s=c?` --filter ${c} `:" "):r==="yarn"?(o=l?` ${l}`:"",s=c?` workspace ${c} `:" "):r==="bun"&&(o=l?` ${l}`:"",s="");let m="";if(o&&!o.startsWith(" --")){const N=o.split(" ").filter(Boolean);m=N[0],o=N.slice(1).join(" ")}const S=`${r}${g?" run":" "}${s} ${m||(h?M(h)||h:n)} ${J(o)?`-- ${o}`:o}`;return h=`${m||(h?M(h):n)}`,e[0]==="find"?p(a.default({text:`tips: pfind ${c} ${h} ${o}`.replace(/\s+/g," "),color:"blue",bold:!0})):p(a.default({text:`tips: prun ${h} ${o}`.replace(/\s+/g," "),color:"blue",bold:!0})),S}async function P(){try{if(!c||r==="bun"||r==="npm")return(await d.getPkg("./package.json"))?.scripts;if(r==="pnpm")return(await O(r))[c]||(await d.getPkg(`${c}/package.json`))?.scripts;if(r==="yarn")return(await O(r))[c]||(await d.getPkg(`${c}/package.json`))?.scripts}catch{}}function J(s){return e[0]==="find",e[1]&&s}async function D(s,o){let g;switch(s&&e[0]!==s&&p(a.default({text:`\u{1F914} ${a.default({text:`'${e[0]}'`,color:"cyan"})} automatically match for you to ${a.default({text:`'${s}${o?` ${o}`:""}'`,color:"cyan"})} `,color:"yellow"})),r){case"npm":{const{status:m}=d.jsShell(`npm run ${s}${o?` -- ${o}`:""}`);g=m;break}case"pnpm":{const{status:m}=d.jsShell(`pnpm run ${s}${o?` ${o}`:""}`);g=m;break}case"yarn":{const{status:m}=d.jsShell(`yarn ${s}${o?` ${o}`:""}`);g=m;break}case"bun":{const{status:m}=d.jsShell(`bun ${s} ${o}`);g=m;break}}return p(g===0?a.default({color:"green",text:`
16
16
  command '${s}${o?` ${o}`:""}' run successfully \u{1F389}`}):a.default({color:"red",text:`
17
- command ${a.default({bold:!0,color:"cyan",text:`'${s||r[0]}${o?` ${o}`:""}'`})} run error \u274C`}))}}async function S(e){if(E)return E;const t=e==="pnpm"?await q.default.readFileSync(V.default.resolve(process.cwd(),"pnpm-workspace.yaml"),"utf-8"):await q.default.readFileSync(V.default.resolve(process.cwd(),"package.json"),"utf-8"),r=e==="pnpm"?de.parse(t)?.packages||[]:JSON.parse(t)?.workspaces||[];return E=await me(r)||{},T=Object.keys(E).filter(n=>E[n]&&Object.keys(E[n]).length),E}function pe(e){const t=e[0];return t?t.startsWith("--")?["","",e.join(" ")]:e[1]&&e[1].startsWith("--")?[t,"",e.slice(1).join(" ")]:[t,e[1],e.slice(2).join(" ")]:["","",""]}async function me(e){if(!e.length)return;const t=await ne.default(e.map(r=>`${r}/package.json`),{dot:!0,ignore:["**/node_modules/**"]});return Promise.all(t.map(async r=>{const n=await p.getPkg(r);if(!n)return;const{name:c,scripts:g}=n;return{name:c,scripts:g}})).then(r=>r.reduce((n,c)=>{const{name:g,scripts:w}=c;return!g||!w||(n[g]=w),n},{}))}function he(e,t){const r=Object.keys(e),n=r.find(c=>c.startsWith(t));if(n)return n;try{const c=new RegExp(t.split("").join("[_-\\w$.:]*"));return r.find(g=>c.test(g))}catch(c){l(a.default({text:`RegExp error: ${c}`,color:"red"})),process.exit(1)}}U(),exports.ccommand=U;
17
+ command ${a.default({bold:!0,color:"cyan",text:`'${s||e[0]}${o?` ${o}`:""}'`})} run error \u274C`}))}}async function O(e){if(y)return y;const r=e==="pnpm"?await B.default.readFile(q.default.resolve(process.cwd(),"pnpm-workspace.yaml"),"utf-8"):await B.default.readFile(q.default.resolve(process.cwd(),"package.json"),"utf-8"),t=e==="pnpm"?pe.parse(r)?.packages||[]:JSON.parse(r)?.workspaces||[];return y=await he(t)||{},j=Object.keys(y).filter(n=>y[n]&&Object.keys(y[n]).length),y}function me(e){const r=e[0];return r?r.startsWith("--")?["","",e.join(" ")]:e[1]&&e[1].startsWith("--")?[r,"",e.slice(1).join(" ")]:[r,e[1],e.slice(2).join(" ")]:["","",""]}async function he(e){if(!e.length)return;const r=await ne.default(e.map(t=>`${t}/package.json`),{dot:!0,ignore:["**/node_modules/**"]});return Promise.all(r.map(async t=>{const n=await d.getPkg(t);if(!n)return;const{name:l,scripts:c}=n;return{name:l,scripts:c}})).then(t=>t.reduce((n,l)=>{const{name:c,scripts:E}=l;return!c||!E||(n[c]=E),n},{}))}function ge(e,r){const t=Object.keys(e),n=t.find(l=>l.startsWith(r));if(n)return n;try{const l=new RegExp(r.split("").join("[_-\\w$.:]*"));return t.find(c=>l.test(c))}catch(l){p(a.default({text:`RegExp error: ${l}`,color:"red"})),process.exit(1)}}U(),exports.ccommand=U;
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import{createRequire as X}from"module";import V from"fs";import G from"path";import{jsShell as w,getPkgTool as Q,getPkg as T}from"lazy-js-utils";import Z from"fast-glob";import i from"@simon_he/colorize";import ee from"os";import re from"tty";var te=X(import.meta.url);const c="\x1B[",F="\x1B]",C="\x07",I=";",H=process.env.TERM_PROGRAM==="Apple_Terminal",u={};u.cursorTo=(r,t)=>{if(typeof r!="number")throw new TypeError("The `x` argument is required");return typeof t!="number"?c+(r+1)+"G":c+(t+1)+";"+(r+1)+"H"},u.cursorMove=(r,t)=>{if(typeof r!="number")throw new TypeError("The `x` argument is required");let e="";return r<0?e+=c+-r+"D":r>0&&(e+=c+r+"C"),t<0?e+=c+-t+"A":t>0&&(e+=c+t+"B"),e},u.cursorUp=(r=1)=>c+r+"A",u.cursorDown=(r=1)=>c+r+"B",u.cursorForward=(r=1)=>c+r+"C",u.cursorBackward=(r=1)=>c+r+"D",u.cursorLeft=c+"G",u.cursorSavePosition=H?"\x1B7":c+"s",u.cursorRestorePosition=H?"\x1B8":c+"u",u.cursorGetPosition=c+"6n",u.cursorNextLine=c+"E",u.cursorPrevLine=c+"F",u.cursorHide=c+"?25l",u.cursorShow=c+"?25h",u.eraseLines=r=>{let t="";for(let e=0;e<r;e++)t+=u.eraseLine+(e<r-1?u.cursorUp():"");return r&&(t+=u.cursorLeft),t},u.eraseEndLine=c+"K",u.eraseStartLine=c+"1K",u.eraseLine=c+"2K",u.eraseDown=c+"J",u.eraseUp=c+"1J",u.eraseScreen=c+"2J",u.scrollUp=c+"S",u.scrollDown=c+"T",u.clearScreen="\x1Bc",u.clearTerminal=process.platform==="win32"?`${u.eraseScreen}${c}0f`:`${u.eraseScreen}${c}3J${c}H`,u.beep=C,u.link=(r,t)=>[F,"8",I,I,t,C,r,F,"8",I,I,C].join(""),u.image=(r,t={})=>{let e=`${F}1337;File=inline=1`;return t.width&&(e+=`;width=${t.width}`),t.height&&(e+=`;height=${t.height}`),t.preserveAspectRatio===!1&&(e+=";preserveAspectRatio=0"),e+":"+r.toString("base64")+C},u.iTerm={setCwd:(r=process.cwd())=>`${F}50;CurrentDir=${r}${C}`,annotation:(r,t={})=>{let e=`${F}1337;`;const n=typeof t.x<"u",a=typeof t.y<"u";if((n||a)&&!(n&&a&&typeof t.length<"u"))throw new Error("`x`, `y` and `length` must be defined when `x` or `y` is defined");return r=r.replace(/\|/g,""),e+=t.isHidden?"AddHiddenAnnotation=":"AddAnnotation=",t.length>0?e+=(n?[r,t.length,t.x,t.y]:[t.length,r]).join("|"):e+=r,e+C}};var Y=(r,t=process.argv)=>{const e=r.startsWith("-")?"":r.length===1?"-":"--",n=t.indexOf(e+r),a=t.indexOf("--");return n!==-1&&(a===-1||n<a)};const ne=ee,W=re,g=Y,{env:f}=process;let R;g("no-color")||g("no-colors")||g("color=false")||g("color=never")?R=0:(g("color")||g("colors")||g("color=true")||g("color=always"))&&(R=1),"FORCE_COLOR"in f&&(f.FORCE_COLOR==="true"?R=1:f.FORCE_COLOR==="false"?R=0:R=f.FORCE_COLOR.length===0?1:Math.min(parseInt(f.FORCE_COLOR,10),3));function v(r){return r===0?!1:{level:r,hasBasic:!0,has256:r>=2,has16m:r>=3}}function A(r,t){if(R===0)return 0;if(g("color=16m")||g("color=full")||g("color=truecolor"))return 3;if(g("color=256"))return 2;if(r&&!t&&R===void 0)return 0;const e=R||0;if(f.TERM==="dumb")return e;if(process.platform==="win32"){const n=ne.release().split(".");return Number(n[0])>=10&&Number(n[2])>=10586?Number(n[2])>=14931?3:2:1}if("CI"in f)return["TRAVIS","CIRCLECI","APPVEYOR","GITLAB_CI","GITHUB_ACTIONS","BUILDKITE"].some(n=>n in f)||f.CI_NAME==="codeship"?1:e;if("TEAMCITY_VERSION"in f)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(f.TEAMCITY_VERSION)?1:0;if(f.COLORTERM==="truecolor")return 3;if("TERM_PROGRAM"in f){const n=parseInt((f.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(f.TERM_PROGRAM){case"iTerm.app":return n>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(f.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(f.TERM)||"COLORTERM"in f?1:e}function oe(r){const t=A(r,r&&r.isTTY);return v(t)}var se={supportsColor:oe,stdout:v(A(!0,W.isatty(1))),stderr:v(A(!0,W.isatty(2)))};const ue=se,O=Y;function q(r){if(/^\d{3,4}$/.test(r)){const e=/(\d{1,2})(\d{2})/.exec(r);return{major:0,minor:parseInt(e[1],10),patch:parseInt(e[2],10)}}const t=(r||"").split(".").map(e=>parseInt(e,10));return{major:t[0],minor:t[1],patch:t[2]}}function L(r){const{env:t}=process;if("FORCE_HYPERLINK"in t)return!(t.FORCE_HYPERLINK.length>0&&parseInt(t.FORCE_HYPERLINK,10)===0);if(O("no-hyperlink")||O("no-hyperlinks")||O("hyperlink=false")||O("hyperlink=never"))return!1;if(O("hyperlink=true")||O("hyperlink=always")||"NETLIFY"in t)return!0;if(!ue.supportsColor(r)||r&&!r.isTTY||process.platform==="win32"||"CI"in t||"TEAMCITY_VERSION"in t)return!1;if("TERM_PROGRAM"in t){const e=q(t.TERM_PROGRAM_VERSION);switch(t.TERM_PROGRAM){case"iTerm.app":return e.major===3?e.minor>=1:e.major>3;case"WezTerm":return e.major>=20200620;case"vscode":return e.major>1||e.major===1&&e.minor>=72}}if("VTE_VERSION"in t){if(t.VTE_VERSION==="0.50.0")return!1;const e=q(t.VTE_VERSION);return e.major>0||e.minor>=50}return!1}var M={supportsHyperlink:L,stdout:L(process.stdout),stderr:L(process.stderr)};function k(r,t,{target:e="stdout",...n}={}){return M[e]?u.link(r,t):n.fallback===!1?r:typeof n.fallback=="function"?n.fallback(r,t):`${r} (\u200B${t}\u200B)`}k.isSupported=M.stdout,k.stderr=(r,t,e={})=>k(r,t,{target:"stderr",...e}),k.stderr.isSupported=M.stderr;var ie="1.0.37";const ce=te("yamljs");let S=[],y=null;const l=console.log;async function K(){const r="__ccommand__split",{status:t}=w("gum -v","pipe");if(t!==0){l(i({color:"blue",text:"install gum..."}));const{status:o}=w("brew install gum");if(o!==0){const{status:s}=w(`sudo mkdir -p /etc/apt/keyrings
2
+ import{createRequire as X}from"module";import D from"fs/promises";import V from"path";import{jsShell as E,getPkgTool as Q,getPkg as O}from"lazy-js-utils";import Z from"fast-glob";import i from"@simon_he/colorize";import ee from"os";import re from"tty";var te=X(import.meta.url);const c="\x1B[",b="\x1B]",k="\x07",F=";",G=process.env.TERM_PROGRAM==="Apple_Terminal",u={};u.cursorTo=(e,r)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");return typeof r!="number"?c+(e+1)+"G":c+(r+1)+";"+(e+1)+"H"},u.cursorMove=(e,r)=>{if(typeof e!="number")throw new TypeError("The `x` argument is required");let t="";return e<0?t+=c+-e+"D":e>0&&(t+=c+e+"C"),r<0?t+=c+-r+"A":r>0&&(t+=c+r+"B"),t},u.cursorUp=(e=1)=>c+e+"A",u.cursorDown=(e=1)=>c+e+"B",u.cursorForward=(e=1)=>c+e+"C",u.cursorBackward=(e=1)=>c+e+"D",u.cursorLeft=c+"G",u.cursorSavePosition=G?"\x1B7":c+"s",u.cursorRestorePosition=G?"\x1B8":c+"u",u.cursorGetPosition=c+"6n",u.cursorNextLine=c+"E",u.cursorPrevLine=c+"F",u.cursorHide=c+"?25l",u.cursorShow=c+"?25h",u.eraseLines=e=>{let r="";for(let t=0;t<e;t++)r+=u.eraseLine+(t<e-1?u.cursorUp():"");return e&&(r+=u.cursorLeft),r},u.eraseEndLine=c+"K",u.eraseStartLine=c+"1K",u.eraseLine=c+"2K",u.eraseDown=c+"J",u.eraseUp=c+"1J",u.eraseScreen=c+"2J",u.scrollUp=c+"S",u.scrollDown=c+"T",u.clearScreen="\x1Bc",u.clearTerminal=process.platform==="win32"?`${u.eraseScreen}${c}0f`:`${u.eraseScreen}${c}3J${c}H`,u.beep=k,u.link=(e,r)=>[b,"8",F,F,r,k,e,b,"8",F,F,k].join(""),u.image=(e,r={})=>{let t=`${b}1337;File=inline=1`;return r.width&&(t+=`;width=${r.width}`),r.height&&(t+=`;height=${r.height}`),r.preserveAspectRatio===!1&&(t+=";preserveAspectRatio=0"),t+":"+e.toString("base64")+k},u.iTerm={setCwd:(e=process.cwd())=>`${b}50;CurrentDir=${e}${k}`,annotation:(e,r={})=>{let t=`${b}1337;`;const n=typeof r.x<"u",a=typeof r.y<"u";if((n||a)&&!(n&&a&&typeof r.length<"u"))throw new Error("`x`, `y` and `length` must be defined when `x` or `y` is defined");return e=e.replace(/\|/g,""),t+=r.isHidden?"AddHiddenAnnotation=":"AddAnnotation=",r.length>0?t+=(n?[e,r.length,r.x,r.y]:[r.length,e]).join("|"):t+=e,t+k}};var H=(e,r=process.argv)=>{const t=e.startsWith("-")?"":e.length===1?"-":"--",n=r.indexOf(t+e),a=r.indexOf("--");return n!==-1&&(a===-1||n<a)};const ne=ee,Y=re,$=H,{env:f}=process;let w;$("no-color")||$("no-colors")||$("color=false")||$("color=never")?w=0:($("color")||$("colors")||$("color=true")||$("color=always"))&&(w=1),"FORCE_COLOR"in f&&(f.FORCE_COLOR==="true"?w=1:f.FORCE_COLOR==="false"?w=0:w=f.FORCE_COLOR.length===0?1:Math.min(parseInt(f.FORCE_COLOR,10),3));function S(e){return e===0?!1:{level:e,hasBasic:!0,has256:e>=2,has16m:e>=3}}function j(e,r){if(w===0)return 0;if($("color=16m")||$("color=full")||$("color=truecolor"))return 3;if($("color=256"))return 2;if(e&&!r&&w===void 0)return 0;const t=w||0;if(f.TERM==="dumb")return t;if(process.platform==="win32"){const n=ne.release().split(".");return Number(n[0])>=10&&Number(n[2])>=10586?Number(n[2])>=14931?3:2:1}if("CI"in f)return["TRAVIS","CIRCLECI","APPVEYOR","GITLAB_CI","GITHUB_ACTIONS","BUILDKITE"].some(n=>n in f)||f.CI_NAME==="codeship"?1:t;if("TEAMCITY_VERSION"in f)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(f.TEAMCITY_VERSION)?1:0;if(f.COLORTERM==="truecolor")return 3;if("TERM_PROGRAM"in f){const n=parseInt((f.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(f.TERM_PROGRAM){case"iTerm.app":return n>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(f.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(f.TERM)||"COLORTERM"in f?1:t}function oe(e){const r=j(e,e&&e.isTTY);return S(r)}var se={supportsColor:oe,stdout:S(j(!0,Y.isatty(1))),stderr:S(j(!0,Y.isatty(2)))};const ue=se,C=H;function W(e){if(/^\d{3,4}$/.test(e)){const t=/(\d{1,2})(\d{2})/.exec(e);return{major:0,minor:parseInt(t[1],10),patch:parseInt(t[2],10)}}const r=(e||"").split(".").map(t=>parseInt(t,10));return{major:r[0],minor:r[1],patch:r[2]}}function v(e){const{env:r}=process;if("FORCE_HYPERLINK"in r)return!(r.FORCE_HYPERLINK.length>0&&parseInt(r.FORCE_HYPERLINK,10)===0);if(C("no-hyperlink")||C("no-hyperlinks")||C("hyperlink=false")||C("hyperlink=never"))return!1;if(C("hyperlink=true")||C("hyperlink=always")||"NETLIFY"in r)return!0;if(!ue.supportsColor(e)||e&&!e.isTTY||process.platform==="win32"||"CI"in r||"TEAMCITY_VERSION"in r)return!1;if("TERM_PROGRAM"in r){const t=W(r.TERM_PROGRAM_VERSION);switch(r.TERM_PROGRAM){case"iTerm.app":return t.major===3?t.minor>=1:t.major>3;case"WezTerm":return t.major>=20200620;case"vscode":return t.major>1||t.major===1&&t.minor>=72}}if("VTE_VERSION"in r){if(r.VTE_VERSION==="0.50.0")return!1;const t=W(r.VTE_VERSION);return t.major>0||t.minor>=50}return!1}var A={supportsHyperlink:v,stdout:v(process.stdout),stderr:v(process.stderr)};function R(e,r,{target:t="stdout",...n}={}){return A[t]?u.link(e,r):n.fallback===!1?e:typeof n.fallback=="function"?n.fallback(e,r):`${e} (\u200B${r}\u200B)`}R.isSupported=A.stdout,R.stderr=(e,r,t={})=>R(e,r,{target:"stderr",...t}),R.stderr.isSupported=A.stderr;var ie="1.0.38";const L=console.log;function ce(){const{status:e}=E("gum -v","pipe");if(e!==0){L(i({color:"blue",text:"install gum..."}));const{status:r}=E("brew install gum");if(r!==0){const{status:t}=E(`sudo mkdir -p /etc/apt/keyrings
3
3
  curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg
4
4
  echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | sudo tee /etc/apt/sources.list.d/charm.list
5
- sudo apt update && sudo apt install gum`);if(s!==0){const d=k("the official website of gum","https://github.com/charmbracelet/gum#installation");return l(i({color:"red",text:`gum install error, you can install it yourself through ${i({color:"yellow",text:d,bold:!0})}`}))}}l(i({color:"green",text:"gum install successfully \u{1F389}"}))}const e=process.argv.slice(2);if(e[0]==="-v"||e[0]==="--version")return l(i({text:`ccommand Version: ${ie}`,color:"green"}));if(e[0]==="-h"||e[0]==="--help"){const o=k("open an issue","https://github.com/Simon-He95/ccommand/issues"),s=k("\u2728star it","https://github.com/Simon-He95/ccommand");return l(i({color:"white",text:`
5
+ sudo apt update && sudo apt install gum`);if(t!==0){const n=R("the official website of gum","https://github.com/charmbracelet/gum#installation");return L(i({color:"red",text:`gum install error, you can install it yourself through ${i({color:"yellow",text:n,bold:!0})}`}))}}L(i({color:"green",text:"gum install successfully \u{1F389}"}))}}const ae=te("yamljs");let _=[],g=null;const p=console.log,q="__ccommand__split";async function K(){ce();const e=process.argv.slice(2);if(e[0]==="-v"||e[0]==="--version")return p(i({text:`ccommand Version: ${ie}`,color:"green"}));if(e[0]==="-h"||e[0]==="--help"){const o=R("open an issue","https://github.com/Simon-He95/ccommand/issues"),s=R("\u2728star it","https://github.com/Simon-He95/ccommand");return p(i({color:"white",text:`
6
6
  ${i({bold:!0,text:"Common Commands:",bgColor:"blue"})}
7
7
  ${i({text:`- ccommand -v \u67E5\u770B\u5F53\u524D\u7248\u672C
8
8
  - ccommand -help \u67E5\u770B\u5E2E\u52A9
@@ -10,9 +10,9 @@ import{createRequire as X}from"module";import V from"fs";import G from"path";imp
10
10
  - ccommand find \u67E5\u627E\u5F53\u524Dworkspace\u7684\u6240\u6709\u76EE\u5F55
11
11
  `,color:"cyan"})}
12
12
  If you encounter any problems, you can ${i({color:"magenta",text:o})}.
13
- If you like it, please ${i({text:s,bold:!0,color:"cyan"})} `}))}const n=Q(),[a,h,E]=ae(e);let p=a,b;if(e[0]==="find"){if(h)await _(n),p=S.filter(o=>o.includes(h))[0];else if(n==="yarn"){await _(n);const{result:o}=w(`echo ${S.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(p=o,!p)return l(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else if(n==="pnpm"){await _(n);const{result:o}=w(`echo ${S.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(p=o.trim(),!p)return l(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else return l(i({color:"red",text:"find command only support yarn or pnpm"}));b=await N()}else{if(b=await N(),a&&y&&!y[a]||!y)try{const o=(await T("./package.json")||{})?.scripts;if(o&&o[e[0]])return l(i({text:`ccommand is executing ${i({color:"cyan",text:`'${e[0]}'`})} \u{1F914} `,color:"yellow"})),B(e[0],e.slice(1).join(" "));if(o&&a){const s=fe(o,e[0]),d=e.slice(1).join(" ");return B(s,d)}}catch{}y&&!y[e[0]]&&(l(i({color:"red",text:`"${e[0]}" is not found in workspace, current directory
14
- or current scripts, please check`})),process.exit())}if(!b)return l(i({color:"red",text:"No scripts found"}));const x=[];let $="";if(!h||e[0]==="find"&&(!e[2]||e[2].startsWith("--"))){const o=Object.keys(b).reduce((d,m)=>{const j=b[m];return x.push(m),d+=`"${m}: ${j.replace(/\"/g,"'")}"${r}`,d},""),{result:s}=w(`echo ${o} | sed "s/${r}/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u6307\u4EE4"`,"pipe");$=s.substring(0,s.indexOf(": "))}if(!h&&!$)return l(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"})),process.exit();const{status:z}=await w(U());if(z===0)return l(i({color:"green",text:`
15
- command '${(e[0]==="find"?e[2]:e[1])||$}' run successfully \u{1F389}`})),process.exit();l(i({color:"red",text:`
16
- command '${$}' run error \u274C`}));function P(o){return x.find(s=>s===o)??x.find(s=>o.startsWith(s))}function U(){let o="",s="";const d=n!=="yarn";n==="npm"?(s=E?` -- ${E}`:"",o=p?` --prefix ${p} `:" "):n==="pnpm"?(s=E?` ${E}`:"",o=p?` --filter ${p} `:" "):n==="yarn"?(s=E?` ${E}`:"",o=p?` workspace ${p} `:" "):n==="bun"&&(s=E?` ${E}`:"",o="");let m="";if(s&&!s.startsWith(" --")){const D=s.split(" ").filter(Boolean);m=D[0],s=D.slice(1).join(" ")}const j=`${n}${d?" run":" "}${o} ${m||($?P($)||$:h)} ${J(s)?`-- ${s}`:s}`;return $=`${m||($?P($):h)}`,e[0]==="find"?l(i({text:`tips: pfind ${p} ${$} ${s}`.replace(/\s+/g," "),color:"blue",bold:!0})):l(i({text:`tips: prun ${$} ${s}`.replace(/\s+/g," "),color:"blue",bold:!0})),j}async function N(){try{if(!p||n==="bun"||n==="npm")return(await T("./package.json"))?.scripts;if(n==="pnpm")return(await _(n))[p]||(await T(`${p}/package.json`))?.scripts;if(n==="yarn")return(await _(n))[p]||(await T(`${p}/package.json`))?.scripts}catch{}}function J(o){return e[0]==="find",e[1]&&o}async function B(o,s){let d;switch(o&&e[0]!==o&&l(i({text:`\u{1F914} ${i({text:`'${e[0]}'`,color:"cyan"})} automatically match for you to ${i({text:`'${o}${s?` ${s}`:""}'`,color:"cyan"})} `,color:"yellow"})),n){case"npm":{const{status:m}=w(`npm run ${o}${s?` -- ${s}`:""}`);d=m;break}case"pnpm":{const{status:m}=w(`pnpm run ${o}${s?` ${s}`:""}`);d=m;break}case"yarn":{const{status:m}=w(`yarn ${o}${s?` ${s}`:""}`);d=m;break}case"bun":{const{status:m}=w(`bun ${o} ${s}`);d=m;break}}return l(d===0?i({color:"green",text:`
13
+ If you like it, please ${i({text:s,bold:!0,color:"cyan"})} `}))}const r=Q(),[t,n,a]=le(e);let l=t,y;if(e[0]==="find"){if(n)await T(r),l=_.filter(o=>o.includes(n))[0];else if(r==="yarn"){await T(r);const{result:o}=E(`echo ${_.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(l=o,!l)return p(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else if(r==="pnpm"){await T(r);const{result:o}=E(`echo ${_.join(",")} | sed "s/,/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u76EE\u5F55"`,"pipe");if(l=o.trim(),!l)return p(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"}))}else return p(i({color:"red",text:"find command only support yarn or pnpm"}));y=await P()}else{if(y=await P(),t&&g&&!g[t]||!g)try{const o=(await O("./package.json")||{})?.scripts;if(o&&o[e[0]])return p(i({text:`ccommand is executing ${i({color:"cyan",text:`'${e[0]}'`})} \u{1F914} `,color:"yellow"})),N(e[0],e.slice(1).join(" "));if(o&&t){const s=pe(o,e[0]),h=e.slice(1).join(" ");return N(s,h)}}catch{}g&&!g[e[0]]&&(p(i({color:"red",text:`"${e[0]}" is not found in workspace, current directory
14
+ or current scripts, please check`})),process.exit())}if(!y)return p(i({color:"red",text:"No scripts found"}));const I=[];let d="";if(!n||e[0]==="find"&&(!e[2]||e[2].startsWith("--"))){const o=Object.keys(y).reduce((h,m)=>{const x=y[m];return I.push(m),h+=`"${m}: ${x.replace(/\"/g,"'")}"${q}`,h},""),{result:s}=E(`echo ${o} | sed "s/${q}/\\n/g" | gum filter --placeholder=" \u{1F914}\u8BF7\u9009\u62E9\u4E00\u4E2A\u8981\u6267\u884C\u7684\u6307\u4EE4"`,"pipe");d=s.substring(0,s.indexOf(": "))}if(!n&&!d)return p(i({color:"yellow",text:"\u5DF2\u53D6\u6D88"})),process.exit();const{status:z}=await E(U());if(z===0)return p(i({color:"green",text:`
15
+ command '${(e[0]==="find"?e[2]:e[1])||d}' run successfully \u{1F389}`})),process.exit();p(i({color:"red",text:`
16
+ command '${d}' run error \u274C`}));function M(o){return I.find(s=>s===o)??I.find(s=>o.startsWith(s))}function U(){let o="",s="";const h=r!=="yarn";r==="npm"?(s=a?` -- ${a}`:"",o=l?` --prefix ${l} `:" "):r==="pnpm"?(s=a?` ${a}`:"",o=l?` --filter ${l} `:" "):r==="yarn"?(s=a?` ${a}`:"",o=l?` workspace ${l} `:" "):r==="bun"&&(s=a?` ${a}`:"",o="");let m="";if(s&&!s.startsWith(" --")){const B=s.split(" ").filter(Boolean);m=B[0],s=B.slice(1).join(" ")}const x=`${r}${h?" run":" "}${o} ${m||(d?M(d)||d:n)} ${J(s)?`-- ${s}`:s}`;return d=`${m||(d?M(d):n)}`,e[0]==="find"?p(i({text:`tips: pfind ${l} ${d} ${s}`.replace(/\s+/g," "),color:"blue",bold:!0})):p(i({text:`tips: prun ${d} ${s}`.replace(/\s+/g," "),color:"blue",bold:!0})),x}async function P(){try{if(!l||r==="bun"||r==="npm")return(await O("./package.json"))?.scripts;if(r==="pnpm")return(await T(r))[l]||(await O(`${l}/package.json`))?.scripts;if(r==="yarn")return(await T(r))[l]||(await O(`${l}/package.json`))?.scripts}catch{}}function J(o){return e[0]==="find",e[1]&&o}async function N(o,s){let h;switch(o&&e[0]!==o&&p(i({text:`\u{1F914} ${i({text:`'${e[0]}'`,color:"cyan"})} automatically match for you to ${i({text:`'${o}${s?` ${s}`:""}'`,color:"cyan"})} `,color:"yellow"})),r){case"npm":{const{status:m}=E(`npm run ${o}${s?` -- ${s}`:""}`);h=m;break}case"pnpm":{const{status:m}=E(`pnpm run ${o}${s?` ${s}`:""}`);h=m;break}case"yarn":{const{status:m}=E(`yarn ${o}${s?` ${s}`:""}`);h=m;break}case"bun":{const{status:m}=E(`bun ${o} ${s}`);h=m;break}}return p(h===0?i({color:"green",text:`
17
17
  command '${o}${s?` ${s}`:""}' run successfully \u{1F389}`}):i({color:"red",text:`
18
- command ${i({bold:!0,color:"cyan",text:`'${o||e[0]}${s?` ${s}`:""}'`})} run error \u274C`}))}}async function _(r){if(y)return y;const t=r==="pnpm"?await V.readFileSync(G.resolve(process.cwd(),"pnpm-workspace.yaml"),"utf-8"):await V.readFileSync(G.resolve(process.cwd(),"package.json"),"utf-8"),e=r==="pnpm"?ce.parse(t)?.packages||[]:JSON.parse(t)?.workspaces||[];return y=await le(e)||{},S=Object.keys(y).filter(n=>y[n]&&Object.keys(y[n]).length),y}function ae(r){const t=r[0];return t?t.startsWith("--")?["","",r.join(" ")]:r[1]&&r[1].startsWith("--")?[t,"",r.slice(1).join(" ")]:[t,r[1],r.slice(2).join(" ")]:["","",""]}async function le(r){if(!r.length)return;const t=await Z(r.map(e=>`${e}/package.json`),{dot:!0,ignore:["**/node_modules/**"]});return Promise.all(t.map(async e=>{const n=await T(e);if(!n)return;const{name:a,scripts:h}=n;return{name:a,scripts:h}})).then(e=>e.reduce((n,a)=>{const{name:h,scripts:E}=a;return!h||!E||(n[h]=E),n},{}))}function fe(r,t){const e=Object.keys(r),n=e.find(a=>a.startsWith(t));if(n)return n;try{const a=new RegExp(t.split("").join("[_-\\w$.:]*"));return e.find(h=>a.test(h))}catch(a){l(i({text:`RegExp error: ${a}`,color:"red"})),process.exit(1)}}K();export{K as ccommand};
18
+ command ${i({bold:!0,color:"cyan",text:`'${o||e[0]}${s?` ${s}`:""}'`})} run error \u274C`}))}}async function T(e){if(g)return g;const r=e==="pnpm"?await D.readFile(V.resolve(process.cwd(),"pnpm-workspace.yaml"),"utf-8"):await D.readFile(V.resolve(process.cwd(),"package.json"),"utf-8"),t=e==="pnpm"?ae.parse(r)?.packages||[]:JSON.parse(r)?.workspaces||[];return g=await fe(t)||{},_=Object.keys(g).filter(n=>g[n]&&Object.keys(g[n]).length),g}function le(e){const r=e[0];return r?r.startsWith("--")?["","",e.join(" ")]:e[1]&&e[1].startsWith("--")?[r,"",e.slice(1).join(" ")]:[r,e[1],e.slice(2).join(" ")]:["","",""]}async function fe(e){if(!e.length)return;const r=await Z(e.map(t=>`${t}/package.json`),{dot:!0,ignore:["**/node_modules/**"]});return Promise.all(r.map(async t=>{const n=await O(t);if(!n)return;const{name:a,scripts:l}=n;return{name:a,scripts:l}})).then(t=>t.reduce((n,a)=>{const{name:l,scripts:y}=a;return!l||!y||(n[l]=y),n},{}))}function pe(e,r){const t=Object.keys(e),n=t.find(a=>a.startsWith(r));if(n)return n;try{const a=new RegExp(r.split("").join("[_-\\w$.:]*"));return t.find(l=>a.test(l))}catch(a){p(i({text:`RegExp error: ${a}`,color:"red"})),process.exit(1)}}K();export{K as ccommand};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccommand",
3
- "version": "1.0.37",
3
+ "version": "1.0.38",
4
4
  "packageManager": "pnpm@6.32.3",
5
5
  "description": "ccommand",
6
6
  "author": "Simon He",
@@ -63,6 +63,7 @@
63
63
  "@types/node": "^17.0.45",
64
64
  "@vitejs/plugin-vue": "^3.0.1",
65
65
  "bumpp": "^8.2.1",
66
+ "commander": "^10.0.0",
66
67
  "eslint": "^8.16.0",
67
68
  "eslint-plugin-n": "^15.2.1",
68
69
  "lint-staged": "^13.1.0",