1

Basically I want to do the equivalent of this How to strip path while archiving with TAR but with the tar commands imported to NodeJS, so currently I'm doing this:

const gzip = zlib.createGzip();
const pack = new tar.Pack(prefix="");
const source = Readable.from('public/images/');
const destination = fs.createWriteStream('public/archive.tar.gz');
pipeline(source, pack, gzip, destination, (err) => {
    if (err) {
        console.error('An error occurred:', err);
        process.exitCode = 1;
    }
});

But doing so leaves me with files like: "/public/images/a.png" and "public/images/b.png", when what I want is files like "/a.png" and "/b.png". I want to know how I can add to this process to strip out the unneeded directories, while keeping the files where they are.

Ethan
  • 161
  • 2
  • 19

1 Answers1

1

You need to change working directory:

// cwd The current working directory for creating the archive. Defaults to process.cwd().
new tar.Pack({ cwd: "./public/images" });
const source = Readable.from('');

Source: documentation of node-tar

Example: https://github.com/npm/node-tar/blob/main/test/pack.js#L93

Šimon Kocúrek
  • 1,591
  • 1
  • 12
  • 22
  • Using it as you have it written there gives me an error, "no such file or directory", and doing it like this "const pack = new tar.Pack({ cwd: "./public/images" });" leaves the contents inside a directory with the name "." so... – Ethan Aug 05 '21 at 00:03
  • Fixed it with this line "const source = Readable.from('');", but now there's an extra folder titled "." – Ethan Aug 05 '21 at 00:55
  • 1
    It's close enough, please add those lines to your answer, so I can accept it. – Ethan Aug 05 '21 at 04:55