soda-nodejs 0.8.3 → 0.8.4

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 CHANGED
@@ -1,23 +1,23 @@
1
- # soda-node
2
-
3
- [![NPM version](https://img.shields.io/npm/v/soda-node.svg?style=flat)](https://npmjs.com/package/soda-node)
4
- [![NPM downloads](http://img.shields.io/npm/dm/soda-node.svg?style=flat)](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
+ [![NPM version](https://img.shields.io/npm/v/soda-node.svg?style=flat)](https://npmjs.com/package/soda-node)
4
+ [![NPM downloads](http://img.shields.io/npm/dm/soda-node.svg?style=flat)](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
@@ -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?.decode;
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;
@@ -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?.decode;
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;
@@ -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
  }
@@ -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",
3
+ "version": "0.8.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -48,7 +48,7 @@
48
48
  "scripts": {
49
49
  "dev": "rslib build --watch",
50
50
  "build": "rslib build",
51
- "prebuild": "tsx scripts/export.ts",
51
+ "prebuild": "bun scripts/export.ts",
52
52
  "lint": "prettier --write ."
53
53
  }
54
54
  }
@@ -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
+ }
@@ -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
+ }
@@ -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
+ }
@@ -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
+ }
@@ -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
+ }
@@ -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
@@ -28,6 +28,10 @@ export type ZipOptions = {
28
28
  * 压缩文件时的工作目录
29
29
  */
30
30
  cwd?: string
31
+ /**
32
+ * 过滤
33
+ */
34
+ filter?: string | string[]
31
35
  }
32
36
 
33
37
  /**
@@ -41,12 +45,13 @@ export type ZipOptions = {
41
45
  * 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
42
46
  * 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
43
47
  */
44
- export async function zip({ input, output, thread = "auto", level, password, cwd }: ZipOptions) {
48
+ export async function zip({ input, output, thread = "auto", level, password, cwd, filter }: ZipOptions) {
45
49
  await which("7z")
50
+ filter = Array.isArray(filter) ? filter : typeof filter === "string" ? [filter] : []
46
51
  input = Array.isArray(input) ? input.join(" ") : input
47
52
  if (thread === "max") thread = cpus().length
48
53
  return await execAsync(
49
- `7z a ${output} ${input} -mmt=${thread === "auto" ? "on" : thread}${typeof level === "number" ? ` -mx=${level}` : ""}${password ? ` -p${password}` : ""}`,
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(" ")}` : ""}`,
50
55
  { cwd },
51
56
  )
52
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
+ }