UNPKG

2.81 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const execa = require('execa')
4const debug = require('debug')('stop-only')
5const argv = require('minimist')(process.argv.slice(2), {
6 string: ['folder', 'skip', 'exclude', 'file'],
7 boolean: 'warn',
8 alias: {
9 warn: 'w',
10 folder: 'f',
11 skip: 's',
12 exclude: 'e'
13 }
14})
15
16if (debug.enabled) {
17 console.log('stop-only arguments')
18 console.log(argv)
19}
20
21const isString = x => typeof x === 'string'
22
23const hasFolderArgument = isString(argv.folder) || Array.isArray(argv.folder)
24const hasFileArgument = isString(argv.file)
25
26if (!hasFolderArgument && !hasFileArgument) {
27 console.error(
28 '🔥 stop-only: pass at least a single folder with --folder, -f argument, or a file path with --file'
29 )
30 process.exit(1)
31}
32
33const toArray = x => (Array.isArray(x) ? x : [x])
34
35const normalizeStrings = listOrString => {
36 const strings = toArray(listOrString)
37 // ? can we just split and flatten result array?
38 let normalized = []
39 strings.forEach(s => {
40 if (s === undefined || s === null) {
41 return
42 }
43
44 if (s.includes(',')) {
45 normalized = normalized.concat(s.split(','))
46 } else {
47 normalized.push(s)
48 }
49 })
50 return normalized
51}
52
53let grepArguments = [
54 '--line-number',
55 '--recursive',
56 '[describe|context|it]\\.only'
57]
58
59if (hasFileArgument) {
60 grepArguments.push(argv.file)
61} else {
62 // checking folder(s)
63
64 // user should be able to pass multiple folders with single argument separated by commas
65 // like "--folder foo,bar,baz"
66 // next code block splits these arguments and normalizes everything into list of strings
67 const splitFolders = normalizeStrings(argv.folder)
68 debug('split folders', splitFolders)
69
70 const skipFolders = normalizeStrings(argv.skip)
71 const skipFiles = normalizeStrings(argv.exclude)
72
73 if (skipFolders.length) {
74 skipFolders.forEach(folder => {
75 grepArguments.push('--exclude-dir', folder)
76 })
77 }
78
79 if (skipFiles.length) {
80 skipFiles.forEach(filename => {
81 grepArguments.push('--exclude', filename)
82 })
83 }
84
85 grepArguments = grepArguments.concat(splitFolders)
86}
87
88if (debug.enabled) {
89 console.log('grep arguments')
90 console.log(grepArguments)
91}
92
93const grepFinished = result => {
94 if (result.code > 1) {
95 console.error('Failed to run grep')
96 console.error('grep arguments were')
97 console.error(grepArguments)
98 console.error(result)
99 process.exit(result.code)
100 }
101
102 if (result.code === 1) {
103 debug('could not find .only anywhere')
104 process.exit(0)
105 }
106
107 // found ".only" somewhere
108 if (argv.warn) {
109 console.log('⚠️ Found .only in')
110 console.log(result.stdout)
111 process.exit(0)
112 } else {
113 console.log('Found .only here 👎')
114 console.log(result.stdout)
115 process.exit(1)
116 }
117}
118
119execa('grep', grepArguments).then(grepFinished, grepFinished)