Skip to content

Configuration

Moritz Jacobs edited this page Feb 25, 2020 · 3 revisions

configure the CLI

The scriptlint CLI is configured by either

  • CLI command flags or
  • a config object that stems from
    • a scriptlint property in package.json
    • a .scriptlintrc file in JSON or YAML format
    • a .scriptlintrc.json file
    • a .scriptlintrc.yaml, .scriptlintrc.yml, or .scriptlintrc.js file
    • a scriptlint.config.js file exporting a JS object

With a combination of both the CLI command flags take precedence!

Default config

{
	"strict": false,
	"fix": false,
	"config": false,
	"json": false,
	"rules": {},
	"ignoreScripts": [],
	"customRules": []
}

strict

This defines what set of rules will be used. Non-strict means minimum rules, "strict": true uses all of the default rules available. See here for in depth explanation for each rule.

rules

Turn a rule on/off like this:

{
	"strict": true,
	"rules": {
		"uses-allowed-namespace": false
	}
}

json

Output the issues in a json format

[
  {
    "message": "Use of unix double ampersand (&&) in script 'test' is not allowed, consider using npm-run-all/run-s (no-unix-double-ampersand)",
    "type": "warning",
    "affected": "test"
  }
]

config

Print the config as json for debugging purposes.

fix

🚨 This alters the contents of your package.json, only use it in a version controlled environment!

Attempt to fix all found issues. Not every rule has a autofix function ready, but some do.

ignore scripts

{
	"strict": true,
	"ignoreScripts": ["my-funny-scriptname"]
}

Scripts in this list will be exempt from linting.

custom rules

You can add custom rule implementations in your config. customRules is an array of objects like this:

{
	// your custom rule's name
	name: string,
	
	// is the rule looking at an individual script or the whole scripts object
	isObjectRule: boolean,
	
	// the error message if the validation fails for this rule, uses template tags {{name}} (and {{names}} for object rules)
	message: string,
	
	// the validation function for the rule
	// for object rules it returns the failing scripts on fail
	// if isObjectRule === true: (scripts: Object) => true | Array<string>
	// for script rules it returns true or false
	// if isObjectRule === false: (key: string, script: string, scripts: Object) => boolean
	validate: function
	
	// if the problem is autofixable, provide a fix function here
	// for object rules it returns a fixed scripts object
	// for script rules it returns an array with [key: string, value: string]
	fix: function
}

As a speaking example here's a custom config (.scriptlintrc.js) to override the camelCase rule and replace it with a kebab-case one:

module.exports = {
  rules: {
    "correct-casing": false,
    "correct-kebab-casing": true
  },
  customRules: [
    {
      name: "correct-kebab-casing",
      isObjectRule: false,
      message: "`{{name}}`: Script name must be kebab case",
      validate: name => /^[\d:a-z\-]+$/.test(name)
    }
  ]
};


scriptlint status
npm version badge
dependency badge
Issue badge
CI badge

Clone this wiki locally