@sshkeda/check 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sshkeda/check",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "homepage": "https://github.com/sshkeda/check#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/sshkeda/check/issues"
@@ -336,6 +336,15 @@ export class ToolExited extends Schema.TaggedError<ToolExited>()("ToolExited", {
336
336
  }
337
337
  }
338
338
 
339
+ export class ToolNotStarted extends Schema.TaggedError<ToolNotStarted>()("ToolNotStarted", {
340
+ command: Schema.String,
341
+ reason: Schema.String,
342
+ }) {
343
+ public override get message() {
344
+ return `${this.command} did not start: ${this.reason}`;
345
+ }
346
+ }
347
+
339
348
  export class ToolStoppedBySignal extends Schema.TaggedError<ToolStoppedBySignal>()(
340
349
  "ToolStoppedBySignal",
341
350
  { program: Schema.String, command: Schema.String, signal: Schema.String },
@@ -280,7 +280,11 @@ const compiledFilesOf = (
280
280
  const projectDirectory = path.dirname(yield* absolutePathOf(projectFile));
281
281
  const listedFilesOfEachProject = yield* Effect.forEach(projects, ([configFile, { program }]) =>
282
282
  typeScriptRequest("list the program files", () => program.getSourceFileNames()).pipe(
283
- Effect.map((fileNames) => ({ configFile, program, fileNames })),
283
+ Effect.map((fileNames) => ({
284
+ configFile,
285
+ program,
286
+ fileNames: fileNames.filter((fileName) => path.isAbsolute(fileName)),
287
+ })),
284
288
  ),
285
289
  );
286
290
  const everyListedFile = listedFilesOfEachProject.flatMap(({ fileNames }) => fileNames);
@@ -1,8 +1,15 @@
1
+ import { open } from "node:fs/promises";
2
+
3
+ import type { Subprocess } from "bun";
1
4
  import type { Array as Arrays, Schema } from "effect";
2
- import { Effect, Stream } from "effect";
3
- import { ChildProcess } from "effect/process";
5
+ import { Effect, FileSystem } from "effect";
4
6
 
5
- import { ToolExited, ToolReportUndecodable } from "./project-config-and-tool-errors.ts";
7
+ import {
8
+ ToolExited,
9
+ ToolNotStarted,
10
+ ToolReportUndecodable,
11
+ ToolStoppedBySignal,
12
+ } from "./project-config-and-tool-errors.ts";
6
13
 
7
14
  const LINE_BREAK = "\n";
8
15
 
@@ -48,6 +55,65 @@ export const mergedExit = (exits: Arrays.NonEmptyReadonlyArray<ToolExited>) => {
48
55
  });
49
56
  };
50
57
 
58
+ const WINDOWS = "win32";
59
+
60
+ const openedStandardOutputFile = (file: string) =>
61
+ Effect.acquireRelease(
62
+ Effect.promise(() => open(file, "w")),
63
+ (handle) => Effect.promise(() => handle.close()),
64
+ );
65
+
66
+ const stopTheProcessGroupOf = (child: Subprocess) =>
67
+ Effect.gen(function* () {
68
+ if (process.platform === WINDOWS) {
69
+ yield* Effect.try(() =>
70
+ Bun.spawn(["taskkill", "/pid", `${child.pid}`, "/T", "/F"], {
71
+ stdin: "ignore",
72
+ stdout: "ignore",
73
+ stderr: "ignore",
74
+ }),
75
+ ).pipe(
76
+ Effect.flatMap((taskkill) => Effect.promise(() => taskkill.exited)),
77
+ Effect.catchTag("UnknownError", () => Effect.void),
78
+ );
79
+ child.kill();
80
+ } else {
81
+ yield* Effect.try(() => process.kill(-child.pid, "SIGKILL")).pipe(
82
+ Effect.catchTag("UnknownError", () => Effect.void),
83
+ );
84
+ }
85
+ yield* Effect.promise(() => child.exited);
86
+ });
87
+
88
+ const spawnedTool = (
89
+ command: string,
90
+ binary: string,
91
+ toolArguments: readonly string[],
92
+ standardOutput: number,
93
+ workingDirectory?: string,
94
+ environment?: Record<string, string>,
95
+ ) =>
96
+ Effect.acquireRelease(
97
+ Effect.try({
98
+ try: () =>
99
+ Bun.spawn([binary, ...toolArguments], {
100
+ ...(workingDirectory === undefined ? {} : { cwd: workingDirectory }),
101
+ env: { ...process.env, ...environment },
102
+ detached: process.platform !== WINDOWS,
103
+ stdin: "ignore",
104
+ stdout: standardOutput,
105
+ stderr: "pipe",
106
+ }),
107
+ catch: (cause) =>
108
+ new ToolNotStarted({
109
+ command,
110
+ reason:
111
+ cause instanceof Error ? cause.message : "the spawn threw a value that is not an Error",
112
+ }),
113
+ }),
114
+ stopTheProcessGroupOf,
115
+ );
116
+
51
117
  export const runTool = (
52
118
  program: string,
53
119
  binary: string,
@@ -57,25 +123,33 @@ export const runTool = (
57
123
  ) =>
58
124
  Effect.scoped(
59
125
  Effect.gen(function* () {
60
- const child = yield* ChildProcess.make(binary, toolArguments, {
61
- ...(workingDirectory === undefined ? {} : { cwd: workingDirectory }),
62
- extendEnv: true,
63
- env: environment,
64
- stdin: "ignore",
65
- stderr: "pipe",
66
- });
67
- const [standardOutput, standardError, code] = yield* Effect.all(
126
+ const fileSystem = yield* FileSystem.FileSystem;
127
+ const standardOutputFile = yield* fileSystem.makeTempFileScoped();
128
+ const handle = yield* openedStandardOutputFile(standardOutputFile);
129
+ const command = [binary, ...toolArguments].join(" ");
130
+ const child = yield* spawnedTool(
131
+ command,
132
+ binary,
133
+ toolArguments,
134
+ handle.fd,
135
+ workingDirectory,
136
+ environment,
137
+ );
138
+ const [standardError, code] = yield* Effect.all(
68
139
  [
69
- Stream.mkString(Stream.decodeText(child.stdout)),
70
- Stream.mkString(Stream.decodeText(child.stderr)),
71
- child.exitCode,
140
+ Effect.promise(() => new Response(child.stderr).text()),
141
+ Effect.promise(() => child.exited),
72
142
  ],
73
143
  { concurrency: "unbounded" },
74
144
  );
145
+ const standardOutput = yield* fileSystem.readFileString(standardOutputFile);
146
+ if (child.signalCode !== null) {
147
+ return yield* new ToolStoppedBySignal({ program, command, signal: `${child.signalCode}` });
148
+ }
75
149
  if (code !== 0) {
76
150
  return yield* new ToolExited({
77
151
  program,
78
- command: [binary, ...toolArguments].join(" "),
152
+ command,
79
153
  code,
80
154
  standardOutput,
81
155
  standardError: standardError.trim(),