symlink-or-copy
Advanced tools
Comparing version 1.2.0 to 1.3.0
# master | ||
# 1.3.0 | ||
* modernize readme | ||
* add new nodes to CI | ||
* ensure symlink/junction/copy works on files/folders on windows regardless of if files or folders are symlinkable | ||
* doc/readme cleanup | ||
# 1.1.8 | ||
@@ -4,0 +11,0 @@ |
112
index.js
@@ -1,12 +0,15 @@ | ||
var fs = require('fs') | ||
'use strict'; | ||
var fs = require('fs'); | ||
var tmpdir = require('os').tmpdir(); | ||
var path = require('path') | ||
var path = require('path'); | ||
var isWindows = process.platform === 'win32' | ||
var canSymlink = testCanSymlink(); | ||
// These can be overridden for testing | ||
var defaultOptions = { | ||
isWindows: isWindows, | ||
canSymlink: testCanSymlink(), | ||
isWindows: process.platform === 'win32', | ||
canSymlinkFile: canSymlink.files, | ||
canSymlinkDirectory: canSymlink.directories, | ||
fs: fs | ||
} | ||
}; | ||
var options = defaultOptions; | ||
@@ -17,22 +20,32 @@ | ||
// its defined | ||
if (isWindows === false) { return true; } | ||
var canLinkSrc = path.join(tmpdir, "canLinkSrc.tmp") | ||
var canLinkDest = path.join(tmpdir, "canLinkDest.tmp") | ||
var result = { | ||
files: false, | ||
directories: false | ||
}; | ||
if (process.platform !== 'win32') { return result; } | ||
var canLinkSrc = path.join(tmpdir, "canLinkSrc.tmp"); | ||
var canLinkDest = path.join(tmpdir, "canLinkDest.tmp"); | ||
try { | ||
fs.writeFileSync(canLinkSrc, ''); | ||
} catch (e) { | ||
return false | ||
return result; | ||
} | ||
try { | ||
fs.symlinkSync(canLinkSrc, canLinkDest) | ||
fs.symlinkSync(canLinkSrc, canLinkDest); | ||
result.files = true; | ||
} catch (e) { | ||
fs.unlinkSync(canLinkSrc) | ||
return false | ||
result.files = false; | ||
} | ||
fs.unlinkSync(canLinkSrc) | ||
fs.unlinkSync(canLinkDest) | ||
fs.unlinkSync(canLinkSrc); | ||
try { | ||
fs.unlinkSync(canLinkDest); | ||
} catch (e) { | ||
// In case the link failed | ||
} | ||
@@ -44,16 +57,17 @@ // Test symlinking a directory. For some reason, sometimes Windows allows | ||
} catch (e) { | ||
return false | ||
result.directories = false; | ||
return result; | ||
} | ||
try { | ||
fs.symlinkSync(canLinkSrc, canLinkDest, 'dir') | ||
fs.symlinkSync(canLinkSrc, canLinkDest, 'dir'); | ||
fs.rmdirSync(canLinkSrc); | ||
fs.rmdirSync(canLinkDest); | ||
result.directories = true; | ||
} catch (e) { | ||
fs.rmdirSync(canLinkSrc) | ||
return false | ||
fs.rmdirSync(canLinkSrc); | ||
result.directories = false; | ||
} | ||
fs.rmdirSync(canLinkSrc) | ||
fs.rmdirSync(canLinkDest) | ||
return true | ||
return result; | ||
} | ||
@@ -63,8 +77,6 @@ | ||
function symlinkOrCopy () { | ||
throw new Error("This function does not exist. Use require('symlink-or-copy').sync") | ||
throw new Error("This function does not exist. Use require('symlink-or-copy').sync"); | ||
} | ||
module.exports.setOptions = setOptions | ||
function setOptions(newOptions) { | ||
module.exports._setOptions = function setOptions(newOptions) { | ||
options = newOptions || defaultOptions; | ||
@@ -74,3 +86,3 @@ } | ||
function cleanup(path) { | ||
if (typeof path !== 'string' ) { return } | ||
if (typeof path !== 'string' ) { return; } | ||
// WSL (Windows Subsystem Linux) has issues with: | ||
@@ -87,5 +99,5 @@ // * https://github.com/ember-cli/ember-cli/issues/6338 | ||
if (options.isWindows) { | ||
symlinkWindows(srcPath, destPath) | ||
symlinkWindows(srcPath, destPath); | ||
} else { | ||
symlink(srcPath, destPath) | ||
symlink(srcPath, destPath); | ||
} | ||
@@ -96,6 +108,18 @@ } | ||
get: function() { | ||
return !!options.canSymlink; | ||
return !!(options.canSymlinkFile && options.canSymlinkDirectory); | ||
} | ||
}); | ||
Object.defineProperty(module.exports, 'canSymlinkFile', { | ||
get: function() { | ||
return !!options.canSymlinkFile; | ||
} | ||
}); | ||
Object.defineProperty(module.exports, 'canSymlinkDirectory', { | ||
get: function() { | ||
return !!options.canSymlinkDirectory; | ||
} | ||
}); | ||
function symlink(_srcPath, _destPath) { | ||
@@ -105,3 +129,3 @@ var srcPath = cleanup(_srcPath); | ||
var lstat = options.fs.lstatSync(srcPath) | ||
var lstat = options.fs.lstatSync(srcPath); | ||
if (lstat.isSymbolicLink()) { | ||
@@ -114,3 +138,3 @@ // When we encounter symlinks, follow them. This prevents indirection | ||
// Can someone please send a patch to Node? :) | ||
srcPath = options.fs.realpathSync(srcPath) | ||
srcPath = options.fs.realpathSync(srcPath); | ||
} else if (srcPath[0] !== '/') { | ||
@@ -124,3 +148,3 @@ // Resolve relative paths. | ||
// patch to Node?) | ||
srcPath = process.cwd() + '/' + srcPath | ||
srcPath = process.cwd() + '/' + srcPath; | ||
} | ||
@@ -135,4 +159,4 @@ options.fs.symlinkSync(srcPath, destPath); | ||
function symlinkWindows(srcPath, destPath) { | ||
var stat = options.fs.lstatSync(srcPath) | ||
var isDir = stat.isDirectory() | ||
var stat = options.fs.lstatSync(srcPath); | ||
var isDir = stat.isDirectory(); | ||
var wasResolved = false; | ||
@@ -149,12 +173,16 @@ | ||
if (options.canSymlink) { | ||
options.fs.symlinkSync(srcPath, destPath, isDir ? 'dir' : 'file'); | ||
if (isDir) { | ||
if (options.canSymlinkDirectory) { | ||
options.fs.symlinkSync(srcPath, destPath, 'dir'); | ||
} else { | ||
options.fs.symlinkSync(srcPath, destPath, 'junction'); | ||
} | ||
} else { | ||
if (isDir) { | ||
options.fs.symlinkSync(srcPath, destPath, 'junction'); | ||
if (options.canSymlinkFile) { | ||
options.fs.symlinkSync(srcPath, destPath, 'file'); | ||
} else { | ||
options.fs.writeFileSync(destPath, options.fs.readFileSync(srcPath), { flag: 'wx', mode: stat.mode }) | ||
options.fs.utimesSync(destPath, stat.atime, stat.mtime) | ||
options.fs.writeFileSync(destPath, options.fs.readFileSync(srcPath), { flag: 'wx', mode: stat.mode }); | ||
options.fs.utimesSync(destPath, stat.atime, stat.mtime); | ||
} | ||
} | ||
} |
{ | ||
"name": "symlink-or-copy", | ||
"description": "Symlink files or directories, falling back to copying on Windows", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": "Jo Liss <joliss42@gmail.com>", | ||
@@ -20,5 +20,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "^2.2.4", | ||
"rimraf": "^2.6.2" | ||
"mocha": "^6.2.2", | ||
"rimraf": "^3.0.0" | ||
} | ||
} |
@@ -18,3 +18,3 @@ # node-symlink-or-copy | ||
```js | ||
var symlinkOrCopySync = require('symlink-or-copy').sync; | ||
const symlinkOrCopySync = require('symlink-or-copy').sync; | ||
@@ -28,3 +28,3 @@ symlinkOrCopySync('src_dir/some_file.txt', 'dest_dir/some_file.txt'); | ||
```js | ||
symlinkOrCopySync(srcPath, destPath) | ||
symlinkOrCopySync(srcPath, destPath); | ||
``` | ||
@@ -31,0 +31,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10338
151