Skip to content

Commit

Permalink
Update dependencies, tests...
Browse files Browse the repository at this point in the history
  • Loading branch information
vladikoff committed Apr 11, 2022
1 parent fdc7056 commit aad3d45
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
23 changes: 22 additions & 1 deletion lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ file.write = function(filepath, contents, options) {
// Read a file, optionally processing its content, then write the output.
// Or read a directory, recursively creating directories, reading files,
// processing content, writing output.
// Handles symlinks by coping them as files or directories.
file.copy = function copy(srcpath, destpath, options) {
if (file.isDir(srcpath)) {
if (file._isSymbolicLink(srcpath)) {
file._copySymbolicLink(srcpath, destpath);
} else if (file.isDir(srcpath)) {
// Copy a directory, recursively.
// Explicitly create new dest directory.
file.mkdir(destpath);
Expand Down Expand Up @@ -449,6 +452,24 @@ file.isPathCwd = function() {
}
};

file._isSymbolicLink = function() {
var filepath = path.join.apply(path, arguments);
return fs.lstatSync(filepath).isSymbolicLink();
};

file._copySymbolicLink = function(srcpath, destpath) {
var destdir = path.join(destpath, '..');
var fileBase = path.basename(srcpath);
// Use the correct relative path for the symlink
if (!grunt.file.isPathAbsolute(srcpath)) {
srcpath = path.relative(destdir, srcpath) || '.';
}
file.mkdir(destdir);
var mode = grunt.file.isDir(srcpath) ? 'dir' : 'file';
var destpath = path.join(destpath, fileBase);
return fs.symlinkSync(srcpath, destpath, mode);
};

// Test to see if a filepath is contained within the CWD.
file.isPathInCwd = function() {
var filepath = path.join.apply(path, arguments);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"exit": "~0.1.2",
"findup-sync": "~0.3.0",
"glob": "~7.1.6",
"grunt-cli": "~1.4.2",
"grunt-cli": "~1.4.3",
"grunt-known-options": "~2.0.0",
"grunt-legacy-log": "~3.0.0",
"grunt-legacy-util": "~2.0.1",
Expand All @@ -56,7 +56,7 @@
"devDependencies": {
"difflet": "~1.0.1",
"eslint-config-grunt": "~1.0.1",
"grunt-contrib-nodeunit": "~3.0.0",
"grunt-contrib-nodeunit": "~4.0.0",
"grunt-contrib-watch": "~1.1.0",
"grunt-eslint": "~18.1.0",
"temporary": "~0.0.4",
Expand Down
23 changes: 23 additions & 0 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,5 +893,28 @@ exports.file = {
test.ok(grunt.file.isPathInCwd(path.resolve('deep')), 'subdirectory is in cwd');
test.done();
},
'symbolicLinkCopy': function(test) {
test.expect(4);
var srcfile = new Tempdir();
fs.symlinkSync(path.resolve('test/fixtures/octocat.png'), path.join(srcfile.path, 'octocat.png'), 'file');
// test symlink copy for files
var destdir = new Tempdir();
grunt.file.copy(path.join(srcfile.path, 'octocat.png'), destdir.path);
test.ok(fs.lstatSync(path.join(srcfile.path, 'octocat.png')).isSymbolicLink());
test.ok(fs.lstatSync(path.join(destdir.path, 'octocat.png')).isSymbolicLink());

// test symlink copy for directories
var srcdir = new Tempdir();
var destdir = new Tempdir();
var fixtures = path.resolve('test/fixtures');
var symlinkSource = path.join(srcdir.path, path.basename(fixtures));
console.log('symlinkSource', symlinkSource);
fs.symlinkSync(fixtures, symlinkSource, 'dir');

grunt.file.copy(symlinkSource, destdir.path);
test.ok(fs.lstatSync(symlinkSource).isSymbolicLink());
test.ok(fs.lstatSync(path.join(destdir.path, path.basename(fixtures))).isSymbolicLink());
test.done();
},
}
};

0 comments on commit aad3d45

Please sign in to comment.