Skip to content

Commit

Permalink
Merge 7471352 into 082342e
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Feb 18, 2017
2 parents 082342e + 7471352 commit 8e87b49
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 135 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,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
Expand All @@ -67,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!")
});
Expand Down Expand Up @@ -128,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:
Expand Down
27 changes: 14 additions & 13 deletions lib/ensure/symlink-paths.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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 {
Expand All @@ -91,6 +92,6 @@ function symlinkPathsSync (srcpath, dstpath) {
}

module.exports = {
'symlinkPaths': symlinkPaths,
'symlinkPathsSync': symlinkPathsSync
symlinkPaths,
symlinkPathsSync
}
14 changes: 9 additions & 5 deletions lib/ensure/symlink-type.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
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)
})
}

function symlinkTypeSync (srcpath, type) {
let stats

if (type) return type
try {
var stats = fs.lstatSync(srcpath)
stats = fs.lstatSync(srcpath)
} catch (e) {
return 'file'
}
return (stats && stats.isDirectory()) ? 'dir' : 'file'
}

module.exports = {
symlinkType: symlinkType,
symlinkTypeSync: symlinkTypeSync
symlinkType,
symlinkTypeSync
}
48 changes: 25 additions & 23 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
@@ -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)
})
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -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
}
})
Expand Down
4 changes: 3 additions & 1 deletion lib/json/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
4 changes: 3 additions & 1 deletion lib/json/jsonfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var jsonFile = require('jsonfile')
'use strict'

const jsonFile = require('jsonfile')

module.exports = {
// jsonfile exports
Expand Down
12 changes: 7 additions & 5 deletions lib/json/output-json-sync.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 9 additions & 7 deletions lib/json/output-json.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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') {
callback = options
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)
})
Expand Down
18 changes: 10 additions & 8 deletions lib/mkdirs/mkdirs-sync.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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) {
Expand Down

0 comments on commit 8e87b49

Please sign in to comment.