diff --git a/CHANGELOG.md b/CHANGELOG.md index 43b1f3a6..7f810c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,25 @@ Unreleased ### Fixed +2.0.0 / 2017-01-16 +------------------ + +### Removed +- **BREAKING:** Removed support for Node `v0.12`. The Node foundation stopped officially supporting it +on Jan 1st, 2017. +- **BREAKING:** Remove `walk()` and `walkSync()`. `walkSync()` was only part of `fs-extra` for a little +over two months. Use [klaw](https://github.com/jprichardson/node-klaw) instead of `walk()`, in fact, `walk()` was just +an alias to klaw. For `walkSync()` use [klaw-sync](https://github.com/mawni/node-klaw-sync). See: [#338], [#339] + +### Changed +- **BREAKING:** Renamed `clobber` to `overwrite`. This affects `copy()`, `copySync()`, and `move()`. [#330], [#333] +- Moved docs, to `docs/`. [#340] + +### Fixed +- Apply filters to directories in `copySync()` like in `copy()`. [#324] +- A specific condition when disk is under heavy use, `copy()` can fail. [#326] + + 1.0.0 / 2016-11-01 ------------------ @@ -348,6 +367,43 @@ from `~` to `^`. #67 * Moved tests from Jasmine to Mocha +[#344]: https://github.com/jprichardson/node-fs-extra/issues/344 "Licence Year" +[#343]: https://github.com/jprichardson/node-fs-extra/pull/343 "Add klaw-sync link to readme" +[#342]: https://github.com/jprichardson/node-fs-extra/pull/342 "allow preserveTimestamps when use move" +[#341]: https://github.com/jprichardson/node-fs-extra/issues/341 "mkdirp(path.dirname(dest) in move() logic needs cleaning up [question]" +[#340]: https://github.com/jprichardson/node-fs-extra/pull/340 "Move docs to seperate docs folder [documentation]" +[#339]: https://github.com/jprichardson/node-fs-extra/pull/339 "Remove walk() & walkSync() [feature-walk]" +[#338]: https://github.com/jprichardson/node-fs-extra/issues/338 "Remove walk() and walkSync() [feature-walk]" +[#337]: https://github.com/jprichardson/node-fs-extra/issues/337 "copy doesn't return a yieldable value" +[#336]: https://github.com/jprichardson/node-fs-extra/pull/336 "Docs enhanced walk sync [documentation, feature-walk]" +[#335]: https://github.com/jprichardson/node-fs-extra/pull/335 "Refactor move() tests [feature-move]" +[#334]: https://github.com/jprichardson/node-fs-extra/pull/334 "Cleanup lib/move/index.js [feature-move]" +[#333]: https://github.com/jprichardson/node-fs-extra/pull/333 "Rename clobber to overwrite [feature-copy, feature-move]" +[#332]: https://github.com/jprichardson/node-fs-extra/pull/332 "BREAKING: Drop Node v0.12 & io.js support" +[#331]: https://github.com/jprichardson/node-fs-extra/issues/331 "Add support for chmodr [enhancement, future]" +[#330]: https://github.com/jprichardson/node-fs-extra/pull/330 "BREAKING: Do not error when copy destination exists & clobber: false [feature-copy]" +[#329]: https://github.com/jprichardson/node-fs-extra/issues/329 "Does .walk() scale to large directories? [question]" +[#328]: https://github.com/jprichardson/node-fs-extra/issues/328 "Copying files corrupts [feature-copy, needs-confirmed]" +[#327]: https://github.com/jprichardson/node-fs-extra/pull/327 "Use writeStream 'finish' event instead of 'close' [bug, feature-copy]" +[#326]: https://github.com/jprichardson/node-fs-extra/issues/326 "fs.copy fails with chmod error when disk under heavy use [bug, feature-copy]" +[#325]: https://github.com/jprichardson/node-fs-extra/issues/325 "ensureDir is difficult to promisify [enhancement]" +[#324]: https://github.com/jprichardson/node-fs-extra/pull/324 "copySync() should apply filter to directories like copy() [bug, feature-copy]" +[#323]: https://github.com/jprichardson/node-fs-extra/issues/323 "Support for `dest` being a directory when using `copy*()`?" +[#322]: https://github.com/jprichardson/node-fs-extra/pull/322 "Add fs-promise as fs-extra-promise alternative" +[#321]: https://github.com/jprichardson/node-fs-extra/issues/321 "fs.copy() with clobber set to false return EEXIST error [feature-copy]" +[#320]: https://github.com/jprichardson/node-fs-extra/issues/320 "fs.copySync: Error: EPERM: operation not permitted, unlink " +[#319]: https://github.com/jprichardson/node-fs-extra/issues/319 "Create directory if not exists" +[#318]: https://github.com/jprichardson/node-fs-extra/issues/318 "Support glob patterns [enhancement, future]" +[#317]: https://github.com/jprichardson/node-fs-extra/pull/317 "Adding copy sync test for src file without write perms" +[#316]: https://github.com/jprichardson/node-fs-extra/pull/316 "Remove move()'s broken limit option [feature-move]" +[#315]: https://github.com/jprichardson/node-fs-extra/pull/315 "Fix move clobber tests to work around graceful-fs bug." +[#314]: https://github.com/jprichardson/node-fs-extra/issues/314 "move() limit option [documentation, enhancement, feature-move]" +[#313]: https://github.com/jprichardson/node-fs-extra/pull/313 "Test that remove() ignores glob characters." +[#312]: https://github.com/jprichardson/node-fs-extra/pull/312 "Enhance walkSync() to return items with path and stats [feature-walk]" +[#311]: https://github.com/jprichardson/node-fs-extra/issues/311 "move() not work when dest name not provided [feature-move]" +[#310]: https://github.com/jprichardson/node-fs-extra/issues/310 "Edit walkSync to return items like what walk emits [documentation, enhancement, feature-walk]" +[#309]: https://github.com/jprichardson/node-fs-extra/issues/309 "moveSync support [enhancement, feature-move]" +[#308]: https://github.com/jprichardson/node-fs-extra/pull/308 "Fix incorrect anchor link" [#307]: https://github.com/jprichardson/node-fs-extra/pull/307 "Fix coverage" [#306]: https://github.com/jprichardson/node-fs-extra/pull/306 "Update devDeps, fix lint error" [#305]: https://github.com/jprichardson/node-fs-extra/pull/305 "Re-add Coveralls" diff --git a/LICENSE b/LICENSE index f109d236..93546dfb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ (The MIT License) -Copyright (c) 2011-2016 JP Richardson +Copyright (c) 2011-2017 JP Richardson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, diff --git a/README.md b/README.md index 7d3f90fe..445b0418 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,6 @@ Node.js: fs-extra Standard JavaScript -**NOTE (2016-11-01):** Node v0.12 will be unsupported on 2016-12-31. - Why? ---- @@ -37,27 +35,27 @@ Usage You don't ever need to include the original `fs` module again: ```js -var fs = require('fs') // this is no longer necessary +const fs = require('fs') // this is no longer necessary ``` you can now do this: ```js -var fs = require('fs-extra') +const fs = require('fs-extra') ``` or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want to name your `fs` variable `fse` like so: ```js -var fse = require('fs-extra') +const fse = require('fs-extra') ``` you can also keep both, but it's redundant: ```js -var fs = require('fs') -var fse = require('fs-extra') +const fs = require('fs') +const fse = require('fs-extra') ``` Sync vs Async @@ -69,9 +67,9 @@ Sync methods on the other hand will throw if an error occurs. Example: ```js -var fs = require('fs-extra') +const fs = require('fs-extra') -fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) { +fs.copy('/tmp/myfile', '/tmp/mynewfile', err => { if (err) return console.error(err) console.log("success!") }); @@ -87,340 +85,38 @@ try { Methods ------- -- [copy](#copy) -- [copySync](#copy) -- [emptyDir](#emptydirdir-callback) -- [emptyDirSync](#emptydirdir-callback) -- [ensureFile](#ensurefilefile-callback) -- [ensureFileSync](#ensurefilefile-callback) -- [ensureDir](#ensuredirdir-callback) -- [ensureDirSync](#ensuredirdir-callback) -- [ensureLink](#ensurelinksrcpath-dstpath-callback) -- [ensureLinkSync](#ensurelinksrcpath-dstpath-callback) -- [ensureSymlink](#ensuresymlinksrcpath-dstpath-type-callback) -- [ensureSymlinkSync](#ensuresymlinksrcpath-dstpath-type-callback) -- [mkdirs](#mkdirsdir-callback) -- [mkdirsSync](#mkdirsdir-callback) -- [move](#movesrc-dest-options-callback) -- [outputFile](#outputfilefile-data-options-callback) -- [outputFileSync](#outputfilefile-data-options-callback) -- [outputJson](#outputjsonfile-data-options-callback) -- [outputJsonSync](#outputjsonfile-data-options-callback) -- [readJson](#readjsonfile-options-callback) -- [readJsonSync](#readjsonfile-options-callback) -- [remove](#removedir-callback) -- [removeSync](#removedir-callback) -- [walk](#walk) -- [walkSync](#walksyncdir) -- [writeJson](#writejsonfile-object-options-callback) -- [writeJsonSync](#writejsonfile-object-options-callback) +- [copy](docs/copy.md) +- [copySync](docs/copy.md) +- [emptyDir](docs/emptyDir.md) +- [emptyDirSync](docs/emptyDir.md) +- [ensureFile](docs/ensureFile.md) +- [ensureFileSync](docs/ensureFile.md) +- [ensureDir](docs/ensureDir.md) +- [ensureDirSync](docs/ensureDir.md) +- [ensureLink](docs/ensureLink.md) +- [ensureLinkSync](docs/ensureLink.md) +- [ensureSymlink](docs/ensureSymlink.md) +- [ensureSymlinkSync](docs/ensureSymlink.md) +- [mkdirs](docs/ensureDir.md) +- [mkdirsSync](docs/ensureDir.md) +- [move](docs/move.md) +- [outputFile](docs/outputFile.md) +- [outputFileSync](docs/outputFile.md) +- [outputJson](docs/outputJson.md) +- [outputJsonSync](docs/outputJson.md) +- [readJson](docs/readJson.md) +- [readJsonSync](docs/readJson.md) +- [remove](docs/remove.md) +- [removeSync](docs/remove.md) +- [writeJson](docs/writeJson.md) +- [writeJsonSync](docs/writeJson.md) **NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`. +### What happened to `walk()` and `walkSync()`? -### copy() - -**copy(src, dest, [options], callback)** - - -Copy a file or directory. The directory can have contents. Like `cp -r`. - -Options: -- overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior. -- errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`. -- dereference (boolean): dereference symlinks, default is `false`. -- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`. -- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background). _Warning: `copySync` currently applies the filter only to files (see [#180](https://github.com/jprichardson/node-fs-extra/issues/180)). This will be fixed in a future release._ - -Sync: `copySync()` - -Example: - -```js -var fs = require('fs-extra') - -fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) { - if (err) return console.error(err) - console.log("success!") -}) // copies file - -fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) { - if (err) return console.error(err) - console.log('success!') -}) // copies directory, even if it has subdirectories or files -``` - - -### emptyDir(dir, [callback]) - -Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted. - -Alias: `emptydir()` - -Sync: `emptyDirSync()`, `emptydirSync()` - -Example: - -```js -var fs = require('fs-extra') - -// assume this directory has a lot of files and folders -fs.emptyDir('/tmp/some/dir', function (err) { - if (!err) console.log('success!') -}) -``` - - -### ensureFile(file, callback) - -Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**. - -Alias: `createFile()` - -Sync: `createFileSync()`,`ensureFileSync()` - - -Example: - -```js -var fs = require('fs-extra') - -var file = '/tmp/this/path/does/not/exist/file.txt' -fs.ensureFile(file, function (err) { - console.log(err) // => null - // file has now been created, including the directory it is to be placed in -}) -``` - - -### ensureDir(dir, callback) - -Ensures that the directory exists. If the directory structure does not exist, it is created. - -Sync: `ensureDirSync()` - - -Example: - -```js -var fs = require('fs-extra') - -var dir = '/tmp/this/path/does/not/exist' -fs.ensureDir(dir, function (err) { - console.log(err) // => null - // dir has now been created, including the directory it is to be placed in -}) -``` - - -### ensureLink(srcpath, dstpath, callback) - -Ensures that the link exists. If the directory structure does not exist, it is created. - -Sync: `ensureLinkSync()` - - -Example: - -```js -var fs = require('fs-extra') - -var srcpath = '/tmp/file.txt' -var dstpath = '/tmp/this/path/does/not/exist/file.txt' -fs.ensureLink(srcpath, dstpath, function (err) { - console.log(err) // => null - // link has now been created, including the directory it is to be placed in -}) -``` - - -### ensureSymlink(srcpath, dstpath, [type], callback) - -Ensures that the symlink exists. If the directory structure does not exist, it is created. - -Sync: `ensureSymlinkSync()` - - -Example: - -```js -var fs = require('fs-extra') - -var srcpath = '/tmp/file.txt' -var dstpath = '/tmp/this/path/does/not/exist/file.txt' -fs.ensureSymlink(srcpath, dstpath, function (err) { - console.log(err) // => null - // symlink has now been created, including the directory it is to be placed in -}) -``` - - -### mkdirs(dir, callback) - -Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`. - -Alias: `mkdirp()` - -Sync: `mkdirsSync()` / `mkdirpSync()` - - -Examples: - -```js -var fs = require('fs-extra') - -fs.mkdirs('/tmp/some/long/path/that/prob/doesnt/exist', function (err) { - if (err) return console.error(err) - console.log("success!") -}) - -fs.mkdirsSync('/tmp/another/path') -``` - - -### move(src, dest, [options], callback) - -Moves a file or directory, even across devices. - -Options: -- overwrite (boolean): overwrite existing file or directory, default is `false` - -Example: - -```js -var fs = require('fs-extra') - -fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function (err) { - if (err) return console.error(err) - console.log("success!") -}) -``` - - -### outputFile(file, data, [options], callback) - -Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback). - -Sync: `outputFileSync()` - - -Example: - -```js -var fs = require('fs-extra') -var file = '/tmp/this/path/does/not/exist/file.txt' - -fs.outputFile(file, 'hello!', function (err) { - console.log(err) // => null - - fs.readFile(file, 'utf8', function (err, data) { - console.log(data) // => hello! - }) -}) -``` - - - -### outputJson(file, data, [options], callback) - -Almost the same as `writeJson`, except that if the directory does not exist, it's created. -`options` are what you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback). - -Alias: `outputJSON()` - -Sync: `outputJsonSync()`, `outputJSONSync()` - - -Example: - -```js -var fs = require('fs-extra') -var file = '/tmp/this/path/does/not/exist/file.txt' - -fs.outputJson(file, {name: 'JP'}, function (err) { - console.log(err) // => null - - fs.readJson(file, function(err, data) { - console.log(data.name) // => JP - }) -}) -``` - - - -### readJson(file, [options], callback) - -Reads a JSON file and then parses it into an object. `options` are the same -that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback). - -Alias: `readJSON()` - -Sync: `readJsonSync()`, `readJSONSync()` - - -Example: - -```js -var fs = require('fs-extra') - -fs.readJson('./package.json', function (err, packageObj) { - console.log(packageObj.version) // => 0.1.3 -}) -``` - -`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example: - -```js -var fs = require('fs-extra') -var file = path.join('/tmp/some-invalid.json') -var data = '{not valid JSON' -fs.writeFileSync(file, data) - -var obj = fs.readJsonSync(file, {throws: false}) -console.log(obj) // => null -``` - - -### remove(dir, callback) - -Removes a file or directory. The directory can have contents. Like `rm -rf`. - -Sync: `removeSync()` - - -Examples: - -```js -var fs = require('fs-extra') - -fs.remove('/tmp/myfile', function (err) { - if (err) return console.error(err) - - console.log('success!') -}) - -fs.removeSync('/home/jprichardson') //I just deleted my entire HOME directory. -``` - - -### writeJson(file, object, [options], callback) - -Writes an object to a JSON file. `options` are the same that -you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback). - -Alias: `writeJSON()` - -Sync: `writeJsonSync()`, `writeJSONSync()` - -Example: - -```js -var fs = require('fs-extra') -fs.writeJson('./package.json', {name: 'fs-extra'}, function (err) { - console.log(err) -}) -``` +They were removed from `fs-extra` in v2.0.0. If you need the functionality, `walk` and `walkSync` are available as separate packages, [`klaw`](https://github.com/jprichardson/node-klaw) and [`klaw-sync`](https://github.com/manidlou/node-klaw-sync). Third Party @@ -432,8 +128,8 @@ Use [Bluebird](https://github.com/petkaantonov/bluebird). See https://github.com explicitly listed as supported. ```js -var Promise = require('bluebird') -var fs = Promise.promisifyAll(require('fs-extra')) +const Promise = require('bluebird') +const fs = Promise.promisifyAll(require('fs-extra')) ``` Or you can use a dedicated package: @@ -442,6 +138,7 @@ Or you can use a dedicated package: - [`fs-promise`](https://github.com/kevinbeaty/fs-promise) uses [Any Promise](https://github.com/kevinbeaty/any-promise) and also covers [`mz/fs`](https://github.com/normalize/mz/blob/master/fs.js). +- [`fs-p`](https://github.com/grammarly/fs-p) - TypeScript-friendly promises implementation ### TypeScript @@ -539,7 +236,7 @@ License Licensed under MIT -Copyright (c) 2011-2016 [JP Richardson](https://github.com/jprichardson) +Copyright (c) 2011-2017 [JP Richardson](https://github.com/jprichardson) [1]: http://nodejs.org/docs/latest/api/fs.html diff --git a/docs/copy.md b/docs/copy.md new file mode 100644 index 00000000..dac76295 --- /dev/null +++ b/docs/copy.md @@ -0,0 +1,47 @@ +# copy(src, dest, [options], callback) + +Copy a file or directory. The directory can have contents. Like `cp -r`. + +**Sync:** `copySync()` + +## Options: +- overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior. +- errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`. +- dereference (boolean): dereference symlinks, default is `false`. +- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`. +- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background). + +## Example: + +```js +const fs = require('fs-extra') + +fs.copy('/tmp/myfile', '/tmp/mynewfile', err => { + if (err) return console.error(err) + + console.log('success!') +}) // copies file + +fs.copy('/tmp/mydir', '/tmp/mynewdir', err => { + if (err) return console.error(err) + + console.log('success!') +}) // copies directory, even if it has subdirectories or files +``` + +**Using filter function** + +```js +const fs = require('fs-extra') + +const filterFunc = (src, dest) => { + // your logic here + // it will be copied if return true +} + +fs.copy('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc }, err => { + if (err) return console.error(err) + + console.log('success!') +}) +``` diff --git a/docs/emptyDir.md b/docs/emptyDir.md new file mode 100644 index 00000000..d5987e74 --- /dev/null +++ b/docs/emptyDir.md @@ -0,0 +1,20 @@ +# emptyDir(dir, [callback]) + +Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted. + +**Alias:** `emptydir()` + +**Sync:** `emptyDirSync()`, `emptydirSync()` + +## Example: + +```js +const fs = require('fs-extra') + +// assume this directory has a lot of files and folders +fs.emptyDir('/tmp/some/dir', err => { + if (err) return console.error(err) + + console.log('success!') +}) +``` diff --git a/docs/ensureDir.md b/docs/ensureDir.md new file mode 100644 index 00000000..9a3015c8 --- /dev/null +++ b/docs/ensureDir.md @@ -0,0 +1,19 @@ +# ensureDir(dir, callback) + +Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`. + +**Aliases:** `mkdirs()`, `mkdirp()` + +**Sync:** `ensureDirSync()`, `mkdirsSync()`, `mkdirpSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const dir = '/tmp/this/path/does/not/exist' +fs.ensureDir(dir, err => { + console.log(err) // => null + // dir has now been created, including the directory it is to be placed in +}) +``` diff --git a/docs/ensureFile.md b/docs/ensureFile.md new file mode 100644 index 00000000..b94d0e27 --- /dev/null +++ b/docs/ensureFile.md @@ -0,0 +1,19 @@ +# ensureFile(file, callback) + +Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**. + +**Alias:** `createFile()` + +**Sync:** `createFileSync()`,`ensureFileSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureFile(file, err => { + console.log(err) // => null + // file has now been created, including the directory it is to be placed in +}) +``` diff --git a/docs/ensureLink.md b/docs/ensureLink.md new file mode 100644 index 00000000..69cbb247 --- /dev/null +++ b/docs/ensureLink.md @@ -0,0 +1,18 @@ +# ensureLink(srcpath, dstpath, callback) + +Ensures that the link exists. If the directory structure does not exist, it is created. + +**Sync:** `ensureLinkSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const srcpath = '/tmp/file.txt' +const dstpath = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureLink(srcpath, dstpath, err => { + console.log(err) // => null + // link has now been created, including the directory it is to be placed in +}) +``` diff --git a/docs/ensureSymlink.md b/docs/ensureSymlink.md new file mode 100644 index 00000000..8240e1fa --- /dev/null +++ b/docs/ensureSymlink.md @@ -0,0 +1,18 @@ +# ensureSymlink(srcpath, dstpath, [type], callback) + +Ensures that the symlink exists. If the directory structure does not exist, it is created. + +**Sync:** `ensureSymlinkSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const srcpath = '/tmp/file.txt' +const dstpath = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureSymlink(srcpath, dstpath, err => { + console.log(err) // => null + // symlink has now been created, including the directory it is to be placed in +}) +``` diff --git a/docs/move.md b/docs/move.md new file mode 100644 index 00000000..c7dc77ed --- /dev/null +++ b/docs/move.md @@ -0,0 +1,18 @@ +# move(src, dest, [options], callback) + +Moves a file or directory, even across devices. + +## Options: +- overwrite (boolean): overwrite existing file or directory, default is `false` + +## Example: + +```js +const fs = require('fs-extra') + +fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', err => { + if (err) return console.error(err) + + console.log('success!') +}) +``` diff --git a/docs/outputFile.md b/docs/outputFile.md new file mode 100644 index 00000000..df320df2 --- /dev/null +++ b/docs/outputFile.md @@ -0,0 +1,20 @@ +# outputFile(file, data, [options], callback) + +Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback). + +**Sync:** `outputFileSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.txt' +fs.outputFile(file, 'hello!', err => { + console.log(err) // => null + + fs.readFile(file, 'utf8', (err, data) => { + console.log(data) // => hello! + }) +}) +``` diff --git a/docs/outputJson.md b/docs/outputJson.md new file mode 100644 index 00000000..0d419025 --- /dev/null +++ b/docs/outputJson.md @@ -0,0 +1,23 @@ +# outputJson(file, data, [options], callback) + +Almost the same as [`writeJson`](writeJson.md), except that if the directory does not exist, it's created. +`options` are what you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback). + +**Alias:** `outputJSON()` + +**Sync:** `outputJsonSync()`, `outputJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.txt' +fs.outputJson(file, {name: 'JP'}, err => { + console.log(err) // => null + + fs.readJson(file, (err, data) => { + console.log(data.name) // => JP + }) +}) +``` diff --git a/docs/readJson.md b/docs/readJson.md new file mode 100644 index 00000000..25a0f973 --- /dev/null +++ b/docs/readJson.md @@ -0,0 +1,32 @@ +# readJson(file, [options], callback) + +Reads a JSON file and then parses it into an object. `options` are the same +that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback). + +**Alias:** `readJSON()` + +**Sync:** `readJsonSync()`, `readJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +fs.readJson('./package.json', (err, packageObj) => { + console.log(packageObj.version) // => 0.1.3 +}) +``` + +--- + +`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example: + +```js +const fs = require('fs-extra') +const file = path.join('/tmp/some-invalid.json') +const data = '{not valid JSON' +fs.writeFileSync(file, data) + +const obj = fs.readJsonSync(file, {throws: false}) +console.log(obj) // => null +``` diff --git a/docs/remove.md b/docs/remove.md new file mode 100644 index 00000000..c96c47b8 --- /dev/null +++ b/docs/remove.md @@ -0,0 +1,19 @@ +# remove(dir, callback) + +Removes a file or directory. The directory can have contents. Like `rm -rf`. + +**Sync:** `removeSync()` + +## Example: + +```js +const fs = require('fs-extra') + +fs.remove('/tmp/myfile', err => { + if (err) return console.error(err) + + console.log('success!') +}) + +fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory. +``` diff --git a/docs/writeJson.md b/docs/writeJson.md new file mode 100644 index 00000000..8f00106b --- /dev/null +++ b/docs/writeJson.md @@ -0,0 +1,24 @@ +# writeJson(file, object, [options], callback) + +Writes an object to a JSON file. `options` are the same that +you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback). + +**Alias:** `writeJSON()` + +**Sync:** `writeJsonSync()`, `writeJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +fs.writeJson('./package.json', {name: 'fs-extra'}, err => { + if (err) return console.error(err) + + console.log('success!') +}) +``` + +--- + +**See also:** [`outputJson()`](outputJson.md) diff --git a/lib/__tests__/fs-integration.test.js b/lib/__tests__/fs-integration.test.js index efd7ba90..4ff52c59 100644 --- a/lib/__tests__/fs-integration.test.js +++ b/lib/__tests__/fs-integration.test.js @@ -1,31 +1,31 @@ -var assert = require('assert') -var path = require('path') -var os = require('os') -var fs = require('fs') -var fse = require('../') +'use strict' + +const os = require('os') +const fs = require('fs') +const fse = require('../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('native fs', function () { - var TEST_DIR +describe('native fs', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'native-fs') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should use native fs methods', function () { - var file = path.join(TEST_DIR, 'write.txt') + it('should use native fs methods', () => { + const file = path.join(TEST_DIR, 'write.txt') fse.writeFileSync(file, 'hello') - var data = fse.readFileSync(file, 'utf8') + const data = fse.readFileSync(file, 'utf8') assert.equal(data, 'hello') }) - it('should have native fs constants', function () { + it('should have native fs constants', () => { // Node.js v0.12 / IO.js if ('F_OK' in fs) { assert.equal(fse.F_OK, fs.F_OK) diff --git a/lib/copy-sync/__tests__/broken-symlink.test.js b/lib/copy-sync/__tests__/broken-symlink.test.js index 7ba184b0..c5772ed2 100644 --- a/lib/copy-sync/__tests__/broken-symlink.test.js +++ b/lib/copy-sync/__tests__/broken-symlink.test.js @@ -1,52 +1,48 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var copySync = require('../copy-sync') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const copySync = require('../copy-sync') /* global afterEach, beforeEach, describe, it */ -describe('copy-sync / broken symlink', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks') - var src = path.join(TEST_DIR, 'src') - var out = path.join(TEST_DIR, 'out') +describe('copy-sync / broken symlink', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks') + const src = path.join(TEST_DIR, 'src') + const out = path.join(TEST_DIR, 'out') - beforeEach(function (done) { - fse.emptyDir(TEST_DIR, function (err) { + beforeEach(done => { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) createFixtures(src, done) }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) - - it('should copy broken symlinks by default', function () { - assert.doesNotThrow(function () { - copySync(src, out) - }) + afterEach(done => fse.remove(TEST_DIR, done)) + it('should copy broken symlinks by default', () => { + assert.doesNotThrow(() => copySync(src, out)) assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), path.join(src, 'does-not-exist')) }) - it('should throw an error when dereference=true', function () { - assert.throws(function () { - copySync(src, out, {dereference: true}) - }, function (err) { - return err.code === 'ENOENT' - }) + it('should throw an error when dereference=true', () => { + assert.throws(() => copySync(src, out, {dereference: true}), err => err.code === 'ENOENT') }) }) function createFixtures (srcDir, callback) { - fs.mkdir(srcDir, function (err) { + fs.mkdir(srcDir, err => { + let brokenFile + let brokenFileLink + if (err) return callback(err) try { - var brokenFile = path.join(srcDir, 'does-not-exist') - var brokenFileLink = path.join(srcDir, 'broken-symlink') + brokenFile = path.join(srcDir, 'does-not-exist') + brokenFileLink = path.join(srcDir, 'broken-symlink') fs.writeFileSync(brokenFile, 'does not matter') fs.symlinkSync(brokenFile, brokenFileLink, 'file') } catch (err) { diff --git a/lib/copy-sync/__tests__/copy-sync-dir.test.js b/lib/copy-sync/__tests__/copy-sync-dir.test.js index d457982a..82c360e9 100644 --- a/lib/copy-sync/__tests__/copy-sync-dir.test.js +++ b/lib/copy-sync/__tests__/copy-sync-dir.test.js @@ -1,84 +1,109 @@ -var assert = require('assert') -var crypto = require('crypto') -var os = require('os') -var path = require('path') -var fs = require(process.cwd()) +'use strict' + +const fs = require(process.cwd()) +const os = require('os') +const path = require('path') +const assert = require('assert') +const crypto = require('crypto') /* global beforeEach, describe, it */ -describe('+ copySync()', function () { - var TEST_DIR - var SIZE = 16 * 64 * 1024 + 7 - var src, dest +describe('+ copySync()', () => { + const SIZE = 16 * 64 * 1024 + 7 + let TEST_DIR + let src, dest - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-dir') src = path.join(TEST_DIR, 'src') dest = path.join(TEST_DIR, 'dest') fs.emptyDir(TEST_DIR, done) }) - describe('> when the source is a directory', function () { - it('should copy the directory synchronously', function () { - var FILES = 2 - var i, j - var src = path.join(TEST_DIR, 'src') - var dest = path.join(TEST_DIR, 'dest') + describe('> when the source is a directory', () => { + it('should copy the directory synchronously', () => { + const FILES = 2 + + src = path.join(TEST_DIR, 'src') + dest = path.join(TEST_DIR, 'dest') - fs.mkdirsSync(src) + fs.mkdirSync(src) - for (i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE)) } - var subdir = path.join(src, 'subdir') + const subdir = path.join(src, 'subdir') - fs.mkdirsSync(subdir) + fs.mkdirSync(subdir) - for (i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE)) } fs.copySync(src, dest) assert(fs.existsSync(dest)) - for (i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { assert(fs.existsSync(path.join(dest, i.toString()))) } - var destSub = path.join(dest, 'subdir') - for (j = 0; j < FILES; ++j) { + const destSub = path.join(dest, 'subdir') + for (let j = 0; j < FILES; ++j) { assert(fs.existsSync(path.join(destSub, j.toString()))) } }) - it('should preserve symbolic links', function () { - fs.mkdirsSync(src) + it('should preserve symbolic links', () => { + fs.mkdirSync(src) fs.symlinkSync('destination', path.join(src, 'symlink')) fs.copySync(src, dest) - var link = fs.readlinkSync(path.join(dest, 'symlink')) + const link = fs.readlinkSync(path.join(dest, 'symlink')) assert.strictEqual(link, 'destination') }) - it('should should apply filter recursively', function () { - var FILES = 2 - var filter = function (s) { - // Don't match anything that ends with a digit higher than 0: - return /(0|\D)$/i.test(s) - } + describe('> when the destination dir does not exist', () => { + it('should create the destination directory and copy the file', () => { + const src = path.join(TEST_DIR, 'data/') + fs.mkdirSync(src) + + const d1 = 'file1' + const d2 = 'file2' + + fs.writeFileSync(path.join(src, 'f1.txt'), d1) + fs.writeFileSync(path.join(src, 'f2.txt'), d2) + + const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') - fs.mkdirsSync(src) + fs.copySync(src, dest) + + const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') + const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') + + assert.strictEqual(d1, o1) + assert.strictEqual(d2, o2) + }) + }) + }) - for (var i = 0; i < FILES; ++i) { + describe('> when filter is used', () => { + it('should should apply filter recursively', () => { + const FILES = 2 + // Don't match anything that ends with a digit higher than 0: + const filter = s => /(0|\D)$/i.test(s) + + fs.mkdirSync(src) + + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE)) } - var subdir = path.join(src, 'subdir') - fs.mkdirsSync(subdir) + const subdir = path.join(src, 'subdir') + fs.mkdirSync(subdir) - for (i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE)) } @@ -87,7 +112,7 @@ describe('+ copySync()', function () { assert(fs.existsSync(dest)) assert(FILES > 1) - for (i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { if (i === 0) { assert(fs.existsSync(path.join(dest, i.toString()))) } else { @@ -95,9 +120,9 @@ describe('+ copySync()', function () { } } - var destSub = path.join(dest, 'subdir') + const destSub = path.join(dest, 'subdir') - for (var j = 0; j < FILES; ++j) { + for (let j = 0; j < FILES; ++j) { if (j === 0) { assert(fs.existsSync(path.join(destSub, j.toString()))) } else { @@ -106,16 +131,14 @@ describe('+ copySync()', function () { } }) - it('should apply the filter to directory names', function () { - var IGNORE = 'ignore' - var filter = function (p) { - return !~p.indexOf(IGNORE) - } + it('should apply the filter to directory names', () => { + const IGNORE = 'ignore' + const filter = p => !~p.indexOf(IGNORE) - fs.mkdirsSync(src) + fs.mkdirSync(src) - var ignoreDir = path.join(src, IGNORE) - fs.mkdirsSync(ignoreDir) + const ignoreDir = path.join(src, IGNORE) + fs.mkdirSync(ignoreDir) fs.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE)) @@ -125,27 +148,57 @@ describe('+ copySync()', function () { assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored') }) - describe('> when the destination dir does not exist', function () { - it('should create the destination directory and copy the file', function () { - var src = path.join(TEST_DIR, 'data/') + it('should apply filter when it is applied only to dest', done => { + const timeCond = new Date().getTime() + + const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond + + const dest = path.join(TEST_DIR, 'dest') + + setTimeout(() => { fs.mkdirSync(src) + fs.writeFileSync(path.join(src, 'somefile.html'), 'some data') + fs.mkdirSync(dest) + try { + fs.copySync(src, dest, filter) + } catch (err) { + assert.ifError(err) + } + assert(!fs.existsSync(path.join(dest, 'somefile.html'))) + done() + }, 1000) + }) - var d1 = 'file1' - var d2 = 'file2' + it('should apply filter when it is applied to both src and dest', done => { + const timeCond = new Date().getTime() + const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond - fs.writeFileSync(path.join(src, 'f1.txt'), d1) - fs.writeFileSync(path.join(src, 'f2.txt'), d2) + const dest = path.join(TEST_DIR, 'dest') - var dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') + setTimeout(() => { + const srcFile1 = path.join(TEST_DIR, '1.html') + const srcFile2 = path.join(TEST_DIR, '2.css') + const srcFile3 = path.join(TEST_DIR, '3.jade') - fs.copySync(src, dest) + fs.writeFileSync(srcFile1, '') + fs.writeFileSync(srcFile2, '') + fs.writeFileSync(srcFile3, '') - var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') - var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') + const destFile1 = path.join(dest, 'dest1.html') + const destFile2 = path.join(dest, 'dest2.css') + const destFile3 = path.join(dest, 'dest3.jade') - assert.strictEqual(d1, o1) - assert.strictEqual(d2, o2) - }) + fs.mkdirSync(dest) + + fs.copySync(srcFile1, destFile1, filter) + fs.copySync(srcFile2, destFile2, filter) + fs.copySync(srcFile3, destFile3, filter) + + assert(fs.existsSync(destFile1)) + assert(!fs.existsSync(destFile2)) + assert(fs.existsSync(destFile3)) + done() + }, 1000) }) }) }) diff --git a/lib/copy-sync/__tests__/copy-sync-file.test.js b/lib/copy-sync/__tests__/copy-sync-file.test.js index 70e2e5ce..2874c120 100644 --- a/lib/copy-sync/__tests__/copy-sync-file.test.js +++ b/lib/copy-sync/__tests__/copy-sync-file.test.js @@ -1,34 +1,34 @@ -var assert = require('assert') -var crypto = require('crypto') -var os = require('os') -var path = require('path') -var fs = require(process.cwd()) +'use strict' + +const fs = require(process.cwd()) +const os = require('os') +const path = require('path') +const assert = require('assert') +const crypto = require('crypto') /* global afterEach, beforeEach, describe, it */ -var SIZE = 16 * 64 * 1024 + 7 +const SIZE = 16 * 64 * 1024 + 7 -describe('+ copySync()', function () { - var TEST_DIR +describe('+ copySync()', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync') fs.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fs.remove(TEST_DIR, done) - }) + afterEach(done => fs.remove(TEST_DIR, done)) - describe('> when the source is a file', function () { - it('should copy the file synchronously', function () { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + describe('> when the source is a file', () => { + it('should copy the file synchronously', () => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE)) - var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') - var destMd5 = '' + const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') + let destMd5 = '' fs.copySync(fileSrc, fileDest) @@ -36,15 +36,15 @@ describe('+ copySync()', function () { assert.strictEqual(srcMd5, destMd5) }) - it('should follow symlinks', function () { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') - var linkSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy_link') + it('should follow symlinks', () => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + const linkSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy_link') fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE)) - var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') - var destMd5 = '' + const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') + let destMd5 = '' fs.symlinkSync(fileSrc, linkSrc) fs.copySync(linkSrc, fileDest) @@ -52,33 +52,33 @@ describe('+ copySync()', function () { assert.strictEqual(srcMd5, destMd5) }) - it('should maintain file mode', function () { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + it('should maintain file mode', () => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE)) fs.chmodSync(fileSrc, parseInt('750', 8)) fs.copySync(fileSrc, fileDest) - var statSrc = fs.statSync(fileSrc) - var statDest = fs.statSync(fileDest) + const statSrc = fs.statSync(fileSrc) + const statDest = fs.statSync(fileDest) assert.strictEqual(statSrc.mode, statDest.mode) }) - it('should only copy files allowed by filter fn', function () { - var srcFile1 = path.join(TEST_DIR, '1.html') - var srcFile2 = path.join(TEST_DIR, '2.css') - var srcFile3 = path.join(TEST_DIR, '3.jade') + it('should only copy files allowed by filter fn', () => { + const srcFile1 = path.join(TEST_DIR, '1.html') + const srcFile2 = path.join(TEST_DIR, '2.css') + const srcFile3 = path.join(TEST_DIR, '3.jade') fs.writeFileSync(srcFile1, '') fs.writeFileSync(srcFile2, '') fs.writeFileSync(srcFile3, '') - var destFile1 = path.join(TEST_DIR, 'dest1.html') - var destFile2 = path.join(TEST_DIR, 'dest2.css') - var destFile3 = path.join(TEST_DIR, 'dest3.jade') + const destFile1 = path.join(TEST_DIR, 'dest1.html') + const destFile2 = path.join(TEST_DIR, 'dest2.css') + const destFile3 = path.join(TEST_DIR, 'dest3.jade') - var filter = function (s) { return s.split('.').pop() !== 'css' } + const filter = s => s.split('.').pop() !== 'css' fs.copySync(srcFile1, destFile1, filter) fs.copySync(srcFile2, destFile2, filter) @@ -89,43 +89,43 @@ describe('+ copySync()', function () { assert(fs.existsSync(destFile3)) }) - describe('> when the destination dir does not exist', function () { - it('should create the destination directory and copy the file', function () { - var src = path.join(TEST_DIR, 'file.txt') - var dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt') - var data = 'did it copy?\n' + describe('> when the destination dir does not exist', () => { + it('should create the destination directory and copy the file', () => { + const src = path.join(TEST_DIR, 'file.txt') + const dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt') + const data = 'did it copy?\n' fs.writeFileSync(src, data, 'utf8') fs.copySync(src, dest) - var data2 = fs.readFileSync(dest, 'utf8') + const data2 = fs.readFileSync(dest, 'utf8') assert.strictEqual(data, data2) }) }) - describe('> when the source file does not have write permissions', function () { - it('should be able to copy contents of file', function () { - var fileSrc = path.join(TEST_DIR, 'file.txt') - var fileDest = path.join(TEST_DIR, 'file-copy.txt') - var data = 'did it copy?' + describe('> when the source file does not have write permissions', () => { + it('should be able to copy contents of file', () => { + const fileSrc = path.join(TEST_DIR, 'file.txt') + const fileDest = path.join(TEST_DIR, 'file-copy.txt') + const data = 'did it copy?' fs.writeFileSync(fileSrc, data, 'utf8') fs.chmodSync(fileSrc, '0444') fs.copySync(fileSrc, fileDest) - var data2 = fs.readFileSync(fileDest, 'utf8') + const data2 = fs.readFileSync(fileDest, 'utf8') assert.strictEqual(data, data2) }) }) - describe('> when overwrite option is passed', function () { - var src, dest - var srcData = 'some src data' + describe('> when overwrite option is passed', () => { + const srcData = 'some src data' + let src, dest - beforeEach(function () { + beforeEach(() => { src = path.join(TEST_DIR, 'src-file') dest = path.join(TEST_DIR, 'des-file') @@ -133,60 +133,59 @@ describe('+ copySync()', function () { fs.writeFileSync(src, srcData) }) - describe('> when destination file does NOT exist', function () { - describe('> when overwrite is true', function () { - it('should copy the file and not throw an error', function () { + describe('> when destination file does NOT exist', () => { + describe('> when overwrite is true', () => { + it('should copy the file and not throw an error', () => { fs.copySync(src, dest, {overwrite: true}) - var destData = fs.readFileSync(dest, 'utf8') + const destData = fs.readFileSync(dest, 'utf8') assert.strictEqual(srcData, destData) }) }) - describe('> when overwrite is false', function () { - it('should copy the file and not throw an error', function () { + describe('> when overwrite is false', () => { + it('should copy the file and not throw an error', () => { fs.copySync(src, dest, {overwrite: false}) - var destData = fs.readFileSync(dest, 'utf8') + const destData = fs.readFileSync(dest, 'utf8') assert.strictEqual(srcData, destData) }) }) }) - describe('when destination file does exist', function () { - var destData - beforeEach(function () { + describe('when destination file does exist', () => { + let destData + + beforeEach(() => { destData = 'some dest data' fs.writeFileSync(dest, destData) }) - describe('> when overwrite is true', function () { - it('should copy the file and not throw an error', function () { + describe('> when overwrite is true', () => { + it('should copy the file and not throw an error', () => { fs.copySync(src, dest, {overwrite: true}) destData = fs.readFileSync(dest, 'utf8') assert.strictEqual(srcData, destData) }) }) - describe('> when overwrite is false', function () { - it('should not throw an error', function () { + describe('> when overwrite is false', () => { + it('should not throw an error', () => { fs.copySync(src, dest, {overwrite: false}) // copy never happened - var destDataNew = fs.readFileSync(dest, 'utf8') + const destDataNew = fs.readFileSync(dest, 'utf8') assert.strictEqual(destData, destDataNew) }) - it('should throw an error when errorOnExist is true', function () { - assert.throws(function () { - fs.copySync(src, dest, {overwrite: false, errorOnExist: true}) - }) + it('should throw an error when errorOnExist is true', () => { + assert.throws(() => fs.copySync(src, dest, {overwrite: false, errorOnExist: true})) // copy never happened - var destDataNew = fs.readFileSync(dest, 'utf8') + const destDataNew = fs.readFileSync(dest, 'utf8') assert.strictEqual(destData, destDataNew) }) }) - describe('> when overwrite is true and dest is readonly', function () { - it('should copy the file and not throw an error', function () { + describe('> when overwrite is true and dest is readonly', () => { + it('should copy the file and not throw an error', () => { try { fs.chmodSync(dest, parseInt('444', 8)) fs.copySync(src, dest, {overwrite: true}) @@ -200,10 +199,10 @@ describe('+ copySync()', function () { }) }) }) - describe('clobber', function () { - var src, dest, srcData, destData + describe('clobber', () => { + let src, dest, srcData, destData - beforeEach(function () { + beforeEach(() => { src = path.join(TEST_DIR, 'src-file') dest = path.join(TEST_DIR, 'des-file') srcData = 'some src data' @@ -212,11 +211,11 @@ describe('+ copySync()', function () { fs.writeFileSync(dest, destData) }) - it('is an alias for overwrite', function () { + it('is an alias for overwrite', () => { fs.copySync(src, dest, {clobber: false}) // copy never happened - var destDataNew = fs.readFileSync(dest, 'utf8') + const destDataNew = fs.readFileSync(dest, 'utf8') assert.strictEqual(destData, destDataNew) }) }) diff --git a/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js b/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js index 8028b739..8c4bfa9f 100644 --- a/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js +++ b/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js @@ -1,35 +1,37 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var copySync = require('../copy-sync') -var utimes = require('../../util/utimes') +'use strict' + +const fs = require('fs') +const os = require('os') +const path = require('path') +const utimes = require('../../util/utimes') +const assert = require('assert') +const copySync = require('../copy-sync') /* global beforeEach, describe, it */ -describe('copy', function () { - var TEST_DIR +describe('copy', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-preserve-time') require(process.cwd()).emptyDir(TEST_DIR, done) }) - describe('> modification option', function () { - var SRC_FIXTURES_DIR = path.join(__dirname, './fixtures') - var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')] + describe('> modification option', () => { + const SRC_FIXTURES_DIR = path.join(__dirname, './fixtures') + const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')] - describe('> when modified option is turned off', function () { - it('should have different timestamps on copy', function () { - var from = path.join(SRC_FIXTURES_DIR) + describe('> when modified option is turned off', () => { + it('should have different timestamps on copy', () => { + const from = path.join(SRC_FIXTURES_DIR) copySync(from, TEST_DIR, {preserveTimestamps: false}) FILES.forEach(testFile({preserveTimestamps: false})) }) }) - describe('> when modified option is turned on', function () { - it('should have the same timestamps on copy', function () { - var from = path.join(SRC_FIXTURES_DIR) + describe('> when modified option is turned on', () => { + it('should have the same timestamps on copy', () => { + const from = path.join(SRC_FIXTURES_DIR) copySync(from, TEST_DIR, {preserveTimestamps: true}) FILES.forEach(testFile({preserveTimestamps: true})) }) @@ -37,10 +39,10 @@ describe('copy', function () { function testFile (options) { return function (file) { - var a = path.join(SRC_FIXTURES_DIR, file) - var b = path.join(TEST_DIR, file) - var fromStat = fs.statSync(a) - var toStat = fs.statSync(b) + const a = path.join(SRC_FIXTURES_DIR, file) + const b = path.join(TEST_DIR, file) + const fromStat = fs.statSync(a) + const toStat = fs.statSync(b) if (options.preserveTimestamps) { // https://github.com/nodejs/io.js/issues/2069 if (process.platform !== 'win32') { diff --git a/lib/copy-sync/__tests__/symlink.test.js b/lib/copy-sync/__tests__/symlink.test.js index 28905837..3764e599 100644 --- a/lib/copy-sync/__tests__/symlink.test.js +++ b/lib/copy-sync/__tests__/symlink.test.js @@ -1,30 +1,32 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var copySync = require('../copy-sync') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const copySync = require('../copy-sync') /* global afterEach, beforeEach, describe, it */ -describe('copy-sync / symlink', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-symlinks') - var src = path.join(TEST_DIR, 'src') - var out = path.join(TEST_DIR, 'out') +describe('copy-sync / symlink', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-symlinks') + const src = path.join(TEST_DIR, 'src') + const out = path.join(TEST_DIR, 'out') - beforeEach(function (done) { - fse.emptyDir(TEST_DIR, function (err) { + beforeEach(done => { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) createFixtures(src, done) }) }) - afterEach(function (done) { + afterEach(done => { fse.remove(TEST_DIR, done) }) - it('copies symlinks by default', function () { - assert.doesNotThrow(function () { + it('copies symlinks by default', () => { + assert.doesNotThrow(() => { copySync(src, out) }) @@ -32,38 +34,38 @@ describe('copy-sync / symlink', function () { assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), path.join(src, 'dir')) }) - it('copies file contents when dereference=true', function () { + it('copies file contents when dereference=true', () => { try { copySync(src, out, {dereference: true}) } catch (err) { assert.ifError(err) } - var fileSymlinkPath = path.join(out, 'file-symlink') + const fileSymlinkPath = path.join(out, 'file-symlink') assert.ok(fs.lstatSync(fileSymlinkPath).isFile()) assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents') - var dirSymlinkPath = path.join(out, 'dir-symlink') + const dirSymlinkPath = path.join(out, 'dir-symlink') assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory()) assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']) }) }) function createFixtures (srcDir, callback) { - fs.mkdir(srcDir, function (err) { + fs.mkdir(srcDir, err => { if (err) return callback(err) // note: third parameter in symlinkSync is type e.g. 'file' or 'dir' // https://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback try { - var fooFile = path.join(srcDir, 'foo') - var fooFileLink = path.join(srcDir, 'file-symlink') + const fooFile = path.join(srcDir, 'foo') + const fooFileLink = path.join(srcDir, 'file-symlink') fs.writeFileSync(fooFile, 'foo contents') fs.symlinkSync(fooFile, fooFileLink, 'file') - var dir = path.join(srcDir, 'dir') - var dirFile = path.join(dir, 'bar') - var dirLink = path.join(srcDir, 'dir-symlink') + const dir = path.join(srcDir, 'dir') + const dirFile = path.join(dir, 'bar') + const dirLink = path.join(srcDir, 'dir-symlink') fs.mkdirSync(dir) fs.writeFileSync(dirFile, 'bar contents') fs.symlinkSync(dir, dirLink, 'dir') diff --git a/lib/copy-sync/copy-file-sync.js b/lib/copy-sync/copy-file-sync.js index fb9fad69..4e4cd3fd 100644 --- a/lib/copy-sync/copy-file-sync.js +++ b/lib/copy-sync/copy-file-sync.js @@ -1,26 +1,28 @@ -var fs = require('graceful-fs') +'use strict' -var BUF_LENGTH = 64 * 1024 -var _buff = new Buffer(BUF_LENGTH) +const fs = require('graceful-fs') + +const BUF_LENGTH = 64 * 1024 +const _buff = new Buffer(BUF_LENGTH) function copyFileSync (srcFile, destFile, options) { - var overwrite = options.overwrite - var errorOnExist = options.errorOnExist - var preserveTimestamps = options.preserveTimestamps + const overwrite = options.overwrite + const errorOnExist = options.errorOnExist + const preserveTimestamps = options.preserveTimestamps if (fs.existsSync(destFile)) { if (overwrite) { fs.unlinkSync(destFile) } else if (errorOnExist) { - throw new Error(destFile + ' already exists') + throw new Error(`${destFile} already exists`) } else return } - var fdr = fs.openSync(srcFile, 'r') - var stat = fs.fstatSync(fdr) - var fdw = fs.openSync(destFile, 'w', stat.mode) - var bytesRead = 1 - var pos = 0 + const fdr = fs.openSync(srcFile, 'r') + const stat = fs.fstatSync(fdr) + const fdw = fs.openSync(destFile, 'w', stat.mode) + let bytesRead = 1 + let pos = 0 while (bytesRead > 0) { bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos) diff --git a/lib/copy-sync/copy-sync.js b/lib/copy-sync/copy-sync.js index 29812f82..9d5639c3 100644 --- a/lib/copy-sync/copy-sync.js +++ b/lib/copy-sync/copy-sync.js @@ -1,7 +1,9 @@ -var fs = require('graceful-fs') -var path = require('path') -var copyFileSync = require('./copy-file-sync') -var mkdir = require('../mkdirs') +'use strict' + +const fs = require('graceful-fs') +const path = require('path') +const copyFileSync = require('./copy-file-sync') +const mkdir = require('../mkdirs') function copySync (src, dest, options) { if (typeof options === 'function' || options instanceof RegExp) { @@ -22,19 +24,19 @@ function copySync (src, dest, options) { // Warn about using preserveTimestamps on 32-bit node: if (options.preserveTimestamps && process.arch === 'ia32') { - console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' + - 'see https://github.com/jprichardson/node-fs-extra/issues/269') + console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n + see https://github.com/jprichardson/node-fs-extra/issues/269`) } - var stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src) - var destFolder = path.dirname(dest) - var destFolderExists = fs.existsSync(destFolder) - var performCopy = false + const stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src) + const destFolder = path.dirname(dest) + const destFolderExists = fs.existsSync(destFolder) + let performCopy = false if (options.filter instanceof RegExp) { console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function') performCopy = options.filter.test(src) - } else if (typeof options.filter === 'function') performCopy = options.filter(src) + } else if (typeof options.filter === 'function') performCopy = options.filter(src, dest) if (stats.isFile() && performCopy) { if (!destFolderExists) mkdir.mkdirsSync(destFolder) @@ -45,14 +47,14 @@ function copySync (src, dest, options) { }) } else if (stats.isDirectory() && performCopy) { if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest) - var contents = fs.readdirSync(src) - contents.forEach(function (content) { - var opts = options + const contents = fs.readdirSync(src) + contents.forEach(content => { + const opts = options opts.recursive = true copySync(path.join(src, content), path.join(dest, content), opts) }) } else if (options.recursive && stats.isSymbolicLink() && performCopy) { - var srcPath = fs.readlinkSync(src) + const srcPath = fs.readlinkSync(src) fs.symlinkSync(srcPath, dest) } } diff --git a/lib/copy/__tests__/async/copy-gh-89.test.js b/lib/copy/__tests__/async/copy-gh-89.test.js index 4ff9269b..4cfe191c 100644 --- a/lib/copy/__tests__/async/copy-gh-89.test.js +++ b/lib/copy/__tests__/async/copy-gh-89.test.js @@ -1,46 +1,48 @@ +'use strict' + // relevant: https://github.com/jprichardson/node-fs-extra/issues/89 // come up with better file name -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('copy / gh #89', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-gh-89') +describe('copy / gh #89', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-gh-89') - beforeEach(function (done) { + beforeEach(done => { fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { + afterEach(done => { fse.remove(TEST_DIR, done) }) - it('should...', function (done) { - var A = path.join(TEST_DIR, 'A') - var B = path.join(TEST_DIR, 'B') + it('should...', done => { + const A = path.join(TEST_DIR, 'A') + const B = path.join(TEST_DIR, 'B') fs.mkdirSync(A) fs.mkdirSync(B) - var one = path.join(A, 'one.txt') - var two = path.join(A, 'two.txt') - var three = path.join(B, 'three.txt') - var four = path.join(B, 'four.txt') + const one = path.join(A, 'one.txt') + const two = path.join(A, 'two.txt') + const three = path.join(B, 'three.txt') + const four = path.join(B, 'four.txt') fs.writeFileSync(one, '1') fs.writeFileSync(two, '2') fs.writeFileSync(three, '3') fs.writeFileSync(four, '4') - var C = path.join(TEST_DIR, 'C') - fse.copy(A, C, function (err) { + const C = path.join(TEST_DIR, 'C') + fse.copy(A, C, err => { if (err) return done(err) - fse.copy(B, C, function (err) { + fse.copy(B, C, err => { if (err) return done(err) assert(fs.existsSync(path.join(C, 'one.txt'))) diff --git a/lib/copy/__tests__/copy-dev-null.test.js b/lib/copy/__tests__/copy-dev-null.test.js index dfdfea50..77482f0d 100644 --- a/lib/copy/__tests__/copy-dev-null.test.js +++ b/lib/copy/__tests__/copy-dev-null.test.js @@ -1,31 +1,33 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var TEST_DIR = '' +let TEST_DIR = '' -describe('fs-extra', function () { - beforeEach(function (done) { +describe('+ copy() - copy /dev/null', () => { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'test', 'fs-extra', 'copy-dev-null') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ copy()', function () { - it('should error', function (done) { + describe('> when src is /dev/null', () => { + it('should copy successfully', done => { // no /dev/null on windows if (process.platform === 'win32') return done() - var tmpFile = path.join(TEST_DIR, 'foo') - fse.copy('/dev/null', tmpFile, function (err) { + + const tmpFile = path.join(TEST_DIR, 'foo') + + fse.copy('/dev/null', tmpFile, err => { assert.ifError(err) - var stats = fs.lstatSync(tmpFile) + const stats = fs.lstatSync(tmpFile) assert.strictEqual(stats.size, 0) done() }) diff --git a/lib/copy/__tests__/copy-permissions.test.js b/lib/copy/__tests__/copy-permissions.test.js index 16516d61..ccc89cd2 100644 --- a/lib/copy/__tests__/copy-permissions.test.js +++ b/lib/copy/__tests__/copy-permissions.test.js @@ -1,36 +1,38 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global beforeEach, describe, it */ -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) -var o444 = parseInt('444', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) +const o444 = parseInt('444', 8) -describe('copy', function () { - var TEST_DIR +describe('copy', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy') fse.emptyDir(TEST_DIR, done) }) // pretty UNIX specific, may not pass on windows... only tested on Mac OS X 10.9 - it('should maintain file permissions and ownership', function (done) { + it('should maintain file permissions and ownership', done => { if (process.platform === 'win32') return done() // var userid = require('userid') // http://man7.org/linux/man-pages/man2/stat.2.html - var S_IFREG = parseInt('0100000', 8) // regular file - var S_IFDIR = parseInt('0040000', 8) // directory + const S_IFREG = parseInt('0100000', 8) // regular file + const S_IFDIR = parseInt('0040000', 8) // directory // these are Mac specific I think (at least staff), should find Linux equivalent - var gidWheel - var gidStaff + let gidWheel + let gidStaff try { gidWheel = process.getgid() // userid.gid('wheel') @@ -44,48 +46,48 @@ describe('copy', function () { gidStaff = process.getgid() } - var permDir = path.join(TEST_DIR, 'perms') + const permDir = path.join(TEST_DIR, 'perms') fs.mkdirSync(permDir) - var srcDir = path.join(permDir, 'src') + const srcDir = path.join(permDir, 'src') fs.mkdirSync(srcDir) - var f1 = path.join(srcDir, 'f1.txt') + const f1 = path.join(srcDir, 'f1.txt') fs.writeFileSync(f1, '') fs.chmodSync(f1, o666) fs.chownSync(f1, process.getuid(), gidWheel) - var f1stats = fs.lstatSync(f1) + const f1stats = fs.lstatSync(f1) assert.strictEqual(f1stats.mode - S_IFREG, o666) - var d1 = path.join(srcDir, 'somedir') + const d1 = path.join(srcDir, 'somedir') fs.mkdirSync(d1) fs.chmodSync(d1, o777) fs.chownSync(d1, process.getuid(), gidStaff) - var d1stats = fs.lstatSync(d1) + const d1stats = fs.lstatSync(d1) assert.strictEqual(d1stats.mode - S_IFDIR, o777) - var f2 = path.join(d1, 'f2.bin') + const f2 = path.join(d1, 'f2.bin') fs.writeFileSync(f2, '') fs.chmodSync(f2, o777) fs.chownSync(f2, process.getuid(), gidStaff) - var f2stats = fs.lstatSync(f2) + const f2stats = fs.lstatSync(f2) assert.strictEqual(f2stats.mode - S_IFREG, o777) - var d2 = path.join(srcDir, 'crazydir') + const d2 = path.join(srcDir, 'crazydir') fs.mkdirSync(d2) fs.chmodSync(d2, o444) fs.chownSync(d2, process.getuid(), gidWheel) - var d2stats = fs.lstatSync(d2) + const d2stats = fs.lstatSync(d2) assert.strictEqual(d2stats.mode - S_IFDIR, o444) - var destDir = path.join(permDir, 'dest') - fse.copy(srcDir, destDir, function (err) { + const destDir = path.join(permDir, 'dest') + fse.copy(srcDir, destDir, err => { assert.ifError(err) - var newf1stats = fs.lstatSync(path.join(permDir, 'dest/f1.txt')) - var newd1stats = fs.lstatSync(path.join(permDir, 'dest/somedir')) - var newf2stats = fs.lstatSync(path.join(permDir, 'dest/somedir/f2.bin')) - var newd2stats = fs.lstatSync(path.join(permDir, 'dest/crazydir')) + const newf1stats = fs.lstatSync(path.join(permDir, 'dest/f1.txt')) + const newd1stats = fs.lstatSync(path.join(permDir, 'dest/somedir')) + const newf2stats = fs.lstatSync(path.join(permDir, 'dest/somedir/f2.bin')) + const newd2stats = fs.lstatSync(path.join(permDir, 'dest/crazydir')) assert.strictEqual(newf1stats.mode, f1stats.mode) assert.strictEqual(newd1stats.mode, d1stats.mode) diff --git a/lib/copy/__tests__/copy-preserve-time.test.js b/lib/copy/__tests__/copy-preserve-time.test.js index 9de5e0cd..1c9fcb0d 100644 --- a/lib/copy/__tests__/copy-preserve-time.test.js +++ b/lib/copy/__tests__/copy-preserve-time.test.js @@ -1,42 +1,44 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var copy = require('../copy') -var utimes = require('../../util/utimes') +'use strict' + +const fs = require('fs') +const os = require('os') +const path = require('path') +const copy = require('../copy') +const utimes = require('../../util/utimes') +const assert = require('assert') /* global beforeEach, describe, it */ -describe('copy', function () { - var TEST_DIR +describe('copy', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy') require(process.cwd()).emptyDir(TEST_DIR, done) }) - describe('> modification option', function () { - var SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures') - var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')] + describe('> modification option', () => { + const SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures') + const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')] - describe('> when modified option is turned off', function () { - it('should have different timestamps on copy', function (done) { - var from = path.join(SRC_FIXTURES_DIR) - var to = path.join(TEST_DIR) + describe('> when modified option is turned off', () => { + it('should have different timestamps on copy', done => { + const from = path.join(SRC_FIXTURES_DIR) + const to = path.join(TEST_DIR) - copy(from, to, {preserveTimestamps: false}, function () { + copy(from, to, {preserveTimestamps: false}, () => { FILES.forEach(testFile({preserveTimestamps: false})) done() }) }) }) - describe('> when modified option is turned on', function () { - it('should have the same timestamps on copy', function (done) { - var from = path.join(SRC_FIXTURES_DIR) - var to = path.join(TEST_DIR) + describe('> when modified option is turned on', () => { + it('should have the same timestamps on copy', done => { + const from = path.join(SRC_FIXTURES_DIR) + const to = path.join(TEST_DIR) - copy(from, to, {preserveTimestamps: true}, function () { + copy(from, to, {preserveTimestamps: true}, () => { FILES.forEach(testFile({preserveTimestamps: true})) done() }) @@ -45,10 +47,10 @@ describe('copy', function () { function testFile (options) { return function (file) { - var a = path.join(SRC_FIXTURES_DIR, file) - var b = path.join(TEST_DIR, file) - var fromStat = fs.statSync(a) - var toStat = fs.statSync(b) + const a = path.join(SRC_FIXTURES_DIR, file) + const b = path.join(TEST_DIR, file) + const fromStat = fs.statSync(a) + const toStat = fs.statSync(b) if (options.preserveTimestamps) { // https://github.com/nodejs/io.js/issues/2069 if (process.platform !== 'win32') { diff --git a/lib/copy/__tests__/copy.test.js b/lib/copy/__tests__/copy.test.js index 5c1efbcd..6ed64ff8 100644 --- a/lib/copy/__tests__/copy.test.js +++ b/lib/copy/__tests__/copy.test.js @@ -1,44 +1,44 @@ -var assert = require('assert') -var crypto = require('crypto') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') +const crypto = require('crypto') /* global afterEach, beforeEach, describe, it */ -var SIZE = 16 * 64 * 1024 + 7 -var TEST_DIR = '' +const SIZE = 16 * 64 * 1024 + 7 +let TEST_DIR = '' -describe('fs-extra', function () { - beforeEach(function (done) { +describe('fs-extra', () => { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ copy()', function () { - it('should return an error if src and dest are the same', function (done) { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') - fse.copy(fileSrc, fileDest, function (err) { + describe('+ copy()', () => { + it('should return an error if src and dest are the same', done => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + fse.copy(fileSrc, fileDest, err => { assert.equal(err.message, 'Source and destination must not be the same.') done() }) }) - describe('> when the source is a file', function () { - it('should copy the file asynchronously', function (done) { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + describe('> when the source is a file', () => { + it('should copy the file asynchronously', done => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE)) - var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') - var destMd5 = '' + const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex') + let destMd5 = '' - fse.copy(fileSrc, fileDest, function (err) { + fse.copy(fileSrc, fileDest, err => { assert(!err) destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest('hex') assert.strictEqual(srcMd5, destMd5) @@ -46,60 +46,39 @@ describe('fs-extra', function () { }) }) - it('should return an error if the source file does not exist', function (done) { - var fileSrc = 'we-simply-assume-this-file-does-not-exist.bin' - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') + it('should return an error if the source file does not exist', done => { + const fileSrc = 'we-simply-assume-this-file-does-not-exist.bin' + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy') - fse.copy(fileSrc, fileDest, function (err) { + fse.copy(fileSrc, fileDest, err => { assert(err) done() }) }) - it('should only copy files allowed by filter fn', function (done) { - var srcFile1 = path.join(TEST_DIR, '1.css') - fs.writeFileSync(srcFile1, '') - var destFile1 = path.join(TEST_DIR, 'dest1.css') - var filter = function (s) { return s.split('.').pop() !== 'css' } - fse.copy(srcFile1, destFile1, filter, function () { - assert(!fs.existsSync(destFile1)) - done() - }) - }) - - it('accepts options object in place of filter', function (done) { - var srcFile1 = path.join(TEST_DIR, '1.jade') - fs.writeFileSync(srcFile1, '') - var destFile1 = path.join(TEST_DIR, 'dest1.jade') - var options = {filter: function (s) { return /.html$|.css$/i.test(s) }} - fse.copy(srcFile1, destFile1, options, function () { - assert(!fs.existsSync(destFile1)) - done() - }) - }) + it('should copy to a destination file with two \'$\' characters in name (eg: TEST_fs-extra_$$_copy)', done => { + const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') + const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_$$_copy') - it('should copy to a destination file with two \'$\' characters in name (eg: TEST_fs-extra_$$_copy)', function (done) { - var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src') - var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_$$_copy') fs.writeFileSync(fileSrc, '') - fse.copy(fileSrc, fileDest, function (err) { + fse.copy(fileSrc, fileDest, err => { assert(!err) fs.statSync(fileDest) done() }) }) - describe('> when the destination dir does not exist', function () { - it('should create the destination directory and copy the file', function (done) { - var src = path.join(TEST_DIR, 'file.txt') - var dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt') - var data = 'did it copy?\n' + describe('> when the destination dir does not exist', () => { + it('should create the destination directory and copy the file', done => { + const src = path.join(TEST_DIR, 'file.txt') + const dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt') + const data = 'did it copy?\n' fs.writeFileSync(src, data, 'utf8') - fse.copy(src, dest, function (err) { - var data2 = fs.readFileSync(dest, 'utf8') + fse.copy(src, dest, err => { + const data2 = fs.readFileSync(dest, 'utf8') assert.strictEqual(data, data2) done(err) }) @@ -107,46 +86,46 @@ describe('fs-extra', function () { }) }) - describe('> when the source is a directory', function () { - describe('> when the source directory does not exist', function () { - it('should return an error', function (done) { - var ts = path.join(TEST_DIR, 'this_dir_does_not_exist') - var td = path.join(TEST_DIR, 'this_dir_really_does_not_matter') - fse.copy(ts, td, function (err) { + describe('> when the source is a directory', () => { + describe('> when the source directory does not exist', () => { + it('should return an error', done => { + const ts = path.join(TEST_DIR, 'this_dir_does_not_exist') + const td = path.join(TEST_DIR, 'this_dir_really_does_not_matter') + fse.copy(ts, td, err => { assert(err) done() }) }) }) - it('should copy the directory asynchronously', function (done) { - var FILES = 2 - var src = path.join(TEST_DIR, 'src') - var dest = path.join(TEST_DIR, 'dest') + it('should copy the directory asynchronously', done => { + const FILES = 2 + const src = path.join(TEST_DIR, 'src') + const dest = path.join(TEST_DIR, 'dest') - fse.mkdirs(src, function (err) { + fse.mkdirs(src, err => { assert(!err) - for (var i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE)) } - var subdir = path.join(src, 'subdir') - fse.mkdirs(subdir, function (err) { + const subdir = path.join(src, 'subdir') + fse.mkdirs(subdir, err => { assert(!err) - for (var i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE)) } - fse.copy(src, dest, function (err) { + fse.copy(src, dest, err => { assert.ifError(err) assert(fs.existsSync(dest)) - for (var i = 0; i < FILES; ++i) { + for (let i = 0; i < FILES; ++i) { assert(fs.existsSync(path.join(dest, i.toString()))) } - var destSub = path.join(dest, 'subdir') - for (var j = 0; j < FILES; ++j) { + const destSub = path.join(dest, 'subdir') + for (let j = 0; j < FILES; ++j) { assert(fs.existsSync(path.join(destSub, j.toString()))) } @@ -156,21 +135,21 @@ describe('fs-extra', function () { }) }) - describe('> when the destination dir does not exist', function () { - it('should create the destination directory and copy the file', function (done) { - var src = path.join(TEST_DIR, 'data/') + describe('> when the destination dir does not exist', () => { + it('should create the destination directory and copy the file', done => { + const src = path.join(TEST_DIR, 'data/') fse.mkdirsSync(src) - var d1 = 'file1' - var d2 = 'file2' + const d1 = 'file1' + const d2 = 'file2' fs.writeFileSync(path.join(src, 'f1.txt'), d1) fs.writeFileSync(path.join(src, 'f2.txt'), d2) - var dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') + const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir') - fse.copy(src, dest, function (err) { - var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') - var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') + fse.copy(src, dest, err => { + const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8') + const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8') assert.strictEqual(d1, o1) assert.strictEqual(d2, o2) @@ -180,14 +159,170 @@ describe('fs-extra', function () { }) }) - describe('> when src dir does not exist', function () { - it('should return an error', function (done) { - fse.copy('/does/not/exist', '/something/else', function (err) { + describe('> when src dir does not exist', () => { + it('should return an error', done => { + fse.copy('/does/not/exist', '/something/else', err => { assert(err instanceof Error) done() }) }) }) }) + + describe('> when filter is used', () => { + it('should only copy files allowed by filter fn', done => { + const srcFile1 = path.join(TEST_DIR, '1.css') + fs.writeFileSync(srcFile1, '') + const destFile1 = path.join(TEST_DIR, 'dest1.css') + const filter = s => s.split('.').pop() !== 'css' + + fse.copy(srcFile1, destFile1, filter, err => { + assert(!err) + assert(!fs.existsSync(destFile1)) + done() + }) + }) + + it('accepts options object in place of filter', done => { + const srcFile1 = path.join(TEST_DIR, '1.jade') + fs.writeFileSync(srcFile1, '') + const destFile1 = path.join(TEST_DIR, 'dest1.jade') + const options = { filter: s => /.html$|.css$/i.test(s) } + + fse.copy(srcFile1, destFile1, options, (err) => { + assert(!err) + assert(!fs.existsSync(destFile1)) + done() + }) + }) + + it('should should apply filter recursively', done => { + const FILES = 2 + // Don't match anything that ends with a digit higher than 0: + const filter = s => /(0|\D)$/i.test(s) + + const src = path.join(TEST_DIR, 'src') + fse.mkdirsSync(src) + + for (let i = 0; i < FILES; ++i) { + fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE)) + } + + const subdir = path.join(src, 'subdir') + fse.mkdirsSync(subdir) + + for (let i = 0; i < FILES; ++i) { + fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE)) + } + const dest = path.join(TEST_DIR, 'dest') + fse.copy(src, dest, filter, err => { + assert(!err) + + assert(fs.existsSync(dest)) + assert(FILES > 1) + + for (let i = 0; i < FILES; ++i) { + if (i === 0) { + assert(fs.existsSync(path.join(dest, i.toString()))) + } else { + assert(!fs.existsSync(path.join(dest, i.toString()))) + } + } + + const destSub = path.join(dest, 'subdir') + + for (let j = 0; j < FILES; ++j) { + if (j === 0) { + assert(fs.existsSync(path.join(destSub, j.toString()))) + } else { + assert(!fs.existsSync(path.join(destSub, j.toString()))) + } + } + done() + }) + }) + + it('should apply the filter to directory names', done => { + const IGNORE = 'ignore' + const filter = p => !~p.indexOf(IGNORE) + + const src = path.join(TEST_DIR, 'src') + fse.mkdirsSync(src) + + const ignoreDir = path.join(src, IGNORE) + fse.mkdirsSync(ignoreDir) + + fse.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE)) + + const dest = path.join(TEST_DIR, 'dest') + + fse.copySync(src, dest, filter) + + assert(!fs.existsSync(path.join(dest, IGNORE)), 'directory was not ignored') + assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored') + done() + }) + + it('should apply filter when it is applied only to dest', done => { + const timeCond = new Date().getTime() + + const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond + + const src = path.join(TEST_DIR, 'src') + fse.mkdirsSync(src) + const subdir = path.join(src, 'subdir') + fse.mkdirsSync(subdir) + + const dest = path.join(TEST_DIR, 'dest') + + setTimeout(() => { + fse.mkdirsSync(dest) + + fse.copy(src, dest, filter, err => { + assert(!err) + assert(!fs.existsSync(path.join(dest, 'subdir'))) + done() + }) + }, 1000) + }) + + it('should apply filter when it is applied to both src and dest', done => { + const timeCond = new Date().getTime() + const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond + + const dest = path.join(TEST_DIR, 'dest') + setTimeout(() => { + fse.mkdirsSync(dest) + + const srcFile1 = path.join(TEST_DIR, '1.html') + const srcFile2 = path.join(TEST_DIR, '2.css') + const srcFile3 = path.join(TEST_DIR, '3.jade') + + fse.writeFileSync(srcFile1, '') + fse.writeFileSync(srcFile2, '') + fse.writeFileSync(srcFile3, '') + + const destFile1 = path.join(dest, 'dest1.html') + const destFile2 = path.join(dest, 'dest2.css') + const destFile3 = path.join(dest, 'dest3.jade') + + fse.copy(srcFile1, destFile1, filter, err => { + assert(!err) + assert(fs.existsSync(destFile1)) + + fse.copy(srcFile2, destFile2, filter, err => { + assert(!err) + assert(!fs.existsSync(destFile2)) + + fse.copy(srcFile3, destFile3, filter, err => { + assert(!err) + assert(fs.existsSync(destFile3)) + done() + }) + }) + }) + }, 1000) + }) + }) }) }) diff --git a/lib/copy/__tests__/ncp/broken-symlink.test.js b/lib/copy/__tests__/ncp/broken-symlink.test.js index 33b0d435..781ac6d9 100644 --- a/lib/copy/__tests__/ncp/broken-symlink.test.js +++ b/lib/copy/__tests__/ncp/broken-symlink.test.js @@ -1,38 +1,38 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var ncp = require('../../ncp') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const ncp = require('../../ncp') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ describe('ncp broken symlink', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-broken-symlinks') - var src = path.join(TEST_DIR, 'src') - var out = path.join(TEST_DIR, 'out') + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-broken-symlinks') + const src = path.join(TEST_DIR, 'src') + const out = path.join(TEST_DIR, 'out') - beforeEach(function (done) { - fse.emptyDir(TEST_DIR, function (err) { + beforeEach(done => { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) createFixtures(src, done) }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should copy broken symlinks by default', function (done) { - ncp(src, out, function (err) { + it('should copy broken symlinks by default', done => { + ncp(src, out, err => { if (err) return done(err) assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), path.join(src, 'does-not-exist')) done() }) }) - it('should return an error when dereference=true', function (done) { - ncp(src, out, {dereference: true}, function (err) { + it('should return an error when dereference=true', done => { + ncp(src, out, {dereference: true}, err => { assert.equal(err.code, 'ENOENT') done() }) @@ -40,12 +40,15 @@ describe('ncp broken symlink', function () { }) function createFixtures (srcDir, callback) { - fs.mkdir(srcDir, function (err) { + fs.mkdir(srcDir, err => { + let brokenFile + let brokenFileLink + if (err) return callback(err) try { - var brokenFile = path.join(srcDir, 'does-not-exist') - var brokenFileLink = path.join(srcDir, 'broken-symlink') + brokenFile = path.join(srcDir, 'does-not-exist') + brokenFileLink = path.join(srcDir, 'broken-symlink') fs.writeFileSync(brokenFile, 'does not matter') fs.symlinkSync(brokenFile, brokenFileLink, 'file') } catch (err) { diff --git a/lib/copy/__tests__/ncp/ncp-error-perm.test.js b/lib/copy/__tests__/ncp/ncp-error-perm.test.js index 50389598..451bbdf7 100644 --- a/lib/copy/__tests__/ncp/ncp-error-perm.test.js +++ b/lib/copy/__tests__/ncp/ncp-error-perm.test.js @@ -1,11 +1,13 @@ +'use strict' + // file in reference: https://github.com/jprichardson/node-fs-extra/issues/56 -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var ncp = require('../../ncp') +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const ncp = require('../../ncp') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ @@ -14,34 +16,32 @@ var ncp = require('../../ncp') // if (os.platform().indexOf('win') === 0) return // eslint-enable globalReturn */ -describe('ncp / error / dest-permission', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-error-dest-perm') - var src = path.join(TEST_DIR, 'src') - var dest = path.join(TEST_DIR, 'dest') +describe('ncp / error / dest-permission', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-error-dest-perm') + const src = path.join(TEST_DIR, 'src') + const dest = path.join(TEST_DIR, 'dest') if (os.platform().indexOf('win') === 0) return - beforeEach(function (done) { - fse.emptyDir(TEST_DIR, function (err) { + beforeEach(done => { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) done() }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should return an error', function (done) { - var someFile = path.join(src, 'some-file') + it('should return an error', done => { + const someFile = path.join(src, 'some-file') fse.outputFileSync(someFile, 'hello') fse.mkdirsSync(dest) fs.chmodSync(dest, parseInt('444', 8)) - var subdest = path.join(dest, 'another-dir') + const subdest = path.join(dest, 'another-dir') - ncp(src, subdest, function (err) { + ncp(src, subdest, err => { assert(err) assert.equal(err.code, 'EACCES') done() diff --git a/lib/copy/__tests__/ncp/ncp.test.js b/lib/copy/__tests__/ncp/ncp.test.js index acb0f421..23ad3fcc 100644 --- a/lib/copy/__tests__/ncp/ncp.test.js +++ b/lib/copy/__tests__/ncp/ncp.test.js @@ -1,30 +1,28 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var rimraf = require('rimraf') -var readDirFiles = require('read-dir-files').read // temporary, will remove -var ncp = require('../../ncp') +'use strict' + +const fs = require('fs') +const ncp = require('../../ncp') +const path = require('path') +const rimraf = require('rimraf') +const assert = require('assert') +const readDirFiles = require('read-dir-files').read // temporary, will remove /* eslint-env mocha */ -var fixturesDir = path.join(__dirname, 'fixtures') +const fixturesDir = path.join(__dirname, 'fixtures') -describe('ncp', function () { - describe('regular files and directories', function () { - var fixtures = path.join(fixturesDir, 'regular-fixtures') - var src = path.join(fixtures, 'src') - var out = path.join(fixtures, 'out') +describe('ncp', () => { + describe('regular files and directories', () => { + const fixtures = path.join(fixturesDir, 'regular-fixtures') + const src = path.join(fixtures, 'src') + const out = path.join(fixtures, 'out') - before(function (cb) { - rimraf(out, function () { - ncp(src, out, cb) - }) - }) + before(cb => rimraf(out, () => ncp(src, out, cb))) - describe('when copying a directory of files', function () { - it('files are copied correctly', function (cb) { - readDirFiles(src, 'utf8', function (srcErr, srcFiles) { - readDirFiles(out, 'utf8', function (outErr, outFiles) { + describe('when copying a directory of files', () => { + it('files are copied correctly', cb => { + readDirFiles(src, 'utf8', (srcErr, srcFiles) => { + readDirFiles(out, 'utf8', (outErr, outFiles) => { assert.ifError(srcErr) assert.deepEqual(srcFiles, outFiles) cb() @@ -33,21 +31,18 @@ describe('ncp', function () { }) }) - describe('when copying files using filter', function () { - before(function (cb) { - var filter = function (name) { - return name.substr(name.length - 1) !== 'a' - } - rimraf(out, function () { - ncp(src, out, {filter: filter}, cb) - }) + describe('when copying files using filter', () => { + before(cb => { + const filter = name => name.substr(name.length - 1) !== 'a' + + rimraf(out, () => ncp(src, out, { filter }, cb)) }) - it('files are copied correctly', function (cb) { - readDirFiles(src, 'utf8', function (srcErr, srcFiles) { + it('files are copied correctly', cb => { + readDirFiles(src, 'utf8', (srcErr, srcFiles) => { function filter (files) { - for (var fileName in files) { - var curFile = files[fileName] + for (let fileName in files) { + const curFile = files[fileName] if (curFile instanceof Object) { return filter(curFile) } @@ -58,7 +53,7 @@ describe('ncp', function () { } } filter(srcFiles) - readDirFiles(out, 'utf8', function (outErr, outFiles) { + readDirFiles(out, 'utf8', (outErr, outFiles) => { assert.ifError(outErr) assert.deepEqual(srcFiles, outFiles) cb() @@ -67,7 +62,7 @@ describe('ncp', function () { }) }) - describe('when using overwrite=true', function () { + describe('when using overwrite=true', () => { before(function () { this.originalCreateReadStream = fs.createReadStream }) @@ -76,41 +71,41 @@ describe('ncp', function () { fs.createReadStream = this.originalCreateReadStream }) - it('the copy is complete after callback', function (done) { - ncp(src, out, {overwrite: true}, function (err) { - fs.createReadStream = function () { - done(new Error('createReadStream after callback')) - } + it('the copy is complete after callback', done => { + ncp(src, out, {overwrite: true}, err => { + fs.createReadStream = () => done(new Error('createReadStream after callback')) + assert.ifError(err) process.nextTick(done) }) }) }) - describe('when using overwrite=false', function () { - beforeEach(function (done) { - rimraf(out, done) - }) - it('works', function (cb) { - ncp(src, out, {overwrite: false}, function (err) { + describe('when using overwrite=false', () => { + beforeEach(done => rimraf(out, done)) + + it('works', cb => { + ncp(src, out, {overwrite: false}, err => { assert.ifError(err) cb() }) }) - it('should not error if files exist', function (cb) { - ncp(src, out, function () { - ncp(src, out, {overwrite: false}, function (err) { + + it('should not error if files exist', cb => { + ncp(src, out, () => { + ncp(src, out, {overwrite: false}, err => { assert.ifError(err) cb() }) }) }) - it('should error if errorOnExist and file exists', function (cb) { - ncp(src, out, function () { + + it('should error if errorOnExist and file exists', cb => { + ncp(src, out, () => { ncp(src, out, { overwrite: false, errorOnExist: true - }, function (err) { + }, err => { assert(err) cb() }) @@ -118,17 +113,15 @@ describe('ncp', function () { }) }) - describe('clobber', function () { - beforeEach(function (done) { - rimraf(out, done) - }) + describe('clobber', () => { + beforeEach(done => rimraf(out, done)) - it('is an alias for overwrite', function (cb) { - ncp(src, out, function () { + it('is an alias for overwrite', cb => { + ncp(src, out, () => { ncp(src, out, { clobber: false, errorOnExist: true - }, function (err) { + }, err => { assert(err) cb() }) @@ -136,10 +129,10 @@ describe('ncp', function () { }) }) - describe('when using transform', function () { - it('file descriptors are passed correctly', function (cb) { + describe('when using transform', () => { + it('file descriptors are passed correctly', cb => { ncp(src, out, { - transform: function (read, write, file) { + transform: (read, write, file) => { assert.notEqual(file.name, undefined) assert.strictEqual(typeof file.mode, 'number') read.pipe(write) @@ -150,20 +143,20 @@ describe('ncp', function () { }) // see https://github.com/AvianFlu/ncp/issues/71 - describe('Issue 71: Odd Async Behaviors', function (cb) { - var fixtures = path.join(__dirname, 'fixtures', 'regular-fixtures') - var src = path.join(fixtures, 'src') - var out = path.join(fixtures, 'out') + describe('Issue 71: Odd Async Behaviors', cb => { + const fixtures = path.join(__dirname, 'fixtures', 'regular-fixtures') + const src = path.join(fixtures, 'src') + const out = path.join(fixtures, 'out') - var totalCallbacks = 0 + let totalCallbacks = 0 function copyAssertAndCount (callback) { // rimraf(out, function() { - ncp(src, out, function (err) { + ncp(src, out, err => { assert(!err) totalCallbacks += 1 - readDirFiles(src, 'utf8', function (srcErr, srcFiles) { - readDirFiles(out, 'utf8', function (outErr, outFiles) { + readDirFiles(src, 'utf8', (srcErr, srcFiles) => { + readDirFiles(out, 'utf8', (outErr, outFiles) => { assert.ifError(srcErr) assert.deepEqual(srcFiles, outFiles) callback() @@ -173,15 +166,15 @@ describe('ncp', function () { // }) } - describe('when copying a directory of files without cleaning the destination', function () { - it('callback fires once per run and directories are equal', function (done) { - var expected = 10 - var count = 10 + describe('when copying a directory of files without cleaning the destination', () => { + it('callback fires once per run and directories are equal', done => { + const expected = 10 + let count = 10 function next () { if (count > 0) { - setTimeout(function () { - copyAssertAndCount(function () { + setTimeout(() => { + copyAssertAndCount(() => { count -= 1 next() }) diff --git a/lib/copy/__tests__/ncp/symlink.test.js b/lib/copy/__tests__/ncp/symlink.test.js index 3315a1cf..1b8816b6 100644 --- a/lib/copy/__tests__/ncp/symlink.test.js +++ b/lib/copy/__tests__/ncp/symlink.test.js @@ -1,30 +1,30 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var ncp = require('../../ncp') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const ncp = require('../../ncp') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('ncp / symlink', function () { - var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-symlinks') - var src = path.join(TEST_DIR, 'src') - var out = path.join(TEST_DIR, 'out') +describe('ncp / symlink', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-symlinks') + const src = path.join(TEST_DIR, 'src') + const out = path.join(TEST_DIR, 'out') - beforeEach(function (done) { - fse.emptyDir(TEST_DIR, function (err) { + beforeEach(done => { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) createFixtures(src, done) }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('copies symlinks by default', function (done) { - ncp(src, out, function (err) { + it('copies symlinks by default', done => { + ncp(src, out, err => { assert.ifError(err) assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), path.join(src, 'foo')) @@ -34,15 +34,15 @@ describe('ncp / symlink', function () { }) }) - it('copies file contents when dereference=true', function (done) { - ncp(src, out, {dereference: true}, function (err) { + it('copies file contents when dereference=true', done => { + ncp(src, out, {dereference: true}, err => { assert.ifError(err) - var fileSymlinkPath = path.join(out, 'file-symlink') + const fileSymlinkPath = path.join(out, 'file-symlink') assert.ok(fs.lstatSync(fileSymlinkPath).isFile()) assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents') - var dirSymlinkPath = path.join(out, 'dir-symlink') + const dirSymlinkPath = path.join(out, 'dir-symlink') assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory()) assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']) @@ -52,20 +52,20 @@ describe('ncp / symlink', function () { }) function createFixtures (srcDir, callback) { - fs.mkdir(srcDir, function (err) { + fs.mkdir(srcDir, err => { if (err) return callback(err) // note: third parameter in symlinkSync is type e.g. 'file' or 'dir' // https://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback try { - var fooFile = path.join(srcDir, 'foo') - var fooFileLink = path.join(srcDir, 'file-symlink') + const fooFile = path.join(srcDir, 'foo') + const fooFileLink = path.join(srcDir, 'file-symlink') fs.writeFileSync(fooFile, 'foo contents') fs.symlinkSync(fooFile, fooFileLink, 'file') - var dir = path.join(srcDir, 'dir') - var dirFile = path.join(dir, 'bar') - var dirLink = path.join(srcDir, 'dir-symlink') + const dir = path.join(srcDir, 'dir') + const dirFile = path.join(dir, 'bar') + const dirLink = path.join(srcDir, 'dir-symlink') fs.mkdirSync(dir) fs.writeFileSync(dirFile, 'bar contents') fs.symlinkSync(dir, dirLink, 'dir') diff --git a/lib/copy/copy.js b/lib/copy/copy.js index 6c5e2243..d66c8981 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -1,7 +1,9 @@ -var fs = require('graceful-fs') -var path = require('path') -var ncp = require('./ncp') -var mkdir = require('../mkdirs') +'use strict' + +const fs = require('graceful-fs') +const path = require('path') +const ncp = require('./ncp') +const mkdir = require('../mkdirs') function copy (src, dest, options, callback) { if (typeof options === 'function' && !callback) { @@ -15,31 +17,31 @@ function copy (src, dest, options, callback) { // Warn about using preserveTimestamps on 32-bit node: if (options.preserveTimestamps && process.arch === 'ia32') { - console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' + - 'see https://github.com/jprichardson/node-fs-extra/issues/269') + console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n + see https://github.com/jprichardson/node-fs-extra/issues/269`) } // don't allow src and dest to be the same - var basePath = process.cwd() - var currentPath = path.resolve(basePath, src) - var targetPath = path.resolve(basePath, dest) + const basePath = process.cwd() + const currentPath = path.resolve(basePath, src) + const targetPath = path.resolve(basePath, dest) if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.')) - fs.lstat(src, function (err, stats) { + fs.lstat(src, (err, stats) => { if (err) return callback(err) - var dir = null + let dir = null if (stats.isDirectory()) { - var parts = dest.split(path.sep) + const parts = dest.split(path.sep) parts.pop() dir = parts.join(path.sep) } else { dir = path.dirname(dest) } - fs.exists(dir, function (dirExists) { + fs.exists(dir, dirExists => { if (dirExists) return ncp(src, dest, options, callback) - mkdir.mkdirs(dir, function (err) { + mkdir.mkdirs(dir, err => { if (err) return callback(err) ncp(src, dest, options, callback) }) diff --git a/lib/copy/ncp.js b/lib/copy/ncp.js index 7137e169..9670ee02 100644 --- a/lib/copy/ncp.js +++ b/lib/copy/ncp.js @@ -41,7 +41,7 @@ function ncp (source, dest, options, callback) { return doneOne(true) } } else if (typeof filter === 'function') { - if (!filter(source)) { + if (!filter(source, dest)) { return doneOne(true) } } diff --git a/lib/empty/__tests__/empty-dir-sync.test.js b/lib/empty/__tests__/empty-dir-sync.test.js index 3587472e..b8a648c7 100644 --- a/lib/empty/__tests__/empty-dir-sync.test.js +++ b/lib/empty/__tests__/empty-dir-sync.test.js @@ -1,15 +1,17 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('+ emptyDir()', function () { - var TEST_DIR +describe('+ emptyDir()', () => { + let TEST_DIR - beforeEach(function () { + beforeEach(() => { TEST_DIR = path.join(os.tmpdir(), 'test-fs-extra', 'empty-dir') if (fs.existsSync(TEST_DIR)) { fse.removeSync(TEST_DIR) @@ -17,12 +19,10 @@ describe('+ emptyDir()', function () { fse.ensureDirSync(TEST_DIR) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('> when directory exists and contains items', function () { - it('should delete all of the items', function () { + describe('> when directory exists and contains items', () => { + it('should delete all of the items', () => { // verify nothing assert.equal(fs.readdirSync(TEST_DIR).length, 0) fse.ensureFileSync(path.join(TEST_DIR, 'some-file')) @@ -35,16 +35,16 @@ describe('+ emptyDir()', function () { }) }) - describe('> when directory exists and contains no items', function () { - it('should do nothing', function () { + describe('> when directory exists and contains no items', () => { + it('should do nothing', () => { assert.equal(fs.readdirSync(TEST_DIR).length, 0) fse.emptyDirSync(TEST_DIR) assert.equal(fs.readdirSync(TEST_DIR).length, 0) }) }) - describe('> when directory does not exist', function () { - it('should create it', function () { + describe('> when directory does not exist', () => { + it('should create it', () => { fse.removeSync(TEST_DIR) assert(!fs.existsSync(TEST_DIR)) fse.emptyDirSync(TEST_DIR) diff --git a/lib/empty/__tests__/empty-dir.test.js b/lib/empty/__tests__/empty-dir.test.js index 8ef13041..4f509172 100644 --- a/lib/empty/__tests__/empty-dir.test.js +++ b/lib/empty/__tests__/empty-dir.test.js @@ -1,15 +1,17 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('+ emptyDir()', function () { - var TEST_DIR +describe('+ emptyDir()', () => { + let TEST_DIR - beforeEach(function () { + beforeEach(() => { TEST_DIR = path.join(os.tmpdir(), 'test-fs-extra', 'empty-dir') if (fs.existsSync(TEST_DIR)) { fse.removeSync(TEST_DIR) @@ -17,12 +19,10 @@ describe('+ emptyDir()', function () { fse.ensureDirSync(TEST_DIR) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('> when directory exists and contains items', function () { - it('should delete all of the items', function (done) { + describe('> when directory exists and contains items', () => { + it('should delete all of the items', done => { // verify nothing assert.equal(fs.readdirSync(TEST_DIR).length, 0) fse.ensureFileSync(path.join(TEST_DIR, 'some-file')) @@ -30,7 +30,7 @@ describe('+ emptyDir()', function () { fse.ensureDirSync(path.join(TEST_DIR, 'some-dir')) assert.equal(fs.readdirSync(TEST_DIR).length, 3) - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) assert.equal(fs.readdirSync(TEST_DIR).length, 0) done() @@ -38,10 +38,10 @@ describe('+ emptyDir()', function () { }) }) - describe('> when directory exists and contains no items', function () { - it('should do nothing', function (done) { + describe('> when directory exists and contains no items', () => { + it('should do nothing', done => { assert.equal(fs.readdirSync(TEST_DIR).length, 0) - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) assert.equal(fs.readdirSync(TEST_DIR).length, 0) done() @@ -49,11 +49,11 @@ describe('+ emptyDir()', function () { }) }) - describe('> when directory does not exist', function () { - it('should create it', function (done) { + describe('> when directory does not exist', () => { + it('should create it', done => { fse.removeSync(TEST_DIR) assert(!fs.existsSync(TEST_DIR)) - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) assert.equal(fs.readdirSync(TEST_DIR).length, 0) done() diff --git a/lib/empty/index.js b/lib/empty/index.js index a17cbae1..180c3f79 100644 --- a/lib/empty/index.js +++ b/lib/empty/index.js @@ -1,23 +1,23 @@ -var fs = require('fs') -var path = require('path') -var mkdir = require('../mkdirs') -var remove = require('../remove') +'use strict' + +const fs = require('fs') +const path = require('path') +const mkdir = require('../mkdirs') +const remove = require('../remove') function emptyDir (dir, callback) { callback = callback || function () {} - fs.readdir(dir, function (err, items) { + fs.readdir(dir, (err, items) => { if (err) return mkdir.mkdirs(dir, callback) - items = items.map(function (item) { - return path.join(dir, item) - }) + items = items.map(item => path.join(dir, item)) deleteItem() function deleteItem () { - var item = items.pop() + const item = items.pop() if (!item) return callback() - remove.remove(item, function (err) { + remove.remove(item, err => { if (err) return callback(err) deleteItem() }) @@ -26,22 +26,22 @@ function emptyDir (dir, callback) { } function emptyDirSync (dir) { - var items + let items try { items = fs.readdirSync(dir) } catch (err) { return mkdir.mkdirsSync(dir) } - items.forEach(function (item) { + items.forEach(item => { item = path.join(dir, item) remove.removeSync(item) }) } module.exports = { - emptyDirSync: emptyDirSync, + emptyDirSync, emptydirSync: emptyDirSync, - emptyDir: emptyDir, + emptyDir, emptydir: emptyDir } diff --git a/lib/ensure/__tests__/create.test.js b/lib/ensure/__tests__/create.test.js index 94ce3c18..bed29669 100644 --- a/lib/ensure/__tests__/create.test.js +++ b/lib/ensure/__tests__/create.test.js @@ -1,29 +1,29 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('fs-extra', function () { - var TEST_DIR +describe('fs-extra', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'create') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ createFile', function () { - describe('> when the file and directory does not exist', function () { - it('should create the file', function (done) { - var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt') + describe('+ createFile', () => { + describe('> when the file and directory does not exist', () => { + it('should create the file', done => { + const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt') assert(!fs.existsSync(file)) - fse.createFile(file, function (err) { + fse.createFile(file, err => { assert.ifError(err) assert(fs.existsSync(file)) done() @@ -31,12 +31,12 @@ describe('fs-extra', function () { }) }) - describe('> when the file does exist', function () { - it('should not modify the file', function (done) { - var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt') + describe('> when the file does exist', () => { + it('should not modify the file', done => { + const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt') fse.mkdirsSync(path.dirname(file)) fs.writeFileSync(file, 'hello world') - fse.createFile(file, function (err) { + fse.createFile(file, err => { assert.ifError(err) assert.equal(fs.readFileSync(file, 'utf8'), 'hello world') done() @@ -45,19 +45,19 @@ describe('fs-extra', function () { }) }) - describe('+ createFileSync', function () { - describe('> when the file and directory does not exist', function () { - it('should create the file', function () { - var file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt') + describe('+ createFileSync', () => { + describe('> when the file and directory does not exist', () => { + it('should create the file', () => { + const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt') assert(!fs.existsSync(file)) fse.createFileSync(file) assert(fs.existsSync(file)) }) }) - describe('> when the file does exist', function () { - it('should not modify the file', function () { - var file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt') + describe('> when the file does exist', () => { + it('should not modify the file', () => { + const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt') fse.mkdirsSync(path.dirname(file)) fs.writeFileSync(file, 'hello world') fse.createFileSync(file) diff --git a/lib/ensure/__tests__/ensure.test.js b/lib/ensure/__tests__/ensure.test.js index 5ff340fe..b7b519e5 100644 --- a/lib/ensure/__tests__/ensure.test.js +++ b/lib/ensure/__tests__/ensure.test.js @@ -1,31 +1,31 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('fs-extra', function () { - var TEST_DIR +describe('fs-extra', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ ensureFile()', function () { - describe('> when file exists', function () { - it('should not do anything', function (done) { - var file = path.join(TEST_DIR, 'file.txt') + describe('+ ensureFile()', () => { + describe('> when file exists', () => { + it('should not do anything', done => { + const file = path.join(TEST_DIR, 'file.txt') fs.writeFileSync(file, 'blah') assert(fs.existsSync(file)) - fse.ensureFile(file, function (err) { + fse.ensureFile(file, err => { assert.ifError(err) assert(fs.existsSync(file)) done() @@ -33,12 +33,12 @@ describe('fs-extra', function () { }) }) - describe('> when file does not exist', function () { - it('should create the file', function (done) { - var file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') + describe('> when file does not exist', () => { + it('should create the file', done => { + const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') assert(!fs.existsSync(file)) - fse.ensureFile(file, function (err) { + fse.ensureFile(file, err => { assert.ifError(err) assert(fs.existsSync(file)) done() @@ -47,10 +47,10 @@ describe('fs-extra', function () { }) }) - describe('+ ensureFileSync()', function () { - describe('> when file exists', function () { - it('should not do anything', function () { - var file = path.join(TEST_DIR, 'file.txt') + describe('+ ensureFileSync()', () => { + describe('> when file exists', () => { + it('should not do anything', () => { + const file = path.join(TEST_DIR, 'file.txt') fs.writeFileSync(file, 'blah') assert(fs.existsSync(file)) @@ -59,9 +59,9 @@ describe('fs-extra', function () { }) }) - describe('> when file does not exist', function () { - it('should create the file', function () { - var file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') + describe('> when file does not exist', () => { + it('should create the file', () => { + const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') assert(!fs.existsSync(file)) fse.ensureFileSync(file) @@ -70,14 +70,14 @@ describe('fs-extra', function () { }) }) - describe('+ ensureDir()', function () { - describe('> when dir exists', function () { - it('should not do anything', function (done) { - var dir = path.join(TEST_DIR, 'dir/does/not/exist') + describe('+ ensureDir()', () => { + describe('> when dir exists', () => { + it('should not do anything', done => { + const dir = path.join(TEST_DIR, 'dir/does/not/exist') fse.mkdirpSync(dir) assert(fs.existsSync(dir)) - fse.ensureDir(dir, function (err) { + fse.ensureDir(dir, err => { assert.ifError(err) assert(fs.existsSync(dir)) done() @@ -85,12 +85,12 @@ describe('fs-extra', function () { }) }) - describe('> when dir does not exist', function () { - it('should create the dir', function (done) { - var dir = path.join(TEST_DIR, 'dir/that/does/not/exist') + describe('> when dir does not exist', () => { + it('should create the dir', done => { + const dir = path.join(TEST_DIR, 'dir/that/does/not/exist') assert(!fs.existsSync(dir)) - fse.ensureDir(dir, function (err) { + fse.ensureDir(dir, err => { assert.ifError(err) assert(fs.existsSync(dir)) done() @@ -99,10 +99,10 @@ describe('fs-extra', function () { }) }) - describe('+ ensureDirSync()', function () { - describe('> when dir exists', function () { - it('should not do anything', function () { - var dir = path.join(TEST_DIR, 'dir/does/not/exist') + describe('+ ensureDirSync()', () => { + describe('> when dir exists', () => { + it('should not do anything', () => { + const dir = path.join(TEST_DIR, 'dir/does/not/exist') fse.mkdirpSync(dir) assert(fs.existsSync(dir)) @@ -111,9 +111,9 @@ describe('fs-extra', function () { }) }) - describe('> when dir does not exist', function () { - it('should create the dir', function () { - var dir = path.join(TEST_DIR, 'dir/that/does/not/exist') + describe('> when dir does not exist', () => { + it('should create the dir', () => { + const dir = path.join(TEST_DIR, 'dir/that/does/not/exist') assert(!fs.existsSync(dir)) fse.ensureDirSync(dir) diff --git a/lib/ensure/__tests__/link.test.js b/lib/ensure/__tests__/link.test.js index f06f4e89..8e27b6fb 100644 --- a/lib/ensure/__tests__/link.test.js +++ b/lib/ensure/__tests__/link.test.js @@ -1,21 +1,21 @@ -var assert = require('assert') -var util = require('util') -var path = require('path') -var os = require('os') -var fs = require('graceful-fs') -var CWD = process.cwd() -var fse = require(CWD) -var ensureLink = fse.ensureLink -var ensureLinkSync = fse.ensureLinkSync +'use strict' -/* global afterEach, beforeEach, describe, it, after, before */ +const CWD = process.cwd() + +const fs = require('graceful-fs') +const os = require('os') +const fse = require(CWD) +const path = require('path') +const assert = require('assert') +const ensureLink = fse.ensureLink +const ensureLinkSync = fse.ensureLinkSync -var TEST_DIR +/* global afterEach, beforeEach, describe, it, after, before */ -describe('fse-ensure-link', function () { - TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') +describe('fse-ensure-link', () => { + const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') - var tests = [ + const tests = [ // [[srcpath, dstpath], fs.link expect, ensureLink expect] [['./foo.txt', './link.txt'], 'file-success', 'file-success'], [['./foo.txt', './dir-foo/link.txt'], 'file-success', 'file-success'], @@ -42,12 +42,12 @@ describe('fse-ensure-link', function () { [[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './link.txt'], 'file-error', 'file-error'] ] - before(function () { + before(() => { fse.emptyDirSync(TEST_DIR) process.chdir(TEST_DIR) }) - beforeEach(function () { + beforeEach(() => { fs.writeFileSync('./foo.txt', 'foo\n') fse.mkdirsSync('empty-dir') fse.mkdirsSync('dir-foo') @@ -57,28 +57,28 @@ describe('fse-ensure-link', function () { fse.mkdirsSync('real-alpha/real-beta/real-gamma') }) - afterEach(function (done) { - fse.emptyDir(TEST_DIR, done) - }) + afterEach(done => fse.emptyDir(TEST_DIR, done)) - after(function () { + after(() => { process.chdir(CWD) fse.removeSync(TEST_DIR) }) function fileSuccess (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + + it(`should create link file using src ${srcpath} and dst ${dstpath}`, done => { + const callback = err => { if (err) return done(err) - var srcContent = fs.readFileSync(srcpath, 'utf8') - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isFile() - var dstContent = fs.readFileSync(dstpath, 'utf8') - var dstDirContents = fs.readdirSync(dstDir) + + const srcContent = fs.readFileSync(srcpath, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isFile() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) + assert.equal(isSymlink, true) assert.equal(srcContent, dstContent) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -90,15 +90,15 @@ describe('fse-ensure-link', function () { } function fileError (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should return error when creating link file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - function callback (err) { + const srcpath = args[0] + const dstpath = args[1] + + it(`should return error when creating link file using src ${srcpath} and dst ${dstpath}`, done => { + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + const callback = err => { assert.equal(err instanceof Error, true) // ensure that directories aren't created if there's an error - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) return done() } @@ -108,14 +108,14 @@ describe('fse-ensure-link', function () { } function fileDestExists (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var destinationContentBefore = fs.readFileSync(dstpath, 'utf8') - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => { + const destinationContentBefore = fs.readFileSync(dstpath, 'utf8') + const callback = err => { if (err) return done(err) - var destinationContentAfter = fs.readFileSync(dstpath, 'utf8') + const destinationContentAfter = fs.readFileSync(dstpath, 'utf8') assert.equal(destinationContentBefore, destinationContentAfter) return done() } @@ -125,17 +125,17 @@ describe('fse-ensure-link', function () { } function fileSuccessSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + + it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => { fn.apply(null, args) - var srcContent = fs.readFileSync(srcpath, 'utf8') - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isFile() - var dstContent = fs.readFileSync(dstpath, 'utf8') - var dstDirContents = fs.readdirSync(dstDir) + const srcContent = fs.readFileSync(srcpath, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isFile() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert.equal(srcContent, dstContent) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -143,78 +143,78 @@ describe('fse-ensure-link', function () { } function fileErrorSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should throw error using` src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + + it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => { // will fail if dstdir is created and there's an error - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - var err = null + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + let err = null try { fn.apply(null, args) } catch (e) { err = e } assert.equal(err instanceof Error, true) - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) }) } function fileDestExistsSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { - var destinationContentBefore = fs.readFileSync(dstpath, 'utf8') + const srcpath = args[0] + const dstpath = args[1] + + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => { + const destinationContentBefore = fs.readFileSync(dstpath, 'utf8') fn.apply(null, args) - var destinationContentAfter = fs.readFileSync(dstpath, 'utf8') + const destinationContentAfter = fs.readFileSync(dstpath, 'utf8') assert.equal(destinationContentBefore, destinationContentAfter) }) } - describe('fs.link()', function () { - var fn = fs.link - tests.forEach(function (test) { - var args = test[0].slice(0) - var nativeBehavior = test[1] - // var newBehavior = test[2] + describe('fs.link()', () => { + const fn = fs.link + tests.forEach(test => { + const args = test[0].slice(0) + const nativeBehavior = test[1] + // const newBehavior = test[2] if (nativeBehavior === 'file-success') fileSuccess(args, fn) if (nativeBehavior === 'file-error') fileError(args, fn) if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn) }) }) - describe('ensureLink()', function () { - var fn = ensureLink - tests.forEach(function (test) { - var args = test[0].slice(0) - // var nativeBehavior = test[1] - var newBehavior = test[2] + describe('ensureLink()', () => { + const fn = ensureLink + tests.forEach(test => { + const args = test[0].slice(0) + // const nativeBehavior = test[1] + const newBehavior = test[2] if (newBehavior === 'file-success') fileSuccess(args, fn) if (newBehavior === 'file-error') fileError(args, fn) if (newBehavior === 'file-dest-exists') fileDestExists(args, fn) }) }) - describe('fs.linkSync()', function () { - var fn = fs.linkSync - tests.forEach(function (test) { - var args = test[0].slice(0) - var nativeBehavior = test[1] - // var newBehavior = test[2] + describe('fs.linkSync()', () => { + const fn = fs.linkSync + tests.forEach(test => { + const args = test[0].slice(0) + const nativeBehavior = test[1] + // const newBehavior = test[2] if (nativeBehavior === 'file-success') fileSuccessSync(args, fn) if (nativeBehavior === 'file-error') fileErrorSync(args, fn) if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn) }) }) - describe('ensureLinkSync()', function () { - var fn = ensureLinkSync - tests.forEach(function (test) { - var args = test[0].slice(0) - // var nativeBehavior = test[1] - var newBehavior = test[2] + describe('ensureLinkSync()', () => { + const fn = ensureLinkSync + tests.forEach(test => { + const args = test[0].slice(0) + // const nativeBehavior = test[1] + const newBehavior = test[2] if (newBehavior === 'file-success') fileSuccessSync(args, fn) if (newBehavior === 'file-error') fileErrorSync(args, fn) if (newBehavior === 'file-dest-exists') fileDestExistsSync(args, fn) diff --git a/lib/ensure/__tests__/symlink-paths.test.js b/lib/ensure/__tests__/symlink-paths.test.js index 8bfaa388..46262b01 100644 --- a/lib/ensure/__tests__/symlink-paths.test.js +++ b/lib/ensure/__tests__/symlink-paths.test.js @@ -1,24 +1,26 @@ -var assert = require('assert') -var util = require('util') -var path = require('path') -var os = require('os') -var fs = require('graceful-fs') -var CWD = process.cwd() -var fse = require(CWD) -var _symlinkPaths = require('../symlink-paths') -var symlinkPaths = _symlinkPaths.symlinkPaths -var symlinkPathsSync = _symlinkPaths.symlinkPathsSync -var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') +'use strict' + +const CWD = process.cwd() + +const fs = require('graceful-fs') +const os = require('os') +const fse = require(CWD) +const path = require('path') +const assert = require('assert') +const _symlinkPaths = require('../symlink-paths') +const symlinkPaths = _symlinkPaths.symlinkPaths +const symlinkPathsSync = _symlinkPaths.symlinkPathsSync +const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') /* global afterEach, beforeEach, describe, it, after, before */ -describe('symlink-type', function () { - before(function () { +describe('symlink-type', () => { + before(() => { fse.emptyDirSync(TEST_DIR) process.chdir(TEST_DIR) }) - beforeEach(function () { + beforeEach(() => { fs.writeFileSync('./foo.txt', 'foo\n') fse.mkdirsSync('./empty-dir') fse.mkdirsSync('./dir-foo') @@ -28,16 +30,14 @@ describe('symlink-type', function () { fse.mkdirsSync('./real-alpha/real-beta/real-gamma') }) - afterEach(function (done) { - fse.emptyDir(TEST_DIR, done) - }) + afterEach(done => fse.emptyDir(TEST_DIR, done)) - after(function () { + after(() => { process.chdir(CWD) fse.removeSync(TEST_DIR) }) - var tests = [ + const tests = [ [['foo.txt', 'symlink.txt'], {toCwd: 'foo.txt', toDst: 'foo.txt'}], // smart && nodestyle [['foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // smart [['../foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // nodestyle @@ -51,7 +51,7 @@ describe('symlink-type', function () { ] // formats paths to pass on multiple operating systems - tests.forEach(function (test) { + tests.forEach(test => { test[0][0] = path.join(test[0][0]) test[0][1] = path.join(test[0][1]) test[1] = { @@ -60,13 +60,12 @@ describe('symlink-type', function () { } }) - describe('symlinkPaths()', function () { - tests.forEach(function (test) { - var args = test[0].slice(0) - var expectedRelativePaths = test[1] - var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedRelativePaths), args[0], args[1]) - it(should, function (done) { - var callback = function (err, relativePaths) { + describe('symlinkPaths()', () => { + tests.forEach(test => { + const args = test[0].slice(0) + const expectedRelativePaths = test[1] + it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, done => { + const callback = (err, relativePaths) => { if (err) done(err) assert.deepEqual(relativePaths, expectedRelativePaths) done() @@ -77,13 +76,12 @@ describe('symlink-type', function () { }) }) - describe('symlinkPathsSync()', function () { - tests.forEach(function (test) { - var args = test[0].slice(0) - var expectedRelativePaths = test[1] - var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedRelativePaths), args[0], args[1]) - it(should, function () { - var relativePaths = symlinkPathsSync.apply(null, args) + describe('symlinkPathsSync()', () => { + tests.forEach(test => { + const args = test[0].slice(0) + const expectedRelativePaths = test[1] + it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, () => { + const relativePaths = symlinkPathsSync.apply(null, args) assert.deepEqual(relativePaths, expectedRelativePaths) }) }) diff --git a/lib/ensure/__tests__/symlink-type.test.js b/lib/ensure/__tests__/symlink-type.test.js index e520863d..2fc29369 100644 --- a/lib/ensure/__tests__/symlink-type.test.js +++ b/lib/ensure/__tests__/symlink-type.test.js @@ -1,24 +1,26 @@ -var assert = require('assert') -var util = require('util') -var path = require('path') -var os = require('os') -var fs = require('graceful-fs') -var CWD = process.cwd() -var fse = require(CWD) -var _symlinkType = require('../symlink-type') -var symlinkType = _symlinkType.symlinkType -var symlinkTypeSync = _symlinkType.symlinkTypeSync -var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') +'use strict' + +const CWD = process.cwd() + +const fs = require('graceful-fs') +const os = require('os') +const fse = require(CWD) +const path = require('path') +const assert = require('assert') +const _symlinkType = require('../symlink-type') +const symlinkType = _symlinkType.symlinkType +const symlinkTypeSync = _symlinkType.symlinkTypeSync +const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') /* global afterEach, beforeEach, describe, it, after, before */ -describe('symlink-type', function () { - before(function () { +describe('symlink-type', () => { + before(() => { fse.emptyDirSync(TEST_DIR) process.chdir(TEST_DIR) }) - beforeEach(function () { + beforeEach(() => { fs.writeFileSync('./foo.txt', 'foo\n') fse.mkdirsSync('./empty-dir') fse.mkdirsSync('./dir-foo') @@ -28,16 +30,14 @@ describe('symlink-type', function () { fse.mkdirsSync('./real-alpha/real-beta/real-gamma') }) - afterEach(function (done) { - fse.emptyDir(TEST_DIR, done) - }) + afterEach(done => fse.emptyDir(TEST_DIR, done)) - after(function () { + after(() => { process.chdir(CWD) fse.removeSync(TEST_DIR) }) - var tests = { + const tests = { success: [ // [{arguments} [srcpath, dirpath, [type] , result] // smart file type checking @@ -91,13 +91,12 @@ describe('symlink-type', function () { ] } - describe('symlinkType()', function () { - tests.success.forEach(function (test) { - var args = test[0].slice(0) - var expectedType = test[1] - var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0]) - it(should, function (done) { - var callback = function (err, type) { + describe('symlinkType()', () => { + tests.success.forEach(test => { + const args = test[0].slice(0) + const expectedType = test[1] + it(`should return '${expectedType}' when src '${args[0]}'`, done => { + const callback = (err, type) => { if (err) done(err) assert.equal(type, expectedType) done() @@ -108,13 +107,12 @@ describe('symlink-type', function () { }) }) - describe('symlinkTypeSync()', function () { - tests.success.forEach(function (test) { - var args = test[0] - var expectedType = test[1] - var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0]) - it(should, function () { - var type = symlinkTypeSync.apply(null, args) + describe('symlinkTypeSync()', () => { + tests.success.forEach(test => { + const args = test[0] + const expectedType = test[1] + it(`should return '${expectedType}' when src '${args[0]}'`, () => { + const type = symlinkTypeSync.apply(null, args) assert.equal(type, expectedType) }) }) diff --git a/lib/ensure/__tests__/symlink.test.js b/lib/ensure/__tests__/symlink.test.js index 2e688ac9..6f939585 100644 --- a/lib/ensure/__tests__/symlink.test.js +++ b/lib/ensure/__tests__/symlink.test.js @@ -1,25 +1,25 @@ -var assert = require('assert') -var util = require('util') -var path = require('path') -var os = require('os') -var fs = require('graceful-fs') -var CWD = process.cwd() -var fse = require(CWD) +'use strict' -var _symlinkPaths = require('../symlink-paths') -var symlinkPathsSync = _symlinkPaths.symlinkPathsSync +const CWD = process.cwd() -var ensureSymlink = fse.ensureSymlink -var ensureSymlinkSync = fse.ensureSymlinkSync +const fs = require('graceful-fs') +const os = require('os') +const fse = require(CWD) +const path = require('path') +const assert = require('assert') +const _symlinkPaths = require('../symlink-paths') +const symlinkPathsSync = _symlinkPaths.symlinkPathsSync +const ensureSymlink = fse.ensureSymlink +const ensureSymlinkSync = fse.ensureSymlinkSync /* global afterEach, beforeEach, describe, it, after, before */ -var TEST_DIR +let TEST_DIR -describe('fse-ensure-symlink', function () { +describe('fse-ensure-symlink', () => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') - var tests = [ + const tests = [ // [[srcpath, dstpath], fs.symlink expect, fse.ensureSymlink expect] [['./foo.txt', './symlink.txt'], 'file-success', 'file-success'], [['../foo.txt', './empty-dir/symlink.txt'], 'file-success', 'file-success'], @@ -56,12 +56,12 @@ describe('fse-ensure-symlink', function () { [[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './symlink.txt'], 'file-broken', 'file-error'] ] - before(function () { + before(() => { fse.emptyDirSync(TEST_DIR) process.chdir(TEST_DIR) }) - beforeEach(function () { + beforeEach(() => { fs.writeFileSync('./foo.txt', 'foo\n') fse.mkdirsSync('empty-dir') fse.mkdirsSync('dir-foo') @@ -71,29 +71,26 @@ describe('fse-ensure-symlink', function () { fse.mkdirsSync('real-alpha/real-beta/real-gamma') }) - afterEach(function (done) { - fse.emptyDir(TEST_DIR, done) - }) + afterEach(done => fse.emptyDir(TEST_DIR, done)) - after(function () { + after(() => { process.chdir(CWD) fse.removeSync(TEST_DIR) }) function fileSuccess (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create symlink file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, done => { + const callback = err => { if (err) return done(err) - var relative = symlinkPathsSync(srcpath, dstpath) - var srcContent = fs.readFileSync(relative.toCwd, 'utf8') - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstContent = fs.readFileSync(dstpath, 'utf8') - var dstDirContents = fs.readdirSync(dstDir) + const relative = symlinkPathsSync(srcpath, dstpath) + const srcContent = fs.readFileSync(relative.toCwd, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert.equal(srcContent, dstContent) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -105,21 +102,18 @@ describe('fse-ensure-symlink', function () { } function fileBroken (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create broken symlink file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, done => { + const callback = err => { if (err) return done(err) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstDirContents = fs.readdirSync(dstDir) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert(dstDirContents.indexOf(dstBasename) >= 0) - assert.throws(function () { - return fs.readFileSync(dstpath, 'utf8') - }, Error) + assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error) return done() } args.push(callback) @@ -128,15 +122,14 @@ describe('fse-ensure-symlink', function () { } function fileError (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should return error when creating symlink file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - function callback (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should return error when creating symlink file using src ${srcpath} and dst ${dstpath}`, done => { + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + const callback = err => { assert.equal(err instanceof Error, true) // ensure that directories aren't created if there's an error - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) return done() } @@ -146,14 +139,13 @@ describe('fse-ensure-symlink', function () { } function fileDestExists (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var destinationContentBefore = fs.readFileSync(dstpath, 'utf8') - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => { + const destinationContentBefore = fs.readFileSync(dstpath, 'utf8') + const callback = err => { if (err) return done(err) - var destinationContentAfter = fs.readFileSync(dstpath, 'utf8') + const destinationContentAfter = fs.readFileSync(dstpath, 'utf8') assert.equal(destinationContentBefore, destinationContentAfter) return done() } @@ -163,19 +155,18 @@ describe('fse-ensure-symlink', function () { } function dirSuccess (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, done => { + const callback = err => { if (err) return done(err) - var relative = symlinkPathsSync(srcpath, dstpath) - var srcContents = fs.readdirSync(relative.toCwd) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstContents = fs.readdirSync(dstpath) - var dstDirContents = fs.readdirSync(dstDir) + const relative = symlinkPathsSync(srcpath, dstpath) + const srcContents = fs.readdirSync(relative.toCwd) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstContents = fs.readdirSync(dstpath) + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert.deepEqual(srcContents, dstContents) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -187,21 +178,18 @@ describe('fse-ensure-symlink', function () { } function dirBroken (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create broken symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, done => { + const callback = err => { if (err) return done(err) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstDirContents = fs.readdirSync(dstDir) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert(dstDirContents.indexOf(dstBasename) >= 0) - assert.throws(function () { - return fs.readdirSync(dstpath) - }, Error) + assert.throws(() => fs.readdirSync(dstpath), Error) return done() } args.push(callback) @@ -210,15 +198,14 @@ describe('fse-ensure-symlink', function () { } function dirError (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should return error when creating symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - function callback (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should return error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, done => { + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + const callback = err => { assert.equal(err instanceof Error, true) // ensure that directories aren't created if there's an error - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) return done() } @@ -228,14 +215,13 @@ describe('fse-ensure-symlink', function () { } function dirDestExists (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function (done) { - var destinationContentBefore = fs.readdirSync(dstpath) - var callback = function (err) { + const srcpath = args[0] + const dstpath = args[1] + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => { + const destinationContentBefore = fs.readdirSync(dstpath) + const callback = err => { if (err) return done(err) - var destinationContentAfter = fs.readdirSync(dstpath) + const destinationContentAfter = fs.readdirSync(dstpath) assert.deepEqual(destinationContentBefore, destinationContentAfter) return done() } @@ -245,18 +231,17 @@ describe('fse-ensure-symlink', function () { } function fileSuccessSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create symlink file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => { fn.apply(null, args) - var relative = symlinkPathsSync(srcpath, dstpath) - var srcContent = fs.readFileSync(relative.toCwd, 'utf8') - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstContent = fs.readFileSync(dstpath, 'utf8') - var dstDirContents = fs.readdirSync(dstDir) + const relative = symlinkPathsSync(srcpath, dstpath) + const srcContent = fs.readFileSync(relative.toCwd, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert.equal(srcContent, dstContent) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -264,66 +249,60 @@ describe('fse-ensure-symlink', function () { } function fileBrokenSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create broken symlink file using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, () => { fn.apply(null, args) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstDirContents = fs.readdirSync(dstDir) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert(dstDirContents.indexOf(dstBasename) >= 0) - assert.throws(function () { - return fs.readFileSync(dstpath, 'utf8') - }, Error) + assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error) }) } function fileErrorSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should throw error using` src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - var err = null + const srcpath = args[0] + const dstpath = args[1] + it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => { + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + let err = null try { fn.apply(null, args) } catch (e) { err = e } assert.equal(err instanceof Error, true) - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) }) } function fileDestExistsSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { - var destinationContentBefore = fs.readFileSync(dstpath, 'utf8') + const srcpath = args[0] + const dstpath = args[1] + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => { + const destinationContentBefore = fs.readFileSync(dstpath, 'utf8') fn.apply(null, args) - var destinationContentAfter = fs.readFileSync(dstpath, 'utf8') + const destinationContentAfter = fs.readFileSync(dstpath, 'utf8') assert.equal(destinationContentBefore, destinationContentAfter) }) } function dirSuccessSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, () => { fn.apply(null, args) - var relative = symlinkPathsSync(srcpath, dstpath) - var srcContents = fs.readdirSync(relative.toCwd) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstContents = fs.readdirSync(dstpath) - var dstDirContents = fs.readdirSync(dstDir) + const relative = symlinkPathsSync(srcpath, dstpath) + const srcContents = fs.readdirSync(relative.toCwd) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstContents = fs.readdirSync(dstpath) + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert.deepEqual(srcContents, dstContents) assert(dstDirContents.indexOf(dstBasename) >= 0) @@ -331,59 +310,54 @@ describe('fse-ensure-symlink', function () { } function dirBrokenSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should create broken symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { + const srcpath = args[0] + const dstpath = args[1] + it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, () => { fn.apply(null, args) - var dstDir = path.dirname(dstpath) - var dstBasename = path.basename(dstpath) - var isSymlink = fs.lstatSync(dstpath).isSymbolicLink() - var dstDirContents = fs.readdirSync(dstDir) + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstDirContents = fs.readdirSync(dstDir) assert.equal(isSymlink, true) assert(dstDirContents.indexOf(dstBasename) >= 0) - assert.throws(function () { - return fs.readdirSync(dstpath) - }, Error) + assert.throws(() => fs.readdirSync(dstpath), Error) }) } function dirErrorSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should throw error when creating symlink dir using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { - var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) - var err = null + const srcpath = args[0] + const dstpath = args[1] + it(`should throw error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, () => { + const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath)) + let err = null try { fn.apply(null, args) } catch (e) { err = e } assert.equal(err instanceof Error, true) - var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) + const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath)) assert.equal(dstdirExistsBefore, dstdirExistsAfter) }) } function dirDestExistsSync (args, fn) { - var srcpath = args[0] - var dstpath = args[1] - var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath) - it(should, function () { - var destinationContentBefore = fs.readdirSync(dstpath) + const srcpath = args[0] + const dstpath = args[1] + it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => { + const destinationContentBefore = fs.readdirSync(dstpath) fn.apply(null, args) - var destinationContentAfter = fs.readdirSync(dstpath) + const destinationContentAfter = fs.readdirSync(dstpath) assert.deepEqual(destinationContentBefore, destinationContentAfter) }) } - describe('fs.symlink()', function () { - var fn = fs.symlink - tests.forEach(function (test) { - var args = test[0].slice(0) - var nativeBehavior = test[1] - // var newBehavior = test[2] + describe('fs.symlink()', () => { + const fn = fs.symlink + tests.forEach(test => { + const args = test[0].slice(0) + const nativeBehavior = test[1] + // const newBehavior = test[2] if (nativeBehavior === 'file-success') fileSuccess(args, fn) if (nativeBehavior === 'file-broken') fileBroken(args, fn) if (nativeBehavior === 'file-error') fileError(args, fn) @@ -396,12 +370,12 @@ describe('fse-ensure-symlink', function () { }) }) - describe('ensureSymlink()', function () { - var fn = ensureSymlink - tests.forEach(function (test) { - var args = test[0] - // var nativeBehavior = test[1] - var newBehavior = test[2] + describe('ensureSymlink()', () => { + const fn = ensureSymlink + tests.forEach(test => { + const args = test[0] + // const nativeBehavior = test[1] + const newBehavior = test[2] if (newBehavior === 'file-success') fileSuccess(args, fn) if (newBehavior === 'file-broken') fileBroken(args, fn) if (newBehavior === 'file-error') fileError(args, fn) @@ -413,12 +387,12 @@ describe('fse-ensure-symlink', function () { }) }) - describe('fs.symlinkSync()', function () { - var fn = fs.symlinkSync - tests.forEach(function (test) { - var args = test[0].slice(0) - var nativeBehavior = test[1] - // var newBehavior = test[2] + describe('fs.symlinkSync()', () => { + const fn = fs.symlinkSync + tests.forEach(test => { + const args = test[0].slice(0) + const nativeBehavior = test[1] + // const newBehavior = test[2] if (nativeBehavior === 'file-success') fileSuccessSync(args, fn) if (nativeBehavior === 'file-broken') fileBrokenSync(args, fn) if (nativeBehavior === 'file-error') fileErrorSync(args, fn) @@ -431,12 +405,12 @@ describe('fse-ensure-symlink', function () { }) }) - describe('ensureSymlinkSync()', function () { - var fn = ensureSymlinkSync - tests.forEach(function (test) { - var args = test[0] - // var nativeBehavior = test[1] - var newBehavior = test[2] + describe('ensureSymlinkSync()', () => { + const fn = ensureSymlinkSync + tests.forEach(test => { + const args = test[0] + // const nativeBehavior = test[1] + const newBehavior = test[2] if (newBehavior === 'file-success') fileSuccessSync(args, fn) if (newBehavior === 'file-broken') fileBrokenSync(args, fn) if (newBehavior === 'file-error') fileErrorSync(args, fn) diff --git a/lib/ensure/file.js b/lib/ensure/file.js index 1c9c2de0..031d1303 100644 --- a/lib/ensure/file.js +++ b/lib/ensure/file.js @@ -1,21 +1,23 @@ -var path = require('path') -var fs = require('graceful-fs') -var mkdir = require('../mkdirs') +'use strict' + +const path = require('path') +const fs = require('graceful-fs') +const mkdir = require('../mkdirs') function createFile (file, callback) { function makeFile () { - fs.writeFile(file, '', function (err) { + fs.writeFile(file, '', err => { if (err) return callback(err) callback() }) } - fs.exists(file, function (fileExists) { + fs.exists(file, fileExists => { if (fileExists) return callback() - var dir = path.dirname(file) - fs.exists(dir, function (dirExists) { + const dir = path.dirname(file) + fs.exists(dir, dirExists => { if (dirExists) return makeFile() - mkdir.mkdirs(dir, function (err) { + mkdir.mkdirs(dir, err => { if (err) return callback(err) makeFile() }) @@ -26,7 +28,7 @@ function createFile (file, callback) { function createFileSync (file) { if (fs.existsSync(file)) return - var dir = path.dirname(file) + const dir = path.dirname(file) if (!fs.existsSync(dir)) { mkdir.mkdirsSync(dir) } @@ -35,8 +37,8 @@ function createFileSync (file) { } module.exports = { - createFile: createFile, - createFileSync: createFileSync, + createFile, + createFileSync, // alias ensureFile: createFile, ensureFileSync: createFileSync diff --git a/lib/ensure/index.js b/lib/ensure/index.js index 26e8705a..c1f67b71 100644 --- a/lib/ensure/index.js +++ b/lib/ensure/index.js @@ -1,6 +1,8 @@ -var file = require('./file') -var link = require('./link') -var symlink = require('./symlink') +'use strict' + +const file = require('./file') +const link = require('./link') +const symlink = require('./symlink') module.exports = { // file diff --git a/lib/ensure/link.js b/lib/ensure/link.js index 4e4e2833..8284fae3 100644 --- a/lib/ensure/link.js +++ b/lib/ensure/link.js @@ -1,27 +1,29 @@ -var path = require('path') -var fs = require('graceful-fs') -var mkdir = require('../mkdirs') +'use strict' + +const path = require('path') +const fs = require('graceful-fs') +const mkdir = require('../mkdirs') function createLink (srcpath, dstpath, callback) { function makeLink (srcpath, dstpath) { - fs.link(srcpath, dstpath, function (err) { + fs.link(srcpath, dstpath, err => { if (err) return callback(err) callback(null) }) } - fs.exists(dstpath, function (destinationExists) { + fs.exists(dstpath, destinationExists => { if (destinationExists) return callback(null) - fs.lstat(srcpath, function (err, stat) { + fs.lstat(srcpath, (err, stat) => { if (err) { err.message = err.message.replace('lstat', 'ensureLink') return callback(err) } - var dir = path.dirname(dstpath) - fs.exists(dir, function (dirExists) { + const dir = path.dirname(dstpath) + fs.exists(dir, dirExists => { if (dirExists) return makeLink(srcpath, dstpath) - mkdir.mkdirs(dir, function (err) { + mkdir.mkdirs(dir, err => { if (err) return callback(err) makeLink(srcpath, dstpath) }) @@ -31,7 +33,7 @@ function createLink (srcpath, dstpath, callback) { } function createLinkSync (srcpath, dstpath, callback) { - var destinationExists = fs.existsSync(dstpath) + const destinationExists = fs.existsSync(dstpath) if (destinationExists) return undefined try { @@ -41,8 +43,8 @@ function createLinkSync (srcpath, dstpath, callback) { throw err } - var dir = path.dirname(dstpath) - var dirExists = fs.existsSync(dir) + const dir = path.dirname(dstpath) + const dirExists = fs.existsSync(dir) if (dirExists) return fs.linkSync(srcpath, dstpath) mkdir.mkdirsSync(dir) @@ -50,8 +52,8 @@ function createLinkSync (srcpath, dstpath, callback) { } module.exports = { - createLink: createLink, - createLinkSync: createLinkSync, + createLink, + createLinkSync, // alias ensureLink: createLink, ensureLinkSync: createLinkSync diff --git a/lib/ensure/symlink-paths.js b/lib/ensure/symlink-paths.js index eebc12e0..e52d0396 100644 --- a/lib/ensure/symlink-paths.js +++ b/lib/ensure/symlink-paths.js @@ -1,6 +1,7 @@ -var path = require('path') -// path.isAbsolute shim for Node.js 0.10 support -var fs = require('graceful-fs') +'use strict' + +const path = require('path') +const fs = require('graceful-fs') /** * Function that returns two types of paths, one relative to symlink, and one @@ -26,7 +27,7 @@ var fs = require('graceful-fs') function symlinkPaths (srcpath, dstpath, callback) { if (path.isAbsolute(srcpath)) { - return fs.lstat(srcpath, function (err, stat) { + return fs.lstat(srcpath, (err, stat) => { if (err) { err.message = err.message.replace('lstat', 'ensureSymlink') return callback(err) @@ -37,16 +38,16 @@ function symlinkPaths (srcpath, dstpath, callback) { }) }) } else { - var dstdir = path.dirname(dstpath) - var relativeToDst = path.join(dstdir, srcpath) - return fs.exists(relativeToDst, function (exists) { + const dstdir = path.dirname(dstpath) + const relativeToDst = path.join(dstdir, srcpath) + return fs.exists(relativeToDst, exists => { if (exists) { return callback(null, { 'toCwd': relativeToDst, 'toDst': srcpath }) } else { - return fs.lstat(srcpath, function (err, stat) { + return fs.lstat(srcpath, (err, stat) => { if (err) { err.message = err.message.replace('lstat', 'ensureSymlink') return callback(err) @@ -62,7 +63,7 @@ function symlinkPaths (srcpath, dstpath, callback) { } function symlinkPathsSync (srcpath, dstpath) { - var exists + let exists if (path.isAbsolute(srcpath)) { exists = fs.existsSync(srcpath) if (!exists) throw new Error('absolute srcpath does not exist') @@ -71,8 +72,8 @@ function symlinkPathsSync (srcpath, dstpath) { 'toDst': srcpath } } else { - var dstdir = path.dirname(dstpath) - var relativeToDst = path.join(dstdir, srcpath) + const dstdir = path.dirname(dstpath) + const relativeToDst = path.join(dstdir, srcpath) exists = fs.existsSync(relativeToDst) if (exists) { return { @@ -91,6 +92,6 @@ function symlinkPathsSync (srcpath, dstpath) { } module.exports = { - 'symlinkPaths': symlinkPaths, - 'symlinkPathsSync': symlinkPathsSync + symlinkPaths, + symlinkPathsSync } diff --git a/lib/ensure/symlink-type.js b/lib/ensure/symlink-type.js index 81e35884..4f8787c2 100644 --- a/lib/ensure/symlink-type.js +++ b/lib/ensure/symlink-type.js @@ -1,10 +1,12 @@ -var fs = require('graceful-fs') +'use strict' + +const fs = require('graceful-fs') function symlinkType (srcpath, type, callback) { callback = (typeof type === 'function') ? type : callback type = (typeof type === 'function') ? false : type if (type) return callback(null, type) - fs.lstat(srcpath, function (err, stats) { + fs.lstat(srcpath, (err, stats) => { if (err) return callback(null, 'file') type = (stats && stats.isDirectory()) ? 'dir' : 'file' callback(null, type) @@ -12,9 +14,11 @@ function symlinkType (srcpath, type, callback) { } function symlinkTypeSync (srcpath, type) { + let stats + if (type) return type try { - var stats = fs.lstatSync(srcpath) + stats = fs.lstatSync(srcpath) } catch (e) { return 'file' } @@ -22,6 +26,6 @@ function symlinkTypeSync (srcpath, type) { } module.exports = { - symlinkType: symlinkType, - symlinkTypeSync: symlinkTypeSync + symlinkType, + symlinkTypeSync } diff --git a/lib/ensure/symlink.js b/lib/ensure/symlink.js index 62447906..2ea9e062 100644 --- a/lib/ensure/symlink.js +++ b/lib/ensure/symlink.js @@ -1,32 +1,34 @@ -var path = require('path') -var fs = require('graceful-fs') -var _mkdirs = require('../mkdirs') -var mkdirs = _mkdirs.mkdirs -var mkdirsSync = _mkdirs.mkdirsSync +'use strict' -var _symlinkPaths = require('./symlink-paths') -var symlinkPaths = _symlinkPaths.symlinkPaths -var symlinkPathsSync = _symlinkPaths.symlinkPathsSync +const path = require('path') +const fs = require('graceful-fs') +const _mkdirs = require('../mkdirs') +const mkdirs = _mkdirs.mkdirs +const mkdirsSync = _mkdirs.mkdirsSync -var _symlinkType = require('./symlink-type') -var symlinkType = _symlinkType.symlinkType -var symlinkTypeSync = _symlinkType.symlinkTypeSync +const _symlinkPaths = require('./symlink-paths') +const symlinkPaths = _symlinkPaths.symlinkPaths +const symlinkPathsSync = _symlinkPaths.symlinkPathsSync + +const _symlinkType = require('./symlink-type') +const symlinkType = _symlinkType.symlinkType +const symlinkTypeSync = _symlinkType.symlinkTypeSync function createSymlink (srcpath, dstpath, type, callback) { callback = (typeof type === 'function') ? type : callback type = (typeof type === 'function') ? false : type - fs.exists(dstpath, function (destinationExists) { + fs.exists(dstpath, destinationExists => { if (destinationExists) return callback(null) - symlinkPaths(srcpath, dstpath, function (err, relative) { + symlinkPaths(srcpath, dstpath, (err, relative) => { if (err) return callback(err) srcpath = relative.toDst - symlinkType(relative.toCwd, type, function (err, type) { + symlinkType(relative.toCwd, type, (err, type) => { if (err) return callback(err) - var dir = path.dirname(dstpath) - fs.exists(dir, function (dirExists) { + const dir = path.dirname(dstpath) + fs.exists(dir, dirExists => { if (dirExists) return fs.symlink(srcpath, dstpath, type, callback) - mkdirs(dir, function (err) { + mkdirs(dir, err => { if (err) return callback(err) fs.symlink(srcpath, dstpath, type, callback) }) @@ -40,22 +42,22 @@ function createSymlinkSync (srcpath, dstpath, type, callback) { callback = (typeof type === 'function') ? type : callback type = (typeof type === 'function') ? false : type - var destinationExists = fs.existsSync(dstpath) + const destinationExists = fs.existsSync(dstpath) if (destinationExists) return undefined - var relative = symlinkPathsSync(srcpath, dstpath) + const relative = symlinkPathsSync(srcpath, dstpath) srcpath = relative.toDst type = symlinkTypeSync(relative.toCwd, type) - var dir = path.dirname(dstpath) - var exists = fs.existsSync(dir) + const dir = path.dirname(dstpath) + const exists = fs.existsSync(dir) if (exists) return fs.symlinkSync(srcpath, dstpath, type) mkdirsSync(dir) return fs.symlinkSync(srcpath, dstpath, type) } module.exports = { - createSymlink: createSymlink, - createSymlinkSync: createSymlinkSync, + createSymlink, + createSymlinkSync, // alias ensureSymlink: createSymlink, ensureSymlinkSync: createSymlinkSync diff --git a/lib/index.js b/lib/index.js index 977a75bd..e840a9dc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,14 +1,16 @@ -var assign = require('./util/assign') +'use strict' -var fse = {} -var gfs = require('graceful-fs') +const assign = require('./util/assign') + +const fse = {} +const gfs = require('graceful-fs') // attach fs methods to fse -Object.keys(gfs).forEach(function (key) { +Object.keys(gfs).forEach(key => { fse[key] = gfs[key] }) -var fs = fse +const fs = fse assign(fs, require('./copy')) assign(fs, require('./copy-sync')) @@ -23,12 +25,10 @@ assign(fs, require('./output')) module.exports = fs // maintain backwards compatibility for awhile -var jsonfile = {} +const jsonfile = {} Object.defineProperty(jsonfile, 'spaces', { - get: function () { - return fs.spaces // found in ./json - }, - set: function (val) { + get: () => fs.spaces, // found in ./json + set: val => { fs.spaces = val } }) diff --git a/lib/json/__tests__/jsonfile-integration.test.js b/lib/json/__tests__/jsonfile-integration.test.js index 925b2dce..27f76654 100644 --- a/lib/json/__tests__/jsonfile-integration.test.js +++ b/lib/json/__tests__/jsonfile-integration.test.js @@ -1,36 +1,36 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('jsonfile-integration', function () { - var TEST_DIR +describe('jsonfile-integration', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'json') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ writeJsonSync / spaces', function () { - it('should read a file and parse the json', function () { - var obj1 = { + describe('+ writeJsonSync / spaces', () => { + it('should read a file and parse the json', () => { + const obj1 = { firstName: 'JP', lastName: 'Richardson' } - var oldSpaces = fse.jsonfile.spaces + const oldSpaces = fse.jsonfile.spaces fse.jsonfile.spaces = 4 - var file = path.join(TEST_DIR, 'file.json') + const file = path.join(TEST_DIR, 'file.json') fse.writeJsonSync(file, obj1) - var data = fs.readFileSync(file, 'utf8') + const data = fs.readFileSync(file, 'utf8') assert.strictEqual(data, JSON.stringify(obj1, null, 4) + '\n') fse.jsonfile.spaces = oldSpaces diff --git a/lib/json/__tests__/output-json-sync.test.js b/lib/json/__tests__/output-json-sync.test.js index 9d4ddd03..aa2d77f2 100644 --- a/lib/json/__tests__/output-json-sync.test.js +++ b/lib/json/__tests__/output-json-sync.test.js @@ -1,45 +1,46 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const outputJsonSync = require('../output-json-sync') /* global beforeEach, describe, it */ -describe('json', function () { - var TEST_DIR +describe('json', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra') fse.emptyDir(TEST_DIR, done) }) - describe('+ outputJsonSync(file, data)', function () { - var outputJsonSync = require('../output-json-sync') - - it('should write the file regardless of whether the directory exists or not', function () { - var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'file.json') + describe('+ outputJsonSync(file, data)', () => { + it('should write the file regardless of whether the directory exists or not', () => { + const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'file.json') assert(!fs.existsSync(file)) - var data = {name: 'JP'} + const data = {name: 'JP'} outputJsonSync(file, data) assert(fs.existsSync(file)) - var newData = JSON.parse(fs.readFileSync(file, 'utf8')) + const newData = JSON.parse(fs.readFileSync(file, 'utf8')) assert.equal(data.name, newData.name) }) - describe('> when an option is passed, like JSON replacer', function () { - it('should pass the option along to jsonfile module', function () { - var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json') + describe('> when an option is passed, like JSON replacer', () => { + it('should pass the option along to jsonfile module', () => { + const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json') assert(!fs.existsSync(file)) - var replacer = function (k, v) { if (v === 'JP') return 'Jon Paul'; else return v } - var data = {name: 'JP'} + const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v + const data = {name: 'JP'} - outputJsonSync(file, data, {replacer: replacer}) - var newData = JSON.parse(fs.readFileSync(file, 'utf8')) + outputJsonSync(file, data, { replacer }) + const newData = JSON.parse(fs.readFileSync(file, 'utf8')) assert.equal(newData.name, 'Jon Paul') }) diff --git a/lib/json/__tests__/output-json.test.js b/lib/json/__tests__/output-json.test.js index 8e6cd4c7..03b4714a 100644 --- a/lib/json/__tests__/output-json.test.js +++ b/lib/json/__tests__/output-json.test.js @@ -1,49 +1,50 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const outputJson = require('../output-json') /* global beforeEach, describe, it */ -describe('json', function () { - var TEST_DIR +describe('json', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra') fse.emptyDir(TEST_DIR, done) }) - describe('+ outputJson(file, data)', function () { - var outputJson = require('../output-json') - - it('should write the file regardless of whether the directory exists or not', function (done) { - var file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json') + describe('+ outputJson(file, data)', () => { + it('should write the file regardless of whether the directory exists or not', done => { + const file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json') assert(!fs.existsSync(file)) - var data = {name: 'JP'} - outputJson(file, data, function (err) { + const data = {name: 'JP'} + outputJson(file, data, err => { if (err) return done(err) assert(fs.existsSync(file)) - var newData = JSON.parse(fs.readFileSync(file, 'utf8')) + const newData = JSON.parse(fs.readFileSync(file, 'utf8')) assert.equal(data.name, newData.name) done() }) }) - describe('> when an option is passed, like JSON replacer', function () { - it('should pass the option along to jsonfile module', function (done) { - var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json') + describe('> when an option is passed, like JSON replacer', () => { + it('should pass the option along to jsonfile module', done => { + const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json') assert(!fs.existsSync(file)) - var replacer = function (k, v) { if (v === 'JP') return 'Jon Paul'; else return v } - var data = {name: 'JP'} + const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v + const data = {name: 'JP'} - outputJson(file, data, {replacer: replacer}, function (err) { + outputJson(file, data, { replacer }, err => { assert.ifError(err) - var newData = JSON.parse(fs.readFileSync(file, 'utf8')) + const newData = JSON.parse(fs.readFileSync(file, 'utf8')) assert.equal(newData.name, 'Jon Paul') done() }) diff --git a/lib/json/__tests__/read.test.js b/lib/json/__tests__/read.test.js index 34bee4c1..f26f221c 100644 --- a/lib/json/__tests__/read.test.js +++ b/lib/json/__tests__/read.test.js @@ -1,45 +1,44 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('read', function () { - var TEST_DIR +describe('read', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'read-json') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ readJSON', function () { - it('should read a file and parse the json', function (done) { - var obj1 = { + describe('+ readJSON', () => { + it('should read a file and parse the json', done => { + const obj1 = { firstName: 'JP', lastName: 'Richardson' } - var file = path.join(TEST_DIR, 'file.json') + const file = path.join(TEST_DIR, 'file.json') fs.writeFileSync(file, JSON.stringify(obj1)) - fse.readJSON(file, function (err, obj2) { + fse.readJSON(file, (err, obj2) => { assert.ifError(err) assert.strictEqual(obj1.firstName, obj2.firstName) assert.strictEqual(obj1.lastName, obj2.lastName) - done() }) }) - it('should error if it cant parse the json', function (done) { - var file = path.join(TEST_DIR, 'file2.json') + it('should error if it cant parse the json', done => { + const file = path.join(TEST_DIR, 'file2.json') fs.writeFileSync(file, '%asdfasdff444') - fse.readJSON(file, function (err, obj) { + fse.readJSON(file, (err, obj) => { assert(err) assert(!obj) done() diff --git a/lib/json/__tests__/spaces.test.js b/lib/json/__tests__/spaces.test.js index f878688f..ab428c87 100644 --- a/lib/json/__tests__/spaces.test.js +++ b/lib/json/__tests__/spaces.test.js @@ -1,34 +1,34 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ // trinity: mocha -describe('json spaces', function () { - var TEST_DIR +describe('json spaces', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'json-spaces') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should write out the file with appropriate spacing (2)', function () { + it('should write out the file with appropriate spacing (2)', () => { fse.spaces = 2 // for legacy compatibility assert.strictEqual(fse.spaces, 2) - var tempFile = path.join(TEST_DIR, 'temp.json') + const tempFile = path.join(TEST_DIR, 'temp.json') - var data = { first: 'JP', last: 'Richardson' } + const data = { first: 'JP', last: 'Richardson' } fse.outputJsonSync(tempFile, data) - var dataRead = fs.readFileSync(tempFile, 'utf8') + const dataRead = fs.readFileSync(tempFile, 'utf8') assert.strictEqual(dataRead, JSON.stringify(data, null, 2) + '\n') }) }) diff --git a/lib/json/index.js b/lib/json/index.js index b13cf54e..42293113 100644 --- a/lib/json/index.js +++ b/lib/json/index.js @@ -1,4 +1,6 @@ -var jsonFile = require('./jsonfile') +'use strict' + +const jsonFile = require('./jsonfile') jsonFile.outputJsonSync = require('./output-json-sync') jsonFile.outputJson = require('./output-json') diff --git a/lib/json/jsonfile.js b/lib/json/jsonfile.js index 51d83906..e3630ee8 100644 --- a/lib/json/jsonfile.js +++ b/lib/json/jsonfile.js @@ -1,4 +1,6 @@ -var jsonFile = require('jsonfile') +'use strict' + +const jsonFile = require('jsonfile') module.exports = { // jsonfile exports diff --git a/lib/json/output-json-sync.js b/lib/json/output-json-sync.js index 76848437..6f76710e 100644 --- a/lib/json/output-json-sync.js +++ b/lib/json/output-json-sync.js @@ -1,10 +1,12 @@ -var fs = require('graceful-fs') -var path = require('path') -var jsonFile = require('./jsonfile') -var mkdir = require('../mkdirs') +'use strict' + +const fs = require('graceful-fs') +const path = require('path') +const mkdir = require('../mkdirs') +const jsonFile = require('./jsonfile') function outputJsonSync (file, data, options) { - var dir = path.dirname(file) + const dir = path.dirname(file) if (!fs.existsSync(dir)) { mkdir.mkdirsSync(dir) diff --git a/lib/json/output-json.js b/lib/json/output-json.js index 7824597b..2f928c50 100644 --- a/lib/json/output-json.js +++ b/lib/json/output-json.js @@ -1,7 +1,9 @@ -var fs = require('graceful-fs') -var path = require('path') -var jsonFile = require('./jsonfile') -var mkdir = require('../mkdirs') +'use strict' + +const fs = require('graceful-fs') +const path = require('path') +const mkdir = require('../mkdirs') +const jsonFile = require('./jsonfile') function outputJson (file, data, options, callback) { if (typeof options === 'function') { @@ -9,12 +11,12 @@ function outputJson (file, data, options, callback) { options = {} } - var dir = path.dirname(file) + const dir = path.dirname(file) - fs.exists(dir, function (itDoes) { + fs.exists(dir, itDoes => { if (itDoes) return jsonFile.writeJson(file, data, options, callback) - mkdir.mkdirs(dir, function (err) { + mkdir.mkdirs(dir, err => { if (err) return callback(err) jsonFile.writeJson(file, data, options, callback) }) diff --git a/lib/mkdirs/__tests__/chmod.test.js b/lib/mkdirs/__tests__/chmod.test.js index cb1b18a4..5063d9d1 100644 --- a/lib/mkdirs/__tests__/chmod.test.js +++ b/lib/mkdirs/__tests__/chmod.test.js @@ -1,24 +1,26 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o744 = parseInt('744', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o744 = parseInt('744', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / chmod', function () { - var TEST_DIR - var TEST_SUBDIR +describe('mkdirp / chmod', () => { + let TEST_DIR + let TEST_SUBDIR - beforeEach(function (done) { - var ps = [] - for (var i = 0; i < 15; i++) { - var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + beforeEach(done => { + const ps = [] + for (let i = 0; i < 15; i++) { + const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) ps.push(dir) } @@ -30,16 +32,14 @@ describe('mkdirp / chmod', function () { fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('chmod-pre', function (done) { - var mode = o744 - fse.mkdirp(TEST_SUBDIR, mode, function (er) { - assert.ifError(er, 'should not error') - fs.stat(TEST_SUBDIR, function (er, stat) { - assert.ifError(er, 'should exist') + it('chmod-pre', done => { + const mode = o744 + fse.mkdirp(TEST_SUBDIR, mode, err => { + assert.ifError(err, 'should not error') + fs.stat(TEST_SUBDIR, (err, stat) => { + assert.ifError(err, 'should exist') assert.ok(stat && stat.isDirectory(), 'should be directory') if (os.platform().indexOf('win') === 0) { @@ -53,12 +53,12 @@ describe('mkdirp / chmod', function () { }) }) - it('chmod', function (done) { - var mode = o755 - fse.mkdirp(TEST_SUBDIR, mode, function (er) { - assert.ifError(er, 'should not error') - fs.stat(TEST_SUBDIR, function (er, stat) { - assert.ifError(er, 'should exist') + it('chmod', done => { + const mode = o755 + fse.mkdirp(TEST_SUBDIR, mode, err => { + assert.ifError(err, 'should not error') + fs.stat(TEST_SUBDIR, (err, stat) => { + assert.ifError(err, 'should exist') assert.ok(stat && stat.isDirectory(), 'should be directory') done() }) diff --git a/lib/mkdirs/__tests__/clobber.test.js b/lib/mkdirs/__tests__/clobber.test.js index f69533bd..67d88112 100644 --- a/lib/mkdirs/__tests__/clobber.test.js +++ b/lib/mkdirs/__tests__/clobber.test.js @@ -1,46 +1,48 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global before, describe, it */ -var o755 = parseInt('755', 8) +const o755 = parseInt('755', 8) -describe('mkdirp / clobber', function () { - var TEST_DIR - var file +describe('mkdirp / clobber', () => { + let TEST_DIR + let file - before(function (done) { + before(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-clobber') - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) - var ps = [TEST_DIR] + const ps = [ TEST_DIR ] - for (var i = 0; i < 15; i++) { - var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + for (let i = 0; i < 15; i++) { + const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) ps.push(dir) } file = ps.join(path.sep) // a file in the way - var itw = ps.slice(0, 2).join(path.sep) + const itw = ps.slice(0, 2).join(path.sep) fs.writeFileSync(itw, 'I AM IN THE WAY, THE TRUTH, AND THE LIGHT.') - fs.stat(itw, function (er, stat) { - assert.ifError(er) + fs.stat(itw, (err, stat) => { + assert.ifError(err) assert.ok(stat && stat.isFile(), 'should be file') done() }) }) }) - it('should clobber', function (done) { - fse.mkdirp(file, o755, function (err) { + it('should clobber', done => { + fse.mkdirp(file, o755, err => { assert.ok(err) if (os.platform().indexOf('win') === 0) { assert.equal(err.code, 'EEXIST') diff --git a/lib/mkdirs/__tests__/issue-209.test.js b/lib/mkdirs/__tests__/issue-209.test.js index 2640eb92..7b16b816 100644 --- a/lib/mkdirs/__tests__/issue-209.test.js +++ b/lib/mkdirs/__tests__/issue-209.test.js @@ -1,20 +1,22 @@ -var assert = require('assert') -var fse = require(process.cwd()) +'use strict' + +const assert = require('assert') +const fse = require(process.cwd()) /* global describe, it */ -describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error', function () { +describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error', () => { // only seems to be an issue on Windows. if (process.platform !== 'win32') return - it('should return a callback', function (done) { - var file = './bad?dir' - fse.mkdirp(file, function (err) { + it('should return a callback', done => { + const file = './bad?dir' + fse.mkdirp(file, err => { assert(err, 'error is present') assert.strictEqual(err.code, 'EINVAL') - var file2 = 'c:\\tmp\foo:moo' - fse.mkdirp(file2, function (err) { + const file2 = 'c:\\tmp\foo:moo' + fse.mkdirp(file2, err => { assert(err, 'error is present') assert.strictEqual(err.code, 'EINVAL') done() @@ -22,11 +24,11 @@ describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error }) }) - describe('> sync', function () { - it('should throw an error', function () { - var didErr + describe('> sync', () => { + it('should throw an error', () => { + let didErr try { - var file = 'c:\\tmp\foo:moo' + const file = 'c:\\tmp\foo:moo' fse.mkdirpSync(file) } catch (err) { // console.error(err) diff --git a/lib/mkdirs/__tests__/issue-93.test.js b/lib/mkdirs/__tests__/issue-93.test.js index f7fc1fb2..771694e6 100644 --- a/lib/mkdirs/__tests__/issue-93.test.js +++ b/lib/mkdirs/__tests__/issue-93.test.js @@ -1,27 +1,29 @@ -var assert = require('assert') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global before, describe, it */ -describe('mkdirp: issue-93, win32, when drive does not exist, it should return a cleaner error', function () { - var TEST_DIR +describe('mkdirp: issue-93, win32, when drive does not exist, it should return a cleaner error', () => { + let TEST_DIR // only seems to be an issue on Windows. if (process.platform !== 'win32') return - before(function (done) { + before(done => { TEST_DIR = path.join(os.tmpdir(), 'tests', 'fs-extra', 'mkdirp-issue-93') - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) done() }) }) - it('should return a cleaner error than inifinite loop, stack crash', function (done) { - var file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor - fse.mkdirp(file, function (err) { + it('should return a cleaner error than inifinite loop, stack crash', done => { + const file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor + fse.mkdirp(file, err => { assert.strictEqual(err.code, 'ENOENT') try { diff --git a/lib/mkdirs/__tests__/mkdir.test.js b/lib/mkdirs/__tests__/mkdir.test.js index adb3eebe..35cb7e67 100644 --- a/lib/mkdirs/__tests__/mkdir.test.js +++ b/lib/mkdirs/__tests__/mkdir.test.js @@ -1,43 +1,43 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('fs-extra', function () { - var TEST_DIR +describe('fs-extra', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdir') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ mkdirs()', function () { - it('should make the directory', function (done) { - var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) + describe('+ mkdirs()', () => { + it('should make the directory', done => { + const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) assert(!fs.existsSync(dir)) - fse.mkdirs(dir, function (err) { + fse.mkdirs(dir, err => { assert.ifError(err) assert(fs.existsSync(dir)) done() }) }) - it('should make the entire directory path', function (done) { - var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) - var newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa') + it('should make the entire directory path', done => { + const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) + const newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa') assert(!fs.existsSync(dir)) - fse.mkdirs(newDir, function (err) { + fse.mkdirs(newDir, err => { assert.ifError(err) assert(fs.existsSync(newDir)) done() @@ -45,9 +45,9 @@ describe('fs-extra', function () { }) }) - describe('+ mkdirsSync()', function () { - it('should make the directory', function (done) { - var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) + describe('+ mkdirsSync()', () => { + it('should make the directory', done => { + const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) assert(!fs.existsSync(dir)) fse.mkdirsSync(dir) @@ -56,9 +56,9 @@ describe('fs-extra', function () { done() }) - it('should make the entire directory path', function (done) { - var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) - var newDir = path.join(dir, 'dfdf', 'ffff', 'aaa') + it('should make the entire directory path', done => { + const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random()) + const newDir = path.join(dir, 'dfdf', 'ffff', 'aaa') assert(!fs.existsSync(newDir)) fse.mkdirsSync(newDir) diff --git a/lib/mkdirs/__tests__/mkdirp.test.js b/lib/mkdirs/__tests__/mkdirp.test.js index 10b1f714..7ad38686 100644 --- a/lib/mkdirs/__tests__/mkdirp.test.js +++ b/lib/mkdirs/__tests__/mkdirp.test.js @@ -1,39 +1,39 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / mkdirp', function () { - var TEST_DIR +describe('mkdirp / mkdirp', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should make the dir', function (done) { - var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + it('should make the dir', done => { + const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var file = path.join(TEST_DIR, x, y, z) + const file = path.join(TEST_DIR, x, y, z) - fse.mkdirp(file, o755, function (err) { + fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) if (os.platform().indexOf('win') === 0) { diff --git a/lib/mkdirs/__tests__/opts-undef.test.js b/lib/mkdirs/__tests__/opts-undef.test.js index 096f77e0..e72e79b5 100644 --- a/lib/mkdirs/__tests__/opts-undef.test.js +++ b/lib/mkdirs/__tests__/opts-undef.test.js @@ -1,26 +1,28 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) -var mkdirs = require('../mkdirs') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const mkdirs = require('../mkdirs') /* global beforeEach, describe, it */ -describe('mkdirs / opts-undef', function () { - var TEST_DIR +describe('mkdirs / opts-undef', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirs') fse.emptyDir(TEST_DIR, done) }) // https://github.com/substack/node-mkdirp/issues/45 - it('should not hang', function (done) { - var newDir = path.join(TEST_DIR, 'doest', 'not', 'exist') + it('should not hang', done => { + const newDir = path.join(TEST_DIR, 'doest', 'not', 'exist') assert(!fs.existsSync(newDir)) - mkdirs(newDir, undefined, function (err) { + mkdirs(newDir, undefined, err => { assert.ifError(err) assert(fs.existsSync(newDir)) done() diff --git a/lib/mkdirs/__tests__/perm.test.js b/lib/mkdirs/__tests__/perm.test.js index d2650864..873ed6ac 100644 --- a/lib/mkdirs/__tests__/perm.test.js +++ b/lib/mkdirs/__tests__/perm.test.js @@ -1,35 +1,35 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / perm', function () { - var TEST_DIR +describe('mkdirp / perm', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-perm') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('async perm', function (done) { - var file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16)) + it('async perm', done => { + let file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16)) - fse.mkdirp(file, o755, function (err) { + fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) if (os.platform().indexOf('win') === 0) { @@ -45,8 +45,8 @@ describe('mkdirp / perm', function () { }) }) - it('async root perm', function (done) { - fse.mkdirp(path.join(os.tmpdir(), 'tmp'), o755, function (err) { + it('async root perm', done => { + fse.mkdirp(path.join(os.tmpdir(), 'tmp'), o755, err => { assert.ifError(err) done() }) diff --git a/lib/mkdirs/__tests__/perm_sync.test.js b/lib/mkdirs/__tests__/perm_sync.test.js index f2fa7cee..cfd53be7 100644 --- a/lib/mkdirs/__tests__/perm_sync.test.js +++ b/lib/mkdirs/__tests__/perm_sync.test.js @@ -1,34 +1,34 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / perm_sync', function () { - var TEST_DIR +describe('mkdirp / perm_sync', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-perm-sync') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('sync perm', function (done) { - var file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16) + '.json') + it('sync perm', done => { + const file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16) + '.json') fse.mkdirpSync(file, o755) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) if (os.platform().indexOf('win') === 0) { @@ -43,12 +43,12 @@ describe('mkdirp / perm_sync', function () { }) }) - it('sync root perm', function (done) { - var file = TEST_DIR + it('sync root perm', done => { + const file = TEST_DIR fse.mkdirpSync(file, o755) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) assert.ok(stat.isDirectory(), 'target not a directory') done() diff --git a/lib/mkdirs/__tests__/race.test.js b/lib/mkdirs/__tests__/race.test.js index 9022df6f..982b7138 100644 --- a/lib/mkdirs/__tests__/race.test.js +++ b/lib/mkdirs/__tests__/race.test.js @@ -1,27 +1,30 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / race', function () { - var TEST_DIR, file +describe('mkdirp / race', () => { + let TEST_DIR + let file - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-race') - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) - var ps = [TEST_DIR] + const ps = [ TEST_DIR ] - for (var i = 0; i < 15; i++) { - var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + for (let i = 0; i < 15; i++) { + const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) ps.push(dir) } @@ -30,26 +33,20 @@ describe('mkdirp / race', function () { }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('race', function (done) { - var res = 2 - mk(file, function () { - if (--res === 0) done() - }) + it('race', done => { + let res = 2 - mk(file, function () { - if (--res === 0) done() - }) + mk(file, () => --res === 0 ? done() : undefined) + mk(file, () => --res === 0 ? done() : undefined) function mk (file, callback) { - fse.mkdirp(file, o755, function (err) { + fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) if (os.platform().indexOf('win') === 0) { diff --git a/lib/mkdirs/__tests__/rel.test.js b/lib/mkdirs/__tests__/rel.test.js index 815d9d65..4971d0ed 100644 --- a/lib/mkdirs/__tests__/rel.test.js +++ b/lib/mkdirs/__tests__/rel.test.js @@ -1,26 +1,31 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const CWD = process.cwd() + +const fs = require('fs') +const os = require('os') +const fse = require(CWD) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / relative', function () { - var TEST_DIR, file +describe('mkdirp / relative', () => { + let TEST_DIR + let file - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-relative') - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) - var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) // relative path file = path.join(x, y, z) @@ -29,22 +34,19 @@ describe('mkdirp / relative', function () { }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should make the directory with relative path', function (done) { - var cwd = process.cwd() + it('should make the directory with relative path', done => { process.chdir(TEST_DIR) - fse.mkdirp(file, o755, function (err) { + fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) // restore - process.chdir(cwd) + process.chdir(CWD) if (os.platform().indexOf('win') === 0) { assert.equal(stat.mode & o777, o666) diff --git a/lib/mkdirs/__tests__/return.test.js b/lib/mkdirs/__tests__/return.test.js index fecea895..ae32ad00 100644 --- a/lib/mkdirs/__tests__/return.test.js +++ b/lib/mkdirs/__tests__/return.test.js @@ -1,37 +1,37 @@ -var assert = require('assert') -var os = require('os') -var path = require('path') -var fse = require('../../') +'use strict' + +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('mkdirp / return value', function () { - var TEST_DIR +describe('mkdirp / return value', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-return') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should', function (done) { - var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + it('should', done => { + const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var dir = TEST_DIR + path.sep - var file = dir + [x, y, z].join(path.sep) + const dir = TEST_DIR + path.sep + const file = dir + [x, y, z].join(path.sep) // should return the first dir created. // By this point, it would be profoundly surprising if /tmp didn't // already exist, since every other test makes things in there. - fse.mkdirp(file, function (err, made) { + fse.mkdirp(file, (err, made) => { assert.ifError(err) assert.equal(made, dir + x) - fse.mkdirp(file, function (err, made) { + fse.mkdirp(file, (err, made) => { assert.ifError(err) assert.equal(made, null) done() diff --git a/lib/mkdirs/__tests__/return_sync.test.js b/lib/mkdirs/__tests__/return_sync.test.js index d16dd905..b2740583 100644 --- a/lib/mkdirs/__tests__/return_sync.test.js +++ b/lib/mkdirs/__tests__/return_sync.test.js @@ -1,35 +1,35 @@ -var assert = require('assert') -var os = require('os') -var path = require('path') -var fse = require('../../') +'use strict' + +const os = require('os') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('mkdirp / return value', function () { - var TEST_DIR +describe('mkdirp / return value', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-return') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should', function () { - var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + it('should', () => { + const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var dir = TEST_DIR + path.sep - var file = dir + [x, y, z].join(path.sep) + const dir = TEST_DIR + path.sep + const file = dir + [x, y, z].join(path.sep) // should return the first dir created. // By this point, it would be profoundly surprising if /tmp didn't // already exist, since every other test makes things in there. // Note that this will throw on failure, which will fail the test. - var made = fse.mkdirpSync(file) + let made = fse.mkdirpSync(file) assert.equal(made, dir + x) // making the same file again should have no effect. diff --git a/lib/mkdirs/__tests__/root.test.js b/lib/mkdirs/__tests__/root.test.js index 5311103c..ee49c39d 100644 --- a/lib/mkdirs/__tests__/root.test.js +++ b/lib/mkdirs/__tests__/root.test.js @@ -1,23 +1,25 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var fse = require('../../') +'use strict' + +const fs = require('fs') +const fse = require('../../') +const path = require('path') +const assert = require('assert') /* global describe, it */ -var o755 = parseInt('755', 8) +const o755 = parseInt('755', 8) -describe('mkdirp / root', function () { +describe('mkdirp / root', () => { // '/' on unix, 'c:/' on windows. - var dir = path.normalize(path.resolve(path.sep)).toLowerCase() + const dir = path.normalize(path.resolve(path.sep)).toLowerCase() // if not 'c:\\' or 'd:\\', it's probably a network mounted drive, this fails then. TODO: investigate if (process.platform === 'win32' && (dir.indexOf('c:\\') === -1) && (dir.indexOf('d:\\') === -1)) return - it('should', function (done) { - fse.mkdirp(dir, o755, function (err) { + it('should', done => { + fse.mkdirp(dir, o755, err => { if (err) throw err - fs.stat(dir, function (er, stat) { + fs.stat(dir, (er, stat) => { if (er) throw er assert.ok(stat.isDirectory(), 'target is a directory') done() diff --git a/lib/mkdirs/__tests__/sync.test.js b/lib/mkdirs/__tests__/sync.test.js index 4290be1d..a7033fef 100644 --- a/lib/mkdirs/__tests__/sync.test.js +++ b/lib/mkdirs/__tests__/sync.test.js @@ -1,26 +1,29 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -var o755 = parseInt('755', 8) -var o777 = parseInt('777', 8) -var o666 = parseInt('666', 8) +const o755 = parseInt('755', 8) +const o777 = parseInt('777', 8) +const o666 = parseInt('666', 8) -describe('mkdirp / sync', function () { - var TEST_DIR, file +describe('mkdirp / sync', () => { + let TEST_DIR + let file - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-sync') - fse.emptyDir(TEST_DIR, function (err) { + fse.emptyDir(TEST_DIR, err => { assert.ifError(err) - var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) file = path.join(TEST_DIR, x, y, z) @@ -28,20 +31,18 @@ describe('mkdirp / sync', function () { }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - it('should', function (done) { + it('should', done => { try { fse.mkdirpSync(file, o755) } catch (err) { assert.fail(err) } - fs.exists(file, function (ex) { + fs.exists(file, ex => { assert.ok(ex, 'file created') - fs.stat(file, function (err, stat) { + fs.stat(file, (err, stat) => { assert.ifError(err) // http://stackoverflow.com/questions/592448/c-how-to-set-file-permissions-cross-platform if (os.platform().indexOf('win') === 0) { diff --git a/lib/mkdirs/__tests__/umask.test.js b/lib/mkdirs/__tests__/umask.test.js index c5bfc312..a3d06e7d 100644 --- a/lib/mkdirs/__tests__/umask.test.js +++ b/lib/mkdirs/__tests__/umask.test.js @@ -1,27 +1,29 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require('../../') +'use strict' + +const assert = require('assert') +const fs = require('fs') +const path = require('path') +const os = require('os') +const fse = require('../../') /* global afterEach, beforeEach, describe, it */ -var o777 = parseInt('777', 8) +const o777 = parseInt('777', 8) -describe('mkdirp', function () { - var TEST_DIR - var _rndDir +describe('mkdirp', () => { + let TEST_DIR + let _rndDir // should investigate this test and file more if (os.platform().indexOf('win') === 0) return - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'mkdirp') - fse.emptyDir(TEST_DIR, function () { + fse.emptyDir(TEST_DIR, () => { // for actual tests - var x = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) - var y = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) - var z = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) + const x = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) + const y = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) + const z = Math.floor(Math.random() * Math.pow(16, 6)).toString(16) _rndDir = path.join(TEST_DIR, [x, y, z].join(path.sep)) @@ -31,20 +33,18 @@ describe('mkdirp', function () { }) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('umask', function () { - describe('async', function () { - it('should have proper umask', function (done) { + describe('umask', () => { + describe('async', () => { + it('should have proper umask', done => { process.umask(0) - fse.mkdirp(_rndDir, function (err) { + fse.mkdirp(_rndDir, err => { assert.ifError(err) - fs.exists(_rndDir, function (ex) { + fs.exists(_rndDir, ex => { assert.ok(ex, 'file created') - fs.stat(_rndDir, function (err, stat) { + fs.stat(_rndDir, (err, stat) => { assert.ifError(err) assert.equal(stat.mode & o777, o777 & (~process.umask())) assert.ok(stat.isDirectory(), 'target not a directory') @@ -55,8 +55,8 @@ describe('mkdirp', function () { }) }) - describe('sync', function () { - it('should have proper umask', function (done) { + describe('sync', () => { + it('should have proper umask', done => { process.umask(0) try { @@ -65,9 +65,9 @@ describe('mkdirp', function () { return done(err) } - fs.exists(_rndDir, function (ex) { + fs.exists(_rndDir, ex => { assert.ok(ex, 'file created') - fs.stat(_rndDir, function (err, stat) { + fs.stat(_rndDir, (err, stat) => { assert.ifError(err) assert.equal(stat.mode & o777, (o777 & (~process.umask()))) assert.ok(stat.isDirectory(), 'target not a directory') diff --git a/lib/mkdirs/mkdirs-sync.js b/lib/mkdirs/mkdirs-sync.js index 3f30680d..a3ece406 100644 --- a/lib/mkdirs/mkdirs-sync.js +++ b/lib/mkdirs/mkdirs-sync.js @@ -1,19 +1,21 @@ -var fs = require('graceful-fs') -var path = require('path') -var invalidWin32Path = require('./win32').invalidWin32Path +'use strict' -var o777 = parseInt('0777', 8) +const fs = require('graceful-fs') +const path = require('path') +const invalidWin32Path = require('./win32').invalidWin32Path + +const o777 = parseInt('0777', 8) function mkdirsSync (p, opts, made) { if (!opts || typeof opts !== 'object') { opts = { mode: opts } } - var mode = opts.mode - var xfs = opts.fs || fs + let mode = opts.mode + const xfs = opts.fs || fs if (process.platform === 'win32' && invalidWin32Path(p)) { - var errInval = new Error(p + ' contains invalid WIN32 path characters.') + const errInval = new Error(p + ' contains invalid WIN32 path characters.') errInval.code = 'EINVAL' throw errInval } @@ -40,7 +42,7 @@ function mkdirsSync (p, opts, made) { // there already. If so, then hooray! If not, then something // is borked. default: - var stat + let stat try { stat = xfs.statSync(p) } catch (err1) { diff --git a/lib/mkdirs/mkdirs.js b/lib/mkdirs/mkdirs.js index 939776c4..18975337 100644 --- a/lib/mkdirs/mkdirs.js +++ b/lib/mkdirs/mkdirs.js @@ -1,8 +1,10 @@ -var fs = require('graceful-fs') -var path = require('path') -var invalidWin32Path = require('./win32').invalidWin32Path +'use strict' -var o777 = parseInt('0777', 8) +const fs = require('graceful-fs') +const path = require('path') +const invalidWin32Path = require('./win32').invalidWin32Path + +const o777 = parseInt('0777', 8) function mkdirs (p, opts, callback, made) { if (typeof opts === 'function') { @@ -13,13 +15,13 @@ function mkdirs (p, opts, callback, made) { } if (process.platform === 'win32' && invalidWin32Path(p)) { - var errInval = new Error(p + ' contains invalid WIN32 path characters.') + const errInval = new Error(p + ' contains invalid WIN32 path characters.') errInval.code = 'EINVAL' return callback(errInval) } - var mode = opts.mode - var xfs = opts.fs || fs + let mode = opts.mode + const xfs = opts.fs || fs if (mode === undefined) { mode = o777 & (~process.umask()) @@ -29,7 +31,7 @@ function mkdirs (p, opts, callback, made) { callback = callback || function () {} p = path.resolve(p) - xfs.mkdir(p, mode, function (er) { + xfs.mkdir(p, mode, er => { if (!er) { made = made || p return callback(null, made) @@ -37,7 +39,7 @@ function mkdirs (p, opts, callback, made) { switch (er.code) { case 'ENOENT': if (path.dirname(p) === p) return callback(er) - mkdirs(path.dirname(p), opts, function (er, made) { + mkdirs(path.dirname(p), opts, (er, made) => { if (er) callback(er, made) else mkdirs(p, opts, callback, made) }) @@ -47,7 +49,7 @@ function mkdirs (p, opts, callback, made) { // there already. If so, then hooray! If not, then something // is borked. default: - xfs.stat(p, function (er2, stat) { + xfs.stat(p, (er2, stat) => { // if the stat fails, then that's super weird. // let the original error be the failure reason. if (er2 || !stat.isDirectory()) callback(er, made) diff --git a/lib/mkdirs/win32.js b/lib/mkdirs/win32.js index 569ac1ae..99b3920f 100644 --- a/lib/mkdirs/win32.js +++ b/lib/mkdirs/win32.js @@ -1,24 +1,25 @@ 'use strict' -var path = require('path') + +const path = require('path') // get drive on windows function getRootPath (p) { p = path.normalize(path.resolve(p)).split(path.sep) if (p.length > 0) return p[0] - else return null + return null } // http://stackoverflow.com/a/62888/10333 contains more accurate // TODO: expand to include the rest -var INVALID_PATH_CHARS = /[<>:"|?*]/ +const INVALID_PATH_CHARS = /[<>:"|?*]/ function invalidWin32Path (p) { - var rp = getRootPath(p) + const rp = getRootPath(p) p = p.replace(rp, '') return INVALID_PATH_CHARS.test(p) } module.exports = { - getRootPath: getRootPath, - invalidWin32Path: invalidWin32Path + getRootPath, + invalidWin32Path } diff --git a/lib/move/__tests__/move.test.js b/lib/move/__tests__/move.test.js index bdf199f5..fc467b96 100644 --- a/lib/move/__tests__/move.test.js +++ b/lib/move/__tests__/move.test.js @@ -1,18 +1,20 @@ -var assert = require('assert') -var os = require('os') -var path = require('path') -var rimraf = require('rimraf') -var fs = require('graceful-fs') -var fse = require(process.cwd()) +'use strict' + +const fs = require('graceful-fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const rimraf = require('rimraf') /* global afterEach, beforeEach, describe, it */ function createAsyncErrFn (errCode) { - var fn = function () { + const fn = function () { fn.callCount++ - var callback = arguments[arguments.length - 1] - setTimeout(function () { - var err = new Error() + const callback = arguments[arguments.length - 1] + setTimeout(() => { + const err = new Error() err.code = errCode callback(err) }, 10) @@ -21,8 +23,8 @@ function createAsyncErrFn (errCode) { return fn } -var originalRename = fs.rename -var originalLink = fs.link +const originalRename = fs.rename +const originalLink = fs.link function setUpMockFs (errCode) { fs.rename = createAsyncErrFn(errCode) @@ -34,10 +36,10 @@ function tearDownMockFs () { fs.link = originalLink } -describe('move', function () { - var TEST_DIR +describe('move', () => { + let TEST_DIR - beforeEach(function () { + beforeEach(() => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move') fse.emptyDirSync(TEST_DIR) @@ -50,18 +52,16 @@ describe('move', function () { fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n') }) - afterEach(function (done) { - rimraf(TEST_DIR, done) - }) + afterEach(done => rimraf(TEST_DIR, done)) - it('should rename a file on the same device', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-file-dest' + it('should rename a file on the same device', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-file-dest` - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) - fs.readFile(dest, 'utf8', function (err, contents) { - var expected = /^sonic the hedgehog\r?\n$/ + fs.readFile(dest, 'utf8', (err, contents) => { + const expected = /^sonic the hedgehog\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) done() @@ -69,43 +69,43 @@ describe('move', function () { }) }) - it('should not overwrite the destination by default', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-folder/another-file' + it('should not overwrite the destination by default', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-folder/another-file` // verify file exists already assert(fs.existsSync(dest)) - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ok(err && err.code === 'EEXIST', 'throw EEXIST') done() }) }) - it('should not overwrite if overwrite = false', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-folder/another-file' + it('should not overwrite if overwrite = false', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-folder/another-file` // verify file exists already assert(fs.existsSync(dest)) - fse.move(src, dest, {overwrite: false}, function (err) { + fse.move(src, dest, {overwrite: false}, err => { assert.ok(err && err.code === 'EEXIST', 'throw EEXIST') done() }) }) - it('should overwrite file if overwrite = true', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-folder/another-file' + it('should overwrite file if overwrite = true', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-folder/another-file` // verify file exists already assert(fs.existsSync(dest)) - fse.move(src, dest, {overwrite: true}, function (err) { + fse.move(src, dest, {overwrite: true}, err => { assert.ifError(err) - fs.readFile(dest, 'utf8', function (err, contents) { - var expected = /^sonic the hedgehog\r?\n$/ + fs.readFile(dest, 'utf8', (err, contents) => { + const expected = /^sonic the hedgehog\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) done() @@ -121,23 +121,23 @@ describe('move', function () { this.timeout(90000) // Create src - var src = path.join(TEST_DIR, 'src') + const src = path.join(TEST_DIR, 'src') fse.ensureDirSync(src) fse.mkdirsSync(path.join(src, 'some-folder')) fs.writeFileSync(path.join(src, 'some-file'), 'hi') - var dest = path.join(TEST_DIR, 'a-folder') + const dest = path.join(TEST_DIR, 'a-folder') // verify dest has stuff in it - var paths = fs.readdirSync(dest) + const paths = fs.readdirSync(dest) assert(paths.indexOf('another-file') >= 0) assert(paths.indexOf('another-folder') >= 0) - fse.move(src, dest, {overwrite: true}, function (err) { + fse.move(src, dest, {overwrite: true}, err => { assert.ifError(err) // verify dest does not have old stuff - var paths = fs.readdirSync(dest) + const paths = fs.readdirSync(dest) assert.strictEqual(paths.indexOf('another-file'), -1) assert.strictEqual(paths.indexOf('another-folder'), -1) @@ -149,30 +149,30 @@ describe('move', function () { }) }) - it('should not create directory structure if mkdirp is false', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/does/not/exist/a-file-dest' + it('should not create directory structure if mkdirp is false', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/does/not/exist/a-file-dest` // verify dest directory does not exist assert(!fs.existsSync(path.dirname(dest))) - fse.move(src, dest, {mkdirp: false}, function (err) { + fse.move(src, dest, {mkdirp: false}, err => { assert.strictEqual(err.code, 'ENOENT') done() }) }) - it('should create directory structure by default', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/does/not/exist/a-file-dest' + it('should create directory structure by default', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/does/not/exist/a-file-dest` // verify dest directory does not exist assert(!fs.existsSync(path.dirname(dest))) - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) - fs.readFile(dest, 'utf8', function (err, contents) { - var expected = /^sonic the hedgehog\r?\n$/ + fs.readFile(dest, 'utf8', (err, contents) => { + const expected = /^sonic the hedgehog\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) done() @@ -180,18 +180,18 @@ describe('move', function () { }) }) - it('should work across devices', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-file-dest' + it('should work across devices', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-file-dest` setUpMockFs('EXDEV') - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) assert.strictEqual(fs.link.callCount, 1) - fs.readFile(dest, 'utf8', function (err, contents) { - var expected = /^sonic the hedgehog\r?\n$/ + fs.readFile(dest, 'utf8', (err, contents) => { + const expected = /^sonic the hedgehog\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) @@ -201,17 +201,17 @@ describe('move', function () { }) }) - it('should move folders', function (done) { - var src = TEST_DIR + '/a-folder' - var dest = TEST_DIR + '/a-folder-dest' + it('should move folders', done => { + const src = `${TEST_DIR}/a-folder` + const dest = `${TEST_DIR}/a-folder-dest` // verify it doesn't exist assert(!fs.existsSync(dest)) - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) - fs.readFile(dest + '/another-file', 'utf8', function (err, contents) { - var expected = /^tails\r?\n$/ + fs.readFile(dest + '/another-file', 'utf8', (err, contents) => { + const expected = /^tails\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) done() @@ -219,18 +219,18 @@ describe('move', function () { }) }) - it('should move folders across devices with EISDIR error', function (done) { - var src = TEST_DIR + '/a-folder' - var dest = TEST_DIR + '/a-folder-dest' + it('should move folders across devices with EISDIR error', done => { + const src = `${TEST_DIR}/a-folder` + const dest = `${TEST_DIR}/a-folder-dest` setUpMockFs('EISDIR') - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) assert.strictEqual(fs.link.callCount, 1) - fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) { - var expected = /^knuckles\r?\n$/ + fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => { + const expected = /^knuckles\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) @@ -241,20 +241,20 @@ describe('move', function () { }) }) - it('should overwrite folders across devices', function (done) { - var src = TEST_DIR + '/a-folder' - var dest = TEST_DIR + '/a-folder-dest' + it('should overwrite folders across devices', done => { + const src = `${TEST_DIR}/a-folder` + const dest = `${TEST_DIR}/a-folder-dest` fs.mkdirSync(dest) setUpMockFs('EXDEV') - fse.move(src, dest, {overwrite: true}, function (err) { + fse.move(src, dest, {overwrite: true}, err => { assert.ifError(err) assert.strictEqual(fs.rename.callCount, 1) - fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) { - var expected = /^knuckles\r?\n$/ + fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => { + const expected = /^knuckles\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) @@ -265,18 +265,18 @@ describe('move', function () { }) }) - it('should move folders across devices with EXDEV error', function (done) { - var src = TEST_DIR + '/a-folder' - var dest = TEST_DIR + '/a-folder-dest' + it('should move folders across devices with EXDEV error', done => { + const src = `${TEST_DIR}/a-folder` + const dest = `${TEST_DIR}/a-folder-dest` setUpMockFs('EXDEV') - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) assert.strictEqual(fs.link.callCount, 1) - fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) { - var expected = /^knuckles\r?\n$/ + fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => { + const expected = /^knuckles\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) @@ -287,18 +287,18 @@ describe('move', function () { }) }) - describe('clobber', function () { - it('should be an alias for overwrite', function (done) { - var src = TEST_DIR + '/a-file' - var dest = TEST_DIR + '/a-folder/another-file' + describe('clobber', () => { + it('should be an alias for overwrite', done => { + const src = `${TEST_DIR}/a-file` + const dest = `${TEST_DIR}/a-folder/another-file` // verify file exists already assert(fs.existsSync(dest)) - fse.move(src, dest, {overwrite: true}, function (err) { + fse.move(src, dest, {overwrite: true}, err => { assert.ifError(err) - fs.readFile(dest, 'utf8', function (err, contents) { - var expected = /^sonic the hedgehog\r?\n$/ + fs.readFile(dest, 'utf8', (err, contents) => { + const expected = /^sonic the hedgehog\r?\n$/ assert.ifError(err) assert.ok(contents.match(expected), `${contents} match ${expected}`) done() @@ -307,16 +307,16 @@ describe('move', function () { }) }) - describe.skip('> when trying to a move a folder into itself', function () { - it('should produce an error', function (done) { - var SRC_DIR = path.join(TEST_DIR, 'test') - var DEST_DIR = path.join(TEST_DIR, 'test', 'test') + describe.skip('> when trying to a move a folder into itself', () => { + it('should produce an error', done => { + const SRC_DIR = path.join(TEST_DIR, 'test') + const DEST_DIR = path.join(TEST_DIR, 'test', 'test') assert(!fs.existsSync(SRC_DIR)) fs.mkdirSync(SRC_DIR) assert(fs.existsSync(SRC_DIR)) - fse.move(SRC_DIR, DEST_DIR, function (err) { + fse.move(SRC_DIR, DEST_DIR, err => { assert(fs.existsSync(SRC_DIR)) assert(err) done() @@ -327,9 +327,9 @@ describe('move', function () { // tested on Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP i686 i686 GNU/Linux // this won't trigger a bug on Mac OS X Yosimite with a USB drive (/Volumes) // see issue #108 - describe('> when actually trying to a move a folder across devices', function () { - var differentDevice = '/mnt' - var __skipTests = false + describe('> when actually trying to a move a folder across devices', () => { + const differentDevice = '/mnt' + let __skipTests = false // must set this up, if not, exit silently if (!fs.existsSync(differentDevice)) { @@ -345,12 +345,12 @@ describe('move', function () { __skipTests = true } - var _it = __skipTests ? it.skip : it + const _it = __skipTests ? it.skip : it - describe('> just the folder', function () { - _it('should move the folder', function (done) { - var src = '/mnt/some/weird/dir-really-weird' - var dest = path.join(TEST_DIR, 'device-weird') + describe('> just the folder', () => { + _it('should move the folder', done => { + const src = '/mnt/some/weird/dir-really-weird' + const dest = path.join(TEST_DIR, 'device-weird') if (!fs.existsSync(src)) { fse.mkdirpSync(src) @@ -360,7 +360,7 @@ describe('move', function () { assert(fs.lstatSync(src).isDirectory()) - fse.move(src, dest, function (err) { + fse.move(src, dest, err => { assert.ifError(err) assert(fs.existsSync(dest)) // console.log(path.normalize(dest)) diff --git a/lib/move/index.js b/lib/move/index.js index 56cf67d6..fb1d5b76 100644 --- a/lib/move/index.js +++ b/lib/move/index.js @@ -1,125 +1,127 @@ +'use strict' + // most of this code was written by Andrew Kelley // licensed under the BSD license: see // https://github.com/andrewrk/node-mv/blob/master/package.json // this needs a cleanup -var fs = require('graceful-fs') -var ncp = require('../copy/ncp') -var path = require('path') -var remove = require('../remove').remove -var mkdirp = require('../mkdirs').mkdirs +const fs = require('graceful-fs') +const ncp = require('../copy/ncp') +const path = require('path') +const remove = require('../remove').remove +const mkdirp = require('../mkdirs').mkdirs var utimes = require('../util/utimes') -function mv (source, dest, options, callback) { +function move (source, dest, options, callback) { if (typeof options === 'function') { - callback = options - options = {} + callback = options + options = {} } - var shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true - var overwrite = options.overwrite || options.clobber || false + const shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true + const overwrite = options.overwrite || options.clobber || false var options_copy = { preserveTimestamps: ('preserveTimestamps' in options) ? options.preserveTimestamps : true }; if (shouldMkdirp) { - mkdirs() + mkdirs() } else { - doRename() + doRename() } function mkdirs () { - mkdirp(path.dirname(dest), function (err) { - if (err) return callback(err) - doRename() - }) + mkdirp(path.dirname(dest), err => { + if (err) return callback(err) + doRename() + }) } function doRename () { - if (overwrite) { - fs.rename(source, dest, options_copy, function (err) { - if (!err) return callback() - - if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') { - remove(dest, function (err) { - if (err) return callback(err) - options.overwrite = false // just overwriteed it, no need to do it again - mv(source, dest, options, callback) - }) - return - } - - // weird Windows shit - if (err.code === 'EPERM') { - setTimeout(function () { - remove(dest, function (err) { - if (err) return callback(err) - options.overwrite = false - mv(source, dest, options, callback) - }) - }, 200) - return - } - - if (err.code !== 'EXDEV') return callback(err) - moveAcrossDevice(source, dest, overwrite, options_copy, callback) - }) - } else { - fs.link(source, dest, function (err) { - if (err) { - if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM') { - moveAcrossDevice(source, dest, overwrite, options_copy, callback) - return - } - callback(err) - return - } - fs.unlink(source, callback) - }) - } + if (overwrite) { + fs.rename(source, dest, options_copy, err => { + if (!err) return callback() + + if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') { + remove(dest, err => { + if (err) return callback(err) + options.overwrite = false // just overwriteed it, no need to do it again + move(source, dest, options, callback) + }) + return + } + + // weird Windows shit + if (err.code === 'EPERM') { + setTimeout(() => { + remove(dest, err => { + if (err) return callback(err) + options.overwrite = false + move(source, dest, options, callback) + }) + }, 200) + return + } + + if (err.code !== 'EXDEV') return callback(err) + moveAcrossDevice(source, dest, overwrite, options_copy, callback) + }) + } else { + fs.link(source, dest, err => { + if (err) { + if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') { + moveAcrossDevice(source, dest, overwrite, options_copy, callback) + return + } + callback(err) + return + } + fs.unlink(source, callback) + }) + } } } function moveAcrossDevice (source, dest, overwrite, options_copy, callback) { - fs.stat(source, function (err, stat) { - if (err) { - callback(err) - return - } + fs.stat(source, (err, stat) => { + if (err) { + callback(err) + return + } let options_temp = Object.assign({}, options_copy, { stat: stat }); - if (stat.isDirectory()) { - moveDirAcrossDevice(source, dest, overwrite, options_temp, callback) - } else { - moveFileAcrossDevice(source, dest, overwrite, options_temp, callback) - } + if (stat.isDirectory()) { + moveDirAcrossDevice(source, dest, overwrite, options_temp, callback) + } else { + moveFileAcrossDevice(source, dest, overwrite, options_temp, callback) + } }) } function moveFileAcrossDevice (source, dest, overwrite, options_temp, callback) { - var outFlags = overwrite ? 'w' : 'wx' - var ins = fs.createReadStream(source) - var outs = fs.createWriteStream(dest, {flags: outFlags}) - - ins.on('error', function (err) { - ins.destroy() - outs.destroy() - outs.removeListener('close', onClose) - - // may want to create a directory but `out` line above - // creates an empty file for us: See #108 - // don't care about error here - fs.unlink(dest, function () { - // note: `err` here is from the input stream errror - if (err.code === 'EISDIR' || err.code === 'EPERM') { - moveDirAcrossDevice(source, dest, overwrite, options_temp, callback) - } else { + const flags = overwrite ? 'w' : 'wx' + const ins = fs.createReadStream(source) + const outs = fs.createWriteStream(dest, { flags }) + + ins.on('error', err => { + ins.destroy() + outs.destroy() + outs.removeListener('close', onClose) + + // may want to create a directory but `out` line above + // creates an empty file for us: See #108 + // don't care about error here + fs.unlink(dest, () => { + // note: `err` here is from the input stream errror + if (err.code === 'EISDIR' || err.code === 'EPERM') { + moveDirAcrossDevice(source, dest, overwrite, callback) + } else { //callback(err) if (options_temp.preserveTimestamps) { @@ -127,51 +129,51 @@ function moveFileAcrossDevice (source, dest, overwrite, options_temp, callback) return callback(err) }) } else { - callback(err) + callback(err) } - } - }) + } + }) }) - outs.on('error', function (err) { - ins.destroy() - outs.destroy() - outs.removeListener('close', onClose) - callback(err) + outs.on('error', err => { + ins.destroy() + outs.destroy() + outs.removeListener('close', onClose) + callback(err) }) outs.once('close', onClose) ins.pipe(outs) function onClose () { - fs.unlink(source, callback) + fs.unlink(source, callback) } } function moveDirAcrossDevice (source, dest, overwrite, options_temp, callback) { - var options = { + const options = { preserveTimestamps: options_temp.preserveTimestamps, overwrite: false } - function startNcp () { - ncp(source, dest, options, function (err) { - if (err) return callback(err) - remove(source, callback) - }) - } - if (overwrite) { - remove(dest, function (err) { - if (err) return callback(err) - startNcp() - }) + remove(dest, err => { + if (err) return callback(err) + startNcp() + }) } else { - startNcp() + startNcp() + } + + function startNcp () { + ncp(source, dest, options, err => { + if (err) return callback(err) + remove(source, callback) + }) } } module.exports = { - move: mv + move } diff --git a/lib/output/__tests__/output.test.js b/lib/output/__tests__/output.test.js index e1a857bb..59ff05fe 100644 --- a/lib/output/__tests__/output.test.js +++ b/lib/output/__tests__/output.test.js @@ -1,29 +1,29 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global afterEach, beforeEach, describe, it */ -describe('output', function () { - var TEST_DIR +describe('output', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'output') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ outputFile', function () { - describe('> when the file and directory does not exist', function () { - it('should create the file', function (done) { - var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt') + describe('+ outputFile', () => { + describe('> when the file and directory does not exist', () => { + it('should create the file', done => { + const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt') assert(!fs.existsSync(file)) - fse.outputFile(file, 'hi jp', function (err) { + fse.outputFile(file, 'hi jp', err => { assert.ifError(err) assert(fs.existsSync(file)) assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp') @@ -32,12 +32,12 @@ describe('output', function () { }) }) - describe('> when the file does exist', function () { - it('should still modify the file', function (done) { - var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt') + describe('> when the file does exist', () => { + it('should still modify the file', done => { + const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt') fse.mkdirsSync(path.dirname(file)) fs.writeFileSync(file, 'hello world') - fse.outputFile(file, 'hello jp', function (err) { + fse.outputFile(file, 'hello jp', err => { if (err) return done(err) assert.equal(fs.readFileSync(file, 'utf8'), 'hello jp') done() @@ -46,10 +46,10 @@ describe('output', function () { }) }) - describe('+ outputFileSync', function () { - describe('> when the file and directory does not exist', function () { - it('should create the file', function () { - var file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt') + describe('+ outputFileSync', () => { + describe('> when the file and directory does not exist', () => { + it('should create the file', () => { + const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt') assert(!fs.existsSync(file)) fse.outputFileSync(file, 'hello man') assert(fs.existsSync(file)) @@ -57,9 +57,9 @@ describe('output', function () { }) }) - describe('> when the file does exist', function () { - it('should still modify the file', function () { - var file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt') + describe('> when the file does exist', () => { + it('should still modify the file', () => { + const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt') fse.mkdirsSync(path.dirname(file)) fs.writeFileSync(file, 'hello world') fse.outputFileSync(file, 'hello man') diff --git a/lib/output/index.js b/lib/output/index.js index e8f45f3f..f4eb9bb6 100644 --- a/lib/output/index.js +++ b/lib/output/index.js @@ -1,6 +1,8 @@ -var path = require('path') -var fs = require('graceful-fs') -var mkdir = require('../mkdirs') +'use strict' + +const fs = require('graceful-fs') +const path = require('path') +const mkdir = require('../mkdirs') function outputFile (file, data, encoding, callback) { if (typeof encoding === 'function') { @@ -8,11 +10,11 @@ function outputFile (file, data, encoding, callback) { encoding = 'utf8' } - var dir = path.dirname(file) - fs.exists(dir, function (itDoes) { + const dir = path.dirname(file) + fs.exists(dir, itDoes => { if (itDoes) return fs.writeFile(file, data, encoding, callback) - mkdir.mkdirs(dir, function (err) { + mkdir.mkdirs(dir, err => { if (err) return callback(err) fs.writeFile(file, data, encoding, callback) @@ -21,7 +23,7 @@ function outputFile (file, data, encoding, callback) { } function outputFileSync (file, data, encoding) { - var dir = path.dirname(file) + const dir = path.dirname(file) if (fs.existsSync(dir)) { return fs.writeFileSync.apply(fs, arguments) } @@ -30,6 +32,6 @@ function outputFileSync (file, data, encoding) { } module.exports = { - outputFile: outputFile, - outputFileSync: outputFileSync + outputFile, + outputFileSync } diff --git a/lib/remove/__tests__/remove-dir.test.js b/lib/remove/__tests__/remove-dir.test.js index 0b0f5be6..6edec0cd 100644 --- a/lib/remove/__tests__/remove-dir.test.js +++ b/lib/remove/__tests__/remove-dir.test.js @@ -1,24 +1,26 @@ -var assert = require('assert') -var fs = require('fs') -var path = require('path') -var os = require('os') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global beforeEach, describe, it */ -describe('remove / async / dir', function () { - var TEST_DIR +describe('remove / async / dir', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-async-dir') fse.emptyDir(TEST_DIR, done) }) - describe('> when dir does not exist', function () { - it('should not throw an error', function (done) { - var someDir = path.join(TEST_DIR, 'some-dir/') + describe('> when dir does not exist', () => { + it('should not throw an error', done => { + const someDir = path.join(TEST_DIR, 'some-dir/') assert.equal(fs.existsSync(someDir), false) - fse.remove(someDir, function (err) { + fse.remove(someDir, err => { assert.ifError(err) done() }) diff --git a/lib/remove/__tests__/remove-sync-dir.test.js b/lib/remove/__tests__/remove-sync-dir.test.js index 7469039b..5372bd66 100644 --- a/lib/remove/__tests__/remove-sync-dir.test.js +++ b/lib/remove/__tests__/remove-sync-dir.test.js @@ -1,28 +1,30 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global beforeEach, describe, it */ -describe('remove/sync', function () { - var TEST_DIR +describe('remove/sync', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync') fse.emptyDir(TEST_DIR, done) }) - describe('+ removeSync()', function () { - it('should delete directories and files synchronously', function () { + describe('+ removeSync()', () => { + it('should delete directories and files synchronously', () => { assert(fs.existsSync(TEST_DIR)) fs.writeFileSync(path.join(TEST_DIR, 'somefile'), 'somedata') fse.removeSync(TEST_DIR) assert(!fs.existsSync(TEST_DIR)) }) - it('should delete an empty directory synchronously', function () { + it('should delete an empty directory synchronously', () => { assert(fs.existsSync(TEST_DIR)) fse.removeSync(TEST_DIR) assert(!fs.existsSync(TEST_DIR)) diff --git a/lib/remove/__tests__/remove-sync-file.test.js b/lib/remove/__tests__/remove-sync-file.test.js index 5a4bed72..68a22966 100644 --- a/lib/remove/__tests__/remove-sync-file.test.js +++ b/lib/remove/__tests__/remove-sync-file.test.js @@ -1,22 +1,24 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var fse = require(process.cwd()) +'use strict' + +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') /* global beforeEach, describe, it */ -describe('remove/sync', function () { - var TEST_DIR +describe('remove/sync', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync') fse.emptyDir(TEST_DIR, done) }) - describe('+ removeSync()', function () { - it('should delete a file synchronously', function () { - var file = path.join(TEST_DIR, 'file') + describe('+ removeSync()', () => { + it('should delete a file synchronously', () => { + const file = path.join(TEST_DIR, 'file') fs.writeFileSync(file, 'hello') assert(fs.existsSync(file)) fse.removeSync(file) diff --git a/lib/remove/__tests__/remove.test.js b/lib/remove/__tests__/remove.test.js index bf4d5deb..d7c2fbdf 100644 --- a/lib/remove/__tests__/remove.test.js +++ b/lib/remove/__tests__/remove.test.js @@ -1,77 +1,77 @@ -var assert = require('assert') -var fs = require('fs') -var os = require('os') -var path = require('path') -var sr = require('secure-random') -var fse = require(process.cwd()) +'use strict' + +const assert = require('assert') +const fs = require('fs') +const os = require('os') +const path = require('path') +const sr = require('secure-random') +const fse = require(process.cwd()) /* global afterEach, beforeEach, describe, it */ -var TEST_DIR +let TEST_DIR function buildFixtureDir () { - var buf = sr.randomBuffer(5) - var baseDir = path.join(TEST_DIR, 'TEST_fs-extra_remove-' + Date.now()) + const buf = sr.randomBuffer(5) + const baseDir = path.join(TEST_DIR, `TEST_fs-extra_remove-${Date.now()}`) fs.mkdirSync(baseDir) fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf) fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf) - var subDir = path.join(TEST_DIR, Math.random() + '') + const subDir = path.join(TEST_DIR, Math.random() + '') fs.mkdirSync(subDir) fs.writeFileSync(path.join(subDir, Math.random() + '')) return baseDir } -describe('remove', function () { - beforeEach(function (done) { +describe('remove', () => { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove') fse.emptyDir(TEST_DIR, done) }) - afterEach(function (done) { - fse.remove(TEST_DIR, done) - }) + afterEach(done => fse.remove(TEST_DIR, done)) - describe('+ remove()', function () { - it('should delete an empty directory', function (done) { + describe('+ remove()', () => { + it('should delete an empty directory', done => { assert(fs.existsSync(TEST_DIR)) - fse.remove(TEST_DIR, function (err) { + fse.remove(TEST_DIR, err => { assert.ifError(err) assert(!fs.existsSync(TEST_DIR)) done() }) }) - it('should delete a directory full of directories and files', function (done) { + it('should delete a directory full of directories and files', done => { buildFixtureDir() assert(fs.existsSync(TEST_DIR)) - fse.remove(TEST_DIR, function (err) { + fse.remove(TEST_DIR, err => { assert.ifError(err) assert(!fs.existsSync(TEST_DIR)) done() }) }) - it('should delete a file', function (done) { - var file = path.join(TEST_DIR, 'file') + it('should delete a file', done => { + const file = path.join(TEST_DIR, 'file') fs.writeFileSync(file, 'hello') assert(fs.existsSync(file)) - fse.remove(file, function (err) { + fse.remove(file, err => { assert.ifError(err) assert(!fs.existsSync(file)) done() }) }) - it('should delete without a callback', function (done) { - var file = path.join(TEST_DIR, 'file') + it('should delete without a callback', done => { + const file = path.join(TEST_DIR, 'file') fs.writeFileSync(file, 'hello') assert(fs.existsSync(file)) - var existsChecker = setInterval(function () { - fs.exists(file, function (itDoes) { + const existsChecker = setInterval(() => { + fs.exists(file, (itDoes) => { if (!itDoes) { clearInterval(existsChecker) done() @@ -82,22 +82,20 @@ describe('remove', function () { }) it('shouldn’t delete glob matches', function (done) { - var file = path.join(TEST_DIR, 'file?') + const file = path.join(TEST_DIR, 'file?') try { fs.writeFileSync(file, 'hello') } catch (ex) { - if (ex.code === 'ENOENT') { - return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.') - } + if (ex.code === 'ENOENT') return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.') throw ex } - var wrongFile = path.join(TEST_DIR, 'file1') + const wrongFile = path.join(TEST_DIR, 'file1') fs.writeFileSync(wrongFile, 'yo') assert(fs.existsSync(file)) assert(fs.existsSync(wrongFile)) - fse.remove(file, function (err) { + fse.remove(file, err => { assert.ifError(err) assert(!fs.existsSync(file)) assert(fs.existsSync(wrongFile)) @@ -105,15 +103,15 @@ describe('remove', function () { }) }) - it('shouldn’t delete glob matches when file doesn’t exist', function (done) { - var nonexistentFile = path.join(TEST_DIR, 'file?') + it('shouldn’t delete glob matches when file doesn’t exist', done => { + const nonexistentFile = path.join(TEST_DIR, 'file?') - var wrongFile = path.join(TEST_DIR, 'file1') + const wrongFile = path.join(TEST_DIR, 'file1') fs.writeFileSync(wrongFile, 'yo') assert(!fs.existsSync(nonexistentFile)) assert(fs.existsSync(wrongFile)) - fse.remove(nonexistentFile, function (err) { + fse.remove(nonexistentFile, err => { assert.ifError(err) assert(!fs.existsSync(nonexistentFile)) assert(fs.existsSync(wrongFile)) diff --git a/lib/remove/index.js b/lib/remove/index.js index 566b8664..0e3b5dd1 100644 --- a/lib/remove/index.js +++ b/lib/remove/index.js @@ -1,15 +1,17 @@ -var rimraf = require('./rimraf') +'use strict' + +const rimraf = require('./rimraf') function removeSync (dir) { return rimraf.sync(dir, {disableGlob: true}) } function remove (dir, callback) { - var options = {disableGlob: true} + const options = {disableGlob: true} return callback ? rimraf(dir, options, callback) : rimraf(dir, options, function () {}) } module.exports = { - remove: remove, - removeSync: removeSync + remove, + removeSync } diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js index da55a62e..42eefe52 100644 --- a/lib/remove/rimraf.js +++ b/lib/remove/rimraf.js @@ -1,14 +1,13 @@ -module.exports = rimraf -rimraf.sync = rimrafSync +'use strict' -var assert = require('assert') -var path = require('path') -var fs = require('graceful-fs') +const fs = require('graceful-fs') +const path = require('path') +const assert = require('assert') -var isWindows = (process.platform === 'win32') +const isWindows = (process.platform === 'win32') function defaults (options) { - var methods = [ + const methods = [ 'unlink', 'chmod', 'stat', @@ -16,7 +15,7 @@ function defaults (options) { 'rmdir', 'readdir' ] - methods.forEach(function (m) { + methods.forEach(m => { options[m] = options[m] || fs[m] m = m + 'Sync' options[m] = options[m] || fs[m] @@ -26,6 +25,8 @@ function defaults (options) { } function rimraf (p, options, cb) { + let busyTries = 0 + if (typeof options === 'function') { cb = options options = {} @@ -39,18 +40,14 @@ function rimraf (p, options, cb) { defaults(options) - var busyTries = 0 - rimraf_(p, options, function CB (er) { if (er) { if (isWindows && (er.code === 'EBUSY' || er.code === 'ENOTEMPTY' || er.code === 'EPERM') && busyTries < options.maxBusyTries) { busyTries++ - var time = busyTries * 100 + let time = busyTries * 100 // try again, with the same exact callback as this one. - return setTimeout(function () { - rimraf_(p, options, CB) - }, time) + return setTimeout(() => rimraf_(p, options, CB), time) } // already gone @@ -79,7 +76,7 @@ function rimraf_ (p, options, cb) { // sunos lets the root user unlink directories, which is... weird. // so we have to lstat here and make sure it's not a dir. - options.lstat(p, function (er, st) { + options.lstat(p, (er, st) => { if (er && er.code === 'ENOENT') { return cb(null) } @@ -93,7 +90,7 @@ function rimraf_ (p, options, cb) { return rmdir(p, options, er, cb) } - options.unlink(p, function (er) { + options.unlink(p, er => { if (er) { if (er.code === 'ENOENT') { return cb(null) @@ -120,11 +117,11 @@ function fixWinEPERM (p, options, er, cb) { assert(er instanceof Error) } - options.chmod(p, 666, function (er2) { + options.chmod(p, 666, er2 => { if (er2) { cb(er2.code === 'ENOENT' ? null : er) } else { - options.stat(p, function (er3, stats) { + options.stat(p, (er3, stats) => { if (er3) { cb(er3.code === 'ENOENT' ? null : er) } else if (stats.isDirectory()) { @@ -138,6 +135,8 @@ function fixWinEPERM (p, options, er, cb) { } function fixWinEPERMSync (p, options, er) { + let stats + assert(p) assert(options) if (er) { @@ -155,7 +154,7 @@ function fixWinEPERMSync (p, options, er) { } try { - var stats = options.statSync(p) + stats = options.statSync(p) } catch (er3) { if (er3.code === 'ENOENT') { return @@ -182,7 +181,7 @@ function rmdir (p, options, originalEr, cb) { // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS) // if we guessed wrong, and it's not a directory, then // raise the original error. - options.rmdir(p, function (er) { + options.rmdir(p, er => { if (er && (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM')) { rmkids(p, options, cb) } else if (er && er.code === 'ENOTDIR') { @@ -198,23 +197,20 @@ function rmkids (p, options, cb) { assert(options) assert(typeof cb === 'function') - options.readdir(p, function (er, files) { - if (er) { - return cb(er) - } - var n = files.length - if (n === 0) { - return options.rmdir(p, cb) - } - var errState - files.forEach(function (f) { - rimraf(path.join(p, f), options, function (er) { + options.readdir(p, (er, files) => { + if (er) return cb(er) + + let n = files.length + let errState + + if (n === 0) return options.rmdir(p, cb) + + files.forEach(f => { + rimraf(path.join(p, f), options, er => { if (errState) { return } - if (er) { - return cb(errState = er) - } + if (er) return cb(errState = er) if (--n === 0) { options.rmdir(p, cb) } @@ -227,6 +223,8 @@ function rmkids (p, options, cb) { // tie up the JavaScript thread and fail on excessively // deep directory trees. function rimrafSync (p, options) { + let st + options = options || {} defaults(options) @@ -236,7 +234,7 @@ function rimrafSync (p, options) { assert.equal(typeof options, 'object', 'rimraf: options should be object') try { - var st = options.lstatSync(p) + st = options.lstatSync(p) } catch (er) { if (er.code === 'ENOENT') { return @@ -258,11 +256,9 @@ function rimrafSync (p, options) { } catch (er) { if (er.code === 'ENOENT') { return - } - if (er.code === 'EPERM') { + } else if (er.code === 'EPERM') { return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er) - } - if (er.code !== 'EISDIR') { + } else if (er.code !== 'EISDIR') { throw er } rmdirSync(p, options, er) @@ -281,11 +277,9 @@ function rmdirSync (p, options, originalEr) { } catch (er) { if (er.code === 'ENOENT') { return - } - if (er.code === 'ENOTDIR') { + } else if (er.code === 'ENOTDIR') { throw originalEr - } - if (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM') { + } else if (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM') { rmkidsSync(p, options) } } @@ -294,8 +288,9 @@ function rmdirSync (p, options, originalEr) { function rmkidsSync (p, options) { assert(p) assert(options) - options.readdirSync(p).forEach(function (f) { - rimrafSync(path.join(p, f), options) - }) + options.readdirSync(p).forEach(f => rimrafSync(path.join(p, f), options)) options.rmdirSync(p, options) } + +module.exports = rimraf +rimraf.sync = rimrafSync diff --git a/lib/util/__tests__/utimes.test.js b/lib/util/__tests__/utimes.test.js index 648f4246..23349eec 100644 --- a/lib/util/__tests__/utimes.test.js +++ b/lib/util/__tests__/utimes.test.js @@ -1,22 +1,20 @@ -var assert = require('assert') -var path = require('path') -var os = require('os') -var fs = require('fs') -var fse = require(process.cwd()) -var semver = require('semver') -var proxyquire = require('proxyquire') -var gracefulFsStub -var utimes +'use strict' -/* global beforeEach, describe, it */ +const fs = require('fs') +const os = require('os') +const fse = require(process.cwd()) +const path = require('path') +const assert = require('assert') +const proxyquire = require('proxyquire') +let gracefulFsStub +let utimes -describe('utimes', function () { - var TEST_DIR +/* global beforeEach, describe, it */ - // ignore Node.js v0.10.x - if (semver.lt(process.version, '0.11.0')) return +describe('utimes', () => { + let TEST_DIR - beforeEach(function (done) { + beforeEach(done => { TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'utimes') fse.emptyDir(TEST_DIR, done) // reset stubs @@ -24,54 +22,48 @@ describe('utimes', function () { utimes = proxyquire('../utimes', {'graceful-fs': gracefulFsStub}) }) - describe('hasMillisResSync()', function () { - it('should return a boolean indicating whether it has support', function () { - var res = utimes.hasMillisResSync() + describe('hasMillisResSync()', () => { + it('should return a boolean indicating whether it has support', () => { + const res = utimes.hasMillisResSync() assert.equal(typeof res, 'boolean') // HFS => false - if (process.platform === 'darwin') { - assert.equal(res, false) - } + if (process.platform === 'darwin') assert.equal(res, false) // does anyone use FAT anymore? - /* if (process.platform === 'win32') { - assert.equal(res, true) - } */ + // if (process.platform === 'win32') assert.equal(res, true) // fails on appveyor... could appveyor be using FAT? // this would fail if ext2/ext3 - if (process.platform === 'linux') { - assert.equal(res, true) - } + if (process.platform === 'linux') assert.equal(res, true) }) }) - describe('timeRemoveMills()', function () { - it('should remove millisecond precision from a timestamp', function () { - var ts = 1334990868773 - var ets = 1334990868000 + describe('timeRemoveMills()', () => { + it('should remove millisecond precision from a timestamp', () => { + const ts = 1334990868773 + const ets = 1334990868000 assert.strictEqual(utimes.timeRemoveMillis(ts), ets) assert.strictEqual(utimes.timeRemoveMillis(new Date(ts)).getTime(), ets) }) }) - describe('utimesMillis()', function () { + describe('utimesMillis()', () => { // see discussion https://github.com/jprichardson/node-fs-extra/pull/141 - it('should set the utimes w/ millisecond precision', function (done) { - var tmpFile = path.join(TEST_DIR, 'someFile') + it('should set the utimes w/ millisecond precision', done => { + const tmpFile = path.join(TEST_DIR, 'someFile') fs.writeFileSync(tmpFile, 'hello') - var stats = fs.lstatSync(tmpFile) + let stats = fs.lstatSync(tmpFile) // Apr 21st, 2012 - var awhileAgo = new Date(1334990868773) - var awhileAgoNoMillis = new Date(1334990868000) + const awhileAgo = new Date(1334990868773) + const awhileAgoNoMillis = new Date(1334990868000) assert.notDeepEqual(stats.mtime, awhileAgo) assert.notDeepEqual(stats.atime, awhileAgo) - utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, function (err) { + utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, err => { assert.ifError(err) stats = fs.statSync(tmpFile) if (utimes.hasMillisResSync()) { @@ -85,34 +77,30 @@ describe('utimes', function () { }) }) - it('should close open file desciptors after encountering an error', function (done) { - var fakeFd = Math.random() + it('should close open file desciptors after encountering an error', done => { + const fakeFd = Math.random() - gracefulFsStub.open = function (pathIgnored, flagsIgnored, modeIgnored, callback) { - if (typeof modeIgnored === 'function') { - callback = modeIgnored - } - process.nextTick(function () { - callback(null, fakeFd) - }) + gracefulFsStub.open = (pathIgnored, flagsIgnored, modeIgnored, callback) => { + if (typeof modeIgnored === 'function') callback = modeIgnored + process.nextTick(() => callback(null, fakeFd)) } - var closeCalled = false - gracefulFsStub.close = function (fd, callback) { + let closeCalled = false + gracefulFsStub.close = (fd, callback) => { assert.equal(fd, fakeFd) closeCalled = true if (callback) process.nextTick(callback) } - var testError - gracefulFsStub.futimes = function (fd, atimeIgnored, mtimeIgnored, callback) { - process.nextTick(function () { + let testError + gracefulFsStub.futimes = (fd, atimeIgnored, mtimeIgnored, callback) => { + process.nextTick(() => { testError = new Error('A test error') callback(testError) }) } - utimes.utimesMillis('ignored', 'ignored', 'ignored', function (err) { + utimes.utimesMillis('ignored', 'ignored', 'ignored', err => { assert.equal(err, testError) assert(closeCalled) done() diff --git a/lib/util/assign.js b/lib/util/assign.js index 8e41f9a0..317e5ec4 100644 --- a/lib/util/assign.js +++ b/lib/util/assign.js @@ -1,9 +1,11 @@ +'use strict' + // simple mutable assign function assign () { - var args = [].slice.call(arguments).filter(function (i) { return i }) - var dest = args.shift() - args.forEach(function (src) { - Object.keys(src).forEach(function (key) { + const args = [].slice.call(arguments).filter(i => i) + const dest = args.shift() + args.forEach(src => { + Object.keys(src).forEach(key => { dest[key] = src[key] }) }) diff --git a/lib/util/utimes.js b/lib/util/utimes.js index 118f8a7d..4c320993 100644 --- a/lib/util/utimes.js +++ b/lib/util/utimes.js @@ -1,36 +1,38 @@ -var fs = require('graceful-fs') -var path = require('path') -var os = require('os') +'use strict' + +const fs = require('graceful-fs') +const os = require('os') +const path = require('path') // HFS, ext{2,3}, FAT do not, Node.js v0.10 does not function hasMillisResSync () { - var tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2)) + let tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2)) tmpfile = path.join(os.tmpdir(), tmpfile) // 550 millis past UNIX epoch - var d = new Date(1435410243862) + const d = new Date(1435410243862) fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141') - var fd = fs.openSync(tmpfile, 'r+') + const fd = fs.openSync(tmpfile, 'r+') fs.futimesSync(fd, d, d) fs.closeSync(fd) return fs.statSync(tmpfile).mtime > 1435410243000 } function hasMillisRes (callback) { - var tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2)) + let tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2)) tmpfile = path.join(os.tmpdir(), tmpfile) // 550 millis past UNIX epoch - var d = new Date(1435410243862) - fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', function (err) { + const d = new Date(1435410243862) + fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => { if (err) return callback(err) - fs.open(tmpfile, 'r+', function (err, fd) { + fs.open(tmpfile, 'r+', (err, fd) => { if (err) return callback(err) - fs.futimes(fd, d, d, function (err) { + fs.futimes(fd, d, d, err => { if (err) return callback(err) - fs.close(fd, function (err) { + fs.close(fd, err => { if (err) return callback(err) - fs.stat(tmpfile, function (err, stats) { + fs.stat(tmpfile, (err, stats) => { if (err) return callback(err) callback(null, stats.mtime > 1435410243000) }) @@ -52,10 +54,10 @@ function timeRemoveMillis (timestamp) { function utimesMillis (path, atime, mtime, callback) { // if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback) - fs.open(path, 'r+', function (err, fd) { + fs.open(path, 'r+', (err, fd) => { if (err) return callback(err) - fs.futimes(fd, atime, mtime, function (futimesErr) { - fs.close(fd, function (closeErr) { + fs.futimes(fd, atime, mtime, futimesErr => { + fs.close(fd, closeErr => { if (callback) callback(futimesErr || closeErr) }) }) @@ -63,8 +65,8 @@ function utimesMillis (path, atime, mtime, callback) { } module.exports = { - hasMillisRes: hasMillisRes, - hasMillisResSync: hasMillisResSync, - timeRemoveMillis: timeRemoveMillis, - utimesMillis: utimesMillis + hasMillisRes, + hasMillisResSync, + timeRemoveMillis, + utimesMillis } diff --git a/package.json b/package.json index ffff37fa..6dae7baa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fs-extra", - "version": "1.0.0", + "version": "2.0.0", "description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.", "homepage": "https://github.com/jprichardson/node-fs-extra", "repository": { @@ -46,7 +46,6 @@ "read-dir-files": "^0.1.1", "rimraf": "^2.2.8", "secure-random": "^1.1.1", - "semver": "^5.3.0", "standard": "^8.5.0" }, "main": "./lib/index", diff --git a/test.js b/test.js index 1877731c..659ea683 100644 --- a/test.js +++ b/test.js @@ -1,30 +1,31 @@ -var os = require('os') -var path = require('path') -var Mocha = require('mocha') -var assign = require('./lib/util/assign') -var klaw = require('klaw') +'use strict' -var argv = require('minimist')(process.argv.slice(2)) +const os = require('os') +const path = require('path') +const klaw = require('klaw') +const Mocha = require('mocha') +const assign = require('./lib/util/assign') -var mochaOpts = assign({ +const argv = require('minimist')(process.argv.slice(2)) + +const mochaOpts = assign({ ui: 'bdd', reporter: 'dot', timeout: 30000 }, argv) -var mocha = new Mocha(mochaOpts) +const mocha = new Mocha(mochaOpts) +const testExt = '.test.js' klaw('./lib').on('readable', function () { - var item + let item while ((item = this.read())) { if (!item.stats.isFile()) return - if (item.path.lastIndexOf('.test.js') !== (item.path.length - '.test.js'.length)) return + if (item.path.lastIndexOf(testExt) !== (item.path.length - testExt.length)) return mocha.addFile(item.path) } -}).on('end', function () { - mocha.run(function (failures) { - require('./').remove(path.join(os.tmpdir(), 'fs-extra'), function () { - process.exit(failures) - }) +}).on('end', () => { + mocha.run(failures => { + require('./').remove(path.join(os.tmpdir(), 'fs-extra'), () => process.exit(failures)) }) })