Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active April 8, 2018 02:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/0843fbfe907f39e081e48079c641869f to your computer and use it in GitHub Desktop.
Save isaacs/0843fbfe907f39e081e48079c641869f to your computer and use it in GitHub Desktop.
$ tar cvf x.tar package
a package
a package/.gitignore
$ node rename.js
entry package/
extracting package/
entry package/.gitignore
renaming package/.gitignore to package/.npmignore
extracting package/.npmignore
$ tree output/
output/
└── .npmignore
0 directories, 1 file
'use strict'
const tar = require('./')
const file = 'x.tar'
const mkdirp = require('mkdirp')
const path = require('path')
const rimraf = require('rimraf')
rimraf.sync('output')
mkdirp.sync('output')
const sawIgnores = new Set()
tar.x({
file: file,
cwd: 'output',
strip: 1,
onentry: entry => {
console.error('entry', entry.path)
// Note: This mirrors logic in the fs read operations that are
// employed during tarball creation, in the fstream-npm module.
// It is duplicated here to handle tarballs that are created
// using other means, such as system tar or git archive.
if (entry.type.toLowerCase() === 'file') {
const base = path.basename(entry.path)
if (base === '.npmignore') {
sawIgnores.add(entry.path)
} else if (base === '.gitignore') {
const npmignore = entry.path.replace(/\.gitignore$/, '.npmignore')
if (!sawIgnores.has(npmignore)) {
// Rename, may be clobbered later.
console.log('renaming', entry.path, 'to', npmignore)
entry.path = npmignore
}
}
}
console.log('extracting', entry.path)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment