Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2357391

Browse files
committed
New: Add isSymbolic method and symlink property (closes #79) (#98)
1 parent fa861d0 commit 2357391

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

index.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var Stream = require('stream');
1010
var replaceExt = require('replace-ext');
1111

1212
var builtInFields = [
13-
'_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd',
13+
'_contents', '_symlink', 'contents', 'stat', 'history', 'path', 'base', 'cwd',
1414
];
1515

1616
function File(file) {
@@ -35,6 +35,8 @@ function File(file) {
3535

3636
this._isVinyl = true;
3737

38+
this._symlink = null;
39+
3840
// Set custom properties
3941
Object.keys(file).forEach(function(key) {
4042
if (self.constructor.isCustomProp(key)) {
@@ -55,9 +57,28 @@ File.prototype.isNull = function() {
5557
return isNull(this.contents);
5658
};
5759

58-
// TODO: Should this be moved to vinyl-fs?
5960
File.prototype.isDirectory = function() {
60-
return this.isNull() && this.stat && this.stat.isDirectory();
61+
if (!this.isNull()) {
62+
return false;
63+
}
64+
65+
if (this.stat && typeof this.stat.isDirectory === 'function') {
66+
return this.stat.isDirectory();
67+
}
68+
69+
return false;
70+
};
71+
72+
File.prototype.isSymbolic = function() {
73+
if (!this.isNull()) {
74+
return false;
75+
}
76+
77+
if (this.stat && typeof this.stat.isSymbolicLink === 'function') {
78+
return this.stat.isSymbolicLink();
79+
}
80+
81+
return false;
6182
};
6283

6384
File.prototype.clone = function(opt) {
@@ -267,4 +288,18 @@ Object.defineProperty(File.prototype, 'path', {
267288
},
268289
});
269290

291+
Object.defineProperty(File.prototype, 'symlink', {
292+
get: function() {
293+
return this._symlink;
294+
},
295+
set: function(symlink) {
296+
// TODO: should this set the mode to symbolic if set?
297+
if (typeof symlink !== 'string') {
298+
throw new Error('symlink should be a string');
299+
}
300+
301+
this._symlink = symlink;
302+
},
303+
});
304+
270305
module.exports = File;

test/File.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,46 @@ describe('File', function() {
211211
file.isDirectory().should.equal(true);
212212
done();
213213
});
214+
215+
it('returns false when the stats exist but do not contain isDirectory method', function(done) {
216+
var file = new File({ contents: null, stat: {} });
217+
file.isDirectory().should.equal(false);
218+
done();
219+
});
220+
});
221+
222+
describe('isSymbolic()', function() {
223+
var fakeStat = {
224+
isSymbolicLink: function() {
225+
return true;
226+
},
227+
};
228+
229+
it('should return false when the contents are a Buffer', function(done) {
230+
var val = new Buffer('test');
231+
var file = new File({ contents: val, stat: fakeStat });
232+
file.isSymbolic().should.equal(false);
233+
done();
234+
});
235+
236+
it('should return false when the contents are a Stream', function(done) {
237+
var val = new Stream();
238+
var file = new File({ contents: val, stat: fakeStat });
239+
file.isSymbolic().should.equal(false);
240+
done();
241+
});
242+
243+
it('should return true when the contents are a null', function(done) {
244+
var file = new File({ contents: null, stat: fakeStat });
245+
file.isSymbolic().should.equal(true);
246+
done();
247+
});
248+
249+
it('returns false when the stats exist but do not contain isSymbolicLink method', function(done) {
250+
var file = new File({ contents: null, stat: {} });
251+
file.isSymbolic().should.equal(false);
252+
done();
253+
});
214254
});
215255

216256
describe('clone()', function() {
@@ -972,4 +1012,45 @@ describe('File', function() {
9721012
}).should.throw('path should be string');
9731013
});
9741014
});
1015+
1016+
describe('symlink get/set', function() {
1017+
it('should return null on get when no symlink', function(done) {
1018+
var file = new File();
1019+
var a = file.symlink;
1020+
should.not.exist(a);
1021+
done();
1022+
});
1023+
1024+
it('should return the symlink if set', function(done) {
1025+
var file = new File({
1026+
symlink: '/test/test.coffee',
1027+
});
1028+
file.symlink.should.equal('/test/test.coffee');
1029+
done();
1030+
});
1031+
1032+
it('should error on set with non-string symlink', function(done) {
1033+
var file = new File();
1034+
try {
1035+
file.symlink = null;
1036+
} catch (err) {
1037+
should.exist(err);
1038+
done();
1039+
}
1040+
});
1041+
1042+
it('should set the symlink', function(done) {
1043+
var file = new File();
1044+
file.symlink = '/test/test.coffee';
1045+
file.symlink.should.equal('/test/test.coffee');
1046+
done();
1047+
});
1048+
1049+
it('should set the relative symlink', function(done) {
1050+
var file = new File();
1051+
file.symlink = './test.coffee';
1052+
file.symlink.should.equal('./test.coffee');
1053+
done();
1054+
});
1055+
});
9751056
});

0 commit comments

Comments
 (0)