UNPKG

@lntvow/dev

Version:

Development environment dependencies package

3 lines (2 loc) 4.33 kB
#!/usr/bin/env node import{readFileSync as C,writeFileSync as v}from"fs";import{resolve as h}from"path";import{fileURLToPath as P}from"url";import{program as r}from"commander";import T from"picocolors";import F from"dayjs";import I from"simple-git";import{isPackageExists as R}from"local-pkg";var m="@lntvow/dev",g="5.0.29";import{execSync as w}from"child_process";import{existsSync as j}from"fs";import{resolve as S}from"path";import o from"picocolors";var i={info:e=>console.log(o.blue(`\u25CF ${e}`)),success:e=>console.log(o.green(`\u2713 ${e}`)),warn:e=>console.log(o.yellow(`\u25B2 ${e}`)),error:e=>console.log(o.red(`\u2717 ${e}`))};function a(e,t={}){try{return w(e,{stdio:"inherit",...t})}catch(n){throw i.error(`\u547D\u4EE4\u6267\u884C\u5931\u8D25: ${e}`),n}}function f(){let e=[{file:"pnpm-lock.yaml",manager:"pnpm"},{file:"yarn.lock",manager:"yarn"},{file:"package-lock.json",manager:"npm"}];for(let{file:n,manager:s}of e)if(j(S(process.cwd(),n)))return s;let t=process.env.npm_config_user_agent||"";return t.startsWith("pnpm")?"pnpm":t.startsWith("yarn")?"yarn":"npm"}var u={postinstall:"simple-git-hooks",rimraf:"rimraf ./node_modules/",ncu:"ncu -u --reject @types/node && pnpm install",commit:"git add . && git-cz && pnpm release -y",release:"git add . && bumpp package.json --all --no-tag --no-print-commits","lint:eslint":'eslint "src/**/*.{js,jsx,ts,tsx,vue}" --fix',"lint:tsc":"tsc --noEmit",lint:"run-p lint:eslint lint:tsc"},y=["@commitlint/cli","@commitlint/config-conventional","@lntvow/eslint-config","bumpp","commitizen","cz-conventional-changelog","eslint","lint-staged","npm-run-all","prettier","simple-git-hooks","typescript"].sort((e,t)=>e.localeCompare(t));import{statSync as D,mkdirSync as E,readdirSync as $,existsSync as b,copyFileSync as z}from"fs";import{basename as _,resolve as k}from"path";function p(e,t){if(D(e).isDirectory()){if(_(e)==="node_modules")return;E(t,{recursive:!0}),$(e).forEach(s=>{p(k(e,s),k(t,s))})}else{if(b(t))return;z(e,t)}}var G=P(new URL(".",import.meta.url));r.name(m).version(g,"-v, --version","output the version number");r.command("init").option("-t, --temp","copy template files",!0).option("--no-temp","not copy template files").option("-s, --scripts","init scripts",!0).option("--no-scripts","not init scripts").option("-g, --git","init simple-git-hooks",!0).option("--no-git","not init simple-git-hooks").description("init project").action(async e=>{i.success("Init project by @lntvow/dev"),i.info(`Cwd is ${process.cwd()}`);let{temp:t,git:n,scripts:s}=e;try{L(),t&&N(),s&&U(),n&&M(),i.success("Project initialization completed successfully!")}catch(x){i.error(`Initialization failed: ${x.message}`),process.exit(1)}});function N(){i.info("Copying template files..."),p(h(G,"./configs"),process.cwd()),i.success("Template files copied successfully")}function M(){if(!R("simple-git-hooks")){i.warn("simple-git-hooks not installed, skipping initialization");return}try{i.info("Initializing Git hooks..."),a("npx simple-git-hooks"),i.success("Git hooks initialized successfully")}catch(e){i.error(`Failed to initialize Git hooks: ${e.message}`)}}function U(){i.info("Updating package.json scripts...");let e=h(process.cwd(),"package.json"),t;try{t=C(e,"utf-8");let n=JSON.parse(t),s={...n,scripts:{...u,...n.scripts}};v(e,JSON.stringify(s,null,2)),i.success("Package.json scripts updated successfully")}catch(n){t&&v(e,t),i.error(`Failed to update package.json: ${n.message}`),process.exit(1)}}function L(){try{let e=y.map(s=>`${s}@latest`).join(" "),t=f(),n="";t==="pnpm"?n=`pnpm add -D ${e}`:t==="yarn"?n=`yarn add -D ${e}`:n=`npm install -D ${e}`,i.info(`Installing dependencies: ${n}`),a(n),i.success("Dependencies installed successfully")}catch(e){i.error(`Dependency installation failed: ${e.message}`),process.exit(1)}}r.command("release").description("git release").action(async()=>{console.log(T.green("Release project..."));try{await O(),i.success("Release completed successfully!")}catch(e){i.error(`Release failed: ${e.message}`),process.exit(1)}});async function O(){let e=I(process.cwd());try{let t=F().format("YYYYMMDD");await e.pull(),await e.add("."),await e.commit(t,["--no-verify"]),await e.push("origin"),i.success(`\u63D0\u4EA4\u6210\u529F: ${t}`)}catch(t){i.error(`Git\u64CD\u4F5C\u5931\u8D25: ${t.message}`),process.exit(1)}}r.parse();