Skip to content

Commit

Permalink
Merge pull request #117 from egorAva/feature/cli-options
Browse files Browse the repository at this point in the history
feat: added the ability to add cli options
  • Loading branch information
keplersj authored Feb 8, 2020
2 parents 1247f35 + f5dea8b commit b1fe39a
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 34 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,37 @@ After this run Jest in watch mode and you will see the following line in your wa
```
› Press F to override Stylelint --fix.
```

## Options

This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), so you can provide config via:

- a `jest-runner-stylelint` property in your `package.json`
- a `jest-runner-stylelint.config.js` JS file
- a `.jest-runner-stylelintrc` JSON file

In `package.json`

```json
{
"jest-runner-stylelint": {
"cliOptions": {
// Options here
}
}
}
```

or in `jest-runner-stylelint.config.js`

```js
module.exports = {
cliOptions: {
// Options here
}
};
```

### cliOptions

Follow the [stylelint documentation on configuration](https://stylelint.io/user-guide/cli#options) to create your cli options.
149 changes: 121 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"cosmiconfig": "^6.0.0",
"eslint": "^6.8.0",
"eslint-config-starstuff": "^1.4.5",
"husky": "^4.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/__fixtures__/jest-runner-stylelint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
cliOptions: {
allowEmptyInput: true
}
};
16 changes: 10 additions & 6 deletions src/run.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const { pass, fail } = require("create-jest-runner");
const stylelint = require("stylelint");
const configOverrides = require("./configOverrides");
const getCliOptions = require("./utils/getCliOptions");

module.exports = ({ testPath }) => {
module.exports = ({ testPath, config }) => {
const start = new Date();

const defaultConfig = {
files: testPath,
formatter: "string",
fix: configOverrides.getFix()
};
const { cliOptions = {} } = getCliOptions(config);

return stylelint
.lint({
files: testPath,
formatter: "string",
fix: configOverrides.getFix()
})
.lint(Object.assign({}, cliOptions, defaultConfig))
.then(data => {
if (data.errored) {
return fail({
Expand Down
18 changes: 18 additions & 0 deletions src/utils/__tests__/getCliOptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const getCliOptions = require("../getCliOptions");
const path = require("path");

describe("getCliOptions", () => {
it("check cli options in jest-runner-stylelint.config", () => {
const rootDir = path.resolve(__dirname, "../../__fixtures__");
const config = getCliOptions({ rootDir });

expect(config).toEqual({ cliOptions: { allowEmptyInput: true } });
});

it("check cli options without config", () => {
const rootDir = path.resolve(__dirname, "./");
const config = getCliOptions({ rootDir });

expect(config).toEqual({ cliOptions: {} });
});
});
14 changes: 14 additions & 0 deletions src/utils/__tests__/normalizeConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const normalizeConfig = require("../normalizeConfig");

describe("normalizeConfig", () => {
it("check cliOptions", () => {
const config = normalizeConfig({ cliOptions: { arg: "test arg" } });
expect(config).toEqual({ cliOptions: { arg: "test arg" } });
});

it("check default cliOptions", () => {
const config = normalizeConfig({});

expect(config).toEqual({ cliOptions: {} });
});
});
13 changes: 13 additions & 0 deletions src/utils/getCliOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { cosmiconfigSync } = require("cosmiconfig");
const normalizeConfig = require("./normalizeConfig");

const explorerSync = cosmiconfigSync("jest-runner-stylelint");

const getCliOptions = ({ rootDir }) => {
const result = explorerSync.search(rootDir);
const config = result === null ? {} : result.config;

return normalizeConfig(config);
};

module.exports = getCliOptions;
7 changes: 7 additions & 0 deletions src/utils/normalizeConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const normalizeConfig = config => {
return Object.assign({}, config, {
cliOptions: config.cliOptions || {}
});
};

module.exports = normalizeConfig;

0 comments on commit b1fe39a

Please sign in to comment.