Skip to content

Commit

Permalink
Set mtime after extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed May 20, 2016
1 parent ac4411e commit 90233c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 8 additions & 4 deletions index.js
Expand Up @@ -41,12 +41,15 @@ const extractFile = (input, output, opts) => runPlugins(input, opts).then(files
}

return Promise.all(files.map(x => {
if (x.type === 'directory') {
return pify(mkdirp)(path.join(output, x.path)).then(() => x);
}

const dest = path.join(output, x.path);
const mode = x.mode & ~process.umask();
const now = new Date();

if (x.type === 'directory') {
return pify(mkdirp)(dest)
.then(() => fsP.utimes(dest, now, x.mtime))
.then(() => x);
}

return pify(mkdirp)(path.dirname(dest))
.then(() => {
Expand All @@ -60,6 +63,7 @@ const extractFile = (input, output, opts) => runPlugins(input, opts).then(files

return fsP.writeFile(dest, x.data, {mode});
})
.then(() => x.type === 'file' && fsP.utimes(dest, now, x.mtime))
.then(() => x);
}));
});
Expand Down
9 changes: 8 additions & 1 deletion test.js
Expand Up @@ -40,7 +40,7 @@ test('extract file using buffer', async t => {
t.is(zipFiles[0].path, 'test.jpg');
});

test('extract file to directory', async t => {
test.serial('extract file to directory', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);

t.is(files[0].path, 'test.jpg');
Expand Down Expand Up @@ -92,6 +92,13 @@ test('map option', async t => {
t.is(files[0].path, 'unicorn-test.jpg');
});

test.serial('set mtime', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
const stat = await fsP.stat(path.join(__dirname, 'test.jpg'));
t.deepEqual(files[0].mtime, stat.mtime);
await fsP.unlink(path.join(__dirname, 'test.jpg'));
});

test('return emptpy array if no plugins are set', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {plugins: []});
t.is(files.length, 0);
Expand Down

0 comments on commit 90233c3

Please sign in to comment.