diff --git a/.editorconfig b/.editorconfig index 991900b..818e072 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,3 @@ -# http://editorconfig.org root = true [*] @@ -9,14 +8,6 @@ indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true -[*.md] +[{**/{actual,fixtures,expected,templates}/**,*.md}] trim_trailing_whitespace = false -insert_final_newline = false - -[test/**] -trim_trailing_whitespace = false -insert_final_newline = false - -[templates/**] -trim_trailing_whitespace = false -insert_final_newline = false +insert_final_newline = false \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..948dbdb --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,122 @@ +{ + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true + }, + + "env": { + "browser": false, + "es6": true, + "node": true, + "mocha": true + }, + + "globals": { + "document": false, + "navigator": false, + "window": false + }, + + "rules": { + "accessor-pairs": 2, + "arrow-spacing": [2, { "before": true, "after": true }], + "block-spacing": [2, "always"], + "brace-style": [2, "1tbs", { "allowSingleLine": true }], + "comma-dangle": [2, "never"], + "comma-spacing": [2, { "before": false, "after": true }], + "comma-style": [2, "last"], + "constructor-super": 2, + "curly": [2, "multi-line"], + "dot-location": [2, "property"], + "eol-last": 2, + "eqeqeq": [2, "allow-null"], + "generator-star-spacing": [2, { "before": true, "after": true }], + "handle-callback-err": [2, "^(err|error)$" ], + "indent": [2, 2, { "SwitchCase": 1 }], + "key-spacing": [2, { "beforeColon": false, "afterColon": true }], + "keyword-spacing": [2, { "before": true, "after": true }], + "new-cap": [2, { "newIsCap": true, "capIsNew": false }], + "new-parens": 2, + "no-array-constructor": 2, + "no-caller": 2, + "no-class-assign": 2, + "no-cond-assign": 2, + "no-const-assign": 2, + "no-control-regex": 2, + "no-debugger": 2, + "no-delete-var": 2, + "no-dupe-args": 2, + "no-dupe-class-members": 2, + "no-dupe-keys": 2, + "no-duplicate-case": 2, + "no-empty-character-class": 2, + "no-eval": 2, + "no-ex-assign": 2, + "no-extend-native": 2, + "no-extra-bind": 2, + "no-extra-boolean-cast": 2, + "no-extra-parens": [2, "functions"], + "no-fallthrough": 2, + "no-floating-decimal": 2, + "no-func-assign": 2, + "no-implied-eval": 2, + "no-inner-declarations": [2, "functions"], + "no-invalid-regexp": 2, + "no-irregular-whitespace": 2, + "no-iterator": 2, + "no-label-var": 2, + "no-labels": 2, + "no-lone-blocks": 2, + "no-mixed-spaces-and-tabs": 2, + "no-multi-spaces": 2, + "no-multi-str": 2, + "no-multiple-empty-lines": [2, { "max": 1 }], + "no-native-reassign": 0, + "no-negated-in-lhs": 2, + "no-new": 2, + "no-new-func": 2, + "no-new-object": 2, + "no-new-require": 2, + "no-new-wrappers": 2, + "no-obj-calls": 2, + "no-octal": 2, + "no-octal-escape": 2, + "no-proto": 0, + "no-redeclare": 2, + "no-regex-spaces": 2, + "no-return-assign": 2, + "no-self-compare": 2, + "no-sequences": 2, + "no-shadow-restricted-names": 2, + "no-spaced-func": 2, + "no-sparse-arrays": 2, + "no-this-before-super": 2, + "no-throw-literal": 2, + "no-trailing-spaces": 0, + "no-undef": 2, + "no-undef-init": 2, + "no-unexpected-multiline": 2, + "no-unneeded-ternary": [2, { "defaultAssignment": false }], + "no-unreachable": 2, + "no-unused-vars": [2, { "vars": "all", "args": "none" }], + "no-useless-call": 0, + "no-with": 2, + "one-var": [0, { "initialized": "never" }], + "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], + "padded-blocks": [0, "never"], + "quotes": [2, "single", "avoid-escape"], + "radix": 2, + "semi": [2, "always"], + "semi-spacing": [2, { "before": false, "after": true }], + "space-before-blocks": [2, "always"], + "space-before-function-paren": [2, "never"], + "space-in-parens": [2, "never"], + "space-infix-ops": 2, + "space-unary-ops": [2, { "words": true, "nonwords": false }], + "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], + "use-isnan": 2, + "valid-typeof": 2, + "wrap-iife": [2, "any"], + "yoda": [2, "never"] + } +} diff --git a/.gitignore b/.gitignore index 60759af..0a16ee9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,27 @@ -.DS_Store +# always ignore files +*.DS_Store *.sublime-* -_gh_pages -bower_components + +# test related, or directories generated by tests +test/actual +actual +coverage +.nyc* + +# npm node_modules npm-debug.log -actual -test/actual + +# yarn +yarn.lock +yarn-error.log + +# misc +_gh_pages +_draft +_drafts +bower_components +vendor temp tmp TODO.md -vendor -.idea -benchmark -coverage diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 1a2f7b9..0000000 --- a/.jshintrc +++ /dev/null @@ -1,18 +0,0 @@ -{ - "asi": false, - "boss": true, - "curly": true, - "eqeqeq": true, - "eqnull": true, - "esnext": true, - "immed": true, - "latedef": false, - "laxcomma": false, - "mocha": true, - "newcap": true, - "noarg": true, - "node": true, - "sub": true, - "undef": true, - "unused": true -} diff --git a/.travis.yml b/.travis.yml index 0fc9381..58dcd48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,13 @@ sudo: false +os: + - linux + - osx language: node_js node_js: - - "0.10" - - "0.12" - - "0.13" - - "iojs" -matrix: - fast_finish: true - allow_failures: - - node_js: "0.13" + - node + - '7' + - '6' + - '5' + - '4' + - '0.12' + - '0.10' diff --git a/.verb.md b/.verb.md index 7a2dc9d..8b13789 100644 --- a/.verb.md +++ b/.verb.md @@ -1,408 +1 @@ -# {%= name %} {%= badge("fury") %} -> {%= description %} - -## Usage - -```js -var glob = require('{%= name %}')({ gitignore: true }); -var files = glob.readdirSync('**/*.js'); -``` - -[Run actual examples](./examples/): - - -Jump to docs sections: - -- [glob.readdir](#async) -- [glob.readPromise](#promise) -- [glob.readStream](#stream) -- [glob.readdirSync](#sync) - - -## Table of contents - - - -## Install -{%= include("install-npm", {save: true}) %} - - -## Usage - -**Params** - -All "read" methods take a glob pattern and an `options` object. - -- `pattern` **{String}**: Glob pattern to use for matching. (multiple pattern support is planned) -- `options` **{Object}**: Options for `glob-fs` or middleware. - -**Examples:** - -```js -// sync -var files = glob.readdirSync('*.js', {}); - -// async -glob.readdir('*.js', function(err, files) { - console.log(files); -}); - -// stream -glob.readdirStream('*.js', {}) - .on('data', function(file) { - console.log(file); - }); - -// promise -glob.readdirPromise('*.js') - .then(function(files) { - console.log(file); - }); -``` - - -## API -{%= apidocs("./lib/readers.js") %} -{%= apidocs("index.js") %} - -## Middleware - -glob-fs uses middleware to add file matching and exclusion capabilities, or other features that may or may not eventually become core functionality. - -**What is a middleware?** - -A middleware is a function that "processes" files as they're read from the file system by glob-fs. - -Additionally, middleware can: - -- be chained -- `include` or `exclude` a file based on some condition, like whether or not one of its properties matches a regex or glob pattern. -- determine whether or not to continue recursing in a specific directory -- modifying an existing property to the `file` object -- add a new property to the `file` object - - -### Middleware examples - -**Ignoring files** - -In the following example, `notemp` is a complete and functional middleware for excluding any filepath that has the substring `temp`: - -```js -var glob = require('glob-fs')(); - -function notemp(file) { - if (/temp/.test(file.path)) { - file.exclude = true; - } - return file; -} - -glob.use(notemp) - .readdirStream('**/*.js') - .on('data', function(file) { - console.log(file.relative); - }); -``` - -**Matching** - -Pattern matching is done by default in glob-fs, but you get disable the built-in matchers or get more specific by adding a middleware that uses [micromatch][] or [minimatch][] for matching files. - -```js -var glob = require('glob-fs')({ gitignore: true }); -var mm = require('micromatch'); - -glob.use(function(file) { - if (mm.isMatch(file.relative, 'vendor/**')) file.exclude = true; - return file; - }) - .readdirStream('**/*.js') - .on('data', function(file) { - console.log(file.relative); - }); -``` - -**recursion** - -Here is how a middleware might determine whether or not to recurse based on a certain pattern: - -```js -var glob = require('glob-fs')(); - -// this specific check is already done by glob-fs, it's just used here as an example -function recurse(file) { - // `file.pattern` is an object with a `glob` (string) property - file.recurse = file.pattern.glob.indexOf('**') !== -1; - return file; -} - -// use the middleware -glob.use(recurse) - .readdir('**/*.js', function(err, files) { - console.log(files); - }); -``` - - -**Built-in middleware** - -Currently glob-fs includes and runs the following middleware automatically: - - -{%= related(mm(Object.keys(dependencies), "glob-fs*")) %} - -**Disabling built-ins** - -To disable built-in middleware and prevent them from running, pass `builtins: false` on the global options. This will disable **all built-in middleware**. - -Example: - -```js -var glob = require('glob-fs')({builtins: false}); -``` - -To disable a specific middleware from running, you can usually pass the name of the middleware on the options, like `dotfiles: false`, but it's best to check the readme of that middleware for specifics. - - -### Middleware conventions - -- **Naming**: any middleware published to npm should be prefixed with `glob-fs-`, as in: `glob-fs-dotfiles`. -- **Keywords**: please add `glob-fs` to the keywords array in package.json -- **Options**: all middleware should return a function that takes an `options` object, as in the [Middleware Example](#middleware-example) -- **Return `file`**: all middleware should return the `file` object after processing. - -### Advice for middleware authors - -- A middleware should only do one specific thing. -- Multiple middleware libs can be bundled together to create a single middleware. -- Pattern matching should be extremely specific. Don't force downstream middleware to reverse your mistakes. -- As mentioned in the [middleware conventions](#middleware-conventions) section, **always return the `file` object**. -- A single conditional should only set `file.exclude` to `true`, or `file.include` to `true`, never both. -- It's completely okay to check `this.options` -- Middleware modules should be fully documented. - -## Globbing examples - -Note that the `gitignore` option is already `true` by default, it's just shown here as a placeholder for how options may be defined. - -### async - -```js -var glob = require('{%= name %}')({ gitignore: true }); - -glob.readdir('**/*.js', function(err, files) { - console.log(files); -}); -``` - -### promise - -```js -var glob = require('{%= name %}')({ gitignore: true }); - -glob.readdirPromise('**/*') - .then(function (files) { - console.log(files); - }); -``` - -### stream - -```js -var glob = require('{%= name %}')({ gitignore: true }); - -glob.readdirStream('**/*') - .on('data', function (file) { - console.log(file.path); - }) -``` - -### sync - - -```js -var glob = require('{%= name %}')({ gitignore: true }); - -var files = glob.readdirSync('**/*.js'); -console.log(files); -``` - -## Events - -_(WIP)_ - -The following events are emitted with all "read" methods: - -- `read`: emitted immediately before an iterator calls the first middleware. -- `include`: emits a `file` object when it's matched -- `exclude`: emits a `file` object when it's ignored/excluded -- `file`: emits a `file` object when the iterator pushes it into the results array. Only applies to `sync`, `async` and `promise`. -- `dir`: emits a `file` object when the iterator finds a directory -- `end` when the iterator is finished reading -- `error` on errors - - -### Event examples - -**async** - -```js -var glob = require('..')({ gitignore: true }); - -glob.on('dir', function (file) { - console.log(file); -}); - -glob.readdir('**/*.js', function (err, files) { - if (err) return console.error(err); - console.log(files.length); -}); -``` - -**promise** - -```js -var glob = require('{%= name %}')({ gitignore: true }); - -glob.on('include', function (file) { - console.log('including:', file.path); -}); - -glob.on('exclude', function (file) { - console.log('excluding:', file.path); -}); - -glob.readdirPromise('**/*'); -``` - -**sync** - -Also has an example of a custom event, emitted from a middleware: - -```js -var glob = require('{%= name %}')({ gitignore: true }) - .use(function (file) { - if (/\.js$/.test(file.path)) { - // custom event - this.emit('js', file); - } - return file; - }); - - -glob.on('js', function (file) { - console.log('js file:', file.path); -}); - -glob.on('exclude', function (file) { - console.log('excluded:', i.excludes++); -}); - -glob.on('include', function (file) { - console.log('included:', i.includes++) -}); - -glob.on('end', function () { - console.log('total files:', this.files.length); -}); - -glob.readdirSync('**/*.js'); -``` - -**stream** - -```js -var glob = require('{%= name %}')({ gitignore: true }) - -glob.readdirStream('**/*') - .on('data', function (file) { - console.log(file.path) - }) - .on('error', console.error) - .on('end', function () { - console.log('end'); - }); -``` - -## FAQ - -- when files are read from the file system, an object is created to keep a record of the file's `path`, `dirname`, and fs `stat` object and other pertinent information that makes it easier to make decisions about inclusion and exclusion later on. -- `file` objects are decorated with a `parse` method that is used to calculate the `file.relative` and `file.absolute` properties. -- the `file.parse()` method is called in the iterator, right after the call to `fs.stats` and just before the call to the middleware handler (`.handle()`). This ensures that all middleware have access to necessary path information. -- `file.relative` is the file path that's actually pushed into the `files` array that is ultimately returned. -- `file.relative` is calculated using `path.relative(file.path, cwd)`, where `cwd` is passed on the options (globally, or on a middleware), and `file.path` is typically the absolute, actual file path to the file being globbed. - - -## TODO - -**middleware** - -* [x] middleware -* [x] middleware handler -* [ ] externalize middleware to modules (started, [prs welcome!](#contributing)) - -**events** - -* [x] events - -**tests** - -* [x] unit tests (need to be moved) - -**iterators** - -* [x] sync iterator -* [x] async iterator -* [x] stream iterator -* [x] promise iterator - -**read methods** - -* [x] glob.readdir (async) -* [x] glob.readdirSync -* [x] glob.readdirStream -* [x] glob.readdirPromise - -**patterns** - -* [ ] Multiple pattern support. will need to change pattern handling, middleware handling. this is POC currently -* [ ] Negation patterns (might not do this, since it can be handled in middleware) -* [ ] matching method, memoized/cached/bound to a glob pattern or patterns, so it can be reused without having to recompile the regex. - -**other** - -* [ ] clean up `./lib` -* [ ] comparsion to [node-glob][] - - -## Community middleware - -_(Add your project to the [.verb.md](./.verb.md) template do a PR!)_ - - - -{%= related(['glob-fs-dotfiles', 'glob-fs-gitignore']) %} - -## Related projects -{%= related(['micromatch', 'braces', 'fill-range', 'is-glob']) %} - -## Running tests -{%= include("tests") %} - -## Contributing -{%= include("contributing") %} - -## Author -{%= include("author") %} - -## License -{%= copyright() %} -{%= license() %} - -*** - -{%= include("footer") %} - -{%= reflinks(['verb', 'glob', 'minimatch']) %} \ No newline at end of file diff --git a/LICENSE b/LICENSE index 65f90ac..83b56e7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015, Jon Schlinkert. +Copyright (c) 2015-2017, Jon Schlinkert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/examples/async-events.js b/examples/async-events.js deleted file mode 100644 index e3e3339..0000000 --- a/examples/async-events.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - -glob.on('dir', function (file) { - console.log(file); -}); - -glob.readdir('**/*.js', function (err, files) { - if (err) return console.error(err); - console.log(files.length); -}); diff --git a/examples/async-middleware.js b/examples/async-middleware.js deleted file mode 100644 index cd698d8..0000000 --- a/examples/async-middleware.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -var dotfiles = require('../middleware/dotfiles'); -var glob = require('..'); - -glob({ gitignore: false }) - .exclude('node_modules') - .exclude('*.js') - .exclude('*.css') - .use(dotfiles()) - .readdir('**/*', function (err, files) { - if (err) return console.error(err); - console.log(files.length); - }); - diff --git a/examples/async.js b/examples/async.js deleted file mode 100644 index ce9b9ed..0000000 --- a/examples/async.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - - -glob.readdir('**/*', function (err, files) { - if (err) return console.error(err); - console.log(files.length); -}); diff --git a/examples/copy.js b/examples/copy.js deleted file mode 100644 index 63ceafa..0000000 --- a/examples/copy.js +++ /dev/null @@ -1,17 +0,0 @@ - - -var through = require('through2'); -var glob = require('./')({ gitignore: true, dot: true }) - // .filter('data', function (file) { - // // console.log(file.relative); - // }) - - -glob.readdirStream('*.*') - .pipe(through.obj(function (file, enc, cb) { - this.push(file); - cb(); - })) - .on('data', function (file) { - // console.log(file.relative); - }) diff --git a/examples/exclude.js b/examples/exclude.js deleted file mode 100644 index 0fc6bdf..0000000 --- a/examples/exclude.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -var dotfiles = require('../middleware/dotfiles'); -var glob = require('..'); - - -glob({ gitignore: false }) - .exclude('node_modules') - .use(dotfiles()) - .readdir('**/*.js', function (err, files) { - if (err) return console.error(err); - console.log(files.length); - }); diff --git a/examples/gitignore.js b/examples/gitignore.js deleted file mode 100644 index b338084..0000000 --- a/examples/gitignore.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - -glob.readdir('**/*.js', function (err, files) { - if (err) return console.error(err); - console.log(files.length); -}); diff --git a/examples/middleware.js b/examples/middleware.js deleted file mode 100644 index 6461037..0000000 --- a/examples/middleware.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -var dotfiles = require('../middleware/dotfiles'); -var glob = require('..'); - -/** - * Example middleware for ignoring everything in - * the `./tests/` directory - */ - -function tests(options) { - return function(file) { - if (/^test\//.test(file.dirname)) { - file.exclude = true; - } - return file; - }; -} - -glob({ gitignore: true }) - .use(dotfiles()) // <= 'dotfiles' middleware - .use(tests()) // <= 'tests' middleware - .readdir('**/*.js', function (err, files) { - if (err) return console.error(err); - console.log(files); - }); diff --git a/examples/promise-events.js b/examples/promise-events.js deleted file mode 100644 index 1c051fa..0000000 --- a/examples/promise-events.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }) - .use(dotfiles()) - -glob.on('include', function (file) { - console.log('including:', file.path); -}); - -glob.on('exclude', function (file) { - console.log('excluding:', file.path); -}); - -glob.readdirPromise('**/*'); diff --git a/examples/promise-middleware.js b/examples/promise-middleware.js deleted file mode 100644 index 13b9dde..0000000 --- a/examples/promise-middleware.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -var dotfiles = require('../middleware/dotfiles'); -var glob = require('..')({ gitignore: true }) - .use(dotfiles()) - - -glob.readdirPromise('**/*') - .then(function (files) { - console.log(files.length); - }); diff --git a/examples/promise.js b/examples/promise.js deleted file mode 100644 index 535c87c..0000000 --- a/examples/promise.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - -glob.readdirPromise('**/*') - .then(function (files) { - console.log(files); - console.log(files.length); - }); diff --git a/examples/stream-events.js b/examples/stream-events.js deleted file mode 100644 index 6163ab4..0000000 --- a/examples/stream-events.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - -glob.on('dir', function (file) { - console.log(file.path); -}); - -glob.readdirStream('**/*'); diff --git a/examples/stream-middleware.js b/examples/stream-middleware.js deleted file mode 100644 index 33d4d81..0000000 --- a/examples/stream-middleware.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; - -var dotfiles = require('../middleware/dotfiles'); -var glob = require('..'); -var files = []; - - -// init, load some random middleware -var glob = glob({ gitignore: true }) - .exclude('*.css') - .exclude('*.js') - .exclude('*.yml') - .use(dotfiles()) - - -// get files -glob.readdirStream('**/*') - .on('data', function (file) { - files.push(file.path); - }) - .on('error', console.error) - .on('end', function () { - console.log('total files:', files.length); - }); diff --git a/examples/stream.js b/examples/stream.js deleted file mode 100644 index d4d18c2..0000000 --- a/examples/stream.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); -var i = 0; - -glob.readdirStream('**/*') - .on('data', function (file) { - i++; - console.log(file.path); - }) - .on('end', function () { - console.log(i); - }); diff --git a/examples/sync-events.js b/examples/sync-events.js deleted file mode 100644 index 26cb2d6..0000000 --- a/examples/sync-events.js +++ /dev/null @@ -1,38 +0,0 @@ -'use strict'; - -var unignore = require('../middleware/unignore'); -var dotfiles = require('../middleware/dotfiles'); -var isnt = require('../middleware/isnt'); -var i = {includes: 0, excludes: 0}; - - -var glob = require('..')({ gitignore: true }) - .use(isnt(/^node_modules/)) - .use(unignore(/\.gitignore/)) - .use(dotfiles()) - .use(function (file) { - if (/\.js$/.test(file.path)) { - this.emit('js', file); - } - return file; - }); - - -glob.on('js', function (file) { - console.log('js file:', file.path); -}); - -glob.on('exclude', function (file) { - console.log('excluded:', i.excludes++); -}); - -glob.on('include', function (file) { - // console.log(file.path); - console.log('included:', i.includes++); -}); - -glob.on('end', function () { - console.log('total files:', this.files.length); -}); - -glob.readdirSync('**/*.js'); diff --git a/examples/sync-middleware.js b/examples/sync-middleware.js deleted file mode 100644 index f784e9b..0000000 --- a/examples/sync-middleware.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -var fn = require('../middleware'); -var glob = require('..'); - - -var files = glob() - .use(fn.dotfiles()) - .use(fn.isnt(/^node_modules/)) - .use(fn.unignore(/\.gitignore/)) - .readdirSync('**/*.js'); - -console.log(files); diff --git a/examples/sync.js b/examples/sync.js deleted file mode 100644 index 6084850..0000000 --- a/examples/sync.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -var glob = require('..')({ gitignore: true }); - - -var files = glob.readdirSync('**/*'); -console.log(files.length); diff --git a/examples/track.js b/examples/track.js deleted file mode 100644 index 430a8cc..0000000 --- a/examples/track.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -var gitignore = require('../middleware/gitignore'); -var dotfiles = require('../middleware/dotfiles'); -var files = []; - -var glob = require('..')({ gitignore: true, track: true }) - .exclude('*.css') - .exclude('*.js') - .exclude('*.yml') - .use(dotfiles()) - .use(gitignore()); - - -glob.readdirStream('**/*') - .on('data', function (file) { - console.log(file); - files.push(file); - }) - .on('error', console.error) - .on('end', function () { - console.log(files.length); - }); diff --git a/index.js b/index.js deleted file mode 100644 index 1fd00da..0000000 --- a/index.js +++ /dev/null @@ -1,291 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ - -var omit = require('object.omit'); -var visit = require('object-visit'); -var Emitter = require('component-emitter'); -var exclude = require('./middleware/exclude'); -var include = require('./middleware/include'); -var npm = require('./middleware/npm'); -var symlinks = require('./lib/symlinks'); -var iterators = require('./lib/iterators'); -var Handler = require('./lib/handler'); -var Pattern = require('./lib/pattern'); -var options = require('./lib/options'); -var readers = require('./lib/readers'); -var utils = require('./lib/utils'); -var File = require('./lib/file'); - -/** - * Lazily required module dependencies - */ - -var lazy = require('lazy-cache')(require); -var gitignore = lazy('glob-fs-gitignore'); -var dotfiles = lazy('glob-fs-dotfiles'); - -/** - * Optionally create an instance of `Glob` with the given `options`. - * - * ```js - * var Glob = require('glob-fs').Glob; - * var glob = new Glob(); - * ``` - * - * @param {Object} `options` - * @api public - */ - -function Glob(options) { - if (!(this instanceof Glob)) { - return new Glob(options); - } - - Emitter.call(this); - this.handler = new Handler(this); - this.init(options); -} - -/** - * Glob prototype methods. - */ - -Glob.prototype = Emitter({ - - /** - * Initialize private objects. - */ - - init: function (opts) { - this.options = opts || {}; - this.pattern = null; - this.middleware = {}; - this.includes = {}; - this.excludes = {}; - this.files = []; - this.fns = []; - - options(this); - iterators(this); - symlinks(this); - readers(this); - }, - - /** - * Set configuration defaults. - */ - - defaults: function (glob, opts) { - if (opts.ignore) { - this.map('exclude', opts.ignore, opts); - } - if (opts.exclude) { - this.map('exclude', opts.exclude, opts); - } - if (opts.include) { - this.map('include', opts.include, opts); - } - - // if not disabled by the user, run the built-ins - if (!this.disabled('builtins')) { - if (!this.disabled('npm')) { - this.use(npm(opts)); - } - if (!this.disabled('dotfiles')) { - this.use(dotfiles()(opts)); - } - if (!this.disabled('gitignore')) { - this.use(gitignore()(opts)); - } - } - }, - - /** - * Create an instance of `Pattern` from the current glob pattern. - * - * @param {String} `pattern` - * @param {Object} `options` - */ - - setPattern: function (pattern, options) { - options = options || {}; - this.pattern = new Pattern(pattern, options); - this.recurse = this.shouldRecurse(this.pattern, options); - - // if middleware are registered, use the glob, otherwise regex - var glob = this.fns.length - ? this.pattern.glob - : this.pattern.re; - - this.defaults(glob, options); - this.include(glob, options); - return this; - }, - - /** - * Create a file object with properties that will be used - * by middleware. - * - * @param {String} `file` - * @return {Object} - */ - - createFile: function (file) { - return new File({ - pattern: this.pattern, - recurse: this.recurse, - dirname: file.dirname, - segment: file.segment, - path: file.path - }); - }, - - /** - * Return `true` if the iterator should recurse, based - * on the given glob pattern and options. - * - * @param {String} `pattern` - * @param {Object} `options` - */ - - shouldRecurse: function(pattern, options) { - var opts = this.setDefaults(options); - if (typeof opts.recurse === 'boolean') { - return opts.recurse; - } - return pattern.isGlobstar; - }, - - /** - * Add a middleware to be called in the order defined. - * - * ```js - * var gitignore = require('glob-fs-gitignore'); - * var dotfiles = require('glob-fs-dotfiles'); - * var glob = require('glob-fs')({ foo: true }) - * .use(gitignore()) - * .use(dotfiles()); - * - * var files = glob.readdirSync('**'); - * ``` - * - * @name .use - * @param {Function} `fn` - * @return {Object} Returns the `Glob` instance, for chaining. - * @api public - */ - - use: function(fn) { - this.handler.use(fn); - return this; - }, - - /** - * Include files or directories that match the given `pattern`. - * - * @name .include - * @param {String} `pattern` - * @param {Object} `options` - */ - - include: function(pattern, options) { - var opts = this.setDefaults(options); - this.use(include(pattern, opts)); - return this; - }, - - /** - * Thin wrapper around `.use()` for easily excluding files or - * directories that match the given `pattern`. - * - * ```js - * var gitignore = require('glob-fs-gitignore'); - * var dotfiles = require('glob-fs-dotfiles'); - * var glob = require('glob-fs')() - * .exclude(/\.foo$/) - * .exclude('*.bar') - * .exclude('*.baz'); - * - * var files = glob.readdirSync('**'); - * //=> ['index.js', 'README.md', ...] - * ``` - * - * @name .exclude - * @param {String} `pattern` - * @param {Object} `options` - * @api public - */ - - exclude: function(pattern, options) { - var opts = this.setDefaults(options); - this.use(exclude(pattern, opts)); - return this; - }, - - /** - * Optionally track the history of a file as it travels - * through the middleware stack. - * - * @param {Object} `file` - */ - - track: function(file) { - if (this.options.track === true) { - file.history.push(omit(file, 'history')); - } - }, - - /** - * Handle middleware. - * - * @param {Object} `file` - * @return {Object} - */ - - handle: function(file) { - this.handler.handle(file); - return this; - }, - - /** - * Map the given `method` over `array`. - * - * @param {String} `method` - * @param {Array} `arr` - * @return {Object} `this` for chaining - */ - - map: function(method, arr/*, arguments*/) { - var args = [].slice.call(arguments, 2); - utils.arrayify(arr || []).forEach(function (obj) { - this[method](obj, args); - }.bind(this)); - return this; - }, - - /** - * Call the given method on each value in `obj`. - */ - - visit: function (method, obj) { - visit(this, method, obj); - return this; - }, - - /** - * Add a method to the Glob prototype - */ - - mixin: function (name, fn) { - Glob.prototype[name] = fn; - } -}); - -/** - * Expose `Glob` - */ - -module.exports = Glob; diff --git a/lib/file.js b/lib/file.js deleted file mode 100644 index 7063cf2..0000000 --- a/lib/file.js +++ /dev/null @@ -1,151 +0,0 @@ -'use strict'; - -var path = require('path'); -var relative = require('relative'); - -/** - * Lazily required modules - */ - -var lazy = require('lazy-cache')(require); -var parsePath = lazy('parse-filepath'); -var startsWith = lazy('starts-with'); -var endsWith = lazy('ends-with'); -var isDotfile = lazy('is-dotfile'); -var isDotdir = lazy('is-dotdir'); - -/** - * Create a new `File` from the given `object`. - * - * @param {Object} `object` - * @api public - */ - -function File(file) { - this.cache = new Map(); - this.history = []; - this.pattern = file.pattern; - this.recurse = file.recurse; - this.dirname = file.dirname; - this.segment = file.segment; - this.path = file.path; - this.orig = file.path; -} - -/** - * Parse the `file.path` to add additional path properties - * to the `file` object. This is used in the iterators - * before the middleware handler is called. - * - * @param {String} `cwd` - */ - -File.prototype.parse = function(cwd) { - cwd = cwd || process.cwd(); - - this.relative = relative(cwd, this.path); - var parsed = parsePath()(this.path); - for (var key in parsed) { - if (parsed.hasOwnProperty(key)) { - this[key] = parsed[key]; - } - } - if (this.isDirectory()) { - if (this.pattern.endsWith('/') && !this.endsWith('/')) { - this.relative += '/'; - } - } -}; - -/** - * Returns `true if the file give filepath or `file.path` looks like - * a dotfile. - * - * @param {String} `fp` - * @return {Boolean} - */ - -File.prototype.isDotfile = function(fp) { - return isDotfile()(fp || this.path); -}; - -/** - * Returns `true if the file give filepath or `file.path` looks like - * a dot-directory. - * - * @param {String} `fp` - * @return {Boolean} - */ - -File.prototype.isDotdir = function(fp) { - return isDotdir()(fp || this.path); -}; - -/** - * Return the absolute filepath based on the file's root path. - * - * @param {String} `fp` - * @return {String} - */ - -File.prototype.toAbsolute = function(fp) { - if (typeof fp === 'undefined') { - fp = this.path; - } - if (this.startsWith('/', this.original)) { - return path.join(this.root, fp); - } - if (this.isAbsolute || fp === '') { - return fp; - } - return path.resolve(fp); -}; - -/** - * Return `true` if the given `file.path` ends with the specified - * `character` - * - * @param {String} `character` - * @param {String} `fp` If no filepath is passed, the cached `file.path` is used. - */ - -File.prototype.endsWith = function(ch, fp) { - var key = 'endsWith:' + ch; - if (this.cache.has(key)) return this.cache.get(key); - - if (typeof fp === 'undefined') { - fp = this.relative || this.path; - } - - var res = endsWith()(fp, ch); - this.cache.set(key, res); - return res; -}; - -/** - * Return `true` if the given `filepath` starts with the specified - * `character` - * - * @param {String} `character` - * @param {String} `fp` If no filepath is passed, the cached `file.path` is used. - */ - -File.prototype.startsWith = function(ch, fp) { - var key = 'startsWith:' + ch; - if (this.cache.has(key)) return this.cache.get(key); - - if (typeof fp === 'undefined') { - fp = this.relative || this.path; - } - - var res = startsWith()(fp, ch); - this.cache.set(key, res); - return res; -}; - - -/** - * Expose `File` - */ - -module.exports = File; diff --git a/lib/filter.js b/lib/filter.js deleted file mode 100644 index d32f9f4..0000000 --- a/lib/filter.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -var lazy = require('lazy-cache')(require); -var get = lazy('get-value'); -var mm = lazy('micromatch'); - -module.exports = function (app) { - app.cache = app.cache || {}; - - app.visit('mixin', { - restore: function (name) { - var res = get()(this.cache, name); - res.__proto__ = this; - return res; - }, - - stash: function(name, fp, glob, opts) { - this.cache[name] = this.cache[name] || []; - if (mm().isMatch(fp, glob, opts)) { - this.cache[name].push(fp); - } - return this; - }, - - filter: function(name, fn) { - if (!this.cache[name]) return this; - var res = this.cache[name].filter(fn); - res.__proto__ = this; - return res; - }, - - flush: function() { - this.files = []; - return this; - } - }); -}; diff --git a/lib/handler.js b/lib/handler.js deleted file mode 100644 index d40d17d..0000000 --- a/lib/handler.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict'; - -var util = require('util'); -var Emitter = require('component-emitter'); -var define = require('./utils').defineProp; - -function Handler(app) { - define(this, 'app', app); - Emitter.call(this); - this.fns = []; -} - -util.inherits(Handler, Emitter); - -Handler.prototype.use = function(fn) { - this.fns.push(fn); - return this; -}; - -Handler.prototype.handle = function(file) { - this.fns = this.fns.filter(Boolean); - var len = this.fns.length, i = -1; - this.app.track(file); - - while (++i < len) { - this.fns[i].call(this.app, file); - this.app.track(file); - - if (file.include === true || file.exclude === true) { - break; - } - } -}; - -/** - * Expose `Handler` - */ - -module.exports = Handler; diff --git a/lib/iterators.js b/lib/iterators.js deleted file mode 100644 index f287210..0000000 --- a/lib/iterators.js +++ /dev/null @@ -1,248 +0,0 @@ -'use strict'; - -var fs = require('fs'); -var path = require('path'); -var async = require('async'); -var lazy = require('lazy-cache')(require); -var mixin = require('mixin-object'); -var through = lazy('through2'); -var promise = lazy('bluebird'); -var filter = require('./filter'); - -module.exports = function (app) { - app.visit('mixin', { - - iteratorAsync: function(dir, cb) { - var self = this; - - setImmediate(function () { - fs.exists(dir, function(exists) { - if (!exists) { - return cb(null, []); - } - return walk(dir, cb); - }); - }); - - function walk(dir, cb) { - fs.readdir(dir, function(err, files) { - if (err) return cb(err); - - async.each(files, function(segment, next) { - var fp = path.join(dir, segment); - var file = self.createFile({ - dirname: dir, - segment: segment, - path: fp - }); - - self.realpath(file, function(err, res) { - if (err) return next(err); - - mixin(file, res); - file.parse(self.pattern.cwd); - - var isDir = file.isDirectory(); - - // handle middleware - self.emit('file', file); - self.handle(file); - - // emit directory - if (isDir) self.emit('dir', file); - - // emit excluded file - if (file.exclude === true) { - self.emit('exclude', file); - return next(); - } - - // emit included file - if (file.include === true) { - self.emit('include', file); - self.files.push(file.relative); - } - - if (file.recurse !== false && isDir) { - return walk(fp, next); - } - next(); - }); - - }, cb); - }); - } - return this; - }, - - iteratorSync: function(dir) { - var self = this; - var files = fs.readdirSync(dir); - var len = files.length, i = -1; - - while (++i < len) { - var segment = files[i]; - var fp = path.join(dir, segment); - var file = self.createFile({ - dirname: dir, - segment: segment, - path: fp - }); - - var res = self.realpathSync(file); - mixin(file, res); - - file.parse(this.pattern.cwd); - var isDir = file.isDirectory(); - - // handle middleware - self.emit('file', file); - self.handle(file); - - // emit directory - if (isDir) self.emit('dir', file); - - // emit excluded file - if (file.exclude === true) { - self.emit('exclude', file); - continue; - } - - // console.log(file.path) - // emit included file - if (file.include === true) { - self.emit('include', file); - self.files.push(file.relative); - } - - if (file.recurse !== false && isDir) { - this.iteratorSync(fp); - } - } - return this; - }, - - iteratorStream: function(dir) { - var self = this; - var stream = through().obj(); - var pass = through().obj(); - - setImmediate(function () { - fs.exists(dir, function(exists) { - if (!exists) return; - - walk(dir, function (err) { - if (err) { - stream.emit('error', err); - return; - } - stream.end(); - }); - }); - }); - - function walk(dir, cb) { - fs.readdir(dir, function(err, files) { - if (err) { - stream.emit('error', err); - return; - } - - async.each(files, function(segment, next) { - var fp = path.join(dir, segment); - var file = self.createFile({ - dirname: dir, - segment: segment, - path: fp - }); - - self.realpath(file, function(err, res) { - if (err) return next(err); - mixin(file, res); - - file.parse(self.pattern.cwd); - var isDir = file.isDirectory(); - - // handle middleware - self.emit('file', file); - self.handle(file); - - if (isDir) self.emit('dir', file); - if (file.exclude === true) { - self.emit('exclude', file); - return next(); - } - - if (file.include === true) { - self.emit('include', file); - self.files.push(file.relative); - stream.write(file); - } - - if (file.recurse !== false && isDir) { - return walk(fp, next); - } - next(); - }); - }, cb); - }); - } - stream = stream.pipe(pass); - return stream; - }, - - iteratorPromise: function(dir) { - var Promise = promise(); - var readdir = Promise.promisify(fs.readdir); - var realpath = Promise.promisify(this.realpath); - var self = this; - - return readdir(dir) - .map(function (segment) { - var fp = path.join(dir, segment); - var file = self.createFile({ - dirname: dir, - segment: segment, - path: fp - }); - - return realpath(file) - .then(function (res) { - mixin(file, res); - - file.parse(self.pattern.cwd); - var isDir = file.isDirectory(); - - // handle middleware - self.emit('file', file); - self.handle(file); - - // emit directory - if (isDir) self.emit('dir', file); - - // emit excluded file - if (file.exclude === true) { - self.emit('exclude', file); - return file.path; - } - - // emit included file - if (file.include === true) { - self.emit('include', file); - self.files.push(file.relative); - } - - if (file.recurse !== false && isDir) { - return self.iteratorPromise(fp); - } - return file.path; - }); - }) - - .reduce(function (acc, files) { - return acc.concat(files); - }, []); - }, - - }); -}; diff --git a/lib/options.js b/lib/options.js deleted file mode 100644 index 21799f2..0000000 --- a/lib/options.js +++ /dev/null @@ -1,135 +0,0 @@ -'use strict'; - -var extend = require('extend-shallow'); -var set = require('set-value'); -var get = require('get-value'); -var utils = require('./utils'); - -module.exports = function (app) { - app.visit('mixin', { - - setDefaults: function () { - var args = [].slice.call(arguments); - var opts = [{}].concat(this.options); - return extend.apply(extend, opts.concat(args)); - }, - - /** - * Set or get an option. - * - * ```js - * glob.option('foo.bar', 'baz'); - * glob.option('foo'); - * //=> { bar: 'baz' } - * ``` - * - * @param {String} `key` Dot notation may optionally be used. - * @param {any} `value` - * @return {Object} `Options` to enable chaining - * @api public - */ - - option: function(key, val) { - var len = arguments.length; - if (len === 1 && typeof key === 'string') { - return this.getOption(key); - } - if (utils.isObject(key)) { - this.visit('option', key); - return this; - } - set(this.options, key, val); - this.emit('option', key, val); - return this; - }, - - /** - * Get an option. - * - * @param {String} `prop` Dot notation may optionally be used. - * @return {any} - */ - - getOption: function (prop) { - if (prop.indexOf('.') === -1) { - return this.options[prop]; - } - return get(this.options, prop); - }, - - /** - * Enable `key`. - * - * ```js - * glob.enable('a'); - * ``` - * @param {String} `key` - * @return {Object} `Glob` instance, to enable chaining - * @api public - */ - - enable: function(key) { - this.option(key, true); - return this; - }, - - /** - * Disable `key`. - * - * ```js - * glob.disable('a'); - * ``` - * - * @param {String} `key` The option to disable. - * @return {Object} `Glob` instance, to enable chaining - * @api public - */ - - disable: function(key) { - this.option(key, false); - return this; - }, - - /** - * Check if `key` is enabled (truthy). - * - * ```js - * glob.enabled('a'); - * //=> false - * - * glob.enable('a'); - * glob.enabled('a'); - * //=> true - * ``` - * - * @param {String} `key` - * @return {Boolean} - * @api public - */ - - enabled: function(key) { - return this.options[key] === true; - }, - - /** - * Check if `key` is disabled (falsey). - * - * ```js - * glob.disabled('a'); - * //=> true - * - * glob.enable('a'); - * glob.disabled('a'); - * //=> false - * ``` - * - * @param {String} `key` - * @return {Boolean} - * @api public - */ - - disabled: function(key) { - return this.options[key] === false; - } - }); -}; diff --git a/lib/pattern.js b/lib/pattern.js deleted file mode 100644 index 7824199..0000000 --- a/lib/pattern.js +++ /dev/null @@ -1,215 +0,0 @@ -'use strict'; - -// require('time-require') - -var path = require('path'); -var typeOf = require('kind-of'); -var isGlob = require('is-glob'); -var utils = require('./utils'); -var isWindows = require('is-windows'); - -/** - * Lazily required modules - */ - -var lazy = require('lazy-cache')(require); -var startsWith = lazy('starts-with'); -var endsWith = lazy('ends-with'); -var parent = lazy('glob-parent'); -var mm = lazy('micromatch'); - -/** - * Create an instance of `Pattern` with the given `options`. - * - * @param {String} `glob` - * @param {Object} `options` - * @param {Boolean} `isNegated` - * @api public - */ - -function Pattern(glob, options, isNegated) { - utils.defineProp(this, 'cache', new Map()); - this.negated = !!isNegated; - this.options = options || {}; - this.parse(glob); - return this; -} - -/** - * Parse `pattern` into an object. - * - * @param {String} `pattern` - * @return {Object} - */ - -Pattern.prototype.parse = function(pattern) { - this.original = pattern; - - pattern = pattern || '.'; - this.cwd = path.resolve(this.options.cwd || '.'); - - this.isGlobstar = /(?:[*]{2}|\/\*\/\*\/)/.test(pattern); - // this.isGlobstar = pattern.indexOf('**') !== -1; - - if (this.endsWith('/', pattern)) { - this.hasTrailingSlash = true; - pattern = pattern.substr(0, pattern.length - 1); - - if (this.isGlobstar) { - pattern += '*/*'; - } - } - - if (!isGlob(pattern)) { - this.parent = '.'; - this.base = pattern; - this.glob = pattern; - - } else { - if (this.startsWith('!', pattern)) { - pattern = pattern.slice(1); - this.negated = true; - } - this.parent = parent()(pattern); - this.base = path.join(this.cwd, this.parent); - this.normalizePattern(pattern); - } - this.toRegex(this.glob); - return this; -}; - -/** - * Normalize slashes and dots in `pattern`. - * - * @param {String} `str` - * @return {String} - */ - -Pattern.prototype.normalizePattern = function(pattern) { - var sep = this.parent; - - if (sep === '.') sep = ''; - sep = new RegExp('^' + sep); - - pattern = pattern.replace(sep, ''); - if (this.startsWith('/', pattern)) { - pattern = pattern.slice(1); - this.root = '/'; - } - this.glob = pattern; - return pattern; -}; - -/** - * Return `true` if the given `pattern` ends with the given - * `character` - * - * @param {String} `character` - * @param {String} `pattern` If no `pattern` is passed, the cached value is used. - */ - -Pattern.prototype.endsWith = function(ch, pattern) { - var key = 'endsWith:' + ch; - if (this.cache.has(key)) return this.cache.get(key); - - if (typeof pattern === 'undefined') { - pattern = this.glob || this.original; - } - - var res = endsWith()(pattern, ch); - this.cache.set(key, res); - return res; -}; - -/** - * Return `true` if the given `pattern` starts with the given - * `character` - * - * @param {String} `character` - * @param {String} `pattern` If no `pattern` is passed, the cached value is used. - */ - -Pattern.prototype.startsWith = function(ch, pattern) { - var key = 'startsWith:' + ch; - if (this.cache.has(key)) return this.cache.get(key); - - if (typeof pattern === 'undefined') { - pattern = this.glob || this.original; - } - - var res = startsWith()(pattern, ch); - this.cache.set(key, res); - return res; -}; - -/** - * Return `true` if an actual parent was extracted from - * the glob pattern. e.g. not `.` - * - * @param {String} `parent` - */ - -Pattern.prototype.hasParent = function() { - if (this.parent !== '.' && this.parent.length > 0) { - return true; - } - if (this.cwd !== '.' && this.cwd.length > 0) { - return true; - } - return false; -}; - -/** - * Resolve the root directory. - * - * @param {String} `fp` - * @return {String} - */ - -Pattern.prototype.resolveRoot = function(dir) { - this.root = path.resolve(this.root || path.resolve((dir || this.cwd), '/')); - if (isWindows()) { - this.root = this.root.split('\\').join('/'); - } -}; - -/** - * Convert `pattern` to regex. - * - * @param {String} `str` - * @return {String} - */ - -Pattern.prototype.toRegex = function(pattern) { - if (typeOf(this.regex) === 'regexp') { - return this.regex; - } - - if (!pattern && this.negated) { - this.regex = new RegExp(this.parent); - } else { - if (this.hasTrailingSlash) { - pattern += '{,/}'; - } - this.regex = mm().makeRe(pattern); - } - - if (typeOf(this.regex) !== 'regexp') { - this.regex = new RegExp(this.original); - } -}; - - -Pattern.prototype.test = function(fp) { - if (typeOf(this.regex) === 'regexp') { - return this.regex.test(fp); - } - this.toRegex(this.glob); - return this.regex.test(fp); -}; - -/** - * Expose `Pattern` - */ - -module.exports = Pattern; diff --git a/lib/readers.js b/lib/readers.js deleted file mode 100644 index 919fe9f..0000000 --- a/lib/readers.js +++ /dev/null @@ -1,106 +0,0 @@ -'use strict'; - -module.exports = function (app) { - app.visit('mixin', { - - /** - * Asynchronously glob files or directories that match - * the given `pattern`. - * - * ```js - * var glob = require('glob-fs')({ gitignore: true }); - * - * glob.readdir('*.js', function (err, files) { - * //=> do stuff with `files` - * }); - * ``` - * - * @name .readdir - * @param {String} `pattern` Glob pattern - * @param {Object} `options` - * @param {Function} `cb` Callback - * @api public - */ - - readdir: function(pattern, options, cb) { - if (typeof options === 'function') { - return this.readdir(pattern, {}, options); - } - - this.emit('read'); - this.setPattern(pattern, options); - this.iteratorAsync(this.pattern.base, function (err) { - if (err) return cb(err); - - this.emit('end', this.files); - cb.call(this, null, this.files); - }.bind(this)); - return this; - }, - - /** - * Synchronously glob files or directories that match - * the given `pattern`. - * - * ```js - * var glob = require('glob-fs')({ gitignore: true }); - * - * var files = glob.readdirSync('*.js'); - * //=> do stuff with `files` - * ``` - * - * @name .readdirSync - * @param {String} `pattern` Glob pattern - * @param {Object} `options` - * @returns {Array} Returns an array of files. - * @api public - */ - - readdirSync: function(pattern, options) { - this.emit('read'); - this.setPattern(pattern, options); - this.iteratorSync(this.pattern.base); - this.emit('end', this.files); - return this.files; - }, - - /** - * Stream files or directories that match the given glob `pattern`. - * - * ```js - * var glob = require('glob-fs')({ gitignore: true }); - * - * glob.readdirStream('*.js') - * .on('data', function (file) { - * console.log(file.path); - * }) - * .on('error', console.error) - * .on('end', function () { - * console.log('end'); - * }); - * ``` - * - * @name .readdirStream - * @param {String} `pattern` Glob pattern - * @param {Object} `options` - * @returns {Stream} - * @api public - */ - - readdirStream: function(pattern, options) { - this.emit('read'); - this.setPattern(pattern, options); - var res = this.iteratorStream(this.pattern.base); - this.emit('end', this.files); - return res; - }, - - readdirPromise: function(pattern, options) { - this.emit('read'); - this.setPattern(pattern, options); - var res = this.iteratorPromise(this.pattern.base); - this.emit('end', this.files); - return res; - } - }); -}; diff --git a/lib/symlinks.js b/lib/symlinks.js deleted file mode 100644 index 2a1aa2b..0000000 --- a/lib/symlinks.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict'; - -var path = require('path'); -var fs = require('graceful-fs'); - -/** - * Recursively resolve a filepath to get the real path for a - * symlink. - * - * @param {Object} file - * @param {NA} `enc` not used - * @param {Function} `cb` callback - * @return {Object} - */ - -module.exports = function (app) { - app.visit('mixin', { - - realpath: function realpath(file, cb) { - var self = this; - - fs.lstat(file.path, function (err, stats) { - if (err) return cb(err); - file.stat = stats; - decorate(file); - - if (!file.isSymlink()) { - return cb(null, file); - } - - fs.realpath(file.path, function (err, fp) { - if (err) return cb(err); - - file.base = path.dirname(fp); - file.path = fp; - - // recurse to get real file stat - self.realpath(file, cb); - }); - }); - }, - - realpathSync: function realpathSync(file) { - var self = this; - try { - var stats = fs.lstatSync(file.path); - file.stat = stats; - decorate(file); - - if (!file.isSymlink()) { - return file; - } - } catch(err) { - throw new Error('fs.lstatSync error: ', err); - } - - try { - var fp = fs.realpathSync(file.path); - file.base = path.dirname(fp); - file.path = fp; - - // recurse to get real file stat - return self.realpathSync(file); - } catch (err) { - throw new Error('fs.realpathSync Error: ', err); - } - } - }); -}; - -function decorate(file) { - file.isDirectory = function () { - return file.stat.isDirectory(); - }; - - file.isFile = function () { - return file.stat.isFile(); - }; - - file.isSymlink = function () { - return file.stat.isSymbolicLink(); - }; -} diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index 17ad474..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -/** - * Utils - */ - -var utils = module.exports; - -/** - * Cast `val` to an array. - * - * @param {String|Array} `val` - * @return {Array} - */ - -utils.arrayify = function arrayify(val) { - return val ? (Array.isArray(val) ? val : [val]) : []; -}; - -/** - * Concatenate and flatten multiple arrays, filtering - * falsey values from the result set. - * - * @param {Array} `arrays` One or more arrays - * @return {Array} - */ - -utils.union = function union() { - var arr = [].concat.apply([], [].slice.call(arguments)); - return utils.flatten(arr).filter(Boolean); -}; - -/** - * Add a non-enumerable property to `receiver` - * - * @param {Object} `obj` - * @param {String} `name` - * @param {Function} `val` - */ - -utils.defineProp = function defineProp(receiver, key, value) { - return Object.defineProperty(receiver, key, { - configurable: true, - enumerable: false, - writable: true, - value: value - }); -}; diff --git a/middleware/any.js b/middleware/any.js deleted file mode 100644 index e4a8f40..0000000 --- a/middleware/any.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -var mm = require('micromatch'); - -module.exports = function any(fp, patterns, options) { - options = options || {}; - var res = {is: [], isnt: []}; - res.file = fp; - var len = patterns.length; - var i = -1; - - while (++i < len) { - var pattern = patterns[i]; - if (mm.isMatch(fp, pattern, {dot: true, contains: true})) { - res.is.push(pattern); - break; - } else { - res.isnt.push(pattern); - } - } - return res; -}; - -// console.log(any('node_modules', ignored)); -// console.log(any('test', ignored)); -// console.log(any('.DS_Store', ignored)); -// console.log(any('test/actual/foo.js', ignored)); diff --git a/middleware/end.js b/middleware/end.js deleted file mode 100644 index 52c9de7..0000000 --- a/middleware/end.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -// pseudo-code - this is a placeholder -module.exports = function end(fn) { - return this.files.filter(fn); -}; diff --git a/middleware/exclude.js b/middleware/exclude.js deleted file mode 100644 index 9225275..0000000 --- a/middleware/exclude.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -var mm = require('micromatch'); -var typeOf = require('kind-of'); -var extend = require('extend-shallow'); - -function testPattern(pattern) { - return function (fp) { - return pattern.test(fp); - } -} - -module.exports = function (pattern, options) { - var opts = extend({}, options); - var type = typeOf(pattern); - - var isMatch = type !== 'regexp' - ? mm.matcher(pattern, opts) - : testPattern(pattern); - - return function exclude(file) { - if (file.pattern.hasTrailingSlash && file.isFile()) { - return file; - } - - if (isMatch(file.path)) { - file.exclude = true; - return file; - } - - if (file.pattern.hasParent()) { - if (isMatch(file.relative)) { - file.exclude = true; - return file; - } - - if (file.pattern.test(file.segment) || file.pattern.test(file.relative)) { - file.exclude = true; - return file; - } - } - return file; - }; -}; diff --git a/middleware/include.js b/middleware/include.js deleted file mode 100644 index 610166b..0000000 --- a/middleware/include.js +++ /dev/null @@ -1,55 +0,0 @@ -'use strict'; - -var path = require('path'); -var mm = require('micromatch'); -var typeOf = require('kind-of'); -var extend = require('extend-shallow'); - -function testPattern(pattern) { - return function (fp) { - return pattern.test(fp); - } -} - -module.exports = function (pattern, options) { - var opts = extend({}, options); - var type = typeOf(pattern); - - var isMatch = type !== 'regexp' - ? mm.matcher(pattern, opts) - : testPattern(pattern); - - return function include(file) { - if (this.pattern.hasTrailingSlash && file.isFile()) { - return file; - } - - - if (isMatch(file.path)) { - file.include = true; - return file; - } - - if (this.pattern.hasParent()) { - - if (isMatch(file.relative)) { - file.include = true; - return file; - } - - var cwd = this.pattern.options.cwd || '.'; - var re = this.pattern.regex; - - if (re.test(path.join(cwd, file.relative))) { - file.include = true; - return file; - } - - if (re.test(file.segment) || re.test(file.relative)) { - file.include = true; - return file; - } - } - return file; - }; -}; diff --git a/middleware/index.js b/middleware/index.js deleted file mode 100644 index 23b2930..0000000 --- a/middleware/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('export-files')(__dirname); diff --git a/middleware/is.js b/middleware/is.js deleted file mode 100644 index 56242ef..0000000 --- a/middleware/is.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -module.exports = function (re) { - return function is(file) { - if (re.test(file.path)) { - file.include = true; - } - return file; - }; -}; diff --git a/middleware/isnt.js b/middleware/isnt.js deleted file mode 100644 index b5a119b..0000000 --- a/middleware/isnt.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -module.exports = function (re) { - return function isnt(file) { - if (re.test(file.path)) { - file.exclude = true; - } - return file; - }; -}; diff --git a/middleware/npm.js b/middleware/npm.js deleted file mode 100644 index 705ec91..0000000 --- a/middleware/npm.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -// this is a temporary -module.exports = function (options) { - options = options || {}; - - return function (file) { - if (this.disabled('node_modules')) { - return file; - } - var orig = this.pattern.original; - var pattern = this.pattern.glob; - if (/node_modules/.test(orig) || /node_modules/.test(pattern)) { - return file; - } - if (/node_modules/.test(file.path)) { - file.exclude = true; - } - return file; - }; -}; diff --git a/middleware/unignore.js b/middleware/unignore.js deleted file mode 100644 index 47f4101..0000000 --- a/middleware/unignore.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -var mm = require('micromatch'); -var extend = require('extend-shallow'); - -/** - * Unignore a previously-ignored file. The below code is not a good solution, - * it's just a reminder while this is WIP - * - * @param {String} `pattern` - * @return {Object} - */ - -module.exports = function (pattern, options) { - var opts = extend({ matchBase: true }, options); - var isMatch = mm.matcher(pattern, opts); - - return function unignore(file) { - if (isMatch(file.path)) { - delete file.exclude; - file.include = true; - } - return file; - }; -}; - -module.exports.exclude = function (pattern) { - for (var key in this.excludes) { - if (mm.isMatch(key, pattern)) { - this.includes[key] = this.excludes[key]; - this.files.push(key); - delete this.excludes[key]; - break; - } - } - return this; -}; diff --git a/package.json b/package.json index 2b62d97..21537d4 100644 --- a/package.json +++ b/package.json @@ -4,60 +4,45 @@ "version": "0.1.6", "homepage": "https://github.com/jonschlinkert/glob-fs", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", + "contributors": [ + "Brian Woodward (https://twitter.com/doowb)", + "Jon Schlinkert (http://twitter.com/jonschlinkert)" + ], "repository": "jonschlinkert/glob-fs", "bugs": { "url": "https://github.com/jonschlinkert/glob-fs/issues" }, "license": "MIT", - "files": [ - "index.js", - "middleware/", - "lib/" - ], - "main": "index.js", + "files": [], "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha" }, - "dependencies": { - "async": "^1.3.0", - "bluebird": "^2.9.33", - "component-emitter": "^1.2.0", - "ends-with": "^0.2.0", - "export-files": "^2.0.1", - "extend-shallow": "^2.0.0", - "get-value": "^1.1.5", - "glob-fs-dotfiles": "^0.1.6", - "glob-fs-gitignore": "^0.1.5", - "glob-parent": "^1.2.0", - "graceful-fs": "^4.1.2", - "is-dotdir": "^0.1.0", - "is-dotfile": "^1.0.1", - "is-glob": "^2.0.0", - "is-windows": "^0.1.0", - "kind-of": "^2.0.0", - "lazy-cache": "^0.1.0", - "micromatch": "jonschlinkert/micromatch#2.2.0", - "mixin-object": "^2.0.0", - "object-visit": "^0.1.0", - "object.omit": "^1.1.0", - "parse-filepath": "^0.6.1", - "relative": "^3.0.1", - "set-value": "^0.2.0", - "starts-with": "^1.0.2", - "through2": "^2.0.0" - }, "devDependencies": { - "mocha": "*", - "should": "*" + "mocha": "^3.4.2" }, "keywords": [ "file", + "file-path", + "file-system", "fs", "glob", "match", "path" - ] + ], + "verb": { + "toc": false, + "layout": "default", + "tasks": [ + "readme" + ], + "plugins": [ + "gulp-format-md" + ], + "lint": { + "reflinks": true + } + } } diff --git a/recurse.js b/recurse.js deleted file mode 100644 index 139597f..0000000 --- a/recurse.js +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/test/cwd.js b/test/cwd.js index e20c74d..07ac77f 100644 --- a/test/cwd.js +++ b/test/cwd.js @@ -1,32 +1,33 @@ 'use strict'; +require('mocha'); var should = require('should'); var Glob = require('..'); var path = require('path'); var orig = process.cwd(); var glob; -describe('cwd', function () { - describe('sync', function () { - before(function () { +describe('cwd', function() { + describe('sync', function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('file', function (file) { + glob.on('file', function(file) { // console.log(file.relative) }); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('should use the given cwd:', function () { + it('should use the given cwd:', function() { glob.readdirSync('**/d').should.containDeep([ 'a/b/c/d', 'a/c/d' ]); glob.readdirSync('**/d', { cwd: 'a' }).should.containDeep([ 'b/c/d', 'c/d' ]); glob.readdirSync('**/d', { cwd: 'a/b' }).should.containDeep([ 'c/d' ]); @@ -40,129 +41,127 @@ describe('cwd', function () { glob.readdirSync('b/*.js').should.containDeep(['b/a.js', 'b/b.js', 'b/c.js', 'b/index.js']); }); - it('should get files in the immediate directory:', function () { + it('should get files in the immediate directory:', function() { glob.readdirSync('a/bc/*').should.containDeep([ 'a/bc/e' ]); glob.readdirSync('*/*', {cwd: 'a'}).should.containDeep(['a.txt', 'b', 'bc', 'c']); }); }); - describe('sync cwd', function () { + describe('sync cwd', function() { glob = new Glob(); - it('should glob files from the given cwd', function () { + it('should glob files from the given cwd', function() { glob.readdirSync('*.js', {cwd: 'test'}).should.containDeep(['test.js']); glob.readdirSync('*', {cwd: 'test/fixtures/a'}).should.containDeep(['a.txt', 'b', 'bc', 'c']); }); }); - describe('glob parent', function () { + describe('glob parent', function() { glob = new Glob(); - it('should set the cwd to the glob parent:', function () { + it('should set the cwd to the glob parent:', function() { glob.readdirSync('test/*.js').should.containDeep(['test/test.js']); glob.readdirSync('test/fixtures/a/*').should.containDeep(['test/fixtures/a/a.txt', 'test/fixtures/a/b', 'test/fixtures/a/bc', 'test/fixtures/a/c']); }); }); }); - - -describe('async', function () { - before(function () { +describe('async', function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('', function (done) { - glob.readdir('**/d', function (err, files) { + it('', function(done) { + glob.readdir('**/d', function(err, files) { if (err) return done(err); files.should.containDeep([ 'a/b/c/d', 'a/c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: 'a' }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: 'a' }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'b/c/d', 'c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: 'a/b' }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: 'a/b' }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: path.resolve('a') }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: path.resolve('a') }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'c/d', 'b/c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: path.resolve('a/b') }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: path.resolve('a/b') }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: path.resolve('a/b/') }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: path.resolve('a/b/') }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: path.resolve('a/b/') }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: path.resolve('a/b/') }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('**/d', { cwd: process.cwd() }, function (err, files) { + it('', function(done) { + glob.readdir('**/d', { cwd: process.cwd() }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'a/c/d', 'a/b/c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('a/**/d', { cwd: process.cwd() }, function (err, files) { + it('', function(done) { + glob.readdir('a/**/d', { cwd: process.cwd() }, function(err, files) { if (err) return done(err); files.should.containDeep([ 'a/c/d', 'a/b/c/d' ]); done(); }); }); - it('', function (done) { - glob.readdir('assets/**/m*.css', function (err, files) { + it('', function(done) { + glob.readdir('assets/**/m*.css', function(err, files) { if (err) return done(err); files.should.containDeep(['assets/css/main.css']); done(); }); }); - it('', function (done) { - glob.readdir('b/*.js', function (err, files) { + it('', function(done) { + glob.readdir('b/*.js', function(err, files) { if (err) return done(err); files.should.containDeep(['b/a.js', 'b/b.js', 'b/c.js', 'b/index.js']); done(); }); }); - it('should get files in the immediate directory:', function (done) { - glob.readdir('*/*', {cwd: 'a'}, function (err, files) { + it('should get files in the immediate directory:', function(done) { + glob.readdir('*/*', {cwd: 'a'}, function(err, files) { files.should.containDeep(['a.txt', 'b', 'bc', 'c']); done(); }); diff --git a/test/dotfiles.js b/test/dotfiles.js index d985e8c..9c04c74 100644 --- a/test/dotfiles.js +++ b/test/dotfiles.js @@ -6,32 +6,31 @@ var path = require('path'); var orig = process.cwd(); var glob; -describe('dotfiles', function () { - beforeEach(function () { +describe('dotfiles', function() { + beforeEach(function() { glob = new Glob({ builtins: false }); - glob.on('file', function (file) { + glob.on('file', function(file) { // console.log(file.relative) }); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - - describe('sync', function () { - it('should not return dotfiles or directories by default:', function () { + describe('sync', function() { + it('should not return dotfiles or directories by default:', function() { glob.readdirSync('*').should.containDeep(['LICENSE', 'README.md']); glob.readdirSync('*').should.not.containDeep(['.editorconfig', '.git']); }); - it('should return dotfiles when the pattern has a leading dot:', function () { + it('should return dotfiles when the pattern has a leading dot:', function() { var files = glob.readdirSync('.*'); files.should.containDeep(['.git', '.gitignore']); }); - it('should return dotfiles from a given cwd', function () { + it('should return dotfiles from a given cwd', function() { glob.readdirSync('.*', {cwd: 'test/fixtures', dotfiles: true}).should.containDeep(['.dotfile']); glob.readdirSync('fixtures/.*', {cwd: 'test', dotfiles: true}).should.containDeep(['fixtures/.dotfile']); glob.readdirSync('fixtures/a/b/.*', {cwd: 'test', dotfiles: true}).should.containDeep(['fixtures/a/b/.dotfile']); @@ -39,30 +38,29 @@ describe('dotfiles', function () { glob.readdirSync('**/.*', {cwd: 'test/fixtures', dotfiles: true}).should.containDeep(['a/.dotfile']); }); - it('should return dotfiles and directories when `dot` is true:', function () { + it('should return dotfiles and directories when `dot` is true:', function() { glob.readdirSync('.*', { dot: true }).should.containDeep(['.editorconfig', '.git', '.gitattributes', '.gitignore', '.jshintrc', '.verb.md']); }); - it('should return dotfiles when `dotfiles` is true:', function () { + it('should return dotfiles when `dotfiles` is true:', function() { glob.readdirSync('.*', { dotfiles: true }).should.containDeep(['.editorconfig', '.gitattributes', '.gitignore', '.jshintrc', '.verb.md']); }); - it('should return dotdirs when `dotdirs` is true:', function () { + it('should return dotdirs when `dotdirs` is true:', function() { glob = new Glob({ dotdirs: true }); glob.readdirSync('.*', { dotdirs: true }).should.containDeep(['.git']); }); - it('should return dotdirs when `dotdirs` is defined globally:', function () { + it('should return dotdirs when `dotdirs` is defined globally:', function() { glob = new Glob({ dotfiles: true }); glob.readdirSync('.*').should.containDeep(['.editorconfig', '.gitattributes']); glob.readdirSync('*').should.containDeep(['.gitignore']); }); - it('should return dotdirs when `dotdirs` is defined on a read method:', function () { + it('should return dotdirs when `dotdirs` is defined on a read method:', function() { glob.readdirSync('.*', { dotfiles: true }).should.containDeep(['.editorconfig', '.gitattributes']); // glob.readdirSync('*', { dotfiles: true }).should.containDeep(['.gitignore']); }); }); }); - diff --git a/test/gitignore.js b/test/gitignore.js index f5e1d1e..1ed9683 100644 --- a/test/gitignore.js +++ b/test/gitignore.js @@ -7,30 +7,30 @@ var orig = process.cwd(); var gitignore = require('glob-fs-gitignore'); var glob; -describe('gitignore', function () { - beforeEach(function () { +describe('gitignore', function() { + beforeEach(function() { glob = new Glob(); }); - describe('sync', function () { - describe('when a user has `node_modules` defined in `.gitignore`', function () { - it('should recurse into node_modules when built-ins are disabled:', function () { + describe('sync', function() { + describe('when a user has `node_modules` defined in `.gitignore`', function() { + it('should recurse into node_modules when built-ins are disabled:', function() { glob = new Glob({builtins: false}); glob.readdirSync('**').should.containDeep(['node_modules']); }); - it('should not recurse into node_modules when `options.gitignore` is true:', function () { + it('should not recurse into node_modules when `options.gitignore` is true:', function() { var files = glob = new Glob({gitignore: false}) .use(gitignore()) .readdirSync('*'); files.should.not.containDeep(['node_modules']); }); - it('should recurse into node_modules when it\'s specified in the glob pattern:', function () { + it('should recurse into node_modules when it\'s specified in the glob pattern:', function() { glob.readdirSync('./node_modules/micromatch/*.js').should.containDeep(['node_modules/micromatch/index.js']); }); - it('should read parent directories:', function () { + it('should read parent directories:', function() { // cwd is `test/fixtures` glob.readdirSync('../../*.js', {cwd: 'test/fixtures'}).should.containDeep(['../../index.js']); glob.readdirSync('../**/*.js', {cwd: 'test/fixtures'}).should.containDeep(['../cwd.js']); diff --git a/test/globstar.js b/test/globstar.js index f473024..8d59e17 100644 --- a/test/globstar.js +++ b/test/globstar.js @@ -6,22 +6,22 @@ var orig = process.cwd(); var glob; describe('globstar', function() { - before(function () { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('should not return duplicate files:', function () { + it('should not return duplicate files:', function() { glob.readdirSync('a/**/[gh]').should.eql([ 'a/c/d/e/f/g', 'a/c/d/e/f/g/h', 'a/c/d/e/f/h' ]); }); }); diff --git a/test/hanlder.js b/test/hanlder.js index 80f4de5..cf82107 100644 --- a/test/hanlder.js +++ b/test/hanlder.js @@ -6,24 +6,24 @@ var path = require('path'); var orig = process.cwd(); var glob; -describe("changing cwd and searching for **/d", function () { - before(function () { +describe("changing cwd and searching for **/d", function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('cwd', function () { - // glob.handler.once('gitignore', function (file) { + it('cwd', function() { + // glob.handler.once('gitignore', function(file) { // console.log(file.path); // }); diff --git a/test/root.js b/test/root.js index 0d3bada..045dfe8 100644 --- a/test/root.js +++ b/test/root.js @@ -6,24 +6,24 @@ var path = require('path'); var orig = process.cwd(); var glob; -describe('root', function () { - before(function () { +describe('root', function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it.skip('root', function () { + it.skip('root', function() { glob.readdirSync('/b*/**', { root: '.' }).should.eql(['b.js']); glob.readdirSync('/b*/**', { root: path.resolve('a') }).should.eql(['/b', '/b/c', '/b/c/d', '/bc', '/bc/e', '/bc/e/f']); glob.readdirSync('/b*/**', { root: 'a', cwd: path.resolve('a/b') }).should.eql([ '/b', '/b/c', '/b/c/d', '/bc', '/bc/e', '/bc/e/f' ]); diff --git a/test/slashes.js b/test/slashes.js index 599ff3b..ee8084d 100644 --- a/test/slashes.js +++ b/test/slashes.js @@ -6,33 +6,33 @@ var path = require('path'); var orig = process.cwd(); var glob; -describe("root", function () { - before(function () { +describe("root", function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); var mm = require('micromatch'); - beforeEach(function () { + beforeEach(function() { glob = new Glob(); - glob.on('file', function (file) { + glob.on('file', function(file) { // console.log(mm.isMatch(file.relative, 'a/*/*/*/**/')) }); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('should only return directories when the pattern ends in a slash:', function () { + it('should only return directories when the pattern ends in a slash:', function() { glob.readdirSync('a/*/').should.eql([ 'a/b/', 'a/bc/', 'a/c/' ]); }); - it('should recurse and return directories when the pattern ends in a slash:', function () { + it('should recurse and return directories when the pattern ends in a slash:', function() { glob.readdirSync('a/*/*/*/**/').should.eql([ 'a/b/c/d/', 'a/bc/e/f/', diff --git a/test/test.js b/test/test.js index f9f7173..0491aa7 100644 --- a/test/test.js +++ b/test/test.js @@ -1,42 +1,42 @@ 'use strict'; -/* deps: mocha */ +require('mocha'); var should = require('should'); var Glob = require('..'); var path = require('path'); var orig = process.cwd(); var glob; -describe('basic globbing', function () { - before(function () { +describe('basic globbing', function() { + before(function() { process.chdir(__dirname + '/fixtures'); }); - after(function () { + after(function() { process.chdir(orig); }); - beforeEach(function () { + beforeEach(function() { glob = new Glob({track: true}); - glob.on('read', function () { + glob.on('read', function() { glob.files = []; }); }); - it('should glob files in the specified directory:', function () { + it('should glob files in the specified directory:', function() { glob.readdirSync('less/*.less', { cwd: process.cwd() }).should.eql(['less/a.less', 'less/b.less', 'less/c.less']); }); - it('should glob files in a nested directory:', function () { + it('should glob files in a nested directory:', function() { glob.readdirSync('**/*.less').should.eql(['less/a.less', 'less/b.less', 'less/c.less']); }); - it('should use options.cwd', function () { + it('should use options.cwd', function() { glob.readdirSync('*.less', {cwd: 'less'}).should.eql(['a.less', 'b.less', 'c.less']); }); - it('should glob files in the immediate directory:', function () { + it('should glob files in the immediate directory:', function() { glob.readdirSync('js/*.js').should.eql(['js/scripts.js', 'js/scripts.min.js']); }); });