From aad3d4521c3098fb255fb2db8f2e1d691a033665 Mon Sep 17 00:00:00 2001 From: Vlad Filippov Date: Sun, 10 Apr 2022 23:16:06 -0400 Subject: [PATCH] Update dependencies, tests... --- lib/grunt/file.js | 23 ++++++++++++++++++++++- package.json | 4 ++-- test/grunt/file_test.js | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/grunt/file.js b/lib/grunt/file.js index 7e0e2fb7b..21f4ef29d 100644 --- a/lib/grunt/file.js +++ b/lib/grunt/file.js @@ -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); @@ -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); diff --git a/package.json b/package.json index 444f03d96..167372f8b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/grunt/file_test.js b/test/grunt/file_test.js index b192cad9a..20c4aed44 100644 --- a/test/grunt/file_test.js +++ b/test/grunt/file_test.js @@ -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(); + }, } };