From d667d9dc39910b6b94bdb8c0f5d0566b62a5ac86 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 02:18:44 +0300 Subject: [PATCH 01/12] chore: Add tests --- .github/workflows/tests.yml | 32 ++++++++++++++++++++++++++++++++ .npmignore | 3 ++- bin/lint | 2 +- scripts/modify-package.js | 2 +- test/package.json | 13 +++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/tests.yml create mode 100644 test/package.json diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..cb65815 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,32 @@ +name: Tests + +on: + push: + branches: [master] + pull_request: + branches: ['**'] + +jobs: + test: + name: Node v${{ matrix.node-version }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + node-version: [18.x] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + os: ${{ matrix.os }} + cache: 'npm' + - name: Install packages for project + run: npm ci + - name: Run tests + run: | + cd test + npm start diff --git a/.npmignore b/.npmignore index 224f385..27603c5 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ -/CONTRIBUTING.md \ No newline at end of file +/CONTRIBUTING.md +/test \ No newline at end of file diff --git a/bin/lint b/bin/lint index 288792d..b874582 100755 --- a/bin/lint +++ b/bin/lint @@ -7,7 +7,7 @@ SRCDIR=$(dirname $(dirname $(node -pe "require('fs').realpathSync('$0')"))) while (( $# )); do case "$1" in - --fix ) : + fix ) : FIX=1 ;; install|init ) : diff --git a/scripts/modify-package.js b/scripts/modify-package.js index d8986cb..7c81d39 100644 --- a/scripts/modify-package.js +++ b/scripts/modify-package.js @@ -20,7 +20,7 @@ function configure(command, impl) { } configure('lint', 'lint init && lint'); -configure('lint:fix', 'lint init && lint --fix'); +configure('lint:fix', 'lint init && lint fix'); configure('pre-commit', 'lint init && lint-staged'); configure('prepare', 'husky install || true'); diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..9ea0015 --- /dev/null +++ b/test/package.json @@ -0,0 +1,13 @@ +{ + "name": "test", + "private": true, + "workspaces": [ + "../" + ], + "scripts": { + "start": "npm i @diplodoc/lint && npx @diplodoc/lint init" + }, + "dependencies": { + "@diplodoc/lint": "^1.0.0" + } +} From 74e350c38233cc8e2a28c19a138f81991cac3014 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 02:20:34 +0300 Subject: [PATCH 02/12] fix: Fix install script --- bin/lint | 4 ++-- scripts/modify-package.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/lint b/bin/lint index b874582..e7bb93d 100755 --- a/bin/lint +++ b/bin/lint @@ -20,7 +20,7 @@ while (( $# )); do shift done -if [[ -n "$INIT$UPDATE"]]; then +if [[ -n "$INIT$UPDATE" ]]; then echo "[@diplodoc/lint] Add initial lint configs" cp -r "$SRCDIR/scaffolding/" . @@ -32,7 +32,7 @@ if [[ -n $INIT ]]; then echo "[@diplodoc/lint] Extend package.json configuration" node "$SRCDIR/scripts/modify-package.js" - $BINDIR/husky install + $BINDIR/husky init exit 0 fi diff --git a/scripts/modify-package.js b/scripts/modify-package.js index 7c81d39..f34b31a 100644 --- a/scripts/modify-package.js +++ b/scripts/modify-package.js @@ -8,20 +8,20 @@ try { throw 'Unable to modify ' + filename; } -function configure(command, impl) { +function configure(command, impl, strict = true) { if (pkg.scripts[command]) { - if (pkg.scripts[command] !== impl) { + if (pkg.scripts[command] !== impl && strict) { throw `Lint command '${command}' already configured with different program`; } } else { pkg.scripts[command] = impl; - console.log('=> Add', command, 'script'); + console.log('[@diplodoc/lint]', '=> Add', command, 'script'); } } configure('lint', 'lint init && lint'); configure('lint:fix', 'lint init && lint fix'); configure('pre-commit', 'lint init && lint-staged'); -configure('prepare', 'husky install || true'); +configure('prepare', 'husky install || true', false); writeFileSync(filename, JSON.stringify(pkg, null, 2), 'utf8'); \ No newline at end of file From b82b393cb14fe9ef474bf32645f0aae5f00044c4 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 02:20:57 +0300 Subject: [PATCH 03/12] fix: Proxy husky bin --- bin/husky | 11 +++++++++++ package.json | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100755 bin/husky diff --git a/bin/husky b/bin/husky new file mode 100755 index 0000000..99cc16b --- /dev/null +++ b/bin/husky @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('husky')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'husky' not found" + exit 1 +fi + +# Redirect to original husky bin +$BINDIR/bin.js $@ \ No newline at end of file diff --git a/package.json b/package.json index 99c6580..df6b9df 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,11 @@ "version": "1.0.0", "description": "Diplodoc platform internal utility set for linting", "bin": { - "lint": "./bin/lint" + "lint": "./bin/lint", + "husky": "./bin/husky" }, "scripts": { - "test": "exit 0" + "test": "cd test && npm start" }, "exports": { "./eslint-config": "./eslint-common-config.js", From 379715b295b4e5a27b787fea4dd3c3ef41c8ab22 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 02:21:53 +0300 Subject: [PATCH 04/12] fix: Remove useless install command --- README.md | 2 +- bin/lint | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 95c0427..ea60b3a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ npm install --save-dev @diplodoc/lint Add initial configuration ```sh -npx @diplodoc/lint install +npx @diplodoc/lint init git add --all && git commit -m 'chore: init lint' ``` diff --git a/bin/lint b/bin/lint index e7bb93d..40836af 100755 --- a/bin/lint +++ b/bin/lint @@ -10,7 +10,7 @@ while (( $# )); do fix ) : FIX=1 ;; - install|init ) : + init ) : INIT=1 ;; update ) : From f1f8ba414b037a3a733bcf5ad218eb495f76b94a Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 02:26:35 +0300 Subject: [PATCH 05/12] fix: Fix windows specific --- bin/lint | 4 ++-- scripts/modify-ignore.js | 5 +++-- scripts/modify-package.js | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bin/lint b/bin/lint index 40836af..754e65d 100755 --- a/bin/lint +++ b/bin/lint @@ -2,8 +2,8 @@ set -e -BINDIR=$(dirname $0) -SRCDIR=$(dirname $(dirname $(node -pe "require('fs').realpathSync('$0')"))) +BINDIR=$(dirname "$0") +SRCDIR=$(dirname $(dirname $(node -pe "require('fs').realpathSync('${0//\\//}')"))) while (( $# )); do case "$1" in diff --git a/scripts/modify-ignore.js b/scripts/modify-ignore.js index 4369684..a48565a 100644 --- a/scripts/modify-ignore.js +++ b/scripts/modify-ignore.js @@ -1,4 +1,5 @@ -const {readFileSync, writeFileSync} = require('fs'); +const {join} = require('node:path'); +const {readFileSync, writeFileSync} = require('node:fs'); const SYSTEM = [ '.idea', @@ -49,7 +50,7 @@ const ignores = { }; for (const [file, list] of Object.entries(ignores)) { - const filename = process.cwd() + '/' + file; + const filename = join(process.cwd(), file); let source; try { diff --git a/scripts/modify-package.js b/scripts/modify-package.js index f34b31a..08b345d 100644 --- a/scripts/modify-package.js +++ b/scripts/modify-package.js @@ -1,5 +1,7 @@ -const filename = process.cwd() + '/package.json'; -const {readFileSync, writeFileSync} = require('fs'); +const {join} = require('node:path'); +const {readFileSync, writeFileSync} = require('node:fs'); + +const filename = join(process.cwd(), 'package.json'); let pkg; try { From 68f6229a6909c218cc2dfd6dd6968b170e6624a2 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:46:39 +0300 Subject: [PATCH 06/12] fix: Use update instead of init on lint run --- scripts/modify-package.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/modify-package.js b/scripts/modify-package.js index 08b345d..2514d21 100644 --- a/scripts/modify-package.js +++ b/scripts/modify-package.js @@ -21,9 +21,9 @@ function configure(command, impl, strict = true) { } } -configure('lint', 'lint init && lint'); -configure('lint:fix', 'lint init && lint fix'); -configure('pre-commit', 'lint init && lint-staged'); +configure('lint', 'lint update && lint'); +configure('lint:fix', 'lint update && lint fix'); +configure('pre-commit', 'lint update && lint-staged'); configure('prepare', 'husky install || true', false); writeFileSync(filename, JSON.stringify(pkg, null, 2), 'utf8'); \ No newline at end of file From 2e3aaf9104fd3977890d3bf9de6932ae93a6fe4d Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:47:41 +0300 Subject: [PATCH 07/12] fix: Skip useless css linting for node projects --- bin/lint | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/bin/lint b/bin/lint index 754e65d..242c713 100755 --- a/bin/lint +++ b/bin/lint @@ -26,6 +26,10 @@ if [[ -n "$INIT$UPDATE" ]]; then echo "[@diplodoc/lint] Extend .ignore configuration" node "$SRCDIR/scripts/modify-ignore.js" + + if [[ -z $INIT ]]; then + exit 0 + fi fi if [[ -n $INIT ]]; then @@ -40,17 +44,17 @@ fi if [[ -n $FIX ]]; then echo "Run linters in fix mode" - $BINDIR/eslint '**/*.{js,jsx,ts,tsx}' --fix - $BINDIR/prettier --write 'src/**/*.{js,jsx,ts,tsx}' - $BINDIR/stylelint src/**/*.scss --fix + $BINDIR/eslint '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' --fix + $BINDIR/prettier --write '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' + $BINDIR/stylelint '**/*.{css,scss}' --fix exit 0 fi echo "Run linters" -$BINDIR/eslint '**/*.{js,jsx,ts,tsx}' -$BINDIR/prettier --check 'src/**/*.{js,jsx,ts,tsx}' -$BINDIR/stylelint src/**/*.scss - - +$BINDIR/eslint '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' +$BINDIR/prettier --check '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' +if [[ -n $(find . -type f -name '*.css' -name '*.scss' | grep -vwFf .stylelintignore) ]]; then + $BINDIR/stylelint '**/*.{css,scss}' +fi From d9ce4499a0a7ecfe7d39a9a0b42a0e15ecc34500 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:48:19 +0300 Subject: [PATCH 08/12] fix: Add special rules for eslintrc.json --- eslint-common-config.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/eslint-common-config.js b/eslint-common-config.js index d6805b5..1744d83 100644 --- a/eslint-common-config.js +++ b/eslint-common-config.js @@ -4,9 +4,12 @@ module.exports = { '@gravity-ui/eslint-config', process.env.npm_command && '@gravity-ui/eslint-config/prettier', ].filter(Boolean), - parserOptions: { - project: ["./tsconfig.json"] - }, + overrides: [{ + files: ['.eslintrc.js'], + env: { + node: true, + }, + }], rules: { 'callback-return': 'off', 'consistent-return': 'off', From 51cce83643ca41a87a7a2bca158b688bba2a59de Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:49:12 +0300 Subject: [PATCH 09/12] fix: Extend lint-staged files lists --- scaffolding/.lintstagedrc.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scaffolding/.lintstagedrc.js b/scaffolding/.lintstagedrc.js index 3223be6..f3822d0 100644 --- a/scaffolding/.lintstagedrc.js +++ b/scaffolding/.lintstagedrc.js @@ -1,6 +1,6 @@ module.exports = { - '**/*.{js,jsx,ts,tsx}': ['prettier --write', 'eslint --max-warnings=0 --fix'], + '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}': ['prettier --write', 'eslint --max-warnings=0 --fix'], '**/*.{css,scss}': ['prettier --write', 'stylelint --fix'], '**/*.{json,yaml,yml,md}': ['prettier --write'], - '**/*.{svg}': ['npm run svgo'], + '**/*.{svg}': ['svgo'], }; From 86133c736f2ad8278828e5e80687d66c0ee99ae2 Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:49:56 +0300 Subject: [PATCH 10/12] fix: Proxy all lint binary --- bin/eslint | 11 +++++++++++ bin/lint-staged | 11 +++++++++++ bin/prettier | 11 +++++++++++ bin/stylelint | 11 +++++++++++ bin/svgo | 11 +++++++++++ package.json | 7 ++++++- 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 bin/eslint create mode 100755 bin/lint-staged create mode 100755 bin/prettier create mode 100755 bin/stylelint create mode 100755 bin/svgo diff --git a/bin/eslint b/bin/eslint new file mode 100755 index 0000000..0471f53 --- /dev/null +++ b/bin/eslint @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('eslint/package.json')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'eslint' not found" + exit 1 +fi + +# Redirect to original eslint bin +$BINDIR/bin/eslint.js $@ \ No newline at end of file diff --git a/bin/lint-staged b/bin/lint-staged new file mode 100755 index 0000000..1b1390d --- /dev/null +++ b/bin/lint-staged @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('lint-staged/package.json')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'lint-staged' not found" + exit 1 +fi + +# Redirect to original lint-staged bin +$BINDIR/bin/lint-staged.js $@ \ No newline at end of file diff --git a/bin/prettier b/bin/prettier new file mode 100755 index 0000000..889d62f --- /dev/null +++ b/bin/prettier @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('prettier')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'prettier' not found" + exit 1 +fi + +# Redirect to original prettier bin +$BINDIR/bin/prettier.cjs $@ \ No newline at end of file diff --git a/bin/stylelint b/bin/stylelint new file mode 100755 index 0000000..9b16901 --- /dev/null +++ b/bin/stylelint @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('stylelint/package.json')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'stylelint' not found" + exit 1 +fi + +# Redirect to original stylelint bin +$BINDIR/bin/stylelint.mjs $@ \ No newline at end of file diff --git a/bin/svgo b/bin/svgo new file mode 100755 index 0000000..515e45e --- /dev/null +++ b/bin/svgo @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +BINDIR=$(dirname $(node -pe "require.resolve('svgo/package.json')")) + +if [[ -z "$BINDIR" ]]; then + echo "Required package 'svgo' not found" + exit 1 +fi + +# Redirect to original svgo bin +$BINDIR/bin/svgo $@ \ No newline at end of file diff --git a/package.json b/package.json index df6b9df..08b39d4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,12 @@ "description": "Diplodoc platform internal utility set for linting", "bin": { "lint": "./bin/lint", - "husky": "./bin/husky" + "husky": "./bin/husky", + "eslint": "./bin/eslint", + "prettier": "./bin/prettier", + "stylelint": "./bin/stylelint", + "lint-staged": "./bin/lint-staged", + "svgo": "./bin/svgo" }, "scripts": { "test": "cd test && npm start" From 41a92d263be791cededfe407dd8380f1e8023e8e Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 04:50:32 +0300 Subject: [PATCH 11/12] chore: Add lint run tests --- test/package.json | 2 +- test/test.sh | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100755 test/test.sh diff --git a/test/package.json b/test/package.json index 9ea0015..0d817e4 100644 --- a/test/package.json +++ b/test/package.json @@ -5,7 +5,7 @@ "../" ], "scripts": { - "start": "npm i @diplodoc/lint && npx @diplodoc/lint init" + "start": "sh ./test.sh" }, "dependencies": { "@diplodoc/lint": "^1.0.0" diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..b4ec026 --- /dev/null +++ b/test/test.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +npm i @diplodoc/lint +npx @diplodoc/lint init +npm run lint \ No newline at end of file From 498e8cdd85acd7e95923c2387017608754ba3b6c Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Sun, 11 Aug 2024 05:20:46 +0300 Subject: [PATCH 12/12] fix: Fix Windows specific --- bin/lint | 6 ++++-- eslint-common-config.js | 7 ++++++- test/test.sh | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/lint b/bin/lint index 242c713..540a82b 100755 --- a/bin/lint +++ b/bin/lint @@ -22,7 +22,7 @@ done if [[ -n "$INIT$UPDATE" ]]; then echo "[@diplodoc/lint] Add initial lint configs" - cp -r "$SRCDIR/scaffolding/" . + cp -a "$SRCDIR/scaffolding/." . echo "[@diplodoc/lint] Extend .ignore configuration" node "$SRCDIR/scripts/modify-ignore.js" @@ -46,7 +46,9 @@ if [[ -n $FIX ]]; then $BINDIR/eslint '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' --fix $BINDIR/prettier --write '**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}' - $BINDIR/stylelint '**/*.{css,scss}' --fix + if [[ -n $(find . -type f -name '*.css' -name '*.scss' | grep -vwFf .stylelintignore) ]]; then + $BINDIR/stylelint '**/*.{css,scss}' --fix + fi exit 0 fi diff --git a/eslint-common-config.js b/eslint-common-config.js index 1744d83..fb74ebd 100644 --- a/eslint-common-config.js +++ b/eslint-common-config.js @@ -5,7 +5,12 @@ module.exports = { process.env.npm_command && '@gravity-ui/eslint-config/prettier', ].filter(Boolean), overrides: [{ - files: ['.eslintrc.js'], + files: [ + '.eslintrc.js', + '.prettierrc.js', + '.stylelintrc.js', + '.lintstagedrc.js', + ], env: { node: true, }, diff --git a/test/test.sh b/test/test.sh index b4ec026..9ad8edf 100755 --- a/test/test.sh +++ b/test/test.sh @@ -4,4 +4,4 @@ set -e npm i @diplodoc/lint npx @diplodoc/lint init -npm run lint \ No newline at end of file +npm run lint:fix \ No newline at end of file