2

GitHub - denisraslov/folderslint: 📁 Directory structure linter for Front-End pr...

 2 years ago
source link: https://github.com/denisraslov/folderslint
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

FoldersLint

Directory structure linter for Front-End projects

FoldersLint in action

white_check_mark   Easily configured with simple rules in a single file

white_check_mark   Incredibly fast

white_check_mark   Support for Windows, MacOS and Linux

white_check_mark   Can be used with lint-staged

Make you project sctructure pretty by linting it card_index_dividers

Directory structure rules are important part of any project. These rules help to raise clarity of the project and reduce its complexity. Having a clearly defined structure make developers always know where to put files and where to find them. If the project is big enough, it is necessary to avoid chaos in it.

folderslint let you configure directory structure rules and check if existed or new files fit these rules.

Quick Overview

Install folderslint globally:

npm install -g folderslint

Setup a config file .folderslintrc in the root of the project.

Run folderslint to check the whole project or a directory (i.e. /components):

folderslint components

Configuration

folderslint needs configuration file named .folderslintrc in the root of the project.

The example of the config:

{
  "root": "src", // optional
  "rules": [
    "components/*",
    "pages/components/*/utils",
    "hooks",
    "legacy/**"
   ]
}

root is the directory the structure of which should be checked.

rules is an array of rules which define permitted directory paths.

Root directory

You have to specify root if you want to check structure in a specific directory. Directories which are out of the root will not be checked. If you want all the directories of the project to be checked, you don't need to specify root.

Rules syntax

There are 3 ways to specify a rule:

  • the exact path of a directory,
  • * instead of a directory name if any directory accepted on that level,
  • ** instead of a directory name if any directory accepted on any lower level.

For example:

Rule Meaning
hooks white_check_mark  The directory hooks (and files in it) is accepted.
x  Any nested directory is not accepted.
components/* white_check_mark  The directory components is accepted.
white_check_mark  Any first level nested directory is accepted.
x  Any second level nested directory is not accepted.
components/*/utils white_check_mark  The directory components is accepted.
white_check_mark  Any first level nested directory is accepted.
white_check_mark  The second level nested directory utils is accepted.
x  Any other second level nested directory is not accepted.
legacy/** white_check_mark  The directory legacy is accepted.
white_check_mark  Any nested directory on any level is accepted.

warning A rule like components/*/utils automatically make the components and components/* rules work. So, no need to specify a rule for every level directory. You need to specify the deepest path.

warning It's not recommended to overuse ** pattern. It lets absence of structure to sprout in your project. Still it could be useful for some directories which have messy structure by its nature - i.e. node_modules, not maintained legacy directories.

Usage with lint-staged

It is handy to use folderslint together with lint-staged. In this case folderslint checks only the files which were modified for a commit.

For that, add folderslint to the lint-staged section of your package.json.

For example, this is how package.json can look like if you want to run folderslint as a pre-commit hook via husky tool:

"husky": {
  "hooks": {
    "pre-commit": "lint-staged",
  }
},
"lint-staged": {
  "*.{js,ts,tsx}": [
    "folderslint"
  ]
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK