soda-nodejs 0.8.4 → 0.8.6
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/dist/utils/spawnAsync.d.ts +1 -1
- package/package.json +5 -6
- package/src/utils/compress.ts +2 -0
- package/src/utils/copy.ts +4 -0
- package/src/utils/execAsync.ts +6 -1
- package/src/utils/saveFile.ts +1 -0
- package/src/utils/saveResponse.ts +1 -2
- package/src/utils/spawnAsync.ts +7 -2
- package/src/utils/zip.ts +58 -57
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChildProcess, ChildProcessByStdio, ChildProcessWithoutNullStreams, SpawnOptions,
|
|
1
|
+
import { ChildProcess, ChildProcessByStdio, ChildProcessWithoutNullStreams, SpawnOptions, SpawnOptionsWithoutStdio, SpawnOptionsWithStdioTuple, StdioNull, StdioPipe } from "child_process";
|
|
2
2
|
import { Readable, Writable } from "stream";
|
|
3
3
|
export interface PromiseWithChildProcess<T> extends Promise<T> {
|
|
4
4
|
child: T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soda-nodejs",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -35,16 +35,15 @@
|
|
|
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.2",
|
|
40
40
|
"which": "^5.0.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^
|
|
43
|
+
"@types/node": "^24.10.1",
|
|
44
44
|
"@types/which": "^3.0.4",
|
|
45
|
-
"typescript": "
|
|
45
|
+
"typescript": ">=5.8.3"
|
|
46
46
|
},
|
|
47
|
-
"peerDependencies": {},
|
|
48
47
|
"scripts": {
|
|
49
48
|
"dev": "rslib build --watch",
|
|
50
49
|
"build": "rslib build",
|
package/src/utils/compress.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, mkdirSync } from "fs"
|
|
2
2
|
import { basename, dirname, resolve } from "path"
|
|
3
|
+
|
|
3
4
|
import { create } from "tar"
|
|
4
5
|
|
|
5
6
|
export interface CompressionParams {
|
|
@@ -21,6 +22,7 @@ export async function compress({ input, output }: CompressionParams) {
|
|
|
21
22
|
|
|
22
23
|
// 确保输出目录存在
|
|
23
24
|
const outputDir = dirname(output)
|
|
25
|
+
|
|
24
26
|
if (!existsSync(outputDir)) mkdirSync(outputDir, { recursive: true })
|
|
25
27
|
|
|
26
28
|
// 执行压缩
|
package/src/utils/copy.ts
CHANGED
|
@@ -16,16 +16,20 @@ export async function copy({ input, output, mode }: CopyOptions) {
|
|
|
16
16
|
for (const item of input) await copy({ input: item, output, mode })
|
|
17
17
|
return
|
|
18
18
|
}
|
|
19
|
+
|
|
19
20
|
if (!existsSync(output)) await mkdir(output, { recursive: true })
|
|
20
21
|
const status = await stat(input)
|
|
21
22
|
const { base } = parse(input)
|
|
23
|
+
|
|
22
24
|
if (status.isFile()) {
|
|
23
25
|
await copyFile(input, join(output, base), mode)
|
|
24
26
|
return
|
|
25
27
|
}
|
|
28
|
+
|
|
26
29
|
if (status.isDirectory()) {
|
|
27
30
|
await mkdir(join(output, base), { recursive: true })
|
|
28
31
|
const entries = await readdir(input, { withFileTypes: true })
|
|
32
|
+
|
|
29
33
|
for (const entry of entries) {
|
|
30
34
|
await copy({
|
|
31
35
|
input: join(input, entry.name),
|
package/src/utils/execAsync.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { exec, ExecOptions } from "child_process"
|
|
2
2
|
import { ObjectEncodingOptions } from "fs"
|
|
3
|
+
|
|
3
4
|
import iconv, { Options } from "iconv-lite"
|
|
4
5
|
|
|
5
6
|
export type IconvDecodeOptions = {
|
|
@@ -21,15 +22,19 @@ export async function execAsync(
|
|
|
21
22
|
): Promise<string>
|
|
22
23
|
export async function execAsync(command: string, options?: any) {
|
|
23
24
|
const decode = options?.decode as IconvDecodeOptions | undefined
|
|
25
|
+
|
|
24
26
|
if (typeof options === "object" && options !== null && options.decode) {
|
|
25
27
|
const { decode, ...rest } = options as { decode: IconvDecodeOptions }
|
|
26
28
|
options = rest
|
|
27
29
|
}
|
|
30
|
+
|
|
28
31
|
return await new Promise<string | Buffer>((resolve, reject) => {
|
|
29
32
|
exec(command, options, (error, stdout, stderr) => {
|
|
30
33
|
if (error) return reject(error)
|
|
34
|
+
|
|
31
35
|
// if (stderr) console.warn(stderr)
|
|
32
36
|
if (decode && stdout instanceof Buffer) return resolve(iconv.decode(stdout, decode.encoding, decode.options))
|
|
37
|
+
|
|
33
38
|
resolve(stdout)
|
|
34
39
|
})
|
|
35
40
|
})
|
package/src/utils/saveFile.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type SaveFileOptions = {
|
|
|
14
14
|
export async function saveFile({ input, output }: SaveFileOptions) {
|
|
15
15
|
await new Promise<void>((resolve, reject) => {
|
|
16
16
|
const writeAble = createWriteStream(output)
|
|
17
|
+
|
|
17
18
|
Readable.fromWeb(input.stream() as any)
|
|
18
19
|
.pipe(writeAble)
|
|
19
20
|
.on("finish", resolve)
|
package/src/utils/spawnAsync.ts
CHANGED
|
@@ -2,12 +2,12 @@ import {
|
|
|
2
2
|
ChildProcess,
|
|
3
3
|
ChildProcessByStdio,
|
|
4
4
|
ChildProcessWithoutNullStreams,
|
|
5
|
+
spawn,
|
|
5
6
|
SpawnOptions,
|
|
6
|
-
SpawnOptionsWithStdioTuple,
|
|
7
7
|
SpawnOptionsWithoutStdio,
|
|
8
|
+
SpawnOptionsWithStdioTuple,
|
|
8
9
|
StdioNull,
|
|
9
10
|
StdioPipe,
|
|
10
|
-
spawn,
|
|
11
11
|
} from "child_process"
|
|
12
12
|
import { Readable, Writable } from "stream"
|
|
13
13
|
|
|
@@ -46,6 +46,7 @@ export function setDefaultOptions(options: Options | ((prev: Options) => Options
|
|
|
46
46
|
defaultOptions = options(defaultOptions)
|
|
47
47
|
return
|
|
48
48
|
}
|
|
49
|
+
|
|
49
50
|
defaultOptions = options
|
|
50
51
|
return defaultOptions
|
|
51
52
|
}
|
|
@@ -140,13 +141,17 @@ export function spawnAsync(command: string, args?: any, options?: any): Promise<
|
|
|
140
141
|
const promise = new Promise<any>((resolve, reject) => {
|
|
141
142
|
if (Array.isArray(args)) options = { ...defaultOptions, ...options }
|
|
142
143
|
else args = { ...defaultOptions, ...args }
|
|
144
|
+
|
|
143
145
|
child = spawn(command, args, options)
|
|
146
|
+
|
|
144
147
|
child.on("exit", (code: number) => {
|
|
145
148
|
if (code === 0) return resolve(child)
|
|
149
|
+
|
|
146
150
|
if (Array.isArray(args)) {
|
|
147
151
|
const args2 = args.map((item: string) => item.trim()).filter(Boolean)
|
|
148
152
|
if (args2.length > 0) command = `${command} ${args2.join(" ")}`
|
|
149
153
|
}
|
|
154
|
+
|
|
150
155
|
reject(new Error(`spawn "${command}" failed with code ${code}`))
|
|
151
156
|
return
|
|
152
157
|
})
|
package/src/utils/zip.ts
CHANGED
|
@@ -1,57 +1,58 @@
|
|
|
1
|
-
import { cpus } from "os"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
* -
|
|
41
|
-
* -
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
{
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
import { cpus } from "os"
|
|
2
|
+
|
|
3
|
+
import which from "which"
|
|
4
|
+
|
|
5
|
+
import { execAsync } from "./execAsync"
|
|
6
|
+
|
|
7
|
+
export type ZipOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* 要压缩的文件
|
|
10
|
+
*/
|
|
11
|
+
input: string | string[]
|
|
12
|
+
/**
|
|
13
|
+
* 压缩到的目标位置,文件名的后缀就是压缩格式,例如:.zip、.7z
|
|
14
|
+
*/
|
|
15
|
+
output: string
|
|
16
|
+
/**
|
|
17
|
+
* 线程数
|
|
18
|
+
*/
|
|
19
|
+
thread?: number | "auto" | "max"
|
|
20
|
+
/**
|
|
21
|
+
* 压缩等级,0-9
|
|
22
|
+
*/
|
|
23
|
+
level?: number
|
|
24
|
+
/**
|
|
25
|
+
* 是否加密
|
|
26
|
+
*/
|
|
27
|
+
password?: string
|
|
28
|
+
/**
|
|
29
|
+
* 压缩文件时的工作目录
|
|
30
|
+
*/
|
|
31
|
+
cwd?: string
|
|
32
|
+
/**
|
|
33
|
+
* 过滤
|
|
34
|
+
*/
|
|
35
|
+
filter?: string | string[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 使用 7z 命令压缩文件
|
|
40
|
+
* - 如果没有安装 7z,请先安装 7z 后再执行
|
|
41
|
+
* - 下载地址:https://www.7-zip.org/download.html
|
|
42
|
+
* - 如果已经安装,请按照以下步骤将 7z 添加到环境变量中
|
|
43
|
+
* 1. 设置 → 系统 → 右侧系统信息 → 高级系统设置 → 环境变量
|
|
44
|
+
* 2. 在系统变量中找到并选中 Path,点击编辑
|
|
45
|
+
* 3. 点击新建,输入 7z 的安装路径(默认是 C:\Program Files\7-Zip),点击确定
|
|
46
|
+
* 4. 重启终端,输入 7z,如果出现 7z 的版本信息,则安装成功
|
|
47
|
+
* 5. 如果没有出现版本信息,请重启电脑,或者检查 7z 的安装路径是否正确
|
|
48
|
+
*/
|
|
49
|
+
export async function zip({ input, output, thread = "auto", level, password, cwd, filter }: ZipOptions) {
|
|
50
|
+
await which("7z")
|
|
51
|
+
filter = Array.isArray(filter) ? filter : typeof filter === "string" ? [filter] : []
|
|
52
|
+
input = Array.isArray(input) ? input.join(" ") : input
|
|
53
|
+
if (thread === "max") thread = cpus().length
|
|
54
|
+
return await execAsync(
|
|
55
|
+
`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(" ")}` : ""}`,
|
|
56
|
+
{ cwd },
|
|
57
|
+
)
|
|
58
|
+
}
|