mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
99 lines
1.9 KiB
TypeScript
99 lines
1.9 KiB
TypeScript
import tar = require("tar-fs");
|
|
import fs = require("fs");
|
|
import path = require("path");
|
|
import stream = require("stream");
|
|
|
|
/*
|
|
* Default test
|
|
*/
|
|
// packing a directory
|
|
tar.pack('./my-directory').pipe(fs.createWriteStream('my-tarball.tar'));
|
|
|
|
// extracting a directory
|
|
fs.createReadStream('my-other-tarball.tar').pipe(tar.extract('./my-other-directory'));
|
|
|
|
/*
|
|
* Ignore various files
|
|
*/
|
|
tar.pack('./my-directory', {
|
|
ignore: (name) => {
|
|
return path.extname(name) === '.bin'; // ignore .bin files when packing
|
|
}
|
|
});
|
|
|
|
tar.extract('./my-other-directory', {
|
|
ignore: (name) => {
|
|
return path.extname(name) === '.bin'; // ignore .bin files inside the tarball when extracing
|
|
}
|
|
});
|
|
|
|
tar.extract('./my-other-other-directory', {
|
|
ignore: (_, header) => {
|
|
// pass files & directories, ignore e.g. symlinks
|
|
return header.type !== 'file' && header.type !== 'directory';
|
|
}
|
|
});
|
|
|
|
/*
|
|
* Specify which entries to pack
|
|
*/
|
|
tar.pack('./my-directory', {
|
|
entries: ['file1', 'subdir/file2'], // only the specific entries will be packed
|
|
});
|
|
|
|
/*
|
|
* Modify the headers by map
|
|
*/
|
|
tar.pack('./my-directory', {
|
|
map: (header) => {
|
|
header.name = 'prefixed/' + header.name;
|
|
return header;
|
|
}
|
|
});
|
|
|
|
tar.extract('./my-directory', {
|
|
map: (header) => {
|
|
header.name = 'another-prefix/' + header.name;
|
|
return header;
|
|
}
|
|
});
|
|
|
|
/*
|
|
* Modify the headers by mapStream
|
|
*/
|
|
tar.pack('./my-directory', {
|
|
mapStream: (fileStream, header) => {
|
|
if (path.extname(header.name) === '.js') {
|
|
return fileStream;
|
|
}
|
|
return fileStream;
|
|
}
|
|
});
|
|
|
|
tar.extract('./my-directory', {
|
|
mapStream: (fileStream, header) => {
|
|
if (path.extname(header.name) === '.js') {
|
|
return fileStream;
|
|
}
|
|
return fileStream;
|
|
}
|
|
});
|
|
|
|
/*
|
|
* The corresponding modes
|
|
*/
|
|
tar.extract('./my-directory', {
|
|
dmode: parseInt("555", 8),
|
|
fmode: parseInt("444", 8),
|
|
readable: true,
|
|
writable: true,
|
|
});
|
|
|
|
/*
|
|
* The other options
|
|
*/
|
|
tar.pack('./my-directory', {
|
|
strict: false,
|
|
dereference: true,
|
|
});
|