summaryrefslogtreecommitdiff
path: root/node_modules/fs-extra/lib/ensure
diff options
context:
space:
mode:
authorArman Shah <[email protected]>2018-02-19 23:50:04 -0800
committerArman Shah <[email protected]>2018-02-19 23:50:04 -0800
commitae34dcfd3823a609ba7182f2d6eda593be876f7d (patch)
treeb9d7f2884c4999349418cbdc4f9ab46d113e0afd /node_modules/fs-extra/lib/ensure
parentInitial commit (diff)
downloadlauncher-ae34dcfd3823a609ba7182f2d6eda593be876f7d.tar.xz
launcher-ae34dcfd3823a609ba7182f2d6eda593be876f7d.zip
add base files
Diffstat (limited to 'node_modules/fs-extra/lib/ensure')
-rw-r--r--node_modules/fs-extra/lib/ensure/file.js43
-rw-r--r--node_modules/fs-extra/lib/ensure/index.js21
-rw-r--r--node_modules/fs-extra/lib/ensure/link.js58
-rw-r--r--node_modules/fs-extra/lib/ensure/symlink-paths.js97
-rw-r--r--node_modules/fs-extra/lib/ensure/symlink-type.js27
-rw-r--r--node_modules/fs-extra/lib/ensure/symlink.js62
6 files changed, 308 insertions, 0 deletions
diff --git a/node_modules/fs-extra/lib/ensure/file.js b/node_modules/fs-extra/lib/ensure/file.js
new file mode 100644
index 0000000..1c9c2de
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/file.js
@@ -0,0 +1,43 @@
+var path = require('path')
+var fs = require('graceful-fs')
+var mkdir = require('../mkdirs')
+
+function createFile (file, callback) {
+ function makeFile () {
+ fs.writeFile(file, '', function (err) {
+ if (err) return callback(err)
+ callback()
+ })
+ }
+
+ fs.exists(file, function (fileExists) {
+ if (fileExists) return callback()
+ var dir = path.dirname(file)
+ fs.exists(dir, function (dirExists) {
+ if (dirExists) return makeFile()
+ mkdir.mkdirs(dir, function (err) {
+ if (err) return callback(err)
+ makeFile()
+ })
+ })
+ })
+}
+
+function createFileSync (file) {
+ if (fs.existsSync(file)) return
+
+ var dir = path.dirname(file)
+ if (!fs.existsSync(dir)) {
+ mkdir.mkdirsSync(dir)
+ }
+
+ fs.writeFileSync(file, '')
+}
+
+module.exports = {
+ createFile: createFile,
+ createFileSync: createFileSync,
+ // alias
+ ensureFile: createFile,
+ ensureFileSync: createFileSync
+}
diff --git a/node_modules/fs-extra/lib/ensure/index.js b/node_modules/fs-extra/lib/ensure/index.js
new file mode 100644
index 0000000..26e8705
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/index.js
@@ -0,0 +1,21 @@
+var file = require('./file')
+var link = require('./link')
+var symlink = require('./symlink')
+
+module.exports = {
+ // file
+ createFile: file.createFile,
+ createFileSync: file.createFileSync,
+ ensureFile: file.createFile,
+ ensureFileSync: file.createFileSync,
+ // link
+ createLink: link.createLink,
+ createLinkSync: link.createLinkSync,
+ ensureLink: link.createLink,
+ ensureLinkSync: link.createLinkSync,
+ // symlink
+ createSymlink: symlink.createSymlink,
+ createSymlinkSync: symlink.createSymlinkSync,
+ ensureSymlink: symlink.createSymlink,
+ ensureSymlinkSync: symlink.createSymlinkSync
+}
diff --git a/node_modules/fs-extra/lib/ensure/link.js b/node_modules/fs-extra/lib/ensure/link.js
new file mode 100644
index 0000000..4e4e283
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/link.js
@@ -0,0 +1,58 @@
+var path = require('path')
+var fs = require('graceful-fs')
+var mkdir = require('../mkdirs')
+
+function createLink (srcpath, dstpath, callback) {
+ function makeLink (srcpath, dstpath) {
+ fs.link(srcpath, dstpath, function (err) {
+ if (err) return callback(err)
+ callback(null)
+ })
+ }
+
+ fs.exists(dstpath, function (destinationExists) {
+ if (destinationExists) return callback(null)
+ fs.lstat(srcpath, function (err, stat) {
+ if (err) {
+ err.message = err.message.replace('lstat', 'ensureLink')
+ return callback(err)
+ }
+
+ var dir = path.dirname(dstpath)
+ fs.exists(dir, function (dirExists) {
+ if (dirExists) return makeLink(srcpath, dstpath)
+ mkdir.mkdirs(dir, function (err) {
+ if (err) return callback(err)
+ makeLink(srcpath, dstpath)
+ })
+ })
+ })
+ })
+}
+
+function createLinkSync (srcpath, dstpath, callback) {
+ var destinationExists = fs.existsSync(dstpath)
+ if (destinationExists) return undefined
+
+ try {
+ fs.lstatSync(srcpath)
+ } catch (err) {
+ err.message = err.message.replace('lstat', 'ensureLink')
+ throw err
+ }
+
+ var dir = path.dirname(dstpath)
+ var 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,
+ // alias
+ ensureLink: createLink,
+ ensureLinkSync: createLinkSync
+}
diff --git a/node_modules/fs-extra/lib/ensure/symlink-paths.js b/node_modules/fs-extra/lib/ensure/symlink-paths.js
new file mode 100644
index 0000000..cc27d04
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/symlink-paths.js
@@ -0,0 +1,97 @@
+var path = require('path')
+// path.isAbsolute shim for Node.js 0.10 support
+path.isAbsolute = (path.isAbsolute) ? path.isAbsolute : require('path-is-absolute')
+var fs = require('graceful-fs')
+
+/**
+ * Function that returns two types of paths, one relative to symlink, and one
+ * relative to the current working directory. Checks if path is absolute or
+ * relative. If the path is relative, this function checks if the path is
+ * relative to symlink or relative to current working directory. This is an
+ * initiative to find a smarter `srcpath` to supply when building symlinks.
+ * This allows you to determine which path to use out of one of three possible
+ * types of source paths. The first is an absolute path. This is detected by
+ * `path.isAbsolute()`. When an absolute path is provided, it is checked to
+ * see if it exists. If it does it's used, if not an error is returned
+ * (callback)/ thrown (sync). The other two options for `srcpath` are a
+ * relative url. By default Node's `fs.symlink` works by creating a symlink
+ * using `dstpath` and expects the `srcpath` to be relative to the newly
+ * created symlink. If you provide a `srcpath` that does not exist on the file
+ * system it results in a broken symlink. To minimize this, the function
+ * checks to see if the 'relative to symlink' source file exists, and if it
+ * does it will use it. If it does not, it checks if there's a file that
+ * exists that is relative to the current working directory, if does its used.
+ * This preserves the expectations of the original fs.symlink spec and adds
+ * the ability to pass in `relative to current working direcotry` paths.
+ */
+
+function symlinkPaths (srcpath, dstpath, callback) {
+ if (path.isAbsolute(srcpath)) {
+ return fs.lstat(srcpath, function (err, stat) {
+ if (err) {
+ err.message = err.message.replace('lstat', 'ensureSymlink')
+ return callback(err)
+ }
+ return callback(null, {
+ 'toCwd': srcpath,
+ 'toDst': srcpath
+ })
+ })
+ } else {
+ var dstdir = path.dirname(dstpath)
+ var relativeToDst = path.join(dstdir, srcpath)
+ return fs.exists(relativeToDst, function (exists) {
+ if (exists) {
+ return callback(null, {
+ 'toCwd': relativeToDst,
+ 'toDst': srcpath
+ })
+ } else {
+ return fs.lstat(srcpath, function (err, stat) {
+ if (err) {
+ err.message = err.message.replace('lstat', 'ensureSymlink')
+ return callback(err)
+ }
+ return callback(null, {
+ 'toCwd': srcpath,
+ 'toDst': path.relative(dstdir, srcpath)
+ })
+ })
+ }
+ })
+ }
+}
+
+function symlinkPathsSync (srcpath, dstpath) {
+ var exists
+ if (path.isAbsolute(srcpath)) {
+ exists = fs.existsSync(srcpath)
+ if (!exists) throw new Error('absolute srcpath does not exist')
+ return {
+ 'toCwd': srcpath,
+ 'toDst': srcpath
+ }
+ } else {
+ var dstdir = path.dirname(dstpath)
+ var relativeToDst = path.join(dstdir, srcpath)
+ exists = fs.existsSync(relativeToDst)
+ if (exists) {
+ return {
+ 'toCwd': relativeToDst,
+ 'toDst': srcpath
+ }
+ } else {
+ exists = fs.existsSync(srcpath)
+ if (!exists) throw new Error('relative srcpath does not exist')
+ return {
+ 'toCwd': srcpath,
+ 'toDst': path.relative(dstdir, srcpath)
+ }
+ }
+ }
+}
+
+module.exports = {
+ 'symlinkPaths': symlinkPaths,
+ 'symlinkPathsSync': symlinkPathsSync
+}
diff --git a/node_modules/fs-extra/lib/ensure/symlink-type.js b/node_modules/fs-extra/lib/ensure/symlink-type.js
new file mode 100644
index 0000000..81e3588
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/symlink-type.js
@@ -0,0 +1,27 @@
+var 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) {
+ if (err) return callback(null, 'file')
+ type = (stats && stats.isDirectory()) ? 'dir' : 'file'
+ callback(null, type)
+ })
+}
+
+function symlinkTypeSync (srcpath, type) {
+ if (type) return type
+ try {
+ var stats = fs.lstatSync(srcpath)
+ } catch (e) {
+ return 'file'
+ }
+ return (stats && stats.isDirectory()) ? 'dir' : 'file'
+}
+
+module.exports = {
+ symlinkType: symlinkType,
+ symlinkTypeSync: symlinkTypeSync
+}
diff --git a/node_modules/fs-extra/lib/ensure/symlink.js b/node_modules/fs-extra/lib/ensure/symlink.js
new file mode 100644
index 0000000..6244790
--- /dev/null
+++ b/node_modules/fs-extra/lib/ensure/symlink.js
@@ -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
+}