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/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/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/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/bin/lint b/bin/lint index 288792d..540a82b 100755 --- a/bin/lint +++ b/bin/lint @@ -2,15 +2,15 @@ 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 - --fix ) : + fix ) : FIX=1 ;; - install|init ) : + init ) : INIT=1 ;; update ) : @@ -20,19 +20,23 @@ 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/" . + cp -a "$SRCDIR/scaffolding/." . echo "[@diplodoc/lint] Extend .ignore configuration" node "$SRCDIR/scripts/modify-ignore.js" + + if [[ -z $INIT ]]; then + exit 0 + fi fi 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 @@ -40,17 +44,19 @@ 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}' + if [[ -n $(find . -type f -name '*.css' -name '*.scss' | grep -vwFf .stylelintignore) ]]; then + $BINDIR/stylelint '**/*.{css,scss}' --fix + fi 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 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/eslint-common-config.js b/eslint-common-config.js index d6805b5..fb74ebd 100644 --- a/eslint-common-config.js +++ b/eslint-common-config.js @@ -4,9 +4,17 @@ 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', + '.prettierrc.js', + '.stylelintrc.js', + '.lintstagedrc.js', + ], + env: { + node: true, + }, + }], rules: { 'callback-return': 'off', 'consistent-return': 'off', diff --git a/package.json b/package.json index 99c6580..08b39d4 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,16 @@ "version": "1.0.0", "description": "Diplodoc platform internal utility set for linting", "bin": { - "lint": "./bin/lint" + "lint": "./bin/lint", + "husky": "./bin/husky", + "eslint": "./bin/eslint", + "prettier": "./bin/prettier", + "stylelint": "./bin/stylelint", + "lint-staged": "./bin/lint-staged", + "svgo": "./bin/svgo" }, "scripts": { - "test": "exit 0" + "test": "cd test && npm start" }, "exports": { "./eslint-config": "./eslint-common-config.js", 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'], }; 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 d8986cb..2514d21 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 { @@ -8,20 +10,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('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 diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..0d817e4 --- /dev/null +++ b/test/package.json @@ -0,0 +1,13 @@ +{ + "name": "test", + "private": true, + "workspaces": [ + "../" + ], + "scripts": { + "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..9ad8edf --- /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:fix \ No newline at end of file