Skip to content

Commit

Permalink
feat: improve file name matching (#395)
Browse files Browse the repository at this point in the history
Allows the user to pass directory, filenames, and globs to the "check"
and "apply" commands:

    yarn d2-style check *.js
    yarn d2-style check .
    yarn d2-style check src
    yarn d2-style check src/**/*.spec.js

Note that the first example "*.js" means that basename matching is
enabled so that if a file has a '/' in the path, it will still be a
successful match.

This allows us to simplify the patterns in d2style.config.js by saying
that:

    patterns.js = '*.js'

Instead of:

    patterns.js = '**/*.js'

On the question why we aren't just passing along the argument as-is to
eslint and prettier (they support "dir|file|glob" in their clis) the
answer is two-fold:

    1.  We want to control which files are passed to the tool to avoid
        e.g. Prettier checking non-js files that it supports when we run
        "check js" for example.

    2.  We want to be able to only run checks against staged files,
        which means we have to filter out unstaged files from the list
        of files we pass to each tool.
  • Loading branch information
varl authored May 7, 2021
1 parent f643a02 commit 77ff9ef
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
4 changes: 2 additions & 2 deletions config/d2-style.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
commitlint: [],
},
patterns: {
js: '**/*.{js,jsx,ts,tsx}',
text: '**/*.{md,json,yml,html}',
js: '*.{js,jsx,ts,tsx}',
text: '*.{md,json,yml,html}',
},
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"find-up": "^5.0.0",
"fs-extra": "^9.1.0",
"husky": "^5.2.0",
"micromatch": "^4.0.4",
"perfy": "^1.1.5",
"prettier": "^2.2.1",
"semver": "^7.3.4",
Expand Down
4 changes: 1 addition & 3 deletions src/commands/types/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ exports.handler = (argv, callback) => {
const jsFiles = selectFiles(files, jsPattern, staged)

if (jsFiles.length === 0) {
log.warn(sayNoFiles('javascript', jsPattern, staged))
log.debug(sayNoFiles('javascript', jsPattern, staged))
return
}

log.debug(`Linting files: ${jsFiles.join(', ')}`)

log.info('javascript > eslint')
eslint({
apply,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/types/structured-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.handler = (argv, callback) => {

const textFiles = selectFiles(files, textPattern, staged)
if (textFiles.length === 0) {
log.warn(sayNoFiles('structured text', textPattern, staged))
log.debug(sayNoFiles('structured text', textPattern, staged))
return
}

Expand Down
47 changes: 7 additions & 40 deletions src/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path')
const log = require('@dhis2/cli-helpers-engine').reporter
const fg = require('fast-glob')
const fs = require('fs-extra')
const mm = require('micromatch')
const { CONSUMING_ROOT, PROJECT_ROOT } = require('./paths.js')
const { spawn } = require('./run.js')

Expand Down Expand Up @@ -42,38 +43,6 @@ function jsonFiles(arr) {
return arr.filter(whitelist)
}

function collectJsFiles(target) {
const whitelist = whitelisted(whitelists.js)
return collectFiles(target).filter(whitelist)
}

function collectAllFiles(target) {
const whitelist = whitelisted(whitelists.all)
return collectFiles(target).filter(whitelist)
}

function collectRejectedFiles(target) {
const whitelist = whitelisted(['.rej'])
return collectFiles(target).filter(whitelist)
}

function collectFiles(target) {
const files = fs.readdirSync(target)

return files
.map(file => {
const fullPath = path.join(target, file)
const stat = fs.statSync(fullPath)

if (stat.isDirectory() && !blacklist.includes(file)) {
return collectFiles(fullPath)
} else {
return fullPath
}
})
.reduce((a, b) => a.concat(b), [])
}

function readFile(fp) {
try {
const text = fs.readFileSync(fp, 'utf8')
Expand Down Expand Up @@ -147,19 +116,21 @@ function selectFiles(files, pattern, staged) {
let codeFiles = []

codeFiles = fg.sync([pattern], {
globstar: true,
absolute: true,
baseNameMatch: true,
dot: true,
globstar: true,
onlyFiles: true,
ignore: blacklist.map(b => `**/${b}/**`),
absolute: true,
cwd: PROJECT_ROOT,
})

log.debug(`Using pattern: ${pattern}`)
log.debug(`Matched files: ${codeFiles.join(', ')}`)

if (files.length > 0) {
codeFiles = files
.filter(f => codeFiles.includes(path.resolve(f)))
codeFiles = codeFiles
.filter(f => mm.contains(f, files))
.map(f => path.resolve(f))
}

Expand Down Expand Up @@ -225,10 +196,6 @@ const relativePath = fp => path.relative(CONSUMING_ROOT, fp)

module.exports = {
copy,
collectFiles,
collectAllFiles,
collectJsFiles,
collectRejectedFiles,
deleteFile,
jsFiles,
jsonFiles,
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3289,6 +3289,14 @@ micromatch@^4.0.2:
braces "^3.0.1"
picomatch "^2.0.5"

micromatch@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.4.tgz#896d519dfe9db25fce94ceb7a500919bf881ebf9"
integrity sha512-pRmzw/XUcwXGpD9aI9q/0XOwLNygjETJ8y0ao0wdqprrzDa4YnxLcz7fQRZr8voh8V10kGhABbNcHVk5wHgWwg==
dependencies:
braces "^3.0.1"
picomatch "^2.2.3"

mime-db@1.44.0:
version "1.44.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92"
Expand Down Expand Up @@ -3832,6 +3840,11 @@ picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==

picomatch@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.3.tgz#465547f359ccc206d3c48e46a1bcb89bf7ee619d"
integrity sha512-KpELjfwcCDUb9PeigTs2mBJzXUPzAuP2oPcA989He8Rte0+YUAjw1JVedDhuTKPkHjSYzMN3npC9luThGYEKdg==

pify@^2.0.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
Expand Down

0 comments on commit 77ff9ef

Please sign in to comment.