Skip to content

Commit

Permalink
feat(debug): add debug with DEBUG=ok-file variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 16, 2017
1 parent b742370 commit a479a44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -26,6 +26,10 @@ Check if given file `foo.txt` exists and is not empty
$(npm bin)/ok-file foo.txt
```

## Debugging

Run program with `DEBUG=ok-file ...` environment variable

### Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
Expand Down
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -84,5 +84,9 @@
"prettier-standard": "7.0.1",
"semantic-release": "^8.0.3",
"standard": "10.0.3"
},
"dependencies": {
"debug": "3.1.0",
"globby": "6.1.0"
}
}
21 changes: 21 additions & 0 deletions src/index.js
@@ -1,8 +1,29 @@
'use strict'

const debug = require('debug')('ok-file')
const fs = require('fs')

function isWildcard (s) {
return s.indexOf('*') !== -1
}

function okWildcard (pattern) {
const globby = require('globby')
const filenames = globby.sync(pattern)
if (!filenames.length) {
console.error('Could not find any files matching "%s"', pattern)
return false
}
return true
}

function okFile (filename) {
if (isWildcard(filename)) {
debug('checking wild card "%s"', filename)
return okWildcard(filename)
}

debug('checking individual file %s', filename)
if (!fs.existsSync(filename)) {
console.error('Cannot find file', filename)
return false
Expand Down

0 comments on commit a479a44

Please sign in to comment.