forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_zip_fixtures.ts
More file actions
executable file
·52 lines (46 loc) · 1.49 KB
/
make_zip_fixtures.ts
File metadata and controls
executable file
·52 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env node
// Generates test fixtures for Zip FS.
import * as fs from 'fs';
import * as path from 'path';
import * as archiver from 'archiver';
const baseFolder = `.`;
const srcFolder = `${baseFolder}/test/fixtures/files`;
const outputFolder = `${baseFolder}/test/fixtures/zipfs`;
// Create a zip file of the fixture data using the given zlib compression level.
function createZip(level: number): void {
const output = fs.createWriteStream(`${outputFolder}/zipfs_fixtures_l${level}.zip`);
const options = { zlib: { level: level } };
const archive = archiver('zip', options);
archive.on('error', (err: any) => {
throw err;
});
archive.pipe(output);
addFolder(archive, srcFolder);
archive.finalize();
}
// Recursively add folders and their files to the zip file.
function addFolder(archive: any, folder: string) {
fs.readdirSync(folder).forEach((file) => {
const fullpath = path.join(folder, file);
if (fs.statSync(fullpath).isDirectory()) {
addFolder(archive, fullpath);
} else {
addFile(archive, fullpath);
}
});
}
// Add the given file to the zip file.
function addFile(archive: any, fileName: string) {
const fileNameRelative = path.relative(baseFolder, fileName);
archive.append(fs.createReadStream(fileName), {name: fileNameRelative});
}
// Ensure output folder exists
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
}
// Store
createZip(0);
// Middle-of-the-road compression
createZip(4);
// Maximum compression
createZip(9);