Skip to content

Commit

Permalink
Merge pull request #1740 from gruntjs/update-deps-22-10
Browse files Browse the repository at this point in the history
Update dependencies, tests...
  • Loading branch information
vladikoff authored Apr 11, 2022
2 parents fdc7056 + 47d32de commit 3eda6ae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [10, 12, 14]
node: [12, 14, 16]
os: [ubuntu-latest, windows-latest]

steps:
Expand Down
27 changes: 26 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,28 @@ 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);
if (fs.existsSync(destpath)) {
// skip symlink if file already exists
return;
}
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
22 changes: 22 additions & 0 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,5 +893,27 @@ 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));
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 3eda6ae

Please sign in to comment.