UNPKG

@legislato/legislato-conventions

Version:

This package facilitates the configuration of commit conventions and changelog for Next.js and Express projects

5 lines 5.23 kB
#!/usr/bin/env node import{fileURLToPath as J}from"url";import D from"path";var N=()=>J(import.meta.url),A=()=>D.dirname(N()),p=A();import i from"chalk";import{exec as q}from"child_process";import{Command as G}from"commander";import*as F from"fs";import H from"ora";import*as C from"path";import{promisify as M}from"util";import l from"chalk";import*as n from"fs";import E from"inquirer";import T from"ora";import*as a from"path";async function x(){let t=T("Copying configuration files...").start(),r=a.join(p,"../configs"),e=process.cwd(),s=async(c,o)=>{if(n.existsSync(c))if(n.statSync(c).isDirectory()){n.existsSync(o)||n.mkdirSync(o);let m=n.readdirSync(c);for(let k of m)await s(a.join(c,k),a.join(o,k))}else try{n.existsSync(o)?(await E.prompt([{type:"confirm",name:"overwrite",message:l.yellow(`File ${o} already exists. Overwrite?`),default:!1}])).overwrite?(n.copyFileSync(c,o),t.info(l.gray(`File ${o} overwritten successfully!`))):t.warn(l.yellow(`File ${o} skipped.`)):(n.copyFileSync(c,o),t.info(l.gray(`File ${o} copied successfully!`)))}catch(m){throw t.fail(l.red(`Error copying file ${o}:`)),console.error(m),m}},d=n.readdirSync(r),b={".commitlintrc.template.json":".commitlintrc.json",".lintstagedrc.template.json":".lintstagedrc.json",".lintstagedrc.template.cjs":".lintstagedrc.cjs",".release-it.template.ts":".release-it.ts","changelog.config.template.cjs":"changelog.config.cjs","getAllContributors.template.cjs":"getAllContributors.cjs"};try{let c=n.existsSync(a.join(e,"next.config.js"))||n.existsSync(a.join(e,"next.config.ts"));for(let o of d){let v=a.join(r,o),j=b[o]||o,w=a.join(e,j);await s(v,w)}if(c){let o=a.join(e,".lintstagedrc.json");n.existsSync(o)&&n.unlinkSync(o)}else{let o=a.join(e,".lintstagedrc.cjs");n.existsSync(o)&&n.unlinkSync(o)}t.succeed(l.green("Configuration files copied successfully!"))}catch(c){throw t.fail(l.red("Error copying configuration files:")),console.error(c),c}}import g from"chalk";import{exec as $}from"child_process";import z from"ora";import{promisify as O}from"util";var R=O($);async function S(){let t=z("Installing dependencies...").start(),r=["@commitlint/cli@^19.0.0","@commitlint/config-conventional@^19.0.0","@release-it/conventional-changelog@^10.0.0","commitizen@^4.0.0","conventional-changelog-conventionalcommits@^8.0.0","dotenv-cli@^8.0.0","git-cz@^4.0.0","husky@^9.0.0","inquirer@^9.0.0","lint-staged@^15.0.0","release-it@^18.0.0","typescript@^5.0.0"];try{let{stdout:e,stderr:s}=await R(`npm install --save-dev ${r.join(" ")}`);e&&console.log(g.gray(e)),s&&console.error(g.gray(s)),t.succeed(g.green("Dependencies installed successfully!"))}catch(e){if(t.fail(g.red("Error installing dependencies:")),e instanceof Error){let s=e;s.stderr?console.error(s.stderr):s.stdout?console.error(s.stdout):console.error(e.message)}else console.error(e);throw e}}import y from"chalk";import*as f from"fs";import I from"ora";import*as P from"path";function _(t){let r=I("Modifying package.json...").start(),e=P.join(process.cwd(),"package.json");if(!f.existsSync(e)){r.fail(y.red("package.json file not found."));return}try{let s=JSON.parse(f.readFileSync(e,"utf-8")),d=t(s);f.writeFileSync(e,JSON.stringify(d,null,2)),r.succeed(y.green("package.json modified successfully!"))}catch(s){throw r.fail(y.red("Error modifying package.json:")),console.error(s),s}}var U=M(q),h=new G,W=C.join(p,"../package.json"),B=JSON.parse(F.readFileSync(W,"utf-8"));h.version(B.version).description("Automatically configures conventional commits and changelogs.");async function K(){let t=H("Setting up conventions...").start();try{let r=process.env.npm_config_user_agent;(!r||!r.startsWith("npm"))&&(t.fail(i.red("This package only supports npm. Please use npm to install dependencies.")),process.exit(1)),await S(),await x(),_(e=>(e.scripts={...e.scripts??{},prepare:"husky",release:"dotenv release-it -- --verbose","release:only-version":"dotenv release-it -- --verbose --only-version","release:no-npm":"dotenv release-it -- --no-npm.publish","release:changelog":"dotenv release-it -- --changelog","release:version":"dotenv release-it -- --release-version"},e.config={commitizen:{path:"./node_modules/git-cz"}},e)),t.start("Running npm run prepare..."),await U("npm run prepare"),t.succeed(i.green("npm run prepare executed successfully!")),t.succeed(i.green("Configuration completed successfully!")),console.log(i.bold(` Next steps:`)),console.log(i.white("1. Create a .env file in your project root.")),console.log(i.white("2. Add the following line to the .env file:")),console.log(i.cyan(" GITHUB_TOKEN=your_github_personal_access_token")),console.log(i.gray(" (Replace your_github_personal_access_token with your actual token)")),console.log(i.white("3. This will allow release-it to automate releases on GitHub.")),console.log(i.white("4. If you want to run lint, test or build before running release, please update the release scripts in your package.json accordingly.")),console.log(i.green(` \u{1F389} Congratulations! Your project is now ready for organized commits and automated releases!`))}catch(r){t.fail(i.red("Configuration failed")),console.error(r),process.exit(1)}}h.action(K);h.parse(process.argv);export{K as main}; //# sourceMappingURL=index.mjs.map