crtxdmp.

A collection of ideas, snippets and other things.


Visualize and prettify pre-commit with husky

File: projectRoot/.prettierignore
# package.json is formatted by package managers, so we ignore it here
package.json
File: projectRoot/package.json
{
  "name": "application",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "fix": "run-s fix:*",
    "fix:format": "./node_modules/.bin/prettier --ignore-path=.prettierignore --config=.prettierrc --write '{./,src/**/,./.storybook/*}*.{md,ts,tsx,css,html,json,js,scss}'",
    "visualize": "run-s visualize:*",
    "visualize:architecture-raw": "./node_modules/.bin/depcruise --validate .dependency-cruiser.json --output-type json src -f ./.visualization/result.json",
    "visualize:architecture-overview": "./node_modules/.bin/depcruise-fmt ./.visualization/result.json --output-type ddot src | dot -T svg | depcruise-wrap-stream-in-html > ./.visualization/overview.html",
    "visualize:architecture-detailed": "./node_modules/.bin/depcruise-fmt ./.visualization/result.json --output-type dot src | dot -T svg | depcruise-wrap-stream-in-html > ./.visualization/detailed.html"
  },
  "dependencies": {
  },
  "devDependencies": {
    "dependency-cruiser": "^9.17.0",
    "husky": "^4.3.8",
    "lint-staged": ">=10",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.1.2"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged && yarn run visualize && git add ./.visualization/*"
    }
  },
  "lint-staged": {
    "*.{md,ts,tsx,css,html,json,js,scss}": "prettier --config=.prettierrc --write"
  }
}
File: projectRoot/.dependency-cruiser.json
{
  "forbidden": [
    {
      "name": "not-to-test",
      "comment": "Don't allow dependencies from outside the test folder to test",
      "severity": "error",
      "from": {
        "pathNot": "^(test|spec)"
      },
      "to": {
        "path": "^(test|spec)"
      }
    },
    {
      "name": "not-to-spec",
      "comment": "Don't allow dependencies to (typescript/ javascript/ coffeescript) spec files",
      "severity": "error",
      "from": {},
      "to": {
        "path": "\\.spec\\.(js|ts|ls|coffee|litcoffee|coffee\\.md)$"
      }
    },
    {
      "name": "no-circular",
      "severity": "warn",
      "comment": "Warn in case there's circular dependencies",
      "from": {},
      "to": {
        "circular": true
      }
    },
    {
      "name": "no-orphans",
      "severity": "info",
      "comment": "Inform in case there's orphans hiding in the code base",
      "from": {
        "orphan": true,
        "pathNot": "\\.d\\.ts$"
      },
      "to": {}
    },
    {
      "name": "no-deprecated-core",
      "comment": "Warn about dependencies on deprecated core modules.",
      "severity": "warn",
      "from": {},
      "to": {
        "dependencyTypes": [
          "core"
        ],
        "path": "^(punycode|domain|constants|sys|_linklist)$"
      }
    },
    {
      "name": "no-deprecated-npm",
      "comment": "These npm modules are deprecated - find an alternative.",
      "severity": "warn",
      "from": {},
      "to": {
        "dependencyTypes": [
          "deprecated"
        ]
      }
    },
    {
      "name": "not-to-unresolvable",
      "comment": "Don't allow dependencies on modules dependency-cruiser can't resolve to files on disk (which probably means they don't exist)",
      "severity": "error",
      "from": {},
      "to": {
        "couldNotResolve": true
      }
    },
    {
      "name": "not-to-dev-dep",
      "severity": "error",
      "comment": "Don't allow dependencies from src/app/lib to a development only package",
      "from": {
        "path": "^(src|app|lib)",
        "pathNot": "\\.spec\\.(js|ts|ls|coffee|litcoffee|coffee\\.md)$"
      },
      "to": {
        "dependencyTypes": [
          "npm-dev"
        ]
      }
    },
    {
      "name": "no-non-package-json",
      "severity": "error",
      "comment": "Don't allow dependencies to packages not in package.json (except from within node_modules)",
      "from": {
        "pathNot": "^node_modules"
      },
      "to": {
        "dependencyTypes": [
          "unknown",
          "undetermined",
          "npm-no-pkg",
          "npm-unknown"
        ]
      }
    },
    {
      "name": "optional-deps-used",
      "severity": "info",
      "comment": "nothing serious - but just check you have some serious try/ catches around the import/ requires of these",
      "from": {},
      "to": {
        "dependencyTypes": [
          "npm-optional"
        ]
      }
    },
    {
      "name": "peer-deps-used",
      "comment": "Warn about the use of a peer dependency (peer dependencies are deprecated with the advent of npm 3 - and probably gone with version 4).",
      "severity": "warn",
      "from": {},
      "to": {
        "dependencyTypes": [
          "npm-peer"
        ]
      }
    },
    {
      "name": "no-duplicate-dep-types",
      "comment": "Warn if a dependency you're actually using occurs in your package.json more than once (technically: has more than one dependency type)",
      "severity": "warn",
      "from": {},
      "to": {
        "moreThanOneDependencyType": true
      }
    }
  ],
  "options": {
    "doNotFollow": "node_modules",
    "exclude": "(^node_modules|__snapshots__)|([a-zA-Z/].(test|stories|spec).(ts|tsx))",
    "tsPreCompilationDeps": true,
    "tsConfig": {
      "fileName": "./tsconfig.json"
    }
  }
}

, , — Mar 24, 2021