Creating checks
Checks can created to standardize any existing configuration or workflow.
All checks have two required properties:
message
: The default message that will be shown for the check in the CLI and Commonality Studiovalidate
: A function that returns a boolean or a promise that resolves to a boolean. This function is used to determine if the check passes or fails.
Check context
Checks are run within the context of each package directory. In the example below, we’re accessing the context object that contains metadata about the package to validate that it has a codeowner.
Checking files
Commonality provides utilities like json and text that make it easy to read and write to files within checks.
Below is an example of a check that ensures a package has a build
script with a value of tsc --build
in it’s package.json
.
The following output will be shown if the check fails:
Customizing messages
The default message can be overriden by returning an object from the validate function to provide additional context about a failed check.
We can modify the previous example to provide a more detailed error messages.
The diff utility can be used to suggest file edits needed to pass checks. The suggestion
property is also useful for showing how a fix function will modify a file.
The diff function will return a pretty printed string where unique properties and values in the second argument appear in red and are prefixed with +
.”
The following output will be shown if the check fails:
Composing checks
Since checks are just objects, you can create functions that return checks to make them re-usable in a variety of different scenarios.
Previously, we wrote a lot of code just to validate a single script existed, now we can re-use this check to validate the existence of any script.
Auto-fixable checks
Check failures help enforce standardization at scale but can add friction to developer workflows. To solve this, you can make any check auto-fixable, making it easy for developers to adhere to standards without needing to manually fix issues.
To make a check fixable just add a fix property to your check. This function will only run against a package if the result of validate is not true
.
Commonality provides utilities like json, and text to make it easy to read and write files within fix functions.
Was this page helpful?