soda-nodejs 0.8.3 → 0.8.5
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/README.md +23 -23
- package/dist/utils/execAsync.cjs +1 -1
- package/dist/utils/execAsync.js +1 -1
- package/dist/utils/zip.cjs +5 -2
- package/dist/utils/zip.d.ts +5 -1
- package/dist/utils/zip.js +5 -2
- package/package.json +6 -6
- package/src/utils/compress.ts +37 -37
- package/src/utils/copy.ts +37 -37
- package/src/utils/execAsync.ts +36 -36
- package/src/utils/isPathLike.ts +5 -5
- package/src/utils/saveFile.ts +22 -22
- package/src/utils/saveResponse.ts +17 -17
- package/src/utils/spawnAsync.ts +156 -156
- package/src/utils/unzip.ts +34 -34
- package/src/utils/zip.ts +57 -52
- package/tsconfig.json +15 -15
package/README.md
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
# soda-node
|
|
2
|
-
|
|
3
|
-
[](https://npmjs.com/package/soda-node)
|
|
4
|
-
[](https://npmjs.com/package/soda-node)
|
|
5
|
-
|
|
6
|
-
## Install
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
$ pnpm install
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
```bash
|
|
13
|
-
$ npm run dev
|
|
14
|
-
$ npm run build
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Options
|
|
18
|
-
|
|
19
|
-
TODO
|
|
20
|
-
|
|
21
|
-
## LICENSE
|
|
22
|
-
|
|
23
|
-
MIT
|
|
1
|
+
# soda-node
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/soda-node)
|
|
4
|
+
[](https://npmjs.com/package/soda-node)
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ pnpm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
$ npm run dev
|
|
14
|
+
$ npm run build
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
TODO
|
|
20
|
+
|
|
21
|
+
## LICENSE
|
|
22
|
+
|
|
23
|
+
MIT
|
package/dist/utils/execAsync.cjs
CHANGED
|
@@ -39,7 +39,7 @@ const external_child_process_namespaceObject = require("child_process");
|
|
|
39
39
|
const external_iconv_lite_namespaceObject = require("iconv-lite");
|
|
40
40
|
var external_iconv_lite_default = /*#__PURE__*/ __webpack_require__.n(external_iconv_lite_namespaceObject);
|
|
41
41
|
async function execAsync(command, options) {
|
|
42
|
-
const decode = options
|
|
42
|
+
const decode = null == options ? void 0 : options.decode;
|
|
43
43
|
if ("object" == typeof options && null !== options && options.decode) {
|
|
44
44
|
const { decode, ...rest } = options;
|
|
45
45
|
options = rest;
|
package/dist/utils/execAsync.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { exec } from "child_process";
|
|
2
2
|
import iconv_lite from "iconv-lite";
|
|
3
3
|
async function execAsync(command, options) {
|
|
4
|
-
const decode = options
|
|
4
|
+
const decode = null == options ? void 0 : options.decode;
|
|
5
5
|
if ("object" == typeof options && null !== options && options.decode) {
|
|
6
6
|
const { decode, ...rest } = options;
|
|
7
7
|
options = rest;
|
package/dist/utils/zip.cjs
CHANGED
|
@@ -39,11 +39,14 @@ const external_os_namespaceObject = require("os");
|
|
|
39
39
|
const external_which_namespaceObject = require("which");
|
|
40
40
|
var external_which_default = /*#__PURE__*/ __webpack_require__.n(external_which_namespaceObject);
|
|
41
41
|
const external_execAsync_cjs_namespaceObject = require("./execAsync.cjs");
|
|
42
|
-
async function zip({ input, output, thread = "auto", level, password, cwd }) {
|
|
42
|
+
async function zip({ input, output, thread = "auto", level, password, cwd, filter }) {
|
|
43
43
|
await external_which_default()("7z");
|
|
44
|
+
filter = Array.isArray(filter) ? filter : "string" == typeof filter ? [
|
|
45
|
+
filter
|
|
46
|
+
] : [];
|
|
44
47
|
input = Array.isArray(input) ? input.join(" ") : input;
|
|
45
48
|
if ("max" === thread) thread = (0, external_os_namespaceObject.cpus)().length;
|
|
46
|
-
return await (0, external_execAsync_cjs_namespaceObject.execAsync)(`7z a ${output} ${input} -mmt=${"auto" === thread ? "on" : thread}${"number" == typeof level ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}`, {
|
|
49
|
+
return await (0, external_execAsync_cjs_namespaceObject.execAsync)(`7z a ${output} ${input} -mmt=${"auto" === thread ? "on" : thread}${"number" == typeof level ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}${filter.length > 0 ? ` ${filter.map((item)=>`-x!${item}`).join(" ")}` : ""}`, {
|
|
47
50
|
cwd
|
|
48
51
|
});
|
|
49
52
|
}
|
package/dist/utils/zip.d.ts
CHANGED
|
@@ -23,6 +23,10 @@ export type ZipOptions = {
|
|
|
23
23
|
* 压缩文件时的工作目录
|
|
24
24
|
*/
|
|
25
25
|
cwd?: string;
|
|
26
|
+
/**
|
|
27
|
+
* 过滤
|
|
28
|
+
*/
|
|
29
|
+
filter?: string | string[];
|
|
26
30
|
};
|
|
27
31
|
/**
|
|
28
32
|
* 使用 7z 命令压缩文件
|
|
@@ -35,4 +39,4 @@ export type ZipOptions = {
|
|
|
35
39
|
* 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
|
|
36
40
|
* 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
|
|
37
41
|
*/
|
|
38
|
-
export declare function zip({ input, output, thread, level, password, cwd }: ZipOptions): Promise<string>;
|
|
42
|
+
export declare function zip({ input, output, thread, level, password, cwd, filter }: ZipOptions): Promise<string>;
|
package/dist/utils/zip.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { cpus } from "os";
|
|
2
2
|
import which from "which";
|
|
3
3
|
import { execAsync } from "./execAsync.js";
|
|
4
|
-
async function zip({ input, output, thread = "auto", level, password, cwd }) {
|
|
4
|
+
async function zip({ input, output, thread = "auto", level, password, cwd, filter }) {
|
|
5
5
|
await which("7z");
|
|
6
|
+
filter = Array.isArray(filter) ? filter : "string" == typeof filter ? [
|
|
7
|
+
filter
|
|
8
|
+
] : [];
|
|
6
9
|
input = Array.isArray(input) ? input.join(" ") : input;
|
|
7
10
|
if ("max" === thread) thread = cpus().length;
|
|
8
|
-
return await execAsync(`7z a ${output} ${input} -mmt=${"auto" === thread ? "on" : thread}${"number" == typeof level ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}`, {
|
|
11
|
+
return await execAsync(`7z a ${output} ${input} -mmt=${"auto" === thread ? "on" : thread}${"number" == typeof level ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}${filter.length > 0 ? ` ${filter.map((item)=>`-x!${item}`).join(" ")}` : ""}`, {
|
|
9
12
|
cwd
|
|
10
13
|
});
|
|
11
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soda-nodejs",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -35,20 +35,20 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/1adybug/deepsea/tree/main/packages/soda-nodejs",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"iconv-lite": "^0.
|
|
39
|
-
"tar": "^7.
|
|
38
|
+
"iconv-lite": "^0.7.0",
|
|
39
|
+
"tar": "^7.5.1",
|
|
40
40
|
"which": "^5.0.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^
|
|
43
|
+
"@types/node": "^24.7.0",
|
|
44
44
|
"@types/which": "^3.0.4",
|
|
45
|
-
"typescript": "^5.9.
|
|
45
|
+
"typescript": "^5.9.3"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"dev": "rslib build --watch",
|
|
50
50
|
"build": "rslib build",
|
|
51
|
-
"prebuild": "
|
|
51
|
+
"prebuild": "bun scripts/export.ts",
|
|
52
52
|
"lint": "prettier --write ."
|
|
53
53
|
}
|
|
54
54
|
}
|
package/src/utils/compress.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { existsSync, mkdirSync } from "fs"
|
|
2
|
-
import { basename, dirname, resolve } from "path"
|
|
3
|
-
import { create } from "tar"
|
|
4
|
-
|
|
5
|
-
export interface CompressionParams {
|
|
6
|
-
/**
|
|
7
|
-
* 需要压缩的文件夹
|
|
8
|
-
*/
|
|
9
|
-
input: string
|
|
10
|
-
/**
|
|
11
|
-
* 压缩后的文件路径
|
|
12
|
-
*/
|
|
13
|
-
output: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function compress({ input, output }: CompressionParams) {
|
|
17
|
-
input = resolve(input)
|
|
18
|
-
|
|
19
|
-
// 确保源路径存在
|
|
20
|
-
if (!existsSync(input)) throw new Error("Source folder does not exist")
|
|
21
|
-
|
|
22
|
-
// 确保输出目录存在
|
|
23
|
-
const outputDir = dirname(output)
|
|
24
|
-
if (!existsSync(outputDir)) mkdirSync(outputDir, { recursive: true })
|
|
25
|
-
|
|
26
|
-
// 执行压缩
|
|
27
|
-
await create(
|
|
28
|
-
{
|
|
29
|
-
gzip: true, // 使用 gzip 压缩
|
|
30
|
-
file: output, // 输出文件路径
|
|
31
|
-
cwd: dirname(input), // 设置工作目录
|
|
32
|
-
},
|
|
33
|
-
[basename(input)], // 要压缩的文件夹名
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
return resolve(output)
|
|
37
|
-
}
|
|
1
|
+
import { existsSync, mkdirSync } from "fs"
|
|
2
|
+
import { basename, dirname, resolve } from "path"
|
|
3
|
+
import { create } from "tar"
|
|
4
|
+
|
|
5
|
+
export interface CompressionParams {
|
|
6
|
+
/**
|
|
7
|
+
* 需要压缩的文件夹
|
|
8
|
+
*/
|
|
9
|
+
input: string
|
|
10
|
+
/**
|
|
11
|
+
* 压缩后的文件路径
|
|
12
|
+
*/
|
|
13
|
+
output: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function compress({ input, output }: CompressionParams) {
|
|
17
|
+
input = resolve(input)
|
|
18
|
+
|
|
19
|
+
// 确保源路径存在
|
|
20
|
+
if (!existsSync(input)) throw new Error("Source folder does not exist")
|
|
21
|
+
|
|
22
|
+
// 确保输出目录存在
|
|
23
|
+
const outputDir = dirname(output)
|
|
24
|
+
if (!existsSync(outputDir)) mkdirSync(outputDir, { recursive: true })
|
|
25
|
+
|
|
26
|
+
// 执行压缩
|
|
27
|
+
await create(
|
|
28
|
+
{
|
|
29
|
+
gzip: true, // 使用 gzip 压缩
|
|
30
|
+
file: output, // 输出文件路径
|
|
31
|
+
cwd: dirname(input), // 设置工作目录
|
|
32
|
+
},
|
|
33
|
+
[basename(input)], // 要压缩的文件夹名
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
return resolve(output)
|
|
37
|
+
}
|
package/src/utils/copy.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { existsSync } from "fs"
|
|
2
|
-
import { copyFile, mkdir, readdir, stat } from "fs/promises"
|
|
3
|
-
import { join, parse } from "path"
|
|
4
|
-
|
|
5
|
-
export type CopyOptions = {
|
|
6
|
-
/** 源文件夹 */
|
|
7
|
-
input: string | string[]
|
|
8
|
-
/** 存放的文件夹 */
|
|
9
|
-
output: string
|
|
10
|
-
/** 文件权限 */
|
|
11
|
-
mode?: number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export async function copy({ input, output, mode }: CopyOptions) {
|
|
15
|
-
if (Array.isArray(input)) {
|
|
16
|
-
for (const item of input) await copy({ input: item, output, mode })
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
if (!existsSync(output)) await mkdir(output, { recursive: true })
|
|
20
|
-
const status = await stat(input)
|
|
21
|
-
const { base } = parse(input)
|
|
22
|
-
if (status.isFile()) {
|
|
23
|
-
await copyFile(input, join(output, base), mode)
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
if (status.isDirectory()) {
|
|
27
|
-
await mkdir(join(output, base), { recursive: true })
|
|
28
|
-
const entries = await readdir(input, { withFileTypes: true })
|
|
29
|
-
for (const entry of entries) {
|
|
30
|
-
await copy({
|
|
31
|
-
input: join(input, entry.name),
|
|
32
|
-
output: join(output, base),
|
|
33
|
-
mode,
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
import { existsSync } from "fs"
|
|
2
|
+
import { copyFile, mkdir, readdir, stat } from "fs/promises"
|
|
3
|
+
import { join, parse } from "path"
|
|
4
|
+
|
|
5
|
+
export type CopyOptions = {
|
|
6
|
+
/** 源文件夹 */
|
|
7
|
+
input: string | string[]
|
|
8
|
+
/** 存放的文件夹 */
|
|
9
|
+
output: string
|
|
10
|
+
/** 文件权限 */
|
|
11
|
+
mode?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function copy({ input, output, mode }: CopyOptions) {
|
|
15
|
+
if (Array.isArray(input)) {
|
|
16
|
+
for (const item of input) await copy({ input: item, output, mode })
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
if (!existsSync(output)) await mkdir(output, { recursive: true })
|
|
20
|
+
const status = await stat(input)
|
|
21
|
+
const { base } = parse(input)
|
|
22
|
+
if (status.isFile()) {
|
|
23
|
+
await copyFile(input, join(output, base), mode)
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
if (status.isDirectory()) {
|
|
27
|
+
await mkdir(join(output, base), { recursive: true })
|
|
28
|
+
const entries = await readdir(input, { withFileTypes: true })
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
await copy({
|
|
31
|
+
input: join(input, entry.name),
|
|
32
|
+
output: join(output, base),
|
|
33
|
+
mode,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/utils/execAsync.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import { ExecOptions, exec } from "child_process"
|
|
2
|
-
import { ObjectEncodingOptions } from "fs"
|
|
3
|
-
import iconv, { Options } from "iconv-lite"
|
|
4
|
-
|
|
5
|
-
export type IconvDecodeOptions = {
|
|
6
|
-
encoding: string
|
|
7
|
-
options?: Options
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export async function execAsync(command: string): Promise<string>
|
|
11
|
-
export async function execAsync(command: string, options: { encoding: "buffer" | null } & ExecOptions): Promise<Buffer>
|
|
12
|
-
export async function execAsync(command: string, options: { encoding: "buffer" | null } & ExecOptions & { decode: IconvDecodeOptions }): Promise<string>
|
|
13
|
-
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<string>
|
|
14
|
-
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<string | Buffer>
|
|
15
|
-
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions & { decode: IconvDecodeOptions }): Promise<string>
|
|
16
|
-
export async function execAsync(command: string, options: ExecOptions): Promise<string>
|
|
17
|
-
export async function execAsync(command: string, options: (ObjectEncodingOptions & ExecOptions) | undefined | null): Promise<string | Buffer>
|
|
18
|
-
export async function execAsync(
|
|
19
|
-
command: string,
|
|
20
|
-
options: (ObjectEncodingOptions & ExecOptions & { decode: IconvDecodeOptions }) | undefined | null,
|
|
21
|
-
): Promise<string>
|
|
22
|
-
export async function execAsync(command: string, options?: any) {
|
|
23
|
-
const decode = options?.decode as IconvDecodeOptions | undefined
|
|
24
|
-
if (typeof options === "object" && options !== null && options.decode) {
|
|
25
|
-
const { decode, ...rest } = options as { decode: IconvDecodeOptions }
|
|
26
|
-
options = rest
|
|
27
|
-
}
|
|
28
|
-
return await new Promise<string | Buffer>((resolve, reject) => {
|
|
29
|
-
exec(command, options, (error, stdout, stderr) => {
|
|
30
|
-
if (error) return reject(error)
|
|
31
|
-
// if (stderr) console.warn(stderr)
|
|
32
|
-
if (decode && stdout instanceof Buffer) return resolve(iconv.decode(stdout, decode.encoding, decode.options))
|
|
33
|
-
resolve(stdout)
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
}
|
|
1
|
+
import { ExecOptions, exec } from "child_process"
|
|
2
|
+
import { ObjectEncodingOptions } from "fs"
|
|
3
|
+
import iconv, { Options } from "iconv-lite"
|
|
4
|
+
|
|
5
|
+
export type IconvDecodeOptions = {
|
|
6
|
+
encoding: string
|
|
7
|
+
options?: Options
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function execAsync(command: string): Promise<string>
|
|
11
|
+
export async function execAsync(command: string, options: { encoding: "buffer" | null } & ExecOptions): Promise<Buffer>
|
|
12
|
+
export async function execAsync(command: string, options: { encoding: "buffer" | null } & ExecOptions & { decode: IconvDecodeOptions }): Promise<string>
|
|
13
|
+
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<string>
|
|
14
|
+
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<string | Buffer>
|
|
15
|
+
export async function execAsync(command: string, options: { encoding: BufferEncoding } & ExecOptions & { decode: IconvDecodeOptions }): Promise<string>
|
|
16
|
+
export async function execAsync(command: string, options: ExecOptions): Promise<string>
|
|
17
|
+
export async function execAsync(command: string, options: (ObjectEncodingOptions & ExecOptions) | undefined | null): Promise<string | Buffer>
|
|
18
|
+
export async function execAsync(
|
|
19
|
+
command: string,
|
|
20
|
+
options: (ObjectEncodingOptions & ExecOptions & { decode: IconvDecodeOptions }) | undefined | null,
|
|
21
|
+
): Promise<string>
|
|
22
|
+
export async function execAsync(command: string, options?: any) {
|
|
23
|
+
const decode = options?.decode as IconvDecodeOptions | undefined
|
|
24
|
+
if (typeof options === "object" && options !== null && options.decode) {
|
|
25
|
+
const { decode, ...rest } = options as { decode: IconvDecodeOptions }
|
|
26
|
+
options = rest
|
|
27
|
+
}
|
|
28
|
+
return await new Promise<string | Buffer>((resolve, reject) => {
|
|
29
|
+
exec(command, options, (error, stdout, stderr) => {
|
|
30
|
+
if (error) return reject(error)
|
|
31
|
+
// if (stderr) console.warn(stderr)
|
|
32
|
+
if (decode && stdout instanceof Buffer) return resolve(iconv.decode(stdout, decode.encoding, decode.options))
|
|
33
|
+
resolve(stdout)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
}
|
package/src/utils/isPathLike.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PathLike } from "fs"
|
|
2
|
-
|
|
3
|
-
export function isPathLike(path: unknown): path is PathLike {
|
|
4
|
-
return typeof path === "string" || path instanceof Buffer || path instanceof URL
|
|
5
|
-
}
|
|
1
|
+
import { PathLike } from "fs"
|
|
2
|
+
|
|
3
|
+
export function isPathLike(path: unknown): path is PathLike {
|
|
4
|
+
return typeof path === "string" || path instanceof Buffer || path instanceof URL
|
|
5
|
+
}
|
package/src/utils/saveFile.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { createWriteStream } from "fs"
|
|
2
|
-
import { Readable } from "stream"
|
|
3
|
-
|
|
4
|
-
export type SaveFileOptions = {
|
|
5
|
-
/** 要保存的文件 */
|
|
6
|
-
input: File
|
|
7
|
-
/** 保存文件的目标位置 */
|
|
8
|
-
output: string
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 保存文件
|
|
13
|
-
*/
|
|
14
|
-
export async function saveFile({ input, output }: SaveFileOptions) {
|
|
15
|
-
await new Promise<void>((resolve, reject) => {
|
|
16
|
-
const writeAble = createWriteStream(output)
|
|
17
|
-
Readable.fromWeb(input.stream() as any)
|
|
18
|
-
.pipe(writeAble)
|
|
19
|
-
.on("finish", resolve)
|
|
20
|
-
.on("error", reject)
|
|
21
|
-
})
|
|
22
|
-
}
|
|
1
|
+
import { createWriteStream } from "fs"
|
|
2
|
+
import { Readable } from "stream"
|
|
3
|
+
|
|
4
|
+
export type SaveFileOptions = {
|
|
5
|
+
/** 要保存的文件 */
|
|
6
|
+
input: File
|
|
7
|
+
/** 保存文件的目标位置 */
|
|
8
|
+
output: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 保存文件
|
|
13
|
+
*/
|
|
14
|
+
export async function saveFile({ input, output }: SaveFileOptions) {
|
|
15
|
+
await new Promise<void>((resolve, reject) => {
|
|
16
|
+
const writeAble = createWriteStream(output)
|
|
17
|
+
Readable.fromWeb(input.stream() as any)
|
|
18
|
+
.pipe(writeAble)
|
|
19
|
+
.on("finish", resolve)
|
|
20
|
+
.on("error", reject)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { createWriteStream } from "fs"
|
|
2
|
-
import { Readable } from "stream"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 从 Response 对象中下载文件
|
|
6
|
-
* @param response Response 对象
|
|
7
|
-
* @param file 文件路径
|
|
8
|
-
*/
|
|
9
|
-
export async function saveResponse(response: Response, file: string) {
|
|
10
|
-
const writeable = createWriteStream(file)
|
|
11
|
-
await new Promise<void>((resolve, reject) =>
|
|
12
|
-
Readable.fromWeb(response.body! as any)
|
|
13
|
-
.pipe(writeable)
|
|
14
|
-
.on("close", resolve)
|
|
15
|
-
.on("error", reject),
|
|
16
|
-
)
|
|
17
|
-
}
|
|
1
|
+
import { createWriteStream } from "fs"
|
|
2
|
+
import { Readable } from "stream"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 从 Response 对象中下载文件
|
|
6
|
+
* @param response Response 对象
|
|
7
|
+
* @param file 文件路径
|
|
8
|
+
*/
|
|
9
|
+
export async function saveResponse(response: Response, file: string) {
|
|
10
|
+
const writeable = createWriteStream(file)
|
|
11
|
+
await new Promise<void>((resolve, reject) =>
|
|
12
|
+
Readable.fromWeb(response.body! as any)
|
|
13
|
+
.pipe(writeable)
|
|
14
|
+
.on("close", resolve)
|
|
15
|
+
.on("error", reject),
|
|
16
|
+
)
|
|
17
|
+
}
|
package/src/utils/spawnAsync.ts
CHANGED
|
@@ -1,156 +1,156 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChildProcess,
|
|
3
|
-
ChildProcessByStdio,
|
|
4
|
-
ChildProcessWithoutNullStreams,
|
|
5
|
-
SpawnOptions,
|
|
6
|
-
SpawnOptionsWithStdioTuple,
|
|
7
|
-
SpawnOptionsWithoutStdio,
|
|
8
|
-
StdioNull,
|
|
9
|
-
StdioPipe,
|
|
10
|
-
spawn,
|
|
11
|
-
} from "child_process"
|
|
12
|
-
import { Readable, Writable } from "stream"
|
|
13
|
-
|
|
14
|
-
export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
15
|
-
child: T
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type Options =
|
|
19
|
-
| SpawnOptionsWithoutStdio
|
|
20
|
-
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>
|
|
21
|
-
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>
|
|
22
|
-
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>
|
|
23
|
-
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>
|
|
24
|
-
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>
|
|
25
|
-
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>
|
|
26
|
-
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>
|
|
27
|
-
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>
|
|
28
|
-
| SpawnOptions
|
|
29
|
-
|
|
30
|
-
let defaultOptions: Options = {}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @description get default options for spawnAsync
|
|
34
|
-
* @returns {Options} options
|
|
35
|
-
*/
|
|
36
|
-
export function getDefaultOptions(): Options {
|
|
37
|
-
return defaultOptions
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @description set default options for spawnAsync
|
|
42
|
-
* @param {Options | ((prev: Options) => Options)} options
|
|
43
|
-
*/
|
|
44
|
-
export function setDefaultOptions(options: Options | ((prev: Options) => Options)) {
|
|
45
|
-
if (typeof options === "function") {
|
|
46
|
-
defaultOptions = options(defaultOptions)
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
defaultOptions = options
|
|
50
|
-
return defaultOptions
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>
|
|
54
|
-
export function spawnAsync(
|
|
55
|
-
command: string,
|
|
56
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
|
|
57
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>
|
|
58
|
-
export function spawnAsync(
|
|
59
|
-
command: string,
|
|
60
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
|
|
61
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>
|
|
62
|
-
export function spawnAsync(
|
|
63
|
-
command: string,
|
|
64
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
|
|
65
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>
|
|
66
|
-
export function spawnAsync(
|
|
67
|
-
command: string,
|
|
68
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
|
|
69
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>
|
|
70
|
-
export function spawnAsync(
|
|
71
|
-
command: string,
|
|
72
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
|
|
73
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>
|
|
74
|
-
export function spawnAsync(
|
|
75
|
-
command: string,
|
|
76
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
|
|
77
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>
|
|
78
|
-
export function spawnAsync(
|
|
79
|
-
command: string,
|
|
80
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
|
|
81
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>
|
|
82
|
-
export function spawnAsync(
|
|
83
|
-
command: string,
|
|
84
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
|
|
85
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>
|
|
86
|
-
export function spawnAsync(command: string, options: SpawnOptions): PromiseWithChildProcess<ChildProcess>
|
|
87
|
-
export function spawnAsync(
|
|
88
|
-
command: string,
|
|
89
|
-
args?: readonly string[],
|
|
90
|
-
options?: SpawnOptionsWithoutStdio,
|
|
91
|
-
): PromiseWithChildProcess<ChildProcessWithoutNullStreams>
|
|
92
|
-
export function spawnAsync(
|
|
93
|
-
command: string,
|
|
94
|
-
args: readonly string[],
|
|
95
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
|
|
96
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>
|
|
97
|
-
export function spawnAsync(
|
|
98
|
-
command: string,
|
|
99
|
-
args: readonly string[],
|
|
100
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
|
|
101
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>
|
|
102
|
-
export function spawnAsync(
|
|
103
|
-
command: string,
|
|
104
|
-
args: readonly string[],
|
|
105
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
|
|
106
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>
|
|
107
|
-
export function spawnAsync(
|
|
108
|
-
command: string,
|
|
109
|
-
args: readonly string[],
|
|
110
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
|
|
111
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>
|
|
112
|
-
export function spawnAsync(
|
|
113
|
-
command: string,
|
|
114
|
-
args: readonly string[],
|
|
115
|
-
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
|
|
116
|
-
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>
|
|
117
|
-
export function spawnAsync(
|
|
118
|
-
command: string,
|
|
119
|
-
args: readonly string[],
|
|
120
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
|
|
121
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>
|
|
122
|
-
export function spawnAsync(
|
|
123
|
-
command: string,
|
|
124
|
-
args: readonly string[],
|
|
125
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
|
|
126
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>
|
|
127
|
-
export function spawnAsync(
|
|
128
|
-
command: string,
|
|
129
|
-
args: readonly string[],
|
|
130
|
-
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
|
|
131
|
-
): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>
|
|
132
|
-
export function spawnAsync(command: string, args: readonly string[], options: SpawnOptions): PromiseWithChildProcess<ChildProcess>
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* @description wait for the command to exit
|
|
136
|
-
* @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.
|
|
137
|
-
*/
|
|
138
|
-
export function spawnAsync(command: string, args?: any, options?: any): Promise<any> {
|
|
139
|
-
let child: any
|
|
140
|
-
const promise = new Promise<any>((resolve, reject) => {
|
|
141
|
-
if (Array.isArray(args)) options = { ...defaultOptions, ...options }
|
|
142
|
-
else args = { ...defaultOptions, ...args }
|
|
143
|
-
child = spawn(command, args, options)
|
|
144
|
-
child.on("exit", (code: number) => {
|
|
145
|
-
if (code === 0) return resolve(child)
|
|
146
|
-
if (Array.isArray(args)) {
|
|
147
|
-
const args2 = args.map((item: string) => item.trim()).filter(Boolean)
|
|
148
|
-
if (args2.length > 0) command = `${command} ${args2.join(" ")}`
|
|
149
|
-
}
|
|
150
|
-
reject(new Error(`spawn "${command}" failed with code ${code}`))
|
|
151
|
-
return
|
|
152
|
-
})
|
|
153
|
-
}) as PromiseWithChildProcess<any>
|
|
154
|
-
promise.child = child
|
|
155
|
-
return promise
|
|
156
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ChildProcess,
|
|
3
|
+
ChildProcessByStdio,
|
|
4
|
+
ChildProcessWithoutNullStreams,
|
|
5
|
+
SpawnOptions,
|
|
6
|
+
SpawnOptionsWithStdioTuple,
|
|
7
|
+
SpawnOptionsWithoutStdio,
|
|
8
|
+
StdioNull,
|
|
9
|
+
StdioPipe,
|
|
10
|
+
spawn,
|
|
11
|
+
} from "child_process"
|
|
12
|
+
import { Readable, Writable } from "stream"
|
|
13
|
+
|
|
14
|
+
export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
15
|
+
child: T
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type Options =
|
|
19
|
+
| SpawnOptionsWithoutStdio
|
|
20
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>
|
|
21
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>
|
|
22
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>
|
|
23
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>
|
|
24
|
+
| SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>
|
|
25
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>
|
|
26
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>
|
|
27
|
+
| SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>
|
|
28
|
+
| SpawnOptions
|
|
29
|
+
|
|
30
|
+
let defaultOptions: Options = {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @description get default options for spawnAsync
|
|
34
|
+
* @returns {Options} options
|
|
35
|
+
*/
|
|
36
|
+
export function getDefaultOptions(): Options {
|
|
37
|
+
return defaultOptions
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description set default options for spawnAsync
|
|
42
|
+
* @param {Options | ((prev: Options) => Options)} options
|
|
43
|
+
*/
|
|
44
|
+
export function setDefaultOptions(options: Options | ((prev: Options) => Options)) {
|
|
45
|
+
if (typeof options === "function") {
|
|
46
|
+
defaultOptions = options(defaultOptions)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
defaultOptions = options
|
|
50
|
+
return defaultOptions
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function spawnAsync(command: string, options?: SpawnOptionsWithoutStdio): PromiseWithChildProcess<ChildProcessWithoutNullStreams>
|
|
54
|
+
export function spawnAsync(
|
|
55
|
+
command: string,
|
|
56
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
|
|
57
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>
|
|
58
|
+
export function spawnAsync(
|
|
59
|
+
command: string,
|
|
60
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
|
|
61
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>
|
|
62
|
+
export function spawnAsync(
|
|
63
|
+
command: string,
|
|
64
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
|
|
65
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>
|
|
66
|
+
export function spawnAsync(
|
|
67
|
+
command: string,
|
|
68
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
|
|
69
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>
|
|
70
|
+
export function spawnAsync(
|
|
71
|
+
command: string,
|
|
72
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
|
|
73
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>
|
|
74
|
+
export function spawnAsync(
|
|
75
|
+
command: string,
|
|
76
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
|
|
77
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>
|
|
78
|
+
export function spawnAsync(
|
|
79
|
+
command: string,
|
|
80
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
|
|
81
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>
|
|
82
|
+
export function spawnAsync(
|
|
83
|
+
command: string,
|
|
84
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
|
|
85
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>
|
|
86
|
+
export function spawnAsync(command: string, options: SpawnOptions): PromiseWithChildProcess<ChildProcess>
|
|
87
|
+
export function spawnAsync(
|
|
88
|
+
command: string,
|
|
89
|
+
args?: readonly string[],
|
|
90
|
+
options?: SpawnOptionsWithoutStdio,
|
|
91
|
+
): PromiseWithChildProcess<ChildProcessWithoutNullStreams>
|
|
92
|
+
export function spawnAsync(
|
|
93
|
+
command: string,
|
|
94
|
+
args: readonly string[],
|
|
95
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
|
|
96
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, Readable>>
|
|
97
|
+
export function spawnAsync(
|
|
98
|
+
command: string,
|
|
99
|
+
args: readonly string[],
|
|
100
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
|
|
101
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, Readable, null>>
|
|
102
|
+
export function spawnAsync(
|
|
103
|
+
command: string,
|
|
104
|
+
args: readonly string[],
|
|
105
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
|
|
106
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, Readable>>
|
|
107
|
+
export function spawnAsync(
|
|
108
|
+
command: string,
|
|
109
|
+
args: readonly string[],
|
|
110
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
|
|
111
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, Readable>>
|
|
112
|
+
export function spawnAsync(
|
|
113
|
+
command: string,
|
|
114
|
+
args: readonly string[],
|
|
115
|
+
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
|
|
116
|
+
): PromiseWithChildProcess<ChildProcessByStdio<Writable, null, null>>
|
|
117
|
+
export function spawnAsync(
|
|
118
|
+
command: string,
|
|
119
|
+
args: readonly string[],
|
|
120
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
|
|
121
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, Readable, null>>
|
|
122
|
+
export function spawnAsync(
|
|
123
|
+
command: string,
|
|
124
|
+
args: readonly string[],
|
|
125
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
|
|
126
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, null, Readable>>
|
|
127
|
+
export function spawnAsync(
|
|
128
|
+
command: string,
|
|
129
|
+
args: readonly string[],
|
|
130
|
+
options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
|
|
131
|
+
): PromiseWithChildProcess<ChildProcessByStdio<null, null, null>>
|
|
132
|
+
export function spawnAsync(command: string, args: readonly string[], options: SpawnOptions): PromiseWithChildProcess<ChildProcess>
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @description wait for the command to exit
|
|
136
|
+
* @returns a promise that resolves with the child process when the command exits successfully. Also, the child process is attached to the promise as a property.
|
|
137
|
+
*/
|
|
138
|
+
export function spawnAsync(command: string, args?: any, options?: any): Promise<any> {
|
|
139
|
+
let child: any
|
|
140
|
+
const promise = new Promise<any>((resolve, reject) => {
|
|
141
|
+
if (Array.isArray(args)) options = { ...defaultOptions, ...options }
|
|
142
|
+
else args = { ...defaultOptions, ...args }
|
|
143
|
+
child = spawn(command, args, options)
|
|
144
|
+
child.on("exit", (code: number) => {
|
|
145
|
+
if (code === 0) return resolve(child)
|
|
146
|
+
if (Array.isArray(args)) {
|
|
147
|
+
const args2 = args.map((item: string) => item.trim()).filter(Boolean)
|
|
148
|
+
if (args2.length > 0) command = `${command} ${args2.join(" ")}`
|
|
149
|
+
}
|
|
150
|
+
reject(new Error(`spawn "${command}" failed with code ${code}`))
|
|
151
|
+
return
|
|
152
|
+
})
|
|
153
|
+
}) as PromiseWithChildProcess<any>
|
|
154
|
+
promise.child = child
|
|
155
|
+
return promise
|
|
156
|
+
}
|
package/src/utils/unzip.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import which from "which"
|
|
2
|
-
|
|
3
|
-
import { execAsync } from "./execAsync"
|
|
4
|
-
|
|
5
|
-
export type UnzipOptions = {
|
|
6
|
-
/**
|
|
7
|
-
* 要解压的文件
|
|
8
|
-
*/
|
|
9
|
-
input: string
|
|
10
|
-
/**
|
|
11
|
-
* 解压到的目标文件夹位置
|
|
12
|
-
*/
|
|
13
|
-
output: string
|
|
14
|
-
/**
|
|
15
|
-
* 压缩文件时的工作目录
|
|
16
|
-
*/
|
|
17
|
-
cwd?: string
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 使用 7z 命令解压文件
|
|
22
|
-
* - 如果没有安装 7z,请先安装 7z 后再执行
|
|
23
|
-
* - 下载地址:https://www.7-zip.org/download.html
|
|
24
|
-
* - 如果已经安装,请按照以下步骤将 7z 添加到环境变量中
|
|
25
|
-
* 1. 设置 → 系统 → 右侧系统信息 → 高级系统设置 → 环境变量
|
|
26
|
-
* 2. 在系统变量中找到并选中 Path,点击编辑
|
|
27
|
-
* 3. 点击新建,输入 7z 的安装路径(默认是 C:\Program Files\7-Zip),点击确定
|
|
28
|
-
* 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
|
|
29
|
-
* 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
|
|
30
|
-
*/
|
|
31
|
-
export async function unzip({ input, output, cwd }: UnzipOptions) {
|
|
32
|
-
await which("7z")
|
|
33
|
-
return await execAsync(`7z x ${input} -o${output}`, { cwd })
|
|
34
|
-
}
|
|
1
|
+
import which from "which"
|
|
2
|
+
|
|
3
|
+
import { execAsync } from "./execAsync"
|
|
4
|
+
|
|
5
|
+
export type UnzipOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* 要解压的文件
|
|
8
|
+
*/
|
|
9
|
+
input: string
|
|
10
|
+
/**
|
|
11
|
+
* 解压到的目标文件夹位置
|
|
12
|
+
*/
|
|
13
|
+
output: string
|
|
14
|
+
/**
|
|
15
|
+
* 压缩文件时的工作目录
|
|
16
|
+
*/
|
|
17
|
+
cwd?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 使用 7z 命令解压文件
|
|
22
|
+
* - 如果没有安装 7z,请先安装 7z 后再执行
|
|
23
|
+
* - 下载地址:https://www.7-zip.org/download.html
|
|
24
|
+
* - 如果已经安装,请按照以下步骤将 7z 添加到环境变量中
|
|
25
|
+
* 1. 设置 → 系统 → 右侧系统信息 → 高级系统设置 → 环境变量
|
|
26
|
+
* 2. 在系统变量中找到并选中 Path,点击编辑
|
|
27
|
+
* 3. 点击新建,输入 7z 的安装路径(默认是 C:\Program Files\7-Zip),点击确定
|
|
28
|
+
* 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
|
|
29
|
+
* 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
|
|
30
|
+
*/
|
|
31
|
+
export async function unzip({ input, output, cwd }: UnzipOptions) {
|
|
32
|
+
await which("7z")
|
|
33
|
+
return await execAsync(`7z x ${input} -o${output}`, { cwd })
|
|
34
|
+
}
|
package/src/utils/zip.ts
CHANGED
|
@@ -1,52 +1,57 @@
|
|
|
1
|
-
import { cpus } from "os"
|
|
2
|
-
import which from "which"
|
|
3
|
-
|
|
4
|
-
import { execAsync } from "./execAsync"
|
|
5
|
-
|
|
6
|
-
export type ZipOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* 要压缩的文件
|
|
9
|
-
*/
|
|
10
|
-
input: string | string[]
|
|
11
|
-
/**
|
|
12
|
-
* 压缩到的目标位置,文件名的后缀就是压缩格式,例如:.zip、.7z
|
|
13
|
-
*/
|
|
14
|
-
output: string
|
|
15
|
-
/**
|
|
16
|
-
* 线程数
|
|
17
|
-
*/
|
|
18
|
-
thread?: number | "auto" | "max"
|
|
19
|
-
/**
|
|
20
|
-
* 压缩等级,0-9
|
|
21
|
-
*/
|
|
22
|
-
level?: number
|
|
23
|
-
/**
|
|
24
|
-
* 是否加密
|
|
25
|
-
*/
|
|
26
|
-
password?: string
|
|
27
|
-
/**
|
|
28
|
-
* 压缩文件时的工作目录
|
|
29
|
-
*/
|
|
30
|
-
cwd?: string
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
)
|
|
52
|
-
|
|
1
|
+
import { cpus } from "os"
|
|
2
|
+
import which from "which"
|
|
3
|
+
|
|
4
|
+
import { execAsync } from "./execAsync"
|
|
5
|
+
|
|
6
|
+
export type ZipOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* 要压缩的文件
|
|
9
|
+
*/
|
|
10
|
+
input: string | string[]
|
|
11
|
+
/**
|
|
12
|
+
* 压缩到的目标位置,文件名的后缀就是压缩格式,例如:.zip、.7z
|
|
13
|
+
*/
|
|
14
|
+
output: string
|
|
15
|
+
/**
|
|
16
|
+
* 线程数
|
|
17
|
+
*/
|
|
18
|
+
thread?: number | "auto" | "max"
|
|
19
|
+
/**
|
|
20
|
+
* 压缩等级,0-9
|
|
21
|
+
*/
|
|
22
|
+
level?: number
|
|
23
|
+
/**
|
|
24
|
+
* 是否加密
|
|
25
|
+
*/
|
|
26
|
+
password?: string
|
|
27
|
+
/**
|
|
28
|
+
* 压缩文件时的工作目录
|
|
29
|
+
*/
|
|
30
|
+
cwd?: string
|
|
31
|
+
/**
|
|
32
|
+
* 过滤
|
|
33
|
+
*/
|
|
34
|
+
filter?: string | string[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 使用 7z 命令压缩文件
|
|
39
|
+
* - 如果没有安装 7z,请先安装 7z 后再执行
|
|
40
|
+
* - 下载地址:https://www.7-zip.org/download.html
|
|
41
|
+
* - 如果已经安装,请按照以下步骤将 7z 添加到环境变量中
|
|
42
|
+
* 1. 设置 → 系统 → 右侧系统信息 → 高级系统设置 → 环境变量
|
|
43
|
+
* 2. 在系统变量中找到并选中 Path,点击编辑
|
|
44
|
+
* 3. 点击新建,输入 7z 的安装路径(默认是 C:\Program Files\7-Zip),点击确定
|
|
45
|
+
* 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
|
|
46
|
+
* 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
|
|
47
|
+
*/
|
|
48
|
+
export async function zip({ input, output, thread = "auto", level, password, cwd, filter }: ZipOptions) {
|
|
49
|
+
await which("7z")
|
|
50
|
+
filter = Array.isArray(filter) ? filter : typeof filter === "string" ? [filter] : []
|
|
51
|
+
input = Array.isArray(input) ? input.join(" ") : input
|
|
52
|
+
if (thread === "max") thread = cpus().length
|
|
53
|
+
return await execAsync(
|
|
54
|
+
`7z a ${output} ${input} -mmt=${thread === "auto" ? "on" : thread}${typeof level === "number" ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}${filter.length > 0 ? ` ${filter.map(item => `-x!${item}`).join(" ")}` : ""}`,
|
|
55
|
+
{ cwd },
|
|
56
|
+
)
|
|
57
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"baseUrl": ".",
|
|
4
|
-
"strict": true,
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"skipLibCheck": true,
|
|
7
|
-
"target": "ESNext",
|
|
8
|
-
"module": "ESNext",
|
|
9
|
-
"moduleResolution": "Bundler",
|
|
10
|
-
"paths": {
|
|
11
|
-
"@/*": ["src/*"]
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"include": ["src"]
|
|
15
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".",
|
|
4
|
+
"strict": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"target": "ESNext",
|
|
8
|
+
"module": "ESNext",
|
|
9
|
+
"moduleResolution": "Bundler",
|
|
10
|
+
"paths": {
|
|
11
|
+
"@/*": ["src/*"]
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"include": ["src"]
|
|
15
|
+
}
|