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

chore: Add tests #3

Merged
merged 12 commits into from
Aug 11, 2024
Merged
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
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/CONTRIBUTING.md
/CONTRIBUTING.md
/test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

Expand Down
11 changes: 11 additions & 0 deletions bin/eslint
Original file line number Diff line number Diff line change
@@ -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 $@
11 changes: 11 additions & 0 deletions bin/husky
Original file line number Diff line number Diff line change
@@ -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 $@
36 changes: 21 additions & 15 deletions bin/lint
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) :
Expand All @@ -20,37 +20,43 @@ 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

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
11 changes: 11 additions & 0 deletions bin/lint-staged
Original file line number Diff line number Diff line change
@@ -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 $@
11 changes: 11 additions & 0 deletions bin/prettier
Original file line number Diff line number Diff line change
@@ -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 $@
11 changes: 11 additions & 0 deletions bin/stylelint
Original file line number Diff line number Diff line change
@@ -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 $@
11 changes: 11 additions & 0 deletions bin/svgo
Original file line number Diff line number Diff line change
@@ -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 $@
14 changes: 11 additions & 3 deletions eslint-common-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions scaffolding/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -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'],
};
5 changes: 3 additions & 2 deletions scripts/modify-ignore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {readFileSync, writeFileSync} = require('fs');
const {join} = require('node:path');
const {readFileSync, writeFileSync} = require('node:fs');

const SYSTEM = [
'.idea',
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions scripts/modify-package.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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');
13 changes: 13 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test",
"private": true,
"workspaces": [
"../"
],
"scripts": {
"start": "sh ./test.sh"
},
"dependencies": {
"@diplodoc/lint": "^1.0.0"
}
}
7 changes: 7 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

npm i @diplodoc/lint
npx @diplodoc/lint init
npm run lint:fix
Loading