Skip to content

Commit

Permalink
Update: Attach symlink property when passed through dest or `symlin…
Browse files Browse the repository at this point in the history
…k` (fixes #249)
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent a741486 commit 1665b38
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
8 changes: 3 additions & 5 deletions lib/dest/write-contents/write-symbolic-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var path = require('path');
var fo = require('../../file-operations');

function writeSymbolicLink(file, optResolver, onWritten) {
var srcPath = file.symlink;

var isDirectory = file.isDirectory();

// This option provides a way to create a Junction instead of a
Expand All @@ -26,9 +24,9 @@ function writeSymbolicLink(file, optResolver, onWritten) {
var symType = isDirectory ? symDirType : 'file';
var isRelative = optResolver.resolve('relative', file);

// This is done inside prepareWrite to use the adjusted file.base property
// This is done after prepare() to use the adjusted file.base property
if (isRelative && symType !== 'junction') {
srcPath = path.relative(file.base, srcPath);
file.symlink = path.relative(file.base, file.symlink);
}

var flag = optResolver.resolve('flag', file);
Expand All @@ -38,7 +36,7 @@ function writeSymbolicLink(file, optResolver, onWritten) {
type: symType,
};

fo.symlink(srcPath, file.path, opts, onWritten);
fo.symlink(file.symlink, file.path, opts, onWritten);
}

module.exports = writeSymbolicLink;
9 changes: 3 additions & 6 deletions lib/symlink/link-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ var fo = require('../file-operations');
function linkStream(optResolver) {

function linkFile(file, enc, callback) {
// Fetch the path as it was before prepare.dest()
var srcPath = file.history[file.history.length - 2];

var isDirectory = file.isDirectory();

// This option provides a way to create a Junction instead of a
Expand All @@ -31,9 +28,9 @@ function linkStream(optResolver) {
var symType = isDirectory ? symDirType : 'file';
var isRelative = optResolver.resolve('relative', file);

// This is done inside prepareWrite to use the adjusted file.base property
// This is done after prepare() to use the adjusted file.base property
if (isRelative && symType !== 'junction') {
srcPath = path.relative(file.base, srcPath);
file.symlink = path.relative(file.base, file.symlink);
}

var flag = optResolver.resolve('flag', file);
Expand All @@ -43,7 +40,7 @@ function linkStream(optResolver) {
type: symType,
};

fo.symlink(srcPath, file.path, opts, onSymlink);
fo.symlink(file.symlink, file.path, opts, onSymlink);

function onSymlink(symlinkErr) {
if (symlinkErr) {
Expand Down
1 change: 1 addition & 0 deletions lib/symlink/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function prepareSymlink(folderResolver, optResolver) {
file.stat.mode = mode;
file.cwd = cwd;
file.base = basePath;
file.symlink = file.path;
file.path = writePath;

cb(null, file);
Expand Down
12 changes: 12 additions & 0 deletions test/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputPath);
}

Expand Down Expand Up @@ -142,6 +143,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputPath);
}

Expand All @@ -167,6 +169,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputPath);
}

Expand All @@ -191,6 +194,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(path.normalize('../fixtures/test.txt'));
}

Expand All @@ -215,6 +219,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputPath);
}

Expand Down Expand Up @@ -249,6 +254,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputDirpath);
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -286,6 +292,7 @@ describe('symlink stream', function() {
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
// When creating a junction, it seems Windows appends a separator
expect(files[0].symlink + path.sep).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputDirpath + path.sep);
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -322,6 +329,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputDirpath);
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -364,6 +372,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputDirpath);
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -400,6 +409,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(path.normalize('../fixtures/foo'));
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -437,6 +447,7 @@ describe('symlink stream', function() {
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
// When creating a junction, it seems Windows appends a separator
expect(files[0].symlink + path.sep).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(inputDirpath + path.sep);
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down Expand Up @@ -503,6 +514,7 @@ describe('symlink stream', function() {
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputDirpath, 'path should have changed');
expect(files[0].symlink).toEqual(outputLink, 'symlink should be set');
expect(outputLink).toEqual(path.normalize('../fixtures/foo'));
expect(stats.isDirectory()).toEqual(true);
expect(lstats.isDirectory()).toEqual(false);
Expand Down

0 comments on commit 1665b38

Please sign in to comment.