zip-lib 0.7.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cancelable.d.ts +38 -38
- package/lib/cancelable.js +65 -65
- package/lib/fs.js +175 -175
- package/lib/index.d.ts +25 -25
- package/lib/index.js +56 -52
- package/lib/unzip.d.ts +78 -78
- package/lib/unzip.js +358 -358
- package/lib/util.js +49 -49
- package/lib/zip.d.ts +59 -59
- package/lib/zip.js +213 -213
- package/package.json +12 -9
package/lib/cancelable.d.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
export interface ICancelable {
|
|
2
|
-
/**
|
|
3
|
-
* Cancel operation
|
|
4
|
-
*/
|
|
5
|
-
cancel(): void;
|
|
6
|
-
}
|
|
7
|
-
export declare class CancellationToken {
|
|
8
|
-
private _isCancelled;
|
|
9
|
-
private _callbacks;
|
|
10
|
-
/**
|
|
11
|
-
* A flag signalling is cancellation has been requested.
|
|
12
|
-
*/
|
|
13
|
-
get isCancelled(): boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Subscribe a callback when cancellation is requested. The callback
|
|
16
|
-
* only ever fires `once` as cancellation can only happen once.
|
|
17
|
-
* @param cb A function will be called
|
|
18
|
-
* @returns A function that Unsubscribe the cancellation callback.
|
|
19
|
-
*/
|
|
20
|
-
onCancelled(cb: () => void): () => void;
|
|
21
|
-
cancel(): void;
|
|
22
|
-
}
|
|
23
|
-
export declare abstract class Cancelable implements ICancelable {
|
|
24
|
-
abstract cancel(): void;
|
|
25
|
-
/**
|
|
26
|
-
* Ignore any other error if the `cancel` method has been called
|
|
27
|
-
*
|
|
28
|
-
* Error: EBADF: bad file descriptor, read
|
|
29
|
-
* EBADF error may occur when calling the cancel method.
|
|
30
|
-
* see https://travis-ci.org/fpsqdb/zip-lib/jobs/606040627#L124
|
|
31
|
-
* @param error
|
|
32
|
-
*/
|
|
33
|
-
protected wrapError(error: Error, isCanceled: boolean): Error;
|
|
34
|
-
/**
|
|
35
|
-
* Returns an error that signals cancellation.
|
|
36
|
-
*/
|
|
37
|
-
protected canceledError(): Error;
|
|
38
|
-
}
|
|
1
|
+
export interface ICancelable {
|
|
2
|
+
/**
|
|
3
|
+
* Cancel operation
|
|
4
|
+
*/
|
|
5
|
+
cancel(): void;
|
|
6
|
+
}
|
|
7
|
+
export declare class CancellationToken {
|
|
8
|
+
private _isCancelled;
|
|
9
|
+
private _callbacks;
|
|
10
|
+
/**
|
|
11
|
+
* A flag signalling is cancellation has been requested.
|
|
12
|
+
*/
|
|
13
|
+
get isCancelled(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Subscribe a callback when cancellation is requested. The callback
|
|
16
|
+
* only ever fires `once` as cancellation can only happen once.
|
|
17
|
+
* @param cb A function will be called when cancellation is requested.
|
|
18
|
+
* @returns A function that Unsubscribe the cancellation callback.
|
|
19
|
+
*/
|
|
20
|
+
onCancelled(cb: () => void): () => void;
|
|
21
|
+
cancel(): void;
|
|
22
|
+
}
|
|
23
|
+
export declare abstract class Cancelable implements ICancelable {
|
|
24
|
+
abstract cancel(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Ignore any other error if the `cancel` method has been called
|
|
27
|
+
*
|
|
28
|
+
* Error: EBADF: bad file descriptor, read
|
|
29
|
+
* EBADF error may occur when calling the cancel method.
|
|
30
|
+
* see https://travis-ci.org/fpsqdb/zip-lib/jobs/606040627#L124
|
|
31
|
+
* @param error
|
|
32
|
+
*/
|
|
33
|
+
protected wrapError(error: Error, isCanceled: boolean): Error;
|
|
34
|
+
/**
|
|
35
|
+
* Returns an error that signals cancellation.
|
|
36
|
+
*/
|
|
37
|
+
protected canceledError(): Error;
|
|
38
|
+
}
|
package/lib/cancelable.js
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Cancelable = exports.CancellationToken = void 0;
|
|
4
|
-
class CancellationToken {
|
|
5
|
-
constructor() {
|
|
6
|
-
this._isCancelled = false;
|
|
7
|
-
this._callbacks = new Set();
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* A flag signalling is cancellation has been requested.
|
|
11
|
-
*/
|
|
12
|
-
get isCancelled() {
|
|
13
|
-
return this._isCancelled;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Subscribe a callback when cancellation is requested. The callback
|
|
17
|
-
* only ever fires `once` as cancellation can only happen once.
|
|
18
|
-
* @param cb A function will be called
|
|
19
|
-
* @returns A function that Unsubscribe the cancellation callback.
|
|
20
|
-
*/
|
|
21
|
-
onCancelled(cb) {
|
|
22
|
-
if (this.isCancelled) {
|
|
23
|
-
cb();
|
|
24
|
-
return () => {
|
|
25
|
-
// noop
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
this._callbacks.add(cb);
|
|
29
|
-
return () => this._callbacks.delete(cb);
|
|
30
|
-
}
|
|
31
|
-
cancel() {
|
|
32
|
-
if (this._isCancelled) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
this._isCancelled = true;
|
|
36
|
-
this._callbacks.forEach((cb) => cb());
|
|
37
|
-
this._callbacks.clear();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.CancellationToken = CancellationToken;
|
|
41
|
-
class Cancelable {
|
|
42
|
-
/**
|
|
43
|
-
* Ignore any other error if the `cancel` method has been called
|
|
44
|
-
*
|
|
45
|
-
* Error: EBADF: bad file descriptor, read
|
|
46
|
-
* EBADF error may occur when calling the cancel method.
|
|
47
|
-
* see https://travis-ci.org/fpsqdb/zip-lib/jobs/606040627#L124
|
|
48
|
-
* @param error
|
|
49
|
-
*/
|
|
50
|
-
wrapError(error, isCanceled) {
|
|
51
|
-
if (isCanceled) {
|
|
52
|
-
return this.canceledError();
|
|
53
|
-
}
|
|
54
|
-
return error;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Returns an error that signals cancellation.
|
|
58
|
-
*/
|
|
59
|
-
canceledError() {
|
|
60
|
-
const error = new Error("Canceled");
|
|
61
|
-
error.name = error.message;
|
|
62
|
-
return error;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.Cancelable = Cancelable;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Cancelable = exports.CancellationToken = void 0;
|
|
4
|
+
class CancellationToken {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._isCancelled = false;
|
|
7
|
+
this._callbacks = new Set();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A flag signalling is cancellation has been requested.
|
|
11
|
+
*/
|
|
12
|
+
get isCancelled() {
|
|
13
|
+
return this._isCancelled;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Subscribe a callback when cancellation is requested. The callback
|
|
17
|
+
* only ever fires `once` as cancellation can only happen once.
|
|
18
|
+
* @param cb A function will be called when cancellation is requested.
|
|
19
|
+
* @returns A function that Unsubscribe the cancellation callback.
|
|
20
|
+
*/
|
|
21
|
+
onCancelled(cb) {
|
|
22
|
+
if (this.isCancelled) {
|
|
23
|
+
cb();
|
|
24
|
+
return () => {
|
|
25
|
+
// noop
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
this._callbacks.add(cb);
|
|
29
|
+
return () => this._callbacks.delete(cb);
|
|
30
|
+
}
|
|
31
|
+
cancel() {
|
|
32
|
+
if (this._isCancelled) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this._isCancelled = true;
|
|
36
|
+
this._callbacks.forEach((cb) => cb());
|
|
37
|
+
this._callbacks.clear();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.CancellationToken = CancellationToken;
|
|
41
|
+
class Cancelable {
|
|
42
|
+
/**
|
|
43
|
+
* Ignore any other error if the `cancel` method has been called
|
|
44
|
+
*
|
|
45
|
+
* Error: EBADF: bad file descriptor, read
|
|
46
|
+
* EBADF error may occur when calling the cancel method.
|
|
47
|
+
* see https://travis-ci.org/fpsqdb/zip-lib/jobs/606040627#L124
|
|
48
|
+
* @param error
|
|
49
|
+
*/
|
|
50
|
+
wrapError(error, isCanceled) {
|
|
51
|
+
if (isCanceled) {
|
|
52
|
+
return this.canceledError();
|
|
53
|
+
}
|
|
54
|
+
return error;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns an error that signals cancellation.
|
|
58
|
+
*/
|
|
59
|
+
canceledError() {
|
|
60
|
+
const error = new Error("Canceled");
|
|
61
|
+
error.name = error.message;
|
|
62
|
+
return error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Cancelable = Cancelable;
|
package/lib/fs.js
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isRootPath = exports.rimraf = exports.pathExists = exports.ensureFolder = exports.getFileEntry = exports.readdirp = void 0;
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const util = require("./util");
|
|
6
|
-
async function readdirp(folder) {
|
|
7
|
-
const result = [];
|
|
8
|
-
const files = await util.readdir(folder);
|
|
9
|
-
for (const item of files) {
|
|
10
|
-
const file = path.join(folder, item);
|
|
11
|
-
const entry = await getFileEntry(file);
|
|
12
|
-
if (!entry.isSymbolicLink && entry.type === "dir") {
|
|
13
|
-
const subFiles = await readdirp(file);
|
|
14
|
-
if (subFiles.length > 0) {
|
|
15
|
-
result.push(...subFiles);
|
|
16
|
-
// If the folder is not empty, don't need to add the folder itself.
|
|
17
|
-
// continue and skip the code below
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
result.push(entry);
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
exports.readdirp = readdirp;
|
|
26
|
-
async function getFileEntry(target) {
|
|
27
|
-
const stat = await util.lstat(target);
|
|
28
|
-
let isSymbolicLink = false;
|
|
29
|
-
let fileType = "file";
|
|
30
|
-
if (stat.isDirectory()) {
|
|
31
|
-
fileType = "dir";
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
if (stat.isSymbolicLink()) {
|
|
35
|
-
isSymbolicLink = true;
|
|
36
|
-
// If the path is a link, we must instead use fs.stat() to find out if the
|
|
37
|
-
// link is a directory or not because lstat will always return the stat of
|
|
38
|
-
// the link which is always a file.
|
|
39
|
-
const actualStat = await util.stat(target);
|
|
40
|
-
if (actualStat.isDirectory()) {
|
|
41
|
-
fileType = "dir";
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
path: target,
|
|
47
|
-
isSymbolicLink,
|
|
48
|
-
type: fileType,
|
|
49
|
-
mtime: stat.mtime,
|
|
50
|
-
mode: stat.mode
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
exports.getFileEntry = getFileEntry;
|
|
54
|
-
async function ensureFolder(folder) {
|
|
55
|
-
// stop at root
|
|
56
|
-
if (folder === path.dirname(folder)) {
|
|
57
|
-
return Promise.resolve();
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
await mkdir(folder);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
// ENOENT: a parent folder does not exist yet, continue
|
|
64
|
-
// to create the parent folder and then try again.
|
|
65
|
-
if (error.code === "ENOENT") {
|
|
66
|
-
await ensureFolder(path.dirname(folder));
|
|
67
|
-
return mkdir(folder);
|
|
68
|
-
}
|
|
69
|
-
// Any other error
|
|
70
|
-
return Promise.reject(error);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.ensureFolder = ensureFolder;
|
|
74
|
-
async function pathExists(target) {
|
|
75
|
-
try {
|
|
76
|
-
await util.access(target);
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.pathExists = pathExists;
|
|
84
|
-
async function rimraf(target) {
|
|
85
|
-
if (isRootPath(target)) {
|
|
86
|
-
// refuse to recursively delete root
|
|
87
|
-
return Promise.reject(new Error(`Refuse to recursively delete root, path: "${target}"`));
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
const stat = await util.lstat(target);
|
|
91
|
-
// Folder delete (recursive) - NOT for symbolic links though!
|
|
92
|
-
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
93
|
-
// Children
|
|
94
|
-
const children = await util.readdir(target);
|
|
95
|
-
await Promise.all(children.map(child => rimraf(path.join(target, child))));
|
|
96
|
-
// Folder
|
|
97
|
-
await util.rmdir(target);
|
|
98
|
-
}
|
|
99
|
-
// Single file delete
|
|
100
|
-
else {
|
|
101
|
-
// chmod as needed to allow for unlink
|
|
102
|
-
const mode = stat.mode;
|
|
103
|
-
if (!(mode & 128)) { // 128 === 0200
|
|
104
|
-
await util.chmod(target, mode | 128);
|
|
105
|
-
}
|
|
106
|
-
return util.unlink(target);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
if (error.code !== "ENOENT") {
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.rimraf = rimraf;
|
|
116
|
-
async function mkdir(folder) {
|
|
117
|
-
try {
|
|
118
|
-
await util.mkdir(folder, 0o777);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
// ENOENT: a parent folder does not exist yet or folder name is invalid.
|
|
122
|
-
if (error.code === "ENOENT") {
|
|
123
|
-
return Promise.reject(error);
|
|
124
|
-
}
|
|
125
|
-
// Any other error: check if folder exists and
|
|
126
|
-
// return normally in that case if its a folder
|
|
127
|
-
try {
|
|
128
|
-
const fileStat = await util.stat(folder);
|
|
129
|
-
if (!fileStat.isDirectory()) {
|
|
130
|
-
return Promise.reject(new Error(`"${folder}" exists and is not a directory.`));
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
catch (statError) {
|
|
134
|
-
throw error; // rethrow original error
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
// "A"
|
|
139
|
-
const charA = 65;
|
|
140
|
-
// "Z"
|
|
141
|
-
const charZ = 90;
|
|
142
|
-
// "a"
|
|
143
|
-
const chara = 97;
|
|
144
|
-
// "z"
|
|
145
|
-
const charz = 122;
|
|
146
|
-
// ":"
|
|
147
|
-
const charColon = 58;
|
|
148
|
-
// "\"
|
|
149
|
-
const charWinSep = 92;
|
|
150
|
-
// "/"
|
|
151
|
-
const cahrUnixSep = 47;
|
|
152
|
-
function isDriveLetter(char0) {
|
|
153
|
-
return char0 >= charA && char0 <= charZ || char0 >= chara && char0 <= charz;
|
|
154
|
-
}
|
|
155
|
-
const winSep = "\\";
|
|
156
|
-
const unixSep = "/";
|
|
157
|
-
function isRootPath(target) {
|
|
158
|
-
if (!target) {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
if (target === winSep ||
|
|
162
|
-
target === unixSep) {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
if (process.platform === "win32") {
|
|
166
|
-
if (target.length > 3) {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
return isDriveLetter(target.charCodeAt(0))
|
|
170
|
-
&& target.charCodeAt(1) === charColon
|
|
171
|
-
&& (target.length === 2 || target.charCodeAt(2) === charWinSep || target.charCodeAt(2) === cahrUnixSep);
|
|
172
|
-
}
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
exports.isRootPath = isRootPath;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRootPath = exports.rimraf = exports.pathExists = exports.ensureFolder = exports.getFileEntry = exports.readdirp = void 0;
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const util = require("./util");
|
|
6
|
+
async function readdirp(folder) {
|
|
7
|
+
const result = [];
|
|
8
|
+
const files = await util.readdir(folder);
|
|
9
|
+
for (const item of files) {
|
|
10
|
+
const file = path.join(folder, item);
|
|
11
|
+
const entry = await getFileEntry(file);
|
|
12
|
+
if (!entry.isSymbolicLink && entry.type === "dir") {
|
|
13
|
+
const subFiles = await readdirp(file);
|
|
14
|
+
if (subFiles.length > 0) {
|
|
15
|
+
result.push(...subFiles);
|
|
16
|
+
// If the folder is not empty, don't need to add the folder itself.
|
|
17
|
+
// continue and skip the code below
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
result.push(entry);
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
exports.readdirp = readdirp;
|
|
26
|
+
async function getFileEntry(target) {
|
|
27
|
+
const stat = await util.lstat(target);
|
|
28
|
+
let isSymbolicLink = false;
|
|
29
|
+
let fileType = "file";
|
|
30
|
+
if (stat.isDirectory()) {
|
|
31
|
+
fileType = "dir";
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
if (stat.isSymbolicLink()) {
|
|
35
|
+
isSymbolicLink = true;
|
|
36
|
+
// If the path is a link, we must instead use fs.stat() to find out if the
|
|
37
|
+
// link is a directory or not because lstat will always return the stat of
|
|
38
|
+
// the link which is always a file.
|
|
39
|
+
const actualStat = await util.stat(target);
|
|
40
|
+
if (actualStat.isDirectory()) {
|
|
41
|
+
fileType = "dir";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
path: target,
|
|
47
|
+
isSymbolicLink,
|
|
48
|
+
type: fileType,
|
|
49
|
+
mtime: stat.mtime,
|
|
50
|
+
mode: stat.mode
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.getFileEntry = getFileEntry;
|
|
54
|
+
async function ensureFolder(folder) {
|
|
55
|
+
// stop at root
|
|
56
|
+
if (folder === path.dirname(folder)) {
|
|
57
|
+
return Promise.resolve();
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await mkdir(folder);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
// ENOENT: a parent folder does not exist yet, continue
|
|
64
|
+
// to create the parent folder and then try again.
|
|
65
|
+
if (error.code === "ENOENT") {
|
|
66
|
+
await ensureFolder(path.dirname(folder));
|
|
67
|
+
return mkdir(folder);
|
|
68
|
+
}
|
|
69
|
+
// Any other error
|
|
70
|
+
return Promise.reject(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.ensureFolder = ensureFolder;
|
|
74
|
+
async function pathExists(target) {
|
|
75
|
+
try {
|
|
76
|
+
await util.access(target);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.pathExists = pathExists;
|
|
84
|
+
async function rimraf(target) {
|
|
85
|
+
if (isRootPath(target)) {
|
|
86
|
+
// refuse to recursively delete root
|
|
87
|
+
return Promise.reject(new Error(`Refuse to recursively delete root, path: "${target}"`));
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const stat = await util.lstat(target);
|
|
91
|
+
// Folder delete (recursive) - NOT for symbolic links though!
|
|
92
|
+
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
93
|
+
// Children
|
|
94
|
+
const children = await util.readdir(target);
|
|
95
|
+
await Promise.all(children.map(child => rimraf(path.join(target, child))));
|
|
96
|
+
// Folder
|
|
97
|
+
await util.rmdir(target);
|
|
98
|
+
}
|
|
99
|
+
// Single file delete
|
|
100
|
+
else {
|
|
101
|
+
// chmod as needed to allow for unlink
|
|
102
|
+
const mode = stat.mode;
|
|
103
|
+
if (!(mode & 128)) { // 128 === 0200
|
|
104
|
+
await util.chmod(target, mode | 128);
|
|
105
|
+
}
|
|
106
|
+
return util.unlink(target);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error.code !== "ENOENT") {
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.rimraf = rimraf;
|
|
116
|
+
async function mkdir(folder) {
|
|
117
|
+
try {
|
|
118
|
+
await util.mkdir(folder, 0o777);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// ENOENT: a parent folder does not exist yet or folder name is invalid.
|
|
122
|
+
if (error.code === "ENOENT") {
|
|
123
|
+
return Promise.reject(error);
|
|
124
|
+
}
|
|
125
|
+
// Any other error: check if folder exists and
|
|
126
|
+
// return normally in that case if its a folder
|
|
127
|
+
try {
|
|
128
|
+
const fileStat = await util.stat(folder);
|
|
129
|
+
if (!fileStat.isDirectory()) {
|
|
130
|
+
return Promise.reject(new Error(`"${folder}" exists and is not a directory.`));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (statError) {
|
|
134
|
+
throw error; // rethrow original error
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// "A"
|
|
139
|
+
const charA = 65;
|
|
140
|
+
// "Z"
|
|
141
|
+
const charZ = 90;
|
|
142
|
+
// "a"
|
|
143
|
+
const chara = 97;
|
|
144
|
+
// "z"
|
|
145
|
+
const charz = 122;
|
|
146
|
+
// ":"
|
|
147
|
+
const charColon = 58;
|
|
148
|
+
// "\"
|
|
149
|
+
const charWinSep = 92;
|
|
150
|
+
// "/"
|
|
151
|
+
const cahrUnixSep = 47;
|
|
152
|
+
function isDriveLetter(char0) {
|
|
153
|
+
return char0 >= charA && char0 <= charZ || char0 >= chara && char0 <= charz;
|
|
154
|
+
}
|
|
155
|
+
const winSep = "\\";
|
|
156
|
+
const unixSep = "/";
|
|
157
|
+
function isRootPath(target) {
|
|
158
|
+
if (!target) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (target === winSep ||
|
|
162
|
+
target === unixSep) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
if (process.platform === "win32") {
|
|
166
|
+
if (target.length > 3) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return isDriveLetter(target.charCodeAt(0))
|
|
170
|
+
&& target.charCodeAt(1) === charColon
|
|
171
|
+
&& (target.length === 2 || target.charCodeAt(2) === charWinSep || target.charCodeAt(2) === cahrUnixSep);
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
exports.isRootPath = isRootPath;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { IZipOptions } from "./zip";
|
|
2
|
-
import { IExtractOptions } from "./unzip";
|
|
3
|
-
export * from "./zip";
|
|
4
|
-
export * from "./unzip";
|
|
5
|
-
/**
|
|
6
|
-
* Compress a single file to zip.
|
|
7
|
-
* @param file
|
|
8
|
-
* @param zipFile the zip file path.
|
|
9
|
-
* @param options
|
|
10
|
-
*/
|
|
11
|
-
export declare function archiveFile(file: string, zipFile: string, options?: IZipOptions): Promise<void>;
|
|
12
|
-
/**
|
|
13
|
-
* Compress all the contents of the specified folder to zip.
|
|
14
|
-
* @param folder
|
|
15
|
-
* @param zipFile the zip file path.
|
|
16
|
-
* @param options
|
|
17
|
-
*/
|
|
18
|
-
export declare function archiveFolder(folder: string, zipFile: string, options?: IZipOptions): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Extract the zip file to the specified location.
|
|
21
|
-
* @param zipFile
|
|
22
|
-
* @param targetFolder
|
|
23
|
-
* @param options
|
|
24
|
-
*/
|
|
25
|
-
export declare function extract(zipFile: string, targetFolder: string, options?: IExtractOptions): Promise<void>;
|
|
1
|
+
import { IZipOptions } from "./zip";
|
|
2
|
+
import { IExtractOptions } from "./unzip";
|
|
3
|
+
export * from "./zip";
|
|
4
|
+
export * from "./unzip";
|
|
5
|
+
/**
|
|
6
|
+
* Compress a single file to zip.
|
|
7
|
+
* @param file
|
|
8
|
+
* @param zipFile the zip file path.
|
|
9
|
+
* @param options
|
|
10
|
+
*/
|
|
11
|
+
export declare function archiveFile(file: string, zipFile: string, options?: IZipOptions): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Compress all the contents of the specified folder to zip.
|
|
14
|
+
* @param folder
|
|
15
|
+
* @param zipFile the zip file path.
|
|
16
|
+
* @param options
|
|
17
|
+
*/
|
|
18
|
+
export declare function archiveFolder(folder: string, zipFile: string, options?: IZipOptions): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Extract the zip file to the specified location.
|
|
21
|
+
* @param zipFile
|
|
22
|
+
* @param targetFolder
|
|
23
|
+
* @param options
|
|
24
|
+
*/
|
|
25
|
+
export declare function extract(zipFile: string, targetFolder: string, options?: IExtractOptions): Promise<void>;
|