1 | #!/usr/bin/env node
|
2 |
|
3 | const execa = require('execa')
|
4 | const debug = require('debug')('stop-only')
|
5 | const 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 |
|
16 | if (debug.enabled) {
|
17 | console.log('stop-only arguments')
|
18 | console.log(argv)
|
19 | }
|
20 |
|
21 | const isString = x => typeof x === 'string'
|
22 |
|
23 | const hasFolderArgument = isString(argv.folder) || Array.isArray(argv.folder)
|
24 | const hasFileArgument = isString(argv.file)
|
25 |
|
26 | if (!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 |
|
33 | const toArray = x => (Array.isArray(x) ? x : [x])
|
34 |
|
35 | const normalizeStrings = listOrString => {
|
36 | const strings = toArray(listOrString)
|
37 |
|
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 |
|
53 | let grepArguments = [
|
54 | '--line-number',
|
55 | '--recursive',
|
56 | '[describe|context|it]\\.only'
|
57 | ]
|
58 |
|
59 | if (hasFileArgument) {
|
60 | grepArguments.push(argv.file)
|
61 | } else {
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
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 |
|
88 | if (debug.enabled) {
|
89 | console.log('grep arguments')
|
90 | console.log(grepArguments)
|
91 | }
|
92 |
|
93 | const 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 |
|
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 |
|
119 | execa('grep', grepArguments).then(grepFinished, grepFinished)
|