UNPKG

@lntvow/dev

Version:

Development environment dependencies package

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