Skip to content

Commit

Permalink
Add support for symlinks (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaimi authored and sindresorhus committed Nov 19, 2019
1 parent 8076a93 commit 599d565
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ module.exports = (filename, options) => {
firstFile = file;
}

archive.append(file.contents, {
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
mode: file.stat && file.stat.mode,
date: file.stat && file.stat.mtime ? file.stat.mtime : null,
...options
});
const nameNormalized = file.relative.replace(/\\/g, '/');

if (file.isSymbolic()) {
archive.symlink(nameNormalized, file.symlink);
} else {
archive.append(file.contents, {
name: nameNormalized + (file.isNull() ? '/' : ''),
mode: file.stat && file.stat.mode,
date: file.stat && file.stat.mtime ? file.stat.mtime : null,
...options
});
}

callback();
}, function (callback) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"gulp": "^4.0.2",
"gulp-gzip": "^1.2.0",
"vinyl-fs": "^3.0.3",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"rimraf": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions test/fixture/.symlink/symlink.txt
60 changes: 60 additions & 0 deletions test/symlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env mocha */
const {createReadStream} = require('fs');
const path = require('path');
const zlib = require('zlib');
const assert = require('assert');
const gulp = require('gulp');
const gulpZip = require('gulp-gzip');
const tarfs = require('tar-fs');
const rimraf = require('rimraf');
const vinylFs = require('vinyl-fs');
const gulpTar = require('..');

it('should include symlink', done => {
const onTargz = () => {
const filename = 'archive.tar.gz';
const fullPath = path.join(__dirname, 'dest', filename);
const rs = createReadStream(fullPath);

rs.pipe(zlib.createGunzip())
.pipe(
tarfs.extract(path.join(__dirname, 'dest-out'), {
map: header => {
const expected = expectedNames.pop();
assert.strictEqual(header.name, expected.name);
assert.strictEqual(header.type, expected.type);
if (header.type === 'symlink') {
assert.strictEqual(header.linkname, expected.linkname);
}

return header;
}
})
)
.on('finish', () => {
for (const directory of ['dest', 'dest-out']) {
rimraf.sync(path.join(__dirname, directory));
}

done();
});
};

const expectedNames = [
{name: 'fixture/.symlink/symlink.txt', type: 'symlink', linkname: '../fixture.txt'},
{name: 'fixture/dir-fixture/inside.txt', type: 'file'},
{name: 'fixture/fixture.txt', type: 'file'},
{name: 'fixture/dir-fixture/', type: 'directory'}
];

vinylFs
.src(['fixture/**/*', 'fixture/.symlink/**/*'], {
cwd: __dirname,
base: __dirname,
resolveSymlinks: false
})
.pipe(gulpTar('archive.tar'))
.pipe(gulpZip())
.pipe(gulp.dest('dest', {cwd: __dirname}))
.on('finish', onTargz);
});

0 comments on commit 599d565

Please sign in to comment.