|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const debug = require('debug')('stop-upper') |
| 4 | +const globby = require('globby') |
| 5 | +const { filter, flatten, uniq } = require('ramda') |
| 6 | + |
| 7 | +const argv = require('minimist')(process.argv.slice(2), { |
| 8 | + string: ['folder', 'skip', 'exclude'], |
| 9 | + boolean: 'warn', |
| 10 | + alias: { |
| 11 | + warn: 'w', |
| 12 | + folder: 'f', |
| 13 | + skip: 's', |
| 14 | + exclude: 'e' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +if (debug.enabled) { |
| 19 | + console.log('stop-upper arguments') |
| 20 | + console.log(argv) |
| 21 | +} |
| 22 | + |
| 23 | +if (!argv.folder || !argv.folder.length) { |
| 24 | + console.error( |
| 25 | + '🔥 stop-upper: pass at least a single folder with --folder, -f argument' |
| 26 | + ) |
| 27 | + process.exit(1) |
| 28 | +} |
| 29 | + |
| 30 | +const toArray = x => (Array.isArray(x) ? x : [x]) |
| 31 | + |
| 32 | +const normalizeStrings = listOrString => { |
| 33 | + const strings = toArray(listOrString) |
| 34 | + // ? can we just split and flatten result array? |
| 35 | + let normalized = [] |
| 36 | + strings.forEach(s => { |
| 37 | + if (s === undefined || s === null) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if (s.includes(',')) { |
| 42 | + normalized = normalized.concat(s.split(',')) |
| 43 | + } else { |
| 44 | + normalized.push(s) |
| 45 | + } |
| 46 | + }) |
| 47 | + return normalized |
| 48 | +} |
| 49 | + |
| 50 | +// user should be able to pass multiple folders with single argument separated by commas |
| 51 | +// like "--folder foo,bar,baz" |
| 52 | +// next code block splits these arguments and normalizes everything into list of strings |
| 53 | +const splitFolders = normalizeStrings(argv.folder) |
| 54 | +debug('split folders', splitFolders) |
| 55 | + |
| 56 | +const folders = flatten(splitFolders) |
| 57 | + |
| 58 | +if (debug.enabled) { |
| 59 | + console.log('stop only in folders') |
| 60 | + console.log(folders) |
| 61 | +} |
| 62 | + |
| 63 | +const searchFinished = foundUpperCaseFilename => { |
| 64 | + if (foundUpperCaseFilename.length === 0) { |
| 65 | + debug('could not find upper case filenames') |
| 66 | + process.exit(0) |
| 67 | + } |
| 68 | + |
| 69 | + // found uppercase filename somewhere |
| 70 | + if (argv.warn) { |
| 71 | + console.log('⚠️ Found uppercase filenames') |
| 72 | + console.log(foundUpperCaseFilename.join('\n')) |
| 73 | + process.exit(0) |
| 74 | + } else { |
| 75 | + console.log('Found uppercase filenames 👎') |
| 76 | + console.log(foundUpperCaseFilename.join('\n')) |
| 77 | + process.exit(1) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +const hasUpper = s => s.match(/[A-Z]/) |
| 82 | + |
| 83 | +const onError = e => { |
| 84 | + console.error(e.message) |
| 85 | + process.exit(1) |
| 86 | +} |
| 87 | + |
| 88 | +globby(folders) |
| 89 | + .then(flatten) |
| 90 | + .then(filter(hasUpper)) |
| 91 | + .then(uniq) |
| 92 | + .then(searchFinished) |
| 93 | + .catch(onError) |
0 commit comments