Skip to content

Commit 58c4f26

Browse files
committed
feat: use shelljs
1 parent d43d4f1 commit 58c4f26

File tree

4 files changed

+114
-41
lines changed

4 files changed

+114
-41
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
"@babel/runtime": "^7.17.2",
8282
"multimatch": "5.0.0",
8383
"oss-upload-tool": "latest",
84-
"execa": "5.1.1",
85-
"schema-utils": "^4.0.0"
84+
"schema-utils": "^4.0.0",
85+
"shelljs": "^0.8.5",
86+
"@types/shelljs": "^0.8.11"
8687
}
8788
}

pnpm-lock.yaml

Lines changed: 86 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { Compiler, Stats } from "webpack";
33
import { ossUpload, OSSUploadOptions } from "oss-upload-tool";
44
import multimatch from "multimatch";
55
import { validate } from "schema-utils";
6-
import execa from "execa";
6+
import shell from "shelljs";
7+
import type { ExecOptions, ShellString } from "shelljs";
78

89
import rsync, { checkRsync } from "./rsync";
910
import { pluginName, logPrefix } from "./const";
@@ -63,9 +64,9 @@ export type TargetItem = {
6364
*/
6465
onUploadFinish?: (stats: Stats) => void | Promise<void>;
6566
/**
66-
* @link {execa.SyncOptions}
67+
* @link {import("shelljs").ExecOptions}
6768
*/
68-
execaOptions?: execa.SyncOptions;
69+
execOptions?: ExecOptions;
6970
};
7071

7172
export type WebpackDeployPluginOptions = {
@@ -135,7 +136,7 @@ class WebpackDeployPlugin {
135136
onUploadFinish,
136137
execUploadFinishScripts,
137138
execUploadStartScripts,
138-
execaOptions,
139+
execOptions,
139140
maxAttempts = 3,
140141
timeout,
141142
} = this.target;
@@ -152,14 +153,14 @@ class WebpackDeployPlugin {
152153

153154
onUploadStart?.(stats);
154155
execScripts(execUploadStartScripts, {
155-
...execaOptions,
156+
...execOptions,
156157
cwd: context,
157158
});
158159

159160
if (type === "rsync") {
160161
await rsync(assets, dest, rsyncOptions?.args, {
161162
timeout,
162-
...execaOptions,
163+
...execOptions,
163164
cwd: outputDir,
164165
});
165166
} else if (type === "oss") {
@@ -183,7 +184,7 @@ class WebpackDeployPlugin {
183184

184185
await onUploadFinish?.(stats);
185186
execScripts(execUploadFinishScripts, {
186-
...execaOptions,
187+
...execOptions,
187188
cwd: context,
188189
});
189190

@@ -210,21 +211,15 @@ function logWithError(str: string) {
210211
return new WebpackError(`${logPrefix}${str}`);
211212
}
212213

213-
function execScripts(
214-
scripts: string[][] | string[],
215-
options?: execa.SyncOptions
216-
) {
214+
function execScripts(scripts: string[][] | string[], options?: ExecOptions) {
217215
if (!scripts?.length) return;
218216

219217
const arr = (Array.isArray(scripts[0]) ? scripts : [scripts]) as string[][];
220218
for (const script of arr) {
221-
const { exitCode, stderr } = execa.sync(script[0], script.slice(1), {
222-
stdout: "inherit",
223-
stderr: "inherit",
224-
detached: true,
219+
const { code, stderr } = shell.exec(script.join(" "), {
225220
...options,
226-
});
227-
if (exitCode !== 0) {
221+
}) as ShellString;
222+
if (code !== 0) {
228223
throw new Error(stderr);
229224
}
230225
}

src/rsync.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from "os";
2-
import execa from "execa";
2+
import shell from "shelljs";
3+
import type { ExecOptions, ShellString } from "shelljs";
34

45
import { logPrefix, UnixMaxShellLen, WindowMaxShellLen } from "./const";
56

@@ -15,28 +16,28 @@ function rsync(
1516
source: string | string[],
1617
destination: string,
1718
args?: [string, string?][],
18-
options?: execa.SyncOptions
19+
options?: ExecOptions
1920
) {
2021
function exec(source: string | string[]) {
2122
return new Promise<void>((resolve, reject) => {
2223
const target = Array.isArray(source) ? source : [source];
2324

24-
const res = execa.sync(
25-
"rsync",
25+
const res = shell.exec(
2626
[
27+
"rsync",
2728
"-avzR",
2829
...(args || []).reduce<string[]>((acc, item: string[]) => {
2930
acc.push(`--${item.join("=")}`);
3031
return acc;
3132
}, []),
3233
...target,
3334
destination,
34-
],
35-
{ detached: true, ...options }
36-
);
35+
].join(" "),
36+
{ ...options }
37+
) as ShellString;
3738

3839
console.log("");
39-
if (res.exitCode === 0) {
40+
if (res.code === 0) {
4041
console.log(res.stdout);
4142
resolve();
4243
} else {
@@ -82,10 +83,10 @@ function rsync(
8283

8384
export function checkRsync() {
8485
return new Promise((resolve, reject) => {
85-
const res = execa.sync(os.type() === "Windows_NT" ? "where" : "whereis", [
86-
"rsync",
87-
]);
88-
if (res.exitCode === 0) {
86+
const res = shell.exec(
87+
(os.type() === "Windows_NT" ? "where" : "whereis") + " rsync"
88+
);
89+
if (res.code === 0) {
8990
resolve(res.stdout);
9091
return;
9192
}

0 commit comments

Comments
 (0)