Skip to content

Commit

Permalink
fix: only the apply command was properly whitelisted (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
varl authored Mar 5, 2019
1 parent 3833bea commit 8a7556a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/cmds/js_cmds/apply.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions src/cmds/js_cmds/check.js
Original file line number Diff line number Diff line change
@@ -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.'
Expand All @@ -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
Expand Down
27 changes: 23 additions & 4 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -49,7 +65,10 @@ function writeFile(fp, content) {

module.exports = {
collectFiles,
collectAllFiles,
collectJsFiles,
readFile,
writeFile,
whitelisted,
whitelists,
}

0 comments on commit 8a7556a

Please sign in to comment.