Skip to content

Commit

Permalink
feat(ignorefiles): add consistency to the ignorefiles that are respected
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Dec 16, 2019
1 parent 7a3d12d commit fdba06e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/tools/eslint.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { run } = require('../utils/run.js')
const { resolveIgnoreFile } = require('../utils/files.js')

exports.eslint = ({ files = [], apply = false, config }) => {
const ignoreFile = resolveIgnoreFile()
const cmd = 'npx'
const args = [
'--no-install',
'eslint',
'--no-color',
'--report-unused-disable-directives',
'--ignore',
...(ignoreFile ? ['--ignore-path', ignoreFile] : []),
...(config ? ['--config', config] : []),
...(apply ? ['--fix'] : []),
...files,
Expand Down
6 changes: 5 additions & 1 deletion src/tools/prettier.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { run } = require('../utils/run.js')
const log = require('@dhis2/cli-helpers-engine').reporter

const { run } = require('../utils/run.js')
const { resolveIgnoreFile } = require('../utils/files.js')

exports.prettier = ({ files = [], apply = false, config }) => {
const ignoreFile = resolveIgnoreFile()
const cmd = 'npx'
const args = [
'--no-install',
'prettier',
'--list-different',
...(config ? ['--config', config] : []),
...(ignoreFile ? ['--ignore-path', ignoreFile] : []),
...(apply ? ['--write'] : []),
...files,
]
Expand Down
29 changes: 28 additions & 1 deletion src/utils/files.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const fg = require('fast-glob')
const fs = require('fs')
const fs = require('fs-extra')
const path = require('path')

const log = require('@dhis2/cli-helpers-engine').reporter

const { spawn } = require('./run.js')
const { CONSUMING_ROOT } = require('./paths.js')

// blacklists for files
const blacklist = [
Expand Down Expand Up @@ -151,6 +152,30 @@ const stagedFiles = (files = []) => {
return []
}

const pickFirstExists = (files = [], root) => {
for (const file of files) {
const fp = root
? path.join(root, file)
: path.join(CONSUMING_ROOT, file)

if (fs.existsSync(fp)) {
log.debug(`Using ${fp} as the common ignore file.`)
return fp
}
}

return null
}

const resolveIgnoreFile = () => {
return pickFirstExists([
'.d2styleignore',
'.eslintignore',
'.prettierignore',
'.gitignore',
])
}

module.exports = {
collectFiles,
collectAllFiles,
Expand All @@ -166,4 +191,6 @@ module.exports = {
whitelisted,
whitelists,
blacklist,
pickFirstExists,
resolveIgnoreFile,
}

0 comments on commit fdba06e

Please sign in to comment.