Skip to content

Commit

Permalink
feat: add debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Mar 14, 2022
1 parent 9bb7cf1 commit f2efb1e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import { tar } from "https://deno.land/x/compress@v0.4.2/mod.ts";
// or only import tar
// import { tar } from "https://deno.land/x/compress@v0.4.2/tar/mod.ts";
export interface compressInterface {
excludeSrc?: boolean; // exclude src directory, default: include src directory
excludeSrc?: boolean; // does not contain the src directory
debug?: boolean; // list the files and folders
}
export interface uncompressInterface {
debug?: boolean; // list the files and folders
}
tar.compress(src, dest, options?: compressInterface): Promise<void>;
tar.uncompress(src, dest): Promise<void>;
tar.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
```

### Example
Expand Down Expand Up @@ -152,11 +156,15 @@ const decompressed = gunzip(compressed);
import { tgz } from "https://deno.land/x/compress@v0.4.2/mod.ts";
// or only import tgz
// import { tgz } from "https://deno.land/x/compress@v0.4.2/tgz/mod.ts";
interface compressInterface {
excludeSrc?: boolean; // exclude src directory, default: include src directory
export interface compressInterface {
excludeSrc?: boolean; // does not contain the src directory
debug?: boolean; // list the files and folders
}
export interface uncompressInterface {
debug?: boolean; // list the files and folders
}
tgz.compress(src, dest, options?: compressInterface): Promise<void>;
tgz.uncompress(src, dest): Promise<void>;
tgz.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
```

### Example
Expand All @@ -181,11 +189,15 @@ await tgz.uncompress("./test.tar.gz", "./dest");

```ts
import { zip } from "https://deno.land/x/compress@v0.4.2/mod.ts";
interface compressInterface {
excludeSrc?: boolean; // exclude src directory, default: include src directory
export interface compressInterface {
excludeSrc?: boolean; // does not contain the src directory
debug?: boolean; // list the files and folders
}
export interface uncompressInterface {
debug?: boolean; // list the files and folders
}
zip.compress(src, dest, options?: compressInterface): Promise<void>;
zip.uncompress(src, dest): Promise<void>;
zip.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
```

# test
Expand Down
5 changes: 5 additions & 0 deletions interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface compressInterface {
excludeSrc?: boolean;
debug?: boolean;
}

export interface uncompressInterface {
debug?: boolean;
}
17 changes: 12 additions & 5 deletions tar/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Buffer, copy, ensureDir, path, Tar, Untar } from "../deps.ts";
import type { compressInterface } from "../interface.ts";
import type { compressInterface, uncompressInterface } from "../interface.ts";

export async function uncompress(src: string, dest: string): Promise<void> {
export async function uncompress(
src: string,
dest: string,
options?: uncompressInterface,
): Promise<void> {
const reader = await Deno.open(src, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
const filePath = path.resolve(dest, entry.fileName);
if (options?.debug) console.log(filePath);
if (entry.type === "directory") {
await ensureDir(filePath);
continue;
Expand Down Expand Up @@ -33,6 +38,7 @@ export async function compress(
contentSize: stat.size,
mtime: (stat?.mtime ?? new Date()).valueOf() / 1000,
});
if (options?.debug) console.log(path.resolve(src));
} else {
const appendFolder = async (folder: string, prefix?: string) => {
let nowLoopList: string[][] = [[folder, prefix || ""]];
Expand All @@ -43,8 +49,8 @@ export async function compress(
for await (const entry of Deno.readDir(folder)) {
const { isDirectory, name } = entry;
const fileName = prefix ? `${prefix}/${name}` : name;

const filePath = path.resolve(folder, name);
if (options?.debug) console.log(path.resolve(src));
const stat = await Deno.stat(filePath);
if (isDirectory) {
await tar.append(
Expand Down Expand Up @@ -84,6 +90,7 @@ export async function compress(
// reader: new Deno.Buffer(),
},
);
if (options?.debug) console.log(path.resolve(src));
await appendFolder(src, folderName);
}
}
Expand All @@ -92,7 +99,7 @@ export async function compress(
writer.close();
}

// Recursive way
// Recursive way
// export async function compress(
// src: string,
// dest: string,
Expand Down Expand Up @@ -153,4 +160,4 @@ export async function compress(
// const writer = await Deno.open(dest, { write: true, create: true });
// await copy(tar.getReader(), writer);
// writer.close();
// }
// }
15 changes: 9 additions & 6 deletions test/tar.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.129.0/testing/asserts.ts";
import { tar } from "../mod.ts";

Deno.test("tar.compress file", async () => {
const src = "./test/tar/tar.txt";
const dest = "./test.tar";
try {
await tar.compress(src, dest);
await tar.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
/**
* 2048 = 512 (header) + 512 (content) + 1024 (footer)
*/
assertEquals(stat.size, 2048);
await Deno.remove(dest);
} catch (error) {
console.error(error)
console.error(error);
assert(false);
}
});
Expand All @@ -22,7 +25,7 @@ Deno.test("tar.compress folder", async () => {
const src = "./test/tar";
const dest = "./test.tar";
try {
await tar.compress(src, dest);
await tar.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
/**
* 4096 = 512 (header) + 0 (content) + // tar folder
Expand All @@ -34,7 +37,7 @@ Deno.test("tar.compress folder", async () => {
assertEquals(stat.size, 4096);
await Deno.remove(dest);
} catch (error) {
console.error(error)
console.error(error);
assert(false);
}
});
Expand All @@ -54,7 +57,7 @@ Deno.test("tar.uncompress", async () => {
assertEquals(content, landTxtContent);
await Deno.remove(dest, { recursive: true });
} catch (error) {
console.error(error)
console.error(error);
assert(false);
}
});

0 comments on commit f2efb1e

Please sign in to comment.