lint-staged.config.js 1.6 KB
Newer Older
1
const path = require('path');
2
const fs = require('fs');
3

4 5 6 7 8 9
const getPackages = filenames =>
    [
        ...new Set(
            filenames.map(filename => path.relative(path.join(__dirname, 'packages'), filename).split(path.sep)[0])
        )
    ].filter(p => p !== '..');
10

11
module.exports = {
12
    // lint all files when global package.json or eslint config changes.
13 14
    './(package.json|.eslintrc.js)': () =>
        `eslint --ext .tsx,.jsx.ts,.js --ignore-path ${path.join(__dirname, '.gitignore')} ${__dirname}`,
15 16

    // check types when ts file or package.json changes.
17
    './(packages/*/package.json|packages/*/**/*.ts?(x))': filenames =>
18
        getPackages(filenames)
19 20 21 22 23 24 25 26
            .map(p => path.join(__dirname, 'packages', p, 'tsconfig.json'))
            .filter(p => {
                try {
                    return fs.statSync(p).isFile();
                } catch (e) {
                    return false;
                }
            })
27 28 29
            .map(p => `tsc -p ${p} --noEmit`),

    // lint changed files
30 31
    '**/*.(j|t)s?(x)': filenames => [
        `eslint ${filenames.join(' ')}`,
32 33 34
        ...getPackages(filenames).map(p => {
            const filename = path.join(__dirname, 'packages', p, 'package.json');
            const packageFile = JSON.parse(fs.readFileSync(filename, 'utf-8'));
35
            if (packageFile.scripts.test.startsWith('jest')) {
36 37 38 39 40 41
                return `yarn workspace @visualdl/${p} run test --silent --bail --findRelatedTests ${filenames.join(
                    ' '
                )}`;
            }
            return `yarn workspace @visualdl/${p} run test`;
        })
42
    ]
43
};