From 1a2d2ea3ead0250607048842740ecee5201977a2 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 18:38:35 +0100 Subject: [PATCH 1/7] Refactor copy --- lib/copy/copy.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) 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) }) From 29e61ab6378e625660f343f930468a26b5c197a5 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:00:21 +0100 Subject: [PATCH 2/7] Refactor copy-file-sync --- lib/copy-sync/copy-file-sync.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) 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) From f258e5bfed27daee6244905ebe0a4ce82cf3259a Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:09:36 +0100 Subject: [PATCH 3/7] Refactor copy-sync --- lib/copy-sync/copy-sync.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/copy-sync/copy-sync.js b/lib/copy-sync/copy-sync.js index 29812f82..eadc80a2 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,14 +24,14 @@ 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') @@ -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) } } From 57f43e30306d1cf7619c569cadcfb07731fd2809 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:17:35 +0100 Subject: [PATCH 4/7] Refactor empty --- lib/empty/index.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) 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 } From 535436c80d0d2855d619cda2fa5e567ec3870179 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:21:37 +0100 Subject: [PATCH 5/7] Refactor ensureFile --- lib/ensure/file.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) 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 From 116424660f07a7d6b62e54d172954dea6fe64583 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:28:53 +0100 Subject: [PATCH 6/7] Refactor ensure/index --- lib/ensure/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 01c96feabe9d2c8755a474ed72f8a60caf8bc7f4 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Tue, 14 Feb 2017 19:37:08 +0100 Subject: [PATCH 7/7] Refactor ensureLink --- lib/ensure/link.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) 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