diff --git a/src/cmds/js_cmds/apply.js b/src/cmds/js_cmds/apply.js index e917e62d..8abbc516 100644 --- a/src/cmds/js_cmds/apply.js +++ b/src/cmds/js_cmds/apply.js @@ -1,4 +1,4 @@ -const { collectFiles, whitelisted } = require('../../files.js') +const { collectJsFiles, whitelists, whitelisted } = require('../../files.js') const log = require('@dhis2/cli-helpers-engine').reporter const { apply_fmt } = require('../../prettier.js') @@ -31,11 +31,12 @@ exports.handler = argv => { let codeFiles if (all) { - codeFiles = collectFiles(root_dir).filter(whitelisted) + codeFiles = collectJsFiles(root_dir) } else if (files) { codeFiles = files } else { - codeFiles = staged_files(root_dir).filter(whitelisted) + const whitelist = whitelisted(whitelists.js) + codeFiles = staged_files(root_dir).filter(whitelist) } // debug information about the folders diff --git a/src/cmds/js_cmds/check.js b/src/cmds/js_cmds/check.js index 9b8398f2..789daac8 100644 --- a/src/cmds/js_cmds/check.js +++ b/src/cmds/js_cmds/check.js @@ -1,16 +1,10 @@ const path = require('path') -const { collectFiles } = require('../../files.js') +const { collectJsFiles, whitelists, whitelisted } = require('../../files.js') const log = require('@dhis2/cli-helpers-engine').reporter const { check_fmt } = require('../../prettier.js') const { staged_files } = require('../../git.js') -const whitelist = ['.js', '.jsx', '.ts'] - -function whitelisted(file) { - return whitelist.includes(path.extname(file)) -} - exports.command = 'check [files..]' exports.describe = 'Check JS format.' @@ -30,11 +24,12 @@ exports.handler = argv => { let codeFiles if (all) { - codeFiles = collectFiles(root_dir).filter(whitelisted) + codeFiles = collectJsFiles(root_dir) } else if (files) { codeFiles = files } else { - codeFiles = staged_files(root_dir).filter(whitelisted) + const whitelist = whitelisted(whitelists.js) + codeFiles = staged_files(root_dir).filter(whitelist) } // debug information about the folders diff --git a/src/files.js b/src/files.js index 6da65f71..9de7e604 100644 --- a/src/files.js +++ b/src/files.js @@ -3,11 +3,27 @@ const path = require('path') const log = require('@dhis2/cli-helpers-engine').reporter -const whitelist = ['.js', '.json', '.css', '.scss', '.md', '.jsx'] -const blacklist = ['node_modules', 'build', 'dist', 'target'] +const blacklist = ['node_modules', 'build', 'dist', 'target', '.git'] -function whitelisted(file) { - return whitelist.includes(path.extname(file)) +const whitelists = { + js: ['.js', '.jsx', '.ts'], + all: ['.js', '.json', '.css', '.scss', '.md', '.jsx', '.ts'], +} + +function whitelisted(whitelist) { + return function(file) { + return whitelist.includes(path.extname(file)) + } +} + +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 collectFiles(target) { @@ -49,7 +65,10 @@ function writeFile(fp, content) { module.exports = { collectFiles, + collectAllFiles, + collectJsFiles, readFile, writeFile, whitelisted, + whitelists, }