Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active June 30, 2019 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/0a29c789e050aa1ac35a8d952163df48 to your computer and use it in GitHub Desktop.
Save isaacs/0a29c789e050aa1ac35a8d952163df48 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const tar = require("tar");
const workingDir = path.resolve('working-dir')
// set up the test files
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const paths = ["dir1/", "dir2/", "dir3/", "dir4/"];
const files = [
'dir1/settings.json',
'dir1/prefs.json',
'dir2/stuff.xml',
'dir2/otherstuff.xml',
'dir3/config',
'dir4/config',
]
const exportPath = path.resolve('archive.tgz')
const create = () => {
rimraf.sync(workingDir)
rimraf.sync(exportPath)
paths.forEach(p => mkdirp.sync(path.resolve(workingDir, p)))
files.forEach(f => fs.writeFileSync(path.resolve(workingDir, f), f))
// run the test
const existingPaths = paths.filter(p =>
fs.existsSync(path.join(workingDir, p))
);
if (existingPaths.length !== 0) {
tar
.c(
{
gzip: true,
sync: true,
cwd: workingDir
},
existingPaths
)
.pipe(fs.createWriteStream(exportPath))
} else {
throw "Error: No configuration to export.";
}
}
const list = () => {
tar.t({
file: exportPath,
onentry: entry => console.log(entry.path)
});
}
if (process.argv[2] === 'list')
list()
else
create()
@isaacs
Copy link
Author

isaacs commented Jun 30, 2019

$ node test-gh-218.js create

$ node test-gh-218.js list
dir1/
dir1/prefs.json
dir1/settings.json
dir2/
dir2/otherstuff.xml
dir2/stuff.xml
dir3/
dir3/config
dir4/
dir4/config

$ tree
.
├── working-dir/
│   ├── dir1/
│   │   ├── prefs.json
│   │   └── settings.json
│   ├── dir2/
│   │   ├── otherstuff.xml
│   │   └── stuff.xml
│   ├── dir3/
│   │   └── config
│   └── dir4/
│       └── config
├── .test-gh-218.js.swp
├── archive.tgz
└── test-gh-218.js

5 directories, 9 files

$ tar tvf archive.tgz
drwxr-xr-x  0 isaacs 20          0 Jun 29 22:43 dir1/
-rw-r--r--  0 isaacs 20         15 Jun 29 22:43 dir1/prefs.json
-rw-r--r--  0 isaacs 20         18 Jun 29 22:43 dir1/settings.json
drwxr-xr-x  0 isaacs 20          0 Jun 29 22:43 dir2/
-rw-r--r--  0 isaacs 20         19 Jun 29 22:43 dir2/otherstuff.xml
-rw-r--r--  0 isaacs 20         14 Jun 29 22:43 dir2/stuff.xml
drwxr-xr-x  0 isaacs 20          0 Jun 29 22:43 dir3/
-rw-r--r--  0 isaacs 20         11 Jun 29 22:43 dir3/config
drwxr-xr-x  0 isaacs 20          0 Jun 29 22:43 dir4/
-rw-r--r--  0 isaacs 20         11 Jun 29 22:43 dir4/config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment