From 90d0086e5c1ed6aa6f61ccbcbc116a35252a07ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Tue, 9 May 2023 12:42:03 +0200 Subject: [PATCH 1/7] feat: add better regex checking BEM convention --- stylelint-config-selleo/bem/.stylelintrc.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stylelint-config-selleo/bem/.stylelintrc.json b/stylelint-config-selleo/bem/.stylelintrc.json index 1b86377..1d856ca 100644 --- a/stylelint-config-selleo/bem/.stylelintrc.json +++ b/stylelint-config-selleo/bem/.stylelintrc.json @@ -1,7 +1,7 @@ { "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-z0-9]+(-[a-z0-9]+)*)\\s*\\{\\s*\n\\s*&__([a-z0-9]+(-[a-z0-9]+)*)\\s*\\{\\s*\n\\s*&\\.-([a-z0-9]+(-[a-z0-9]+)*)\\s*\\{+$", { "message": "Please follow BEM naming conventions", "resolveNestedSelectors": true @@ -9,3 +9,4 @@ ] } } + From e9527a3dae1069b2316eb64591a193135c9d0ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Tue, 9 May 2023 15:02:20 +0200 Subject: [PATCH 2/7] feat: add better regex checking BEM convention --- stylelint-config-selleo/bem/.stylelintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stylelint-config-selleo/bem/.stylelintrc.json b/stylelint-config-selleo/bem/.stylelintrc.json index 1d856ca..cb35fb9 100644 --- a/stylelint-config-selleo/bem/.stylelintrc.json +++ b/stylelint-config-selleo/bem/.stylelintrc.json @@ -1,7 +1,7 @@ { "rules": { "selector-class-pattern": [ - "^\\.?([a-z0-9]+(-[a-z0-9]+)*)\\s*\\{\\s*\n\\s*&__([a-z0-9]+(-[a-z0-9]+)*)\\s*\\{\\s*\n\\s*&\\.-([a-z0-9]+(-[a-z0-9]+)*)\\s*\\{+$", + "^\\.([a-z]+(-[a-z]+)*)(\\s*\\{(\\s*&__([a-z]+(-[a-z]+)*)\\s*(-[a-z]+)?\\s*)*\\})?$", { "message": "Please follow BEM naming conventions", "resolveNestedSelectors": true From 1c0d3bee6f146137a5a11b97c5f57ba147ec9bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Tue, 9 May 2023 15:32:01 +0200 Subject: [PATCH 3/7] feat: add cli commands that checks if some style issues occure --- src/commands/check-all.ts | 15 +++++++++++++++ src/commands/check-current.ts | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/commands/check-all.ts create mode 100644 src/commands/check-current.ts diff --git a/src/commands/check-all.ts b/src/commands/check-all.ts new file mode 100644 index 0000000..205d5c7 --- /dev/null +++ b/src/commands/check-all.ts @@ -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 { + const listAllIssues = `stylelint **/*.{scss,css}`; + + exec(listAllIssues, (error, stdout) => { + console.error(stdout); + if (error) throw new Error("Some style issues ocurred"); + }); + } +} diff --git a/src/commands/check-current.ts b/src/commands/check-current.ts new file mode 100644 index 0000000..bd266ee --- /dev/null +++ b/src/commands/check-current.ts @@ -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 { + const currenBranchCmd = `git branch --no-color | grep -E '^\*'`; + const listCurrentIssues = `git diff $(${currenBranchCmd}) --name-only --diff-filter=AM -- '*.scss' '*.css' | tail | xargs -r stylelint`; + + exec(listCurrentIssues, (error, stdout) => { + console.error(stdout); + if (error) throw new Error("Some style issues ocurred"); + }); + } +} From c386bb128f0883187609cb183e3febddaf6affbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Tue, 9 May 2023 16:25:59 +0200 Subject: [PATCH 4/7] feat: add cli command that automatically inits husky and creates pre-commit hook --- src/commands/husky-init.ts | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/commands/husky-init.ts diff --git a/src/commands/husky-init.ts b/src/commands/husky-init.ts new file mode 100644 index 0000000..fe9dccf --- /dev/null +++ b/src/commands/husky-init.ts @@ -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 { + 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 { + const file = await readFile( + `${this._absolutePath}/${this._packageJsonFile}`, + { encoding: "utf8" } + ); + + return JSON.parse(file); + } + + private async _writeFile(file: string): Promise { + await writeFile(`${this._absolutePath}/${this._packageJsonFile}`, file); + } +} From 23f31804767de6c460340d5c87b12f988f734e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Wed, 10 May 2023 09:42:12 +0200 Subject: [PATCH 5/7] chore: update readme --- README.md | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 738480a..064f812 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,23 @@ This will create a `.tgz` file that you need to link to in the target repository - select appropriate extensions - in your project root directory `.stylelintrc.json` file will appear with previously selected extensions +--- +## 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 +``` +--- + If you use `prettier`: Create or update existing `.prettierignore` following rule - ignore prettier for css, sccs and sass files - use stylelint by default ``` @@ -84,16 +101,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. -``` From 47d867f0e1dae3d0bd5f780997a4369bb4fe502b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BBupa?= Date: Wed, 10 May 2023 10:48:59 +0200 Subject: [PATCH 6/7] feat: add stylelin support for .sass extension --- src/commands/check-all.ts | 2 +- src/commands/check-current.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/check-all.ts b/src/commands/check-all.ts index 205d5c7..a0a4434 100644 --- a/src/commands/check-all.ts +++ b/src/commands/check-all.ts @@ -5,7 +5,7 @@ export default class CheckAll extends Command { static description = "Check if some stylelint issues occur for all files"; public async run(): Promise { - const listAllIssues = `stylelint **/*.{scss,css}`; + const listAllIssues = `stylelint **/*.{scss,css,sass}`; exec(listAllIssues, (error, stdout) => { console.error(stdout); diff --git a/src/commands/check-current.ts b/src/commands/check-current.ts index bd266ee..ff790f6 100644 --- a/src/commands/check-current.ts +++ b/src/commands/check-current.ts @@ -7,7 +7,7 @@ export default class CheckCurrent extends Command { public async run(): Promise { const currenBranchCmd = `git branch --no-color | grep -E '^\*'`; - const listCurrentIssues = `git diff $(${currenBranchCmd}) --name-only --diff-filter=AM -- '*.scss' '*.css' | tail | xargs -r stylelint`; + const listCurrentIssues = `git diff $(${currenBranchCmd}) --name-only --diff-filter=AM -- '*.scss' '*.css' '*.sass | tail | xargs -r stylelint`; exec(listCurrentIssues, (error, stdout) => { console.error(stdout); From 5c604be68b744265a98ca931e48ad4aac9c16ed0 Mon Sep 17 00:00:00 2001 From: Arek Janik Date: Wed, 10 May 2023 11:25:25 +0200 Subject: [PATCH 7/7] add readme about husky --- README.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 064f812..2bf5e0b 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,18 @@ This will create a `.tgz` file that you need to link to in the target repository - select appropriate extensions - in your project root directory `.stylelintrc.json` file will appear with previously selected extensions ---- +If you use `prettier`: +Create or update existing `.prettierignore` following rule - ignore prettier for css, sccs and sass files - use stylelint by default +``` +**/*.css +**/*.scss +**/*.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 +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 @@ -47,15 +55,10 @@ Run `npm run stylelint-config-selleo [command]` to run a command `list-current` List styles issues for currently changed files `help` Display help for stylelint-config-selleo ``` ---- -If you use `prettier`: -Create or update existing `.prettierignore` following rule - ignore prettier for css, sccs and sass files - use stylelint by default -``` -**/*.css -**/*.scss -**/*.sass -``` +### Install pre-commit check +- `npm i husky` +- `npm run stylelint-config-selleo husky-init` --- # IDE setup