Last active
June 30, 2019 05:49
-
-
Save isaacs/0a29c789e050aa1ac35a8d952163df48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
isaacs
commented
Jun 30, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment