Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10 block commiting if the linter threw some warning #20

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ Create or update existing `.prettierignore` following rule - ignore prettier for
**/*.sass
```

## CLI / CI Commands
Run `npm run stylelint-config-selleo` to see all available commands.

Run `npm run stylelint-config-selleo [command]` to run a command.
```
`init` Initialize the Stylelint Selleo Config by selecting extensions
`husky-init` Init husky pre-commit check
`check-all` Check if some stylelint issues occur for all files
`check-current` Check if some stylelint issues occur for current changes
`fix-all` Run auto fix for all changed files
`fix-current` Run auto fix for currently changed files
`list-all` List all styles issues
`list-current` List styles issues for currently changed files
`help` Display help for stylelint-config-selleo
```

### Install pre-commit check
- `npm i husky`
- `npm run stylelint-config-selleo husky-init`

---
# IDE setup

Expand Down Expand Up @@ -84,16 +104,3 @@ not work on file save, but you can instead run it by `Right Click > External Too
5. Set `Arguments` to `$FilePath$ --fix`
6. Set `Working directory` to `$ProjectFileDir$`
7. Save and Apply changes

---
# CLI / CI
Run `npm run stylelint-config-selleo` to see all available commands:
```
fix-all Run auto fix for all changed files
fix-current Run auto fix for currently changed files
help Display help for stylelint-config-selleo.
init Initialize the Stylelint Selleo Config by selecting extensions
list-all List all styles issues
list-current List styles issues for currently changed files
plugins List installed plugins.
```
15 changes: 15 additions & 0 deletions src/commands/check-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Command } from "@oclif/core";
import { exec } from "child_process";

export default class CheckAll extends Command {
static description = "Check if some stylelint issues occur for all files";

public async run(): Promise<void> {
const listAllIssues = `stylelint **/*.{scss,css,sass}`;

exec(listAllIssues, (error, stdout) => {
console.error(stdout);
if (error) throw new Error("Some style issues ocurred");
});
}
}
17 changes: 17 additions & 0 deletions src/commands/check-current.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command } from "@oclif/core";
import { exec } from "child_process";

export default class CheckCurrent extends Command {
static description =
"Check if some stylelint issues occur for current changes";

public async run(): Promise<void> {
const currenBranchCmd = `git branch --no-color | grep -E '^\*'`;
const listCurrentIssues = `git diff $(${currenBranchCmd}) --name-only --diff-filter=AM -- '*.scss' '*.css' '*.sass | tail | xargs -r stylelint`;

exec(listCurrentIssues, (error, stdout) => {
console.error(stdout);
if (error) throw new Error("Some style issues ocurred");
});
}
}
56 changes: 56 additions & 0 deletions src/commands/husky-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Command } from "@oclif/core";
import { exec } from "child_process";
import { readFile, writeFile } from "fs/promises";

type Scripts = {
scripts: {
[key: string]: string;
};
};

export default class HuskyInit extends Command {
static description = "Init husky pre-commit check";
private readonly _packageJsonFile = "package.json";
private readonly _absolutePath = `${process.cwd()}`;

public async run(): Promise<void> {
this._installHusky();
this._enableGitHooks();
const packageJsonFile = await this._readFile();
this._addHuskyScript(packageJsonFile);
this._writeFile(JSON.stringify(packageJsonFile, null, 2));
this._createHook();
}

private _installHusky(): void {
const cmd = `npm install husky --save-dev`;
exec(cmd);
}

private _enableGitHooks(): void {
const cmd = `npx husky install`;
exec(cmd);
}

private _addHuskyScript(packageJsonFile: Scripts): void {
packageJsonFile.scripts["prepare"] = "husky install";
}

private _createHook(): void {
const cmd = `npx husky add .husky/pre-commit "npm run stylelint-config-selleo check-current"`;
exec(cmd);
}

private async _readFile(): Promise<Scripts> {
const file = await readFile(
`${this._absolutePath}/${this._packageJsonFile}`,
{ encoding: "utf8" }
);

return JSON.parse(file);
}

private async _writeFile(file: string): Promise<void> {
await writeFile(`${this._absolutePath}/${this._packageJsonFile}`, file);
}
}
3 changes: 2 additions & 1 deletion stylelint-config-selleo/bem/.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"rules": {
"selector-class-pattern": [
"^([a-z0-9']+)-([a-z0-9']+)$|^-([a-z][a-z0-9]*)(-[a-z0-9]+)*$|^([a-z0-9']+)$|^([a-z0-9']+)__([a-z0-9']+)$",
"^\\.([a-z]+(-[a-z]+)*)(\\s*\\{(\\s*&__([a-z]+(-[a-z]+)*)\\s*(-[a-z]+)?\\s*)*\\})?$",
{
"message": "Please follow BEM naming conventions",
"resolveNestedSelectors": true
}
]
}
}