Lightweight, zero-dependency library to lint file paths in a project.
- ❔ But Why?
- ❤ Neat Things
- ⚙ Installing
- 🛠 Configuration
- 🔄 Usage
- 👦 Contributing
- 👨 Authors
- 📄 License
- 📈 Future Improvements
- 📣 Shout-outs
Consistency is always the best teacher
Well, my therapist says that I'm a bit too keen on static analysis. I can't really help it though, so recently, I started to look for a way to enforce a file naming convention for a big software project.
The NPM registry is not overly saturated with solutions to this problem, and the ones I found left me hungry for more. Some way or the other, there was always something bothering me.
For a while, I also wanted to do some open source project on my own, just for the sake
of trying myself in this game.
So here it is!
It is currently ~7kb in size, comes with no dependencies. The size could be smaller, I sacrificed it a bit on the altar of structural well-being.
Enjoy!
- ~7kb package size
- no dependencies
- supports file extension linting
- supports different naming convention per folder
- did I say it's flexible? It uses regular expressions, after all
- 100% test coverage
path-linter
should be added as a devDependency
:
npm install --save-dev @attilagyongyosi/path-linter
or
yarn add --dev @attilagyongyosi/path-linter
path-linter
needs a JSON configuration file somewhere in your project where you specify
your linting rules.
You can specify different linting rules for different directories in your project.
Rules can either be a regular expression or one of the built-in naming conventions that
path-linter
supports out of the box.
Place a file named path-linter.json
, .path-linter.json
or .pathlinterrc
in your project root
and path-linter
will detect them automatically.
If your configuration is placed elsewhere or named otherwise, you can specify
it with the --config <config-file-path>
CLI switch. See 5. Usage.
path-linter
supports the following naming conventions, so you don't need to configure a
regular expression for them:
kebab-case
You can configure linting severity in the top-level severity
configuration property.
It is error
by default which will fail the linting process when file path do not adhere
to configured conventions, or can be warning
to just log warnings on failing files.
There are situations where you want to skip certain parts of a file path and not have them linted.
One common example would be when you want your paths to adhere to kebab-case
naming but you
also use Jest for testing. Jest mocks need to be in a directory called __mocks__
which break
the linting rule.
To accommodate this situation, you can specify an ignore
property for a linting rule config.
This property should be an array of strings and path-linter
will ignore these substrings in file paths.
{
"colorize": true,
"severity": "warning",
"rules": [{
"directory": "src",
"rule": "kebab-case",
"ignore": [ "__tests__", "__mocks__" ]
}, {
"directory": "tests",
"rule": ".*\\.spec\\.ts"
}]
}
You can also find an example in sample-config.json.
Wire it into your NPM scripts in package.json
.
For example:
{
"scripts": {
"lint:paths": "path-linter --config some-config.json --colorize"
}
}
Then you can execute it with
npm run lint:paths
or
yarn lint:paths
--config <path>
Specifies the relative path to a configuration file. If not specified, the library will try to look up a
path-linter.json
file in the project root.
--colorize
Enables colorization for the console output.
This can also be set in the configuration file's colorize
property.
Feel free to open issues or pull requests if you have some improvement on the library. I'm open to everything!
- Attila Gyöngyösi - GitHub
This project is licensed under the MIT License - see the LICENSE.md file for details
- Support for programmatic usage
- Support for opt-in path auto-fixing
- Support for
camelCase
,dot.notation
andsnake_case
- Thanks pamepeixinho for the Jest coverage badges!