Skip to content

Commit

Permalink
Merge pull request #40 from broccolijs/cleanup
Browse files Browse the repository at this point in the history
consistency
  • Loading branch information
stefanpenner authored Jul 27, 2018
2 parents 967e16b + d034215 commit be01247
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
61 changes: 30 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
'use strict';

var fs = require('fs')
var fs = require('fs');
var tmpdir = require('os').tmpdir();
var path = require('path')
var path = require('path');

var isWindows = process.platform === 'win32'
var isWindows = process.platform === 'win32';
// These can be overridden for testing
var defaultOptions = {
isWindows: isWindows,
canSymlink: testCanSymlink(),
fs: fs
}
};
var options = defaultOptions;

function testCanSymlink () {
// We can't use options here because this function gets called before
// its defined
if (isWindows === false) { return true; }

var canLinkSrc = path.join(tmpdir, "canLinkSrc.tmp")
var canLinkDest = path.join(tmpdir, "canLinkDest.tmp")
var canLinkSrc = path.join(tmpdir, "canLinkSrc.tmp");
var canLinkDest = path.join(tmpdir, "canLinkDest.tmp");

try {
fs.writeFileSync(canLinkSrc, '');
} catch (e) {
return false
return false;
}

try {
fs.symlinkSync(canLinkSrc, canLinkDest)
fs.symlinkSync(canLinkSrc, canLinkDest);
} catch (e) {
fs.unlinkSync(canLinkSrc)
fs.unlinkSync(canLinkSrc);
return false
}

fs.unlinkSync(canLinkSrc)
fs.unlinkSync(canLinkDest)
fs.unlinkSync(canLinkSrc);
fs.unlinkSync(canLinkDest);

// Test symlinking a directory. For some reason, sometimes Windows allows
// symlinking a file but not symlinking a directory...
try {
fs.mkdirSync(canLinkSrc);
} catch (e) {
return false
return false;
}

try {
fs.symlinkSync(canLinkSrc, canLinkDest, 'dir')
fs.symlinkSync(canLinkSrc, canLinkDest, 'dir');
} catch (e) {
fs.rmdirSync(canLinkSrc)
return false
fs.rmdirSync(canLinkSrc);
return false;
}

fs.rmdirSync(canLinkSrc)
fs.rmdirSync(canLinkDest)
fs.rmdirSync(canLinkSrc);
fs.rmdirSync(canLinkDest);

return true
return true;
}

module.exports = symlinkOrCopy;
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
module.exports.setOptions = setOptions;
function setOptions(newOptions) {

options = newOptions || defaultOptions;
}

function cleanup(path) {
if (typeof path !== 'string' ) { return }
if (typeof path !== 'string' ) { return; }
// WSL (Windows Subsystem Linux) has issues with:
// * https://github.com/ember-cli/ember-cli/issues/6338
// * trailing `/` on symlinked directories
Expand All @@ -82,9 +81,9 @@ function cleanup(path) {
module.exports.sync = symlinkOrCopySync
function symlinkOrCopySync (srcPath, destPath) {
if (options.isWindows) {
symlinkWindows(srcPath, destPath)
symlinkWindows(srcPath, destPath);
} else {
symlink(srcPath, destPath)
symlink(srcPath, destPath);
}
}

Expand All @@ -98,15 +97,15 @@ function symlink(_srcPath, _destPath) {
var srcPath = cleanup(_srcPath);
var destPath = cleanup(_destPath);

var lstat = options.fs.lstatSync(srcPath)
var lstat = options.fs.lstatSync(srcPath);
if (lstat.isSymbolicLink()) {
// When we encounter symlinks, follow them. This prevents indirection
// from growing out of control.
// Note: At the moment `realpathSync` on Node is 70x slower than native,
// because it doesn't use the standard library's `realpath`:
// https://github.com/joyent/node/issues/7902
// Can someone please send a patch to Node? :)
srcPath = options.fs.realpathSync(srcPath)
srcPath = options.fs.realpathSync(srcPath);
} else if (srcPath[0] !== '/') {
// Resolve relative paths.
// Note: On Mac and Linux (unlike Windows), process.cwd() never contains
Expand All @@ -115,7 +114,7 @@ function symlink(_srcPath, _destPath) {
// path instead of the slower path.resolve(). (It seems unnecessary in
// principle that path.resolve() is slower. Does anybody want to send a
// patch to Node?)
srcPath = process.cwd() + '/' + srcPath
srcPath = process.cwd() + '/' + srcPath;
}
options.fs.symlinkSync(srcPath, destPath);
}
Expand All @@ -125,8 +124,8 @@ function symlink(_srcPath, _destPath) {
var WINDOWS_PREFIX = "\\\\?\\";

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;

if (stat.isSymbolicLink()) {
Expand All @@ -144,8 +143,8 @@ function symlinkWindows(srcPath, destPath) {
if (isDir) {
options.fs.symlinkSync(srcPath, destPath, 'junction');
} 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);
}
}
}
57 changes: 29 additions & 28 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('symlink-or-copy', function() {
}
}
},
realpathSync: function() {count++},
realpathSync: function() {count++;},
symlinkSync: function() {count++;}
}
});
Expand All @@ -59,56 +59,57 @@ describe('symlink-or-copy', function() {
try {
fs.rmdirSync(sourcePath);
} catch (error) {
if(error.code != 'ENOENT'){
if (error.code != 'ENOENT') {
throw error;
}
}

try {
fs.rmdirSync(destinationPath);
} catch (error) {}
fs.mkdirSync(sourcePath);

symLinkOrCopy.sync(sourcePath, destinationPath);
assert.ok(fs.existsSync(destinationPath), 'destination path should exist');
})
});

it('windows falls back to copy for file', function() {
var count = 0
var lstatSyncCount = 0
var isDirectoryCount = 0
var readFileSyncCount = 0
var writeFileSyncCount = 0
var utimesSyncCount = 0
var count = 0;
var lstatSyncCount = 0;
var isDirectoryCount = 0;
var readFileSyncCount = 0;
var writeFileSyncCount = 0;
var utimesSyncCount = 0;
symLinkOrCopy.setOptions({
isWindows: true,
copyDereferenceSync: function() {
count++
count++;
},
canSymLink: false,
fs: {
lstatSync: function() {
lstatSyncCount++
lstatSyncCount++;
return {
isSymbolicLink: function() {
return true
return true;
},
isDirectory: function() {
isDirectoryCount++
return false
isDirectoryCount++;
return false;
}
};
},
readFileSync: function() {
readFileSyncCount++
readFileSyncCount++;
return 'foo';
},
writeFileSync: function() {
writeFileSyncCount++
writeFileSyncCount++;
return 'foo';
},
realpathSync: function() {count++},
symlinkSync: function() {count++},
utimesSync: function() {utimesSyncCount++}
realpathSync: function() {count++;},
symlinkSync: function() {count++;},
utimesSync: function() {utimesSyncCount++;}
}
});

Expand Down Expand Up @@ -136,7 +137,7 @@ describe('symlink-or-copy', function() {
}
}
},
realpathSync: function() {count++},
realpathSync: function() {count++;},
symlinkSync: function() {count++;}
},
canSymlink: true,
Expand Down Expand Up @@ -176,13 +177,13 @@ describe('symlink-or-copy', function() {
}
},
realpathSync: function(srcPath) {
assert.equal(srcPath, 'foo')
count++
assert.equal(srcPath, 'foo');
count++;
return 'bar';
},
symlinkSync: function(srcPath) {
count++;
assert.equal(srcPath, 'bar')
assert.equal(srcPath, 'bar');
}
}
});
Expand All @@ -198,7 +199,7 @@ describe('symlink-or-copy', function() {
canSymlink: true,
fs: {
lstatSync: function(srcPath) {
assert.equal(srcPath, 'foo/bar/baz')
assert.equal(srcPath, 'foo/bar/baz');
return {
isSymbolicLink: function() {
count++;
Expand All @@ -210,13 +211,13 @@ describe('symlink-or-copy', function() {
}
},
realpathSync: function(srcPath) {
assert.equal(srcPath, 'foo/bar/baz')
count++
assert.equal(srcPath, 'foo/bar/baz');
count++;
return 'foo/bar/baz';
},
symlinkSync: function(srcPath) {
count++;
assert.equal(srcPath, 'foo/bar/baz')
assert.equal(srcPath, 'foo/bar/baz');
}
}
});
Expand Down Expand Up @@ -244,7 +245,7 @@ describe('symlink-or-copy', function() {
}
}
},
realpathSync: function() {count++},
realpathSync: function() {count++;},
symlinkSync: function() {count++;}
}
});
Expand Down

0 comments on commit be01247

Please sign in to comment.