@effect/platform-node 0.33.2 → 0.33.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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Command from "@effect/platform/Command";
|
|
2
2
|
import * as CommandExecutor from "@effect/platform/CommandExecutor";
|
|
3
3
|
import * as FileSystem from "@effect/platform/FileSystem";
|
|
4
|
+
import * as Deferred from "effect/Deferred";
|
|
4
5
|
import * as Effect from "effect/Effect";
|
|
5
6
|
import { constUndefined, pipe } from "effect/Function";
|
|
6
7
|
import * as Layer from "effect/Layer";
|
|
@@ -28,98 +29,73 @@ const runCommand = fileSystem => command => {
|
|
|
28
29
|
switch (command._tag) {
|
|
29
30
|
case "StandardCommand":
|
|
30
31
|
{
|
|
31
|
-
|
|
32
|
-
// Validate that the directory is accessible
|
|
33
|
-
Option.match(command.cwd, {
|
|
34
|
-
onNone: () => Effect.unit,
|
|
35
|
-
onSome: dir => fileSystem.access(dir)
|
|
36
|
-
}), Effect.zipRight(Effect.sync(() => globalThis.process.env)), Effect.flatMap(env => Effect.async(resume => {
|
|
32
|
+
const spawn = Effect.flatMap(Deferred.make(), exitCode => Effect.async(resume => {
|
|
37
33
|
const handle = ChildProcess.spawn(command.command, command.args, {
|
|
38
34
|
stdio: [inputToStdioOption(command.stdin), outputToStdioOption(command.stdout), outputToStdioOption(command.stderr)],
|
|
39
35
|
cwd: Option.getOrElse(command.cwd, constUndefined),
|
|
40
36
|
shell: command.shell,
|
|
41
37
|
env: {
|
|
42
|
-
...env,
|
|
38
|
+
...process.env,
|
|
43
39
|
...Object.fromEntries(command.env)
|
|
44
40
|
}
|
|
45
41
|
});
|
|
46
|
-
let exited = false;
|
|
47
|
-
handle.on("exit", () => {
|
|
48
|
-
exited = true;
|
|
49
|
-
});
|
|
50
|
-
// If starting the process throws an error, make sure to capture it
|
|
51
42
|
handle.on("error", err => {
|
|
52
|
-
handle.kill("SIGKILL");
|
|
53
43
|
resume(Effect.fail(toPlatformError("spawn", err, command)));
|
|
54
44
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
resume(Effect.fail(toPlatformError("exitCode", new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`), command)));
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
// Make sure to terminate the running process if the fiber is
|
|
77
|
-
// terminated
|
|
78
|
-
return Effect.sync(() => {
|
|
79
|
-
handle.kill("SIGKILL");
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
const isRunning = Effect.sync(() => handle.exitCode === null && handle.signalCode === null && !handle.killed);
|
|
83
|
-
const kill = (signal = "SIGTERM") => Effect.async(resume => {
|
|
84
|
-
handle.kill(signal);
|
|
85
|
-
handle.on("exit", () => {
|
|
86
|
-
resume(Effect.unit);
|
|
87
|
-
});
|
|
88
|
-
// Make sure to terminate the running process if the fiber
|
|
89
|
-
// is terminated
|
|
90
|
-
return Effect.sync(() => {
|
|
91
|
-
handle.kill("SIGKILL");
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
resume(Effect.sync(() => {
|
|
95
|
-
const pid = CommandExecutor.ProcessId(handle.pid);
|
|
96
|
-
const stderr = fromReadable(() => handle.stderr, err => toPlatformError("fromReadable(stderr)", toError(err), command));
|
|
97
|
-
let stdout = fromReadable(() => handle.stdout, err => toPlatformError("fromReadable(stdout)", toError(err), command));
|
|
98
|
-
// TODO: add Sink.isSink
|
|
99
|
-
if (typeof command.stdout !== "string") {
|
|
100
|
-
stdout = Stream.transduce(stdout, command.stdout);
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
|
|
104
|
-
pid,
|
|
105
|
-
exitCode,
|
|
106
|
-
isRunning,
|
|
107
|
-
kill,
|
|
108
|
-
stdin,
|
|
109
|
-
stderr,
|
|
110
|
-
stdout
|
|
111
|
-
};
|
|
112
|
-
}));
|
|
45
|
+
handle.on("exit", (...args) => {
|
|
46
|
+
Deferred.unsafeDone(exitCode, Effect.succeed(args));
|
|
47
|
+
});
|
|
48
|
+
handle.on("spawn", () => {
|
|
49
|
+
resume(Effect.succeed([handle, exitCode]));
|
|
50
|
+
});
|
|
51
|
+
return Effect.sync(() => {
|
|
52
|
+
handle.kill("SIGTERM");
|
|
53
|
+
});
|
|
54
|
+
}));
|
|
55
|
+
return pipe(
|
|
56
|
+
// Validate that the directory is accessible
|
|
57
|
+
Option.match(command.cwd, {
|
|
58
|
+
onNone: () => Effect.unit,
|
|
59
|
+
onSome: dir => fileSystem.access(dir)
|
|
60
|
+
}), Effect.zipRight(Effect.acquireRelease(spawn, ([handle, exitCode]) => Effect.flatMap(Deferred.isDone(exitCode), done => done ? Effect.unit : Effect.suspend(() => {
|
|
61
|
+
if (handle.kill("SIGTERM")) {
|
|
62
|
+
return Deferred.await(exitCode);
|
|
113
63
|
}
|
|
114
|
-
return Effect.
|
|
115
|
-
|
|
116
|
-
|
|
64
|
+
return Effect.unit;
|
|
65
|
+
})))), Effect.map(([handle, exitCodeDeferred]) => {
|
|
66
|
+
let stdin = Sink.drain;
|
|
67
|
+
if (handle.stdin !== null) {
|
|
68
|
+
stdin = fromWritable(() => handle.stdin, err => toPlatformError("toWritable", toError(err), command));
|
|
69
|
+
}
|
|
70
|
+
const exitCode = Effect.flatMap(Deferred.await(exitCodeDeferred), ([code, signal]) => {
|
|
71
|
+
if (code !== null) {
|
|
72
|
+
return Effect.succeed(CommandExecutor.ExitCode(code));
|
|
117
73
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
74
|
+
// If code is `null`, then `signal` must be defined. See the NodeJS
|
|
75
|
+
// documentation for the `"exit"` event on a `child_process`.
|
|
76
|
+
// https://nodejs.org/api/child_process.html#child_process_event_exit
|
|
77
|
+
return Effect.fail(toPlatformError("exitCode", new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`), command));
|
|
121
78
|
});
|
|
122
|
-
|
|
79
|
+
const isRunning = Effect.negate(Deferred.isDone(exitCodeDeferred));
|
|
80
|
+
const kill = (signal = "SIGTERM") => Effect.suspend(() => handle.kill(signal) ? Effect.asUnit(Deferred.await(exitCodeDeferred)) : Effect.fail(toPlatformError("kill", new globalThis.Error("Failed to kill process"), command)));
|
|
81
|
+
const pid = CommandExecutor.ProcessId(handle.pid);
|
|
82
|
+
const stderr = fromReadable(() => handle.stderr, err => toPlatformError("fromReadable(stderr)", toError(err), command));
|
|
83
|
+
let stdout = fromReadable(() => handle.stdout, err => toPlatformError("fromReadable(stdout)", toError(err), command));
|
|
84
|
+
// TODO: add Sink.isSink
|
|
85
|
+
if (typeof command.stdout !== "string") {
|
|
86
|
+
stdout = Stream.transduce(stdout, command.stdout);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
|
|
90
|
+
pid,
|
|
91
|
+
exitCode,
|
|
92
|
+
isRunning,
|
|
93
|
+
kill,
|
|
94
|
+
stdin,
|
|
95
|
+
stderr,
|
|
96
|
+
stdout
|
|
97
|
+
};
|
|
98
|
+
}), Effect.tap(process => Option.match(command.stdin, {
|
|
123
99
|
onNone: () => Effect.unit,
|
|
124
100
|
onSome: stdin => Effect.forkDaemon(Stream.run(stdin, process.stdin))
|
|
125
101
|
})));
|
|
@@ -134,7 +110,7 @@ const runCommand = fileSystem => command => {
|
|
|
134
110
|
const tail = flattened.slice(1);
|
|
135
111
|
const initial = tail.slice(0, tail.length - 1);
|
|
136
112
|
const last = tail[tail.length - 1];
|
|
137
|
-
const stream = initial.reduce((stdin, command) => pipe(Command.stdin(command, stdin), runCommand(fileSystem),
|
|
113
|
+
const stream = initial.reduce((stdin, command) => pipe(Command.stdin(command, stdin), runCommand(fileSystem), Effect.map(process => process.stdout), Stream.unwrapScoped), pipe(runCommand(fileSystem)(head), Effect.map(process => process.stdout), Stream.unwrapScoped));
|
|
138
114
|
return pipe(Command.stdin(last, stream), runCommand(fileSystem));
|
|
139
115
|
}
|
|
140
116
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandExecutor.js","names":["Command","CommandExecutor","FileSystem","Effect","constUndefined","pipe","Layer","Option","Sink","Stream","ChildProcess","handleErrnoException","fromWritable","fromReadable","inputToStdioOption","stdin","match","onNone","onSome","outputToStdioOption","output","toError","err","globalThis","Error","String","toPlatformError","method","error","command","flattened","flatten","reduce","acc","curr","args","join","length","runCommand","fileSystem","_tag","
|
|
1
|
+
{"version":3,"file":"commandExecutor.js","names":["Command","CommandExecutor","FileSystem","Deferred","Effect","constUndefined","pipe","Layer","Option","Sink","Stream","ChildProcess","handleErrnoException","fromWritable","fromReadable","inputToStdioOption","stdin","match","onNone","onSome","outputToStdioOption","output","toError","err","globalThis","Error","String","toPlatformError","method","error","command","flattened","flatten","reduce","acc","curr","args","join","length","runCommand","fileSystem","_tag","spawn","flatMap","make","exitCode","async","resume","handle","stdio","stdout","stderr","cwd","getOrElse","shell","env","process","Object","fromEntries","on","fail","unsafeDone","succeed","sync","kill","unit","dir","access","zipRight","acquireRelease","isDone","done","suspend","await","map","exitCodeDeferred","drain","code","signal","ExitCode","isRunning","negate","asUnit","pid","ProcessId","transduce","ProcessTypeId","tap","forkDaemon","run","head","tail","slice","initial","last","stream","unwrapScoped","layer","effect","makeExecutor"],"sources":["../../../src/internal/commandExecutor.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAO,KAAKA,OAAO,MAAM,0BAA0B;AACnD,OAAO,KAAKC,eAAe,MAAM,kCAAkC;AAEnE,OAAO,KAAKC,UAAU,MAAM,6BAA6B;AACzD,OAAO,KAAKC,QAAQ,MAAM,iBA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AO,GAAGF,IAAI,CAACC,KAAK,CAAC,CAAC,EAAED,IAAI,CAACrD,MAAM,GAAG,CAAC,CAAC;QAC9C,MAAMwD,IAAI,GAAGH,IAAI,CAACA,IAAI,CAACrD,MAAM,GAAG,CAAC,CAAC;QAClC,MAAMyD,MAAM,GAAGF,OAAO,CAAC5D,MAAM,CAC3B,CAACjB,KAAK,EAAEc,OAAO,KACbxB,IAAI,CACFN,OAAO,CAACgB,KAAK,CAACc,OAAO,EAAEd,KAAK,CAAC,EAC7BuB,UAAU,CAACC,UAAU,CAAC,EACtBpC,MAAM,CAACsE,GAAG,CAAElB,OAAO,IAAKA,OAAO,CAACN,MAAM,CAAC,EACvCxC,MAAM,CAACsF,YAAY,CACpB,EACH1F,IAAI,CACFiC,UAAU,CAACC,UAAU,CAAC,CAACkD,IAAI,CAAC,EAC5BtF,MAAM,CAACsE,GAAG,CAAElB,OAAO,IAAKA,OAAO,CAACN,MAAM,CAAC,EACvCxC,MAAM,CAACsF,YAAY,CACpB,CACF;QACD,OAAO1F,IAAI,CAACN,OAAO,CAACgB,KAAK,CAAC8E,IAAI,EAAEC,MAAM,CAAC,EAAExD,UAAU,CAACC,UAAU,CAAC,CAAC;MAClE;EACF;AACF,CAAC;AAEH;AACA,OAAO,MAAMyD,KAAK,gBAA+E1F,KAAK,CAAC2F,MAAM,CAC3GjG,eAAe,CAACA,eAAe,eAC/BK,IAAI,CACFJ,UAAU,CAACA,UAAU,eACrBE,MAAM,CAACsE,GAAG,CAAElC,UAAU,IAAKvC,eAAe,CAACkG,YAAY,CAAC5D,UAAU,CAACC,UAAU,CAAC,CAAC,CAAC,CACjF,CACF"}
|
|
@@ -7,6 +7,7 @@ exports.layer = void 0;
|
|
|
7
7
|
var Command = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/platform/Command"));
|
|
8
8
|
var CommandExecutor = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/platform/CommandExecutor"));
|
|
9
9
|
var FileSystem = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/platform/FileSystem"));
|
|
10
|
+
var Deferred = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Deferred"));
|
|
10
11
|
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
11
12
|
var _Function = /*#__PURE__*/require("effect/Function");
|
|
12
13
|
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Layer"));
|
|
@@ -59,98 +60,73 @@ const runCommand = fileSystem => command => {
|
|
|
59
60
|
switch (command._tag) {
|
|
60
61
|
case "StandardCommand":
|
|
61
62
|
{
|
|
62
|
-
|
|
63
|
-
// Validate that the directory is accessible
|
|
64
|
-
Option.match(command.cwd, {
|
|
65
|
-
onNone: () => Effect.unit,
|
|
66
|
-
onSome: dir => fileSystem.access(dir)
|
|
67
|
-
}), Effect.zipRight(Effect.sync(() => globalThis.process.env)), Effect.flatMap(env => Effect.async(resume => {
|
|
63
|
+
const spawn = Effect.flatMap(Deferred.make(), exitCode => Effect.async(resume => {
|
|
68
64
|
const handle = ChildProcess.spawn(command.command, command.args, {
|
|
69
65
|
stdio: [inputToStdioOption(command.stdin), outputToStdioOption(command.stdout), outputToStdioOption(command.stderr)],
|
|
70
66
|
cwd: Option.getOrElse(command.cwd, _Function.constUndefined),
|
|
71
67
|
shell: command.shell,
|
|
72
68
|
env: {
|
|
73
|
-
...env,
|
|
69
|
+
...process.env,
|
|
74
70
|
...Object.fromEntries(command.env)
|
|
75
71
|
}
|
|
76
72
|
});
|
|
77
|
-
let exited = false;
|
|
78
|
-
handle.on("exit", () => {
|
|
79
|
-
exited = true;
|
|
80
|
-
});
|
|
81
|
-
// If starting the process throws an error, make sure to capture it
|
|
82
73
|
handle.on("error", err => {
|
|
83
|
-
handle.kill("SIGKILL");
|
|
84
74
|
resume(Effect.fail(toPlatformError("spawn", err, command)));
|
|
85
75
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
resume(Effect.fail(toPlatformError("exitCode", new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`), command)));
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
// Make sure to terminate the running process if the fiber is
|
|
108
|
-
// terminated
|
|
109
|
-
return Effect.sync(() => {
|
|
110
|
-
handle.kill("SIGKILL");
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
const isRunning = Effect.sync(() => handle.exitCode === null && handle.signalCode === null && !handle.killed);
|
|
114
|
-
const kill = (signal = "SIGTERM") => Effect.async(resume => {
|
|
115
|
-
handle.kill(signal);
|
|
116
|
-
handle.on("exit", () => {
|
|
117
|
-
resume(Effect.unit);
|
|
118
|
-
});
|
|
119
|
-
// Make sure to terminate the running process if the fiber
|
|
120
|
-
// is terminated
|
|
121
|
-
return Effect.sync(() => {
|
|
122
|
-
handle.kill("SIGKILL");
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
resume(Effect.sync(() => {
|
|
126
|
-
const pid = CommandExecutor.ProcessId(handle.pid);
|
|
127
|
-
const stderr = (0, _stream.fromReadable)(() => handle.stderr, err => toPlatformError("fromReadable(stderr)", toError(err), command));
|
|
128
|
-
let stdout = (0, _stream.fromReadable)(() => handle.stdout, err => toPlatformError("fromReadable(stdout)", toError(err), command));
|
|
129
|
-
// TODO: add Sink.isSink
|
|
130
|
-
if (typeof command.stdout !== "string") {
|
|
131
|
-
stdout = Stream.transduce(stdout, command.stdout);
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
|
|
135
|
-
pid,
|
|
136
|
-
exitCode,
|
|
137
|
-
isRunning,
|
|
138
|
-
kill,
|
|
139
|
-
stdin,
|
|
140
|
-
stderr,
|
|
141
|
-
stdout
|
|
142
|
-
};
|
|
143
|
-
}));
|
|
76
|
+
handle.on("exit", (...args) => {
|
|
77
|
+
Deferred.unsafeDone(exitCode, Effect.succeed(args));
|
|
78
|
+
});
|
|
79
|
+
handle.on("spawn", () => {
|
|
80
|
+
resume(Effect.succeed([handle, exitCode]));
|
|
81
|
+
});
|
|
82
|
+
return Effect.sync(() => {
|
|
83
|
+
handle.kill("SIGTERM");
|
|
84
|
+
});
|
|
85
|
+
}));
|
|
86
|
+
return (0, _Function.pipe)(
|
|
87
|
+
// Validate that the directory is accessible
|
|
88
|
+
Option.match(command.cwd, {
|
|
89
|
+
onNone: () => Effect.unit,
|
|
90
|
+
onSome: dir => fileSystem.access(dir)
|
|
91
|
+
}), Effect.zipRight(Effect.acquireRelease(spawn, ([handle, exitCode]) => Effect.flatMap(Deferred.isDone(exitCode), done => done ? Effect.unit : Effect.suspend(() => {
|
|
92
|
+
if (handle.kill("SIGTERM")) {
|
|
93
|
+
return Deferred.await(exitCode);
|
|
144
94
|
}
|
|
145
|
-
return Effect.
|
|
146
|
-
|
|
147
|
-
|
|
95
|
+
return Effect.unit;
|
|
96
|
+
})))), Effect.map(([handle, exitCodeDeferred]) => {
|
|
97
|
+
let stdin = Sink.drain;
|
|
98
|
+
if (handle.stdin !== null) {
|
|
99
|
+
stdin = (0, _sink.fromWritable)(() => handle.stdin, err => toPlatformError("toWritable", toError(err), command));
|
|
100
|
+
}
|
|
101
|
+
const exitCode = Effect.flatMap(Deferred.await(exitCodeDeferred), ([code, signal]) => {
|
|
102
|
+
if (code !== null) {
|
|
103
|
+
return Effect.succeed(CommandExecutor.ExitCode(code));
|
|
148
104
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
105
|
+
// If code is `null`, then `signal` must be defined. See the NodeJS
|
|
106
|
+
// documentation for the `"exit"` event on a `child_process`.
|
|
107
|
+
// https://nodejs.org/api/child_process.html#child_process_event_exit
|
|
108
|
+
return Effect.fail(toPlatformError("exitCode", new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`), command));
|
|
152
109
|
});
|
|
153
|
-
|
|
110
|
+
const isRunning = Effect.negate(Deferred.isDone(exitCodeDeferred));
|
|
111
|
+
const kill = (signal = "SIGTERM") => Effect.suspend(() => handle.kill(signal) ? Effect.asUnit(Deferred.await(exitCodeDeferred)) : Effect.fail(toPlatformError("kill", new globalThis.Error("Failed to kill process"), command)));
|
|
112
|
+
const pid = CommandExecutor.ProcessId(handle.pid);
|
|
113
|
+
const stderr = (0, _stream.fromReadable)(() => handle.stderr, err => toPlatformError("fromReadable(stderr)", toError(err), command));
|
|
114
|
+
let stdout = (0, _stream.fromReadable)(() => handle.stdout, err => toPlatformError("fromReadable(stdout)", toError(err), command));
|
|
115
|
+
// TODO: add Sink.isSink
|
|
116
|
+
if (typeof command.stdout !== "string") {
|
|
117
|
+
stdout = Stream.transduce(stdout, command.stdout);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
|
|
121
|
+
pid,
|
|
122
|
+
exitCode,
|
|
123
|
+
isRunning,
|
|
124
|
+
kill,
|
|
125
|
+
stdin,
|
|
126
|
+
stderr,
|
|
127
|
+
stdout
|
|
128
|
+
};
|
|
129
|
+
}), Effect.tap(process => Option.match(command.stdin, {
|
|
154
130
|
onNone: () => Effect.unit,
|
|
155
131
|
onSome: stdin => Effect.forkDaemon(Stream.run(stdin, process.stdin))
|
|
156
132
|
})));
|
|
@@ -165,7 +141,7 @@ const runCommand = fileSystem => command => {
|
|
|
165
141
|
const tail = flattened.slice(1);
|
|
166
142
|
const initial = tail.slice(0, tail.length - 1);
|
|
167
143
|
const last = tail[tail.length - 1];
|
|
168
|
-
const stream = initial.reduce((stdin, command) => (0, _Function.pipe)(Command.stdin(command, stdin), runCommand(fileSystem),
|
|
144
|
+
const stream = initial.reduce((stdin, command) => (0, _Function.pipe)(Command.stdin(command, stdin), runCommand(fileSystem), Effect.map(process => process.stdout), Stream.unwrapScoped), (0, _Function.pipe)(runCommand(fileSystem)(head), Effect.map(process => process.stdout), Stream.unwrapScoped));
|
|
169
145
|
return (0, _Function.pipe)(Command.stdin(last, stream), runCommand(fileSystem));
|
|
170
146
|
}
|
|
171
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commandExecutor.js","names":["Command","_interopRequireWildcard","require","CommandExecutor","FileSystem","Effect","_Function","Layer","Option","Sink","Stream","ChildProcess","_error","_sink","_stream","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","inputToStdioOption","stdin","match","onNone","onSome","outputToStdioOption","output","toError","err","globalThis","Error","String","toPlatformError","method","error","command","flattened","flatten","reduce","acc","curr","args","join","length","handleErrnoException","runCommand","fileSystem","_tag","
|
|
1
|
+
{"version":3,"file":"commandExecutor.js","names":["Command","_interopRequireWildcard","require","CommandExecutor","FileSystem","Deferred","Effect","_Function","Layer","Option","Sink","Stream","ChildProcess","_error","_sink","_stream","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","inputToStdioOption","stdin","match","onNone","onSome","outputToStdioOption","output","toError","err","globalThis","Error","String","toPlatformError","method","error","command","flattened","flatten","reduce","acc","curr","args","join","length","handleErrnoException","runCommand","fileSystem","_tag","spawn","flatMap","make","exitCode","async","resume","handle","stdio","stdout","stderr","cwd","getOrElse","constUndefined","shell","env","process","fromEntries","on","fail","unsafeDone","succeed","sync","kill","pipe","unit","dir","access","zipRight","acquireRelease","isDone","done","suspend","await","map","exitCodeDeferred","drain","fromWritable","code","signal","ExitCode","isRunning","negate","asUnit","pid","ProcessId","fromReadable","transduce","ProcessTypeId","tap","forkDaemon","run","head","tail","slice","initial","last","stream","unwrapScoped","layer","exports","effect","makeExecutor"],"sources":["../src/internal/commandExecutor.ts"],"sourcesContent":[null],"mappings":";;;;;;AAAA,IAAAA,OAAA,gBAAAC,uBAAA,eAAAC,OAAA;AACA,IAAAC,eAAA,gBAAAF,uBAAA,eAAAC,OAAA;AAEA,IAAAE,UAAA,gBAAAH,uBAAA,eAAAC,OAAA;AACA,IAAAG,QAAA,gBAAAJ,uBAAA,eAAAC,OAAA;AACA,IAAAI,MAAA,gBAAAL,uBAAA,eAAAC,OAAA;AACA,IAAAK,SAAA,gBAAAL,OAAA;AACA,IAAAM,KAAA,gBAAAP,uBAAA,eAAAC,OAAA;AACA,IAAAO,MAAA,gBAAAR,uBAAA,eAAAC,OAAA;AAEA,IAAAQ,IAAA,gBAAAT,uBAAA,eAAAC,OAAA;AACA,IAAAS,MAAA,gBAAAV,uBAAA,eAAAC,OAAA;AACA,IAAAU,YAAA,gBAAAX,uBAAA,eAAAC,OAAA;AACA,IAAAW,MAAA,gBAAAX,OAAA;AACA,IAAAY,KAAA,gBAAAZ,OAAA;AACA,IAAAa,OAAA,gBAAAb,OAAA;AAA0C,SAAAc,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAhB,wBAAAgB,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA;EAAA,aAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA;IAAAK,OAAA,EAAAL;EAAA;EAAA,IAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA;EAAA,IAAAC,CAAA,IAAAA,CAAA,CAAAG,GAA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ACkF,IAAI,EAAEC,MAAM,CAAC,EAAE3D,UAAU,CAACC,UAAU,CAAC,CAAC;MAClE;EACF;AACF,CAAC;AAEH;AACO,MAAM4D,KAAK,GAAAC,OAAA,CAAAD,KAAA,gBAA+EnH,KAAK,CAACqH,MAAM,CAC3G1H,eAAe,CAACA,eAAe,eAC/B,IAAAI,SAAA,CAAAiF,IAAI,EACFpF,UAAU,CAACA,UAAU,eACrBE,MAAM,CAAC4F,GAAG,CAAEnC,UAAU,IAAK5D,eAAe,CAAC2H,YAAY,CAAChE,UAAU,CAACC,UAAU,CAAC,CAAC,CAAC,CACjF,CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/platform-node",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.4",
|
|
4
4
|
"description": "Unified interfaces for common platform-specific services",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"mime": "^3.0.0",
|
|
14
14
|
"multipasta": "^0.1.19",
|
|
15
|
-
"@effect/platform": "0.32.
|
|
15
|
+
"@effect/platform": "0.32.2"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
18
|
"effect": "2.0.0-next.58"
|
|
@@ -2,10 +2,12 @@ import * as Command from "@effect/platform/Command"
|
|
|
2
2
|
import * as CommandExecutor from "@effect/platform/CommandExecutor"
|
|
3
3
|
import type * as Error from "@effect/platform/Error"
|
|
4
4
|
import * as FileSystem from "@effect/platform/FileSystem"
|
|
5
|
+
import * as Deferred from "effect/Deferred"
|
|
5
6
|
import * as Effect from "effect/Effect"
|
|
6
7
|
import { constUndefined, pipe } from "effect/Function"
|
|
7
8
|
import * as Layer from "effect/Layer"
|
|
8
9
|
import * as Option from "effect/Option"
|
|
10
|
+
import type * as Scope from "effect/Scope"
|
|
9
11
|
import * as Sink from "effect/Sink"
|
|
10
12
|
import * as Stream from "effect/Stream"
|
|
11
13
|
import * as ChildProcess from "node:child_process"
|
|
@@ -33,151 +35,128 @@ const toPlatformError = (
|
|
|
33
35
|
return handleErrnoException("Command", method)(error, [flattened])
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
type ExitCode = readonly [code: number | null, signal: NodeJS.Signals | null]
|
|
39
|
+
type ExitCodeDeferred = Deferred.Deferred<never, ExitCode>
|
|
40
|
+
|
|
36
41
|
const runCommand =
|
|
37
42
|
(fileSystem: FileSystem.FileSystem) =>
|
|
38
|
-
(command: Command.Command): Effect.Effect<
|
|
43
|
+
(command: Command.Command): Effect.Effect<Scope.Scope, Error.PlatformError, CommandExecutor.Process> => {
|
|
39
44
|
switch (command._tag) {
|
|
40
45
|
case "StandardCommand": {
|
|
46
|
+
const spawn = Effect.flatMap(
|
|
47
|
+
Deferred.make<never, ExitCode>(),
|
|
48
|
+
(exitCode) =>
|
|
49
|
+
Effect.async<never, Error.PlatformError, readonly [ChildProcess.ChildProcess, ExitCodeDeferred]>(
|
|
50
|
+
(resume) => {
|
|
51
|
+
const handle = ChildProcess.spawn(command.command, command.args, {
|
|
52
|
+
stdio: [
|
|
53
|
+
inputToStdioOption(command.stdin),
|
|
54
|
+
outputToStdioOption(command.stdout),
|
|
55
|
+
outputToStdioOption(command.stderr)
|
|
56
|
+
],
|
|
57
|
+
cwd: Option.getOrElse(command.cwd, constUndefined),
|
|
58
|
+
shell: command.shell,
|
|
59
|
+
env: { ...process.env, ...Object.fromEntries(command.env) }
|
|
60
|
+
})
|
|
61
|
+
handle.on("error", (err) => {
|
|
62
|
+
resume(Effect.fail(toPlatformError("spawn", err, command)))
|
|
63
|
+
})
|
|
64
|
+
handle.on("exit", (...args) => {
|
|
65
|
+
Deferred.unsafeDone(exitCode, Effect.succeed(args))
|
|
66
|
+
})
|
|
67
|
+
handle.on("spawn", () => {
|
|
68
|
+
resume(Effect.succeed([handle, exitCode]))
|
|
69
|
+
})
|
|
70
|
+
return Effect.sync(() => {
|
|
71
|
+
handle.kill("SIGTERM")
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
)
|
|
41
76
|
return pipe(
|
|
42
77
|
// Validate that the directory is accessible
|
|
43
78
|
Option.match(command.cwd, {
|
|
44
79
|
onNone: () => Effect.unit,
|
|
45
80
|
onSome: (dir) => fileSystem.access(dir)
|
|
46
81
|
}),
|
|
47
|
-
Effect.zipRight(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
exited = true
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// If starting the process throws an error, make sure to capture it
|
|
66
|
-
handle.on("error", (err) => {
|
|
67
|
-
handle.kill("SIGKILL")
|
|
68
|
-
resume(Effect.fail(toPlatformError("spawn", err, command)))
|
|
69
|
-
})
|
|
82
|
+
Effect.zipRight(
|
|
83
|
+
Effect.acquireRelease(
|
|
84
|
+
spawn,
|
|
85
|
+
([handle, exitCode]) =>
|
|
86
|
+
Effect.flatMap(Deferred.isDone(exitCode), (done) =>
|
|
87
|
+
done ? Effect.unit : Effect.suspend(() => {
|
|
88
|
+
if (handle.kill("SIGTERM")) {
|
|
89
|
+
return Deferred.await(exitCode)
|
|
90
|
+
}
|
|
91
|
+
return Effect.unit
|
|
92
|
+
}))
|
|
93
|
+
)
|
|
94
|
+
),
|
|
95
|
+
Effect.map(([handle, exitCodeDeferred]): CommandExecutor.Process => {
|
|
96
|
+
let stdin: Sink.Sink<never, Error.PlatformError, unknown, never, void> = Sink.drain
|
|
70
97
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
98
|
+
if (handle.stdin !== null) {
|
|
99
|
+
stdin = fromWritable(
|
|
100
|
+
() => handle.stdin!,
|
|
101
|
+
(err) => toPlatformError("toWritable", toError(err), command)
|
|
102
|
+
)
|
|
103
|
+
}
|
|
75
104
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
105
|
+
const exitCode: CommandExecutor.Process["exitCode"] = Effect.flatMap(
|
|
106
|
+
Deferred.await(exitCodeDeferred),
|
|
107
|
+
([code, signal]) => {
|
|
108
|
+
if (code !== null) {
|
|
109
|
+
return Effect.succeed(CommandExecutor.ExitCode(code))
|
|
81
110
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
new globalThis.Error(`Process interrupted due to receipt of signal: ${handle.signalCode}`),
|
|
92
|
-
command
|
|
93
|
-
)
|
|
94
|
-
)
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
handle.on("exit", (code, signal) => {
|
|
99
|
-
if (code !== null) {
|
|
100
|
-
resume(Effect.succeed(CommandExecutor.ExitCode(code)))
|
|
101
|
-
} else {
|
|
102
|
-
// If code is `null`, then `signal` must be defined. See the NodeJS
|
|
103
|
-
// documentation for the `"exit"` event on a `child_process`.
|
|
104
|
-
// https://nodejs.org/api/child_process.html#child_process_event_exit
|
|
105
|
-
resume(
|
|
106
|
-
Effect.fail(
|
|
107
|
-
toPlatformError(
|
|
108
|
-
"exitCode",
|
|
109
|
-
new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`),
|
|
110
|
-
command
|
|
111
|
-
)
|
|
112
|
-
)
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
})
|
|
116
|
-
// Make sure to terminate the running process if the fiber is
|
|
117
|
-
// terminated
|
|
118
|
-
return Effect.sync(() => {
|
|
119
|
-
handle.kill("SIGKILL")
|
|
120
|
-
})
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
const isRunning = Effect.sync(() =>
|
|
124
|
-
handle.exitCode === null &&
|
|
125
|
-
handle.signalCode === null &&
|
|
126
|
-
!handle.killed
|
|
111
|
+
// If code is `null`, then `signal` must be defined. See the NodeJS
|
|
112
|
+
// documentation for the `"exit"` event on a `child_process`.
|
|
113
|
+
// https://nodejs.org/api/child_process.html#child_process_event_exit
|
|
114
|
+
return Effect.fail(
|
|
115
|
+
toPlatformError(
|
|
116
|
+
"exitCode",
|
|
117
|
+
new globalThis.Error(`Process interrupted due to receipt of signal: ${signal}`),
|
|
118
|
+
command
|
|
119
|
+
)
|
|
127
120
|
)
|
|
121
|
+
}
|
|
122
|
+
)
|
|
128
123
|
|
|
129
|
-
|
|
130
|
-
Effect.async((resume) => {
|
|
131
|
-
handle.kill(signal)
|
|
132
|
-
handle.on("exit", () => {
|
|
133
|
-
resume(Effect.unit)
|
|
134
|
-
})
|
|
135
|
-
// Make sure to terminate the running process if the fiber
|
|
136
|
-
// is terminated
|
|
137
|
-
return Effect.sync(() => {
|
|
138
|
-
handle.kill("SIGKILL")
|
|
139
|
-
})
|
|
140
|
-
})
|
|
124
|
+
const isRunning = Effect.negate(Deferred.isDone(exitCodeDeferred))
|
|
141
125
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
resume(Effect.unit)
|
|
177
|
-
})
|
|
178
|
-
})
|
|
179
|
-
})
|
|
180
|
-
),
|
|
126
|
+
const kill: CommandExecutor.Process["kill"] = (signal = "SIGTERM") =>
|
|
127
|
+
Effect.suspend(() =>
|
|
128
|
+
handle.kill(signal)
|
|
129
|
+
? Effect.asUnit(Deferred.await(exitCodeDeferred))
|
|
130
|
+
: Effect.fail(toPlatformError("kill", new globalThis.Error("Failed to kill process"), command))
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
const pid = CommandExecutor.ProcessId(handle.pid!)
|
|
134
|
+
const stderr = fromReadable<Error.PlatformError, Uint8Array>(
|
|
135
|
+
() => handle.stderr!,
|
|
136
|
+
(err) => toPlatformError("fromReadable(stderr)", toError(err), command)
|
|
137
|
+
)
|
|
138
|
+
let stdout: Stream.Stream<never, Error.PlatformError, Uint8Array> = fromReadable<
|
|
139
|
+
Error.PlatformError,
|
|
140
|
+
Uint8Array
|
|
141
|
+
>(
|
|
142
|
+
() => handle.stdout!,
|
|
143
|
+
(err) => toPlatformError("fromReadable(stdout)", toError(err), command)
|
|
144
|
+
)
|
|
145
|
+
// TODO: add Sink.isSink
|
|
146
|
+
if (typeof command.stdout !== "string") {
|
|
147
|
+
stdout = Stream.transduce(stdout, command.stdout)
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
|
|
151
|
+
pid,
|
|
152
|
+
exitCode,
|
|
153
|
+
isRunning,
|
|
154
|
+
kill,
|
|
155
|
+
stdin,
|
|
156
|
+
stderr,
|
|
157
|
+
stdout
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
181
160
|
Effect.tap((process) =>
|
|
182
161
|
Option.match(command.stdin, {
|
|
183
162
|
onNone: () => Effect.unit,
|
|
@@ -200,11 +179,13 @@ const runCommand =
|
|
|
200
179
|
pipe(
|
|
201
180
|
Command.stdin(command, stdin),
|
|
202
181
|
runCommand(fileSystem),
|
|
203
|
-
|
|
182
|
+
Effect.map((process) => process.stdout),
|
|
183
|
+
Stream.unwrapScoped
|
|
204
184
|
),
|
|
205
185
|
pipe(
|
|
206
186
|
runCommand(fileSystem)(head),
|
|
207
|
-
|
|
187
|
+
Effect.map((process) => process.stdout),
|
|
188
|
+
Stream.unwrapScoped
|
|
208
189
|
)
|
|
209
190
|
)
|
|
210
191
|
return pipe(Command.stdin(last, stream), runCommand(fileSystem))
|