UNPKG

@anolilab/lint-staged-config

Version:
91 lines (73 loc) 4.86 kB
#!/usr/bin/env node var g=Object.defineProperty;var h=(n,o)=>g(n,"name",{value:o,configurable:!0});import{existsSync as u}from"node:fs";import{join as s}from"node:path";import{exit as f}from"node:process";import{readFile as p,writeFileSync as r,ensureDirSync as $,isAccessibleSync as _}from"@visulima/fs";import{parsePackageJson as y,hasPackageJsonAnyDependency as d}from"@visulima/package";import __cjs_mod__ from "node:module"; // -- packem CommonJS require shim -- const require = __cjs_mod__.createRequire(import.meta.url); var w=Object.defineProperty,l=h((n,o)=>w(n,"name",{value:o,configurable:!0}),"c");const c=l(n=>_(n)?(console.warn(`⚠️ ${n} already exists;`),!0):!1,"checkIfFileExists"),I=l(async(n,o)=>{const e=".lintstagedrc";for(const i of[e,`${e}.js`,`${e}.cjs`,`${e}.mjs`,`${e}.json`,`${e}.yaml`,`${e}.yml`,"lint-staged.config.js","lint-staged.config.mjs","lint-staged.config.cjs"])if(c(s(n,i))){console.warn(`⚠️ ${i} already exists;`);return}const t=s(n,".lintstagedrc.js");r(t,`${o?'import { defineConfig } from "@anolilab/lint-staged-config"':'const { defineConfig } = require("@anolilab/lint-staged-config")'}; ${o?"export default":"module.exports ="} defineConfig(); `)},"writeLintStagedRc"),x=l(async(n,o)=>{const e=".nano-staged";for(const i of[e,`${e}.js`,`${e}.cjs`,`${e}.mjs`,`${e}.json`,`${e.replace(".","")}.js`,`${e.replace(".","")}.cjs`,`${e.replace(".","")}.mjs`,`${e.replace(".","")}.json`,".nanostagedrc"])if(c(s(n,i))){console.warn(`⚠️ ${i} already exists;`);return}const t=s(n,".nano-staged.js");r(t,`${o?'import { defineConfig } from "@anolilab/lint-staged-config"':'const { defineConfig } = require("@anolilab/lint-staged-config")'}; ${o?"export default":"module.exports ="} defineConfig(); `)},"writeNanoStagedRc"),S=l(async(n,o,e)=>{if(!d(o,["husky"])){console.warn("⚠️ husky is not installed;");return}const t=s(n,".husky");$(t);const i=s(t,"common.sh");c(i)||r(i,`#!/bin/sh command_exists () { command -v "$1" >/dev/null 2>&1 } IS_WINDOWS="false" if [ "$OSTYPE" = "cygwin" ]; then IS_WINDOWS="true" elif [ "$OSTYPE" = "msys" ]; then IS_WINDOWS="true" elif [ "$OSTYPE" = "win32" ]; then IS_WINDOWS="true" fi # Workaround for Windows 10, Git Bash and Yarn if [ "$IS_WINDOWS" = "true" ]; then if command_exists winpty && test -t 1; then exec < /dev/tty fi fi `);const a=s(t,"pre-commit");c(a)||r(a,`#!/bin/sh . "$(dirname "$0")/_/husky.sh" . "$(dirname "$0")/common.sh" # The hook should exit with non-zero status after issuing # an appropriate message if it wants to stop the commit. echo -------------------------------------------- echo Starting Git hook: pre-commit ${e?"./node_modules/.bin/nano-staged":"./node_modules/.bin/lint-staged --verbose --concurrent false"} echo Finished Git hook: pre-commit echo -------------------------------------------- `);const m=s(t,"prepare-commit-msg");d(o,["commitizen"])&&!c(m)&&r(m,`#!/bin/sh . "$(dirname "$0")/_/husky.sh" . "$(dirname "$0")/common.sh" echo -------------------------------------------- echo Starting Git hook: prepare-commit-msg # if we hve a cmd that is running ./node_modules/.bin/cz that means finalize and commit FILE=commit.cmd if test -f "$FILE"; then echo "$FILE exists." rm commit.cmd exit 0; fi # if on Windows, spawn a cmd that will run ./node_modules/.bin/cz case \`uname\` in *CYGWIN*|*MINGW*|*MSYS* ) # Only run commitizen if no commit message was already provided. if [ -z "\${2-}" ]; then export CZ_TYPE="\${CZ_TYPE:-fix}" export CZ_MAX_HEADER_WIDTH=$COMMITLINT_MAX_WIDTH export CZ_MAX_LINE_WIDTH=$CZ_MAX_HEADER_WIDTH echo "./node_modules/.bin/cz && exit" > commit.cmd start commit.cmd exit 1; fi exit 0;; esac # Only run commitizen if no commit message was already provided. if [ -z "\${2-}" ]; then export CZ_TYPE="\${CZ_TYPE:-fix}" export CZ_MAX_HEADER_WIDTH=$COMMITLINT_MAX_WIDTH export CZ_MAX_LINE_WIDTH=$CZ_MAX_HEADER_WIDTH # By default git hooks are not interactive. exec < /dev/tty allows a users terminal to interact with commitizen. exec < /dev/tty && ./node_modules/.bin/cz --hook || true fi echo Finished Git hook: prepare-commit-msg echo -------------------------------------------- `)},"writeHuskyFiles");(async()=>{const n=process.cwd();console.log("Configuring @anolilab/lint-staged-config",n,` `);const o=s(n,"package.json");u(o)||(console.error("No package.json found in the current directory. You need to run this command in a directory with a package.json file."),f(1));const e=y(await p(o)),t=e.type==="module",i=d(e,["lint-staged"]),a=d(e,["nano-staged"]);try{i?await I(n,t):a&&await x(n,t),await S(n,e,a),console.log("Everything went well, have fun!"),f(0)}catch(m){console.error("Something went wrong:",m),f(1)}})();