Skip to content

Commit

Permalink
Config: Add support for a config file (#9)
Browse files Browse the repository at this point in the history
- Adds the ability to store audit arguments in a configuration file named css-audit.config.js in the root of the project, fixes #8
- Supports multiple property-values audits from the config

Co-authored-by: Kelly Dwan <ryelle@users.noreply.github.com>
  • Loading branch information
laras126 and ryelle committed Jan 21, 2021
1 parent bd73cdd commit 0df68c9
Show file tree
Hide file tree
Showing 16 changed files with 3,048 additions and 1,553 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ To run this yourself, download or clone this repo, then install the dependencies
$ git clone git@github.com:ryelle/css-audit.git
$ cd css-audit
$ npm install
$ npm run css-audit
$ npm run css-audit -- <files...>
```

If you want to work on the audits yourself, [fork this repo](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) to your account first. You can submit issues or PRs.

## Running Audits

To run the audits, you need a list of CSS files, and to indicate which audits you want to run. `yarn` and `npm` both automatically expand globs (`folder/*`), so you can use that, or pass in a list of CSS files. The audits are described below.
To run the audits, you need a list of CSS files, and to indicate which audits you want to run. `yarn` and `npm` both automatically expand globs (`folder/*`), so you can use that, or pass in a list of CSS files. The audits are described below, and can be run via the following CLI args, or via configuration file (described in the next section).

```
$ npm run css-audit -- <files ...> [options]
Expand All @@ -35,6 +35,26 @@ Usage: css-audit -- <files...> [options]
--help Show this message.
```


### Configuration File

The program will prioritize configuration from CLI arguments, and will fallback to configuration stored in a file called `css-audit.config.js`.

```
module.exports = {
format: 'json',
audits: [
'colors',
'important',
'display-none',
'selectors',
'media-queries',
[ 'property-values', 'font-size' ],
[ 'property-values', 'padding-top,padding-bottom' ],
],
};
```

## Generating HTML Reports

To generate an HTML report, use the `--format=html` option and specify a name for the file with the `--filename=name` option. This will output a `{name}.html` file in public/ that is viewable on Github Pages.
Expand All @@ -45,6 +65,8 @@ For example, generating a report for wp-admin using the below strategy for pulli
npm run css-audit -- v5.5/**/* --format=html --all --filename=wp-admin
```

In the configuration file, the argument `filename` can be added as a simple property: value combination, the same as `format` in the example.

## Getting core CSS files

You can download the source files of CSS (not minified or RTL'd) from the svn repository. The following code will create a new directory, `v5.5`, and download just the files from each `css` folder.
Expand Down
6 changes: 6 additions & 0 deletions css-audit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
format: 'html',
all: true,
filename: 'wp-admin',
audits: [ [ 'property-values', 'font-size' ] ],
};
38 changes: 38 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Node dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );

/**
* Internal dependencies
*/
const { runAudits } = require( './src/run' );
const { getArg, getFileArgsFromCLI, getHelp } = require( './src/utils/cli' );

const input = getFileArgsFromCLI();

if ( getArg( '--help', true ) || ! input.length ) {
console.log( getHelp() );
process.exit( 0 );
}

const cssFiles = [];
input.forEach( ( file ) => {
const filePath = path.resolve( process.env.INIT_CWD, file );
const stats = fs.statSync( filePath );
if ( stats.isDirectory() ) {
return;
}
if ( file.match( /min\.css$/ ) ) {
return;
}
cssFiles.push( {
name: file,
content: String( fs.readFileSync( filePath ) ),
} );
} );

const result = runAudits( cssFiles );

console.log( result );
Loading

0 comments on commit 0df68c9

Please sign in to comment.