Version 1
[yaffs-website] / node_modules / fs-extra / lib / ensure / symlink.js
diff --git a/node_modules/fs-extra/lib/ensure/symlink.js b/node_modules/fs-extra/lib/ensure/symlink.js
new file mode 100644 (file)
index 0000000..6244790
--- /dev/null
@@ -0,0 +1,62 @@
+var path = require('path')
+var fs = require('graceful-fs')
+var _mkdirs = require('../mkdirs')
+var mkdirs = _mkdirs.mkdirs
+var mkdirsSync = _mkdirs.mkdirsSync
+
+var _symlinkPaths = require('./symlink-paths')
+var symlinkPaths = _symlinkPaths.symlinkPaths
+var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+
+var _symlinkType = require('./symlink-type')
+var symlinkType = _symlinkType.symlinkType
+var 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) {
+    if (destinationExists) return callback(null)
+    symlinkPaths(srcpath, dstpath, function (err, relative) {
+      if (err) return callback(err)
+      srcpath = relative.toDst
+      symlinkType(relative.toCwd, type, function (err, type) {
+        if (err) return callback(err)
+        var dir = path.dirname(dstpath)
+        fs.exists(dir, function (dirExists) {
+          if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
+          mkdirs(dir, function (err) {
+            if (err) return callback(err)
+            fs.symlink(srcpath, dstpath, type, callback)
+          })
+        })
+      })
+    })
+  })
+}
+
+function createSymlinkSync (srcpath, dstpath, type, callback) {
+  callback = (typeof type === 'function') ? type : callback
+  type = (typeof type === 'function') ? false : type
+
+  var destinationExists = fs.existsSync(dstpath)
+  if (destinationExists) return undefined
+
+  var relative = symlinkPathsSync(srcpath, dstpath)
+  srcpath = relative.toDst
+  type = symlinkTypeSync(relative.toCwd, type)
+  var dir = path.dirname(dstpath)
+  var 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,
+  // alias
+  ensureSymlink: createSymlink,
+  ensureSymlinkSync: createSymlinkSync
+}