Skip to content

Commit

Permalink
Refactor ensureLink
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Feb 14, 2017
1 parent 1164246 commit 01c96fe
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/ensure/link.js
Original file line number Diff line number Diff line change
@@ -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)
})
Expand All @@ -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 {
Expand All @@ -41,17 +43,17 @@ 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)

return fs.linkSync(srcpath, dstpath)
}

module.exports = {
createLink: createLink,
createLinkSync: createLinkSync,
createLink,
createLinkSync,
// alias
ensureLink: createLink,
ensureLinkSync: createLinkSync
Expand Down

0 comments on commit 01c96fe

Please sign in to comment.