Skip to content

Commit 6bd5208

Browse files
committed
feat: document in readme
1 parent ac36244 commit 6bd5208

4 files changed

Lines changed: 6550 additions & 4 deletions

File tree

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,76 @@
1414
Requires [Node](https://nodejs.org/en/) version 6 or above.
1515

1616
```sh
17-
npm install --save stop-upper
17+
npm install --save-dev stop-upper
1818
```
1919

2020
## Use
2121

22+
### basic
23+
24+
Assuming all interesting files are in the folders "src" and "bin", I recommend create an NPM run script. Use `--folder` or `-f` to pass the folder(s) to search.
25+
26+
```json
27+
{
28+
"scripts": {
29+
"stop-upper": "stop-upper --folder src --folder bin"
30+
}
31+
}
32+
```
33+
34+
Exits with 1 if there is a file with uppercase somewhere inside the folders. Also checks folder names.
35+
36+
### warn
37+
38+
If you just want to warn on found uppercase names, use `stop-upper --warn <folder>` syntax. Alias `-w`.
39+
40+
### Pre-commit or pre-push hook
41+
42+
If using [pre-git][pre-git] to configure Git hooks, run this tool as a command
43+
44+
```json
45+
{
46+
"config": {
47+
"pre-git": {
48+
"pre-push": ["npm run stop-upper"]
49+
}
50+
}
51+
}
52+
```
53+
54+
See [package.json](package.json) (note here we have just local script name).
55+
56+
**tip** you can warn on commit hook, while fail in pre-push hook.
57+
58+
[pre-git]: github.com/bahmutov/pre-git#readme
59+
60+
### Commas
61+
62+
You can pass multiple folder names as separate arguments or comma-separated. These are equivalent
63+
64+
```
65+
stop-upper --folder foo --folder bar
66+
stop-upper -f foo -f bar
67+
stop-upper -f foo,bar
68+
```
69+
70+
### Debugging
71+
72+
You can see additional diagnostic output from this command by running with environment variable `DEBUG=stop-upper`
73+
74+
### CI
75+
76+
On CI run the tool after install, for example see [.travis.yml](.travis.yml),
77+
(note here we have just local script name).
78+
79+
```
80+
- npm run stop-upper
81+
```
82+
83+
## Related
84+
85+
- [stop-only](https://github.com/bahmutov/stop-only)
86+
2287
### Small print
2388

2489
Author: Gleb Bahmutov &lt;gleb.bahmutov@gmail.com&gt; &copy; 2018

bin/stop-upper.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)