-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the ability to --fix in interactive mode (#45)
- Loading branch information
1 parent
7bb6f24
commit 19bbee1
Showing
6 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ConfigOverrides { | ||
setFix(fix) { | ||
this.fix = fix; | ||
} | ||
|
||
getFix() { | ||
return this.fix; | ||
} | ||
} | ||
|
||
const configOverrides = new ConfigOverrides(); | ||
|
||
module.exports = configOverrides; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { getVersion: getJestVersion } = require("jest"); | ||
const configOverrides = require("./configOverrides"); | ||
|
||
const majorJestVersion = parseInt(getJestVersion().split(".")[0], 10); | ||
|
||
/* istanbul ignore if */ | ||
if (majorJestVersion < 23) { | ||
throw new Error(`Insufficient Jest version for jest-runner-stylelint watch plugin | ||
Watch plugins are only available in Jest 23.0.0 and above. | ||
Upgrade your version of Jest in order to use it. | ||
`); | ||
} | ||
|
||
class StylelintWatchFixPlugin { | ||
constructor({ stdout, config }) { | ||
this._stdout = stdout; | ||
this._key = config.key || "F"; | ||
} | ||
|
||
async run() { | ||
const fix = configOverrides.getFix(); | ||
configOverrides.setFix(!fix); | ||
return true; | ||
} | ||
|
||
getUsageInfo() { | ||
const getPrompt = () => { | ||
const fix = configOverrides.getFix(); | ||
if (fix === undefined) { | ||
return "override Stylelint --fix"; | ||
} | ||
if (!fix) { | ||
return "toggle Stylelint --fix (disabled)"; | ||
} | ||
return "toggle Stylelint --fix (enabled)"; | ||
}; | ||
|
||
return { | ||
key: this._key, | ||
prompt: getPrompt() | ||
}; | ||
} | ||
} | ||
|
||
module.exports = StylelintWatchFixPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
jest.useFakeTimers(); | ||
|
||
let WatchFixPlugin; | ||
let configOverrides; | ||
|
||
describe("watchFixPlugin", () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
configOverrides = require("./configOverrides"); | ||
WatchFixPlugin = require("./watchFixPlugin"); | ||
}); | ||
|
||
it("shows the correct prompt", async () => { | ||
const stdout = { write: jest.fn() }; | ||
const config = {}; | ||
const plugin = new WatchFixPlugin({ stdout, config }); | ||
expect(plugin.getUsageInfo()).toEqual({ | ||
key: "F", | ||
prompt: "override Stylelint --fix" | ||
}); | ||
|
||
await plugin.run(plugin); | ||
|
||
expect(plugin.getUsageInfo()).toEqual({ | ||
key: "F", | ||
prompt: "toggle Stylelint --fix (enabled)" | ||
}); | ||
|
||
await plugin.run(plugin); | ||
|
||
expect(plugin.getUsageInfo()).toEqual({ | ||
key: "F", | ||
prompt: "toggle Stylelint --fix (disabled)" | ||
}); | ||
}); | ||
|
||
it("overrides the setting in configOverrides after each invocation", async () => { | ||
const stdout = { write: jest.fn() }; | ||
const config = {}; | ||
const plugin = new WatchFixPlugin({ stdout, config }); | ||
expect(configOverrides.getFix()).toBeUndefined(); | ||
|
||
await plugin.run(plugin); | ||
|
||
expect(configOverrides.getFix()).toBe(true); | ||
|
||
await plugin.run(plugin); | ||
|
||
expect(configOverrides.getFix()).toBe(false); | ||
}); | ||
|
||
it("can customize the key", () => { | ||
const stdout = { write: jest.fn() }; | ||
const config = { key: "z" }; | ||
const plugin = new WatchFixPlugin({ stdout, config }); | ||
expect(plugin.getUsageInfo()).toEqual({ | ||
key: "z", | ||
prompt: "override Stylelint --fix" | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const StylelintWatchFixPlugin = require("./src/watchFixPlugin"); | ||
|
||
module.exports = StylelintWatchFixPlugin; |