@sshkeda/check 4.0.0 → 4.0.1
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
|
@@ -13,6 +13,15 @@ export class TypeScriptApiFailed extends Schema.TaggedError<TypeScriptApiFailed>
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export class TypeScriptApiProcessStopped extends Schema.TaggedError<TypeScriptApiProcessStopped>()(
|
|
17
|
+
"TypeScriptApiProcessStopped",
|
|
18
|
+
{ reason: Schema.String },
|
|
19
|
+
) {
|
|
20
|
+
public override get message() {
|
|
21
|
+
return this.reason;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
export class TypeScriptReturnedNothing extends Schema.TaggedError<TypeScriptReturnedNothing>()(
|
|
17
26
|
"TypeScriptReturnedNothing",
|
|
18
27
|
{ operation: Schema.String },
|
|
@@ -33,7 +33,7 @@ import {
|
|
|
33
33
|
isTypeLiteralNode,
|
|
34
34
|
isVariableDeclaration,
|
|
35
35
|
} from "typescript/unstable/ast/is";
|
|
36
|
-
import {
|
|
36
|
+
import { SymbolFlags } from "typescript/unstable/async";
|
|
37
37
|
import type { Checker, Program, Symbol as TypeScriptSymbol, Type } from "typescript/unstable/async";
|
|
38
38
|
|
|
39
39
|
import { isDeclarationFile, isScript, packagePathOf } from "./classify-files-by-role.ts";
|
|
@@ -48,11 +48,13 @@ import {
|
|
|
48
48
|
ProgramFilesOutsideProject,
|
|
49
49
|
ProjectReferencesUndecodable,
|
|
50
50
|
TypeScriptApiFailed,
|
|
51
|
+
TypeScriptApiProcessStopped,
|
|
51
52
|
TypeScriptConfigUnparsable,
|
|
52
53
|
TypeScriptProjectFileMissing,
|
|
53
54
|
TypeScriptReturnedNothing,
|
|
54
55
|
} from "./project-config-and-tool-errors.ts";
|
|
55
56
|
import { absolutePathOf } from "./resolve-absolute-paths.ts";
|
|
57
|
+
import { openedApi } from "./watch-the-typescript-api-process.ts";
|
|
56
58
|
|
|
57
59
|
export const TYPESCRIPT_PROJECT_FILE = "tsconfig.json";
|
|
58
60
|
|
|
@@ -91,10 +93,16 @@ const PROJECT_REFERENCES = Schema.Struct({
|
|
|
91
93
|
references: Schema.optionalKey(Schema.Array(Schema.Struct({ path: Schema.String }))),
|
|
92
94
|
});
|
|
93
95
|
|
|
96
|
+
const isProcessStopped = Schema.is(TypeScriptApiProcessStopped);
|
|
97
|
+
|
|
94
98
|
export const typeScriptRequest = <Value>(operation: string, request: () => Promise<Value>) =>
|
|
95
99
|
Effect.tryPromise({
|
|
96
100
|
try: request,
|
|
97
|
-
catch: (cause) =>
|
|
101
|
+
catch: (cause) =>
|
|
102
|
+
new TypeScriptApiFailed({
|
|
103
|
+
operation,
|
|
104
|
+
reason: isProcessStopped(cause) ? cause.reason : String(cause),
|
|
105
|
+
}),
|
|
98
106
|
});
|
|
99
107
|
|
|
100
108
|
export const typeMembers = (type: Type) =>
|
|
@@ -204,19 +212,6 @@ export const projectConfigFiles = (projectFile: string) =>
|
|
|
204
212
|
return configFiles;
|
|
205
213
|
});
|
|
206
214
|
|
|
207
|
-
const openedApi = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
208
|
-
Effect.acquireRelease(
|
|
209
|
-
Effect.sync(
|
|
210
|
-
() =>
|
|
211
|
-
new API(
|
|
212
|
-
sourceTextOverrides.size === 0
|
|
213
|
-
? {}
|
|
214
|
-
: { fs: { readFile: (fileName) => sourceTextOverrides.get(fileName) } },
|
|
215
|
-
),
|
|
216
|
-
),
|
|
217
|
-
(openApi) => Effect.promise(() => openApi.close()),
|
|
218
|
-
);
|
|
219
|
-
|
|
220
215
|
const parseCompilerOptionsAndInputs = (configFile: string) =>
|
|
221
216
|
Effect.gen(function* () {
|
|
222
217
|
const path = yield* Path.Path;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Context, Deferred, Effect, Option, Predicate, Schema } from "effect";
|
|
2
|
+
import type { Duration } from "effect";
|
|
3
|
+
import { API } from "typescript/unstable/async";
|
|
4
|
+
import type { InternalAPI } from "typescript/unstable/async";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
TypeScriptApiFailed,
|
|
8
|
+
TypeScriptApiProcessStopped,
|
|
9
|
+
} from "./project-config-and-tool-errors.ts";
|
|
10
|
+
|
|
11
|
+
const TYPESCRIPT_REQUEST_DEADLINE = "10 minutes";
|
|
12
|
+
|
|
13
|
+
const EXIT_DEADLINE = "10 seconds";
|
|
14
|
+
|
|
15
|
+
const STARTING = "start";
|
|
16
|
+
|
|
17
|
+
const CLIENT_CHANGED = "the TypeScript API client no longer exposes the process Check watches";
|
|
18
|
+
|
|
19
|
+
const CLOSED = "the TypeScript API was closed";
|
|
20
|
+
|
|
21
|
+
const OUTPUT_CLOSED = "the TypeScript API process closed its output";
|
|
22
|
+
|
|
23
|
+
const TERMINATING_SIGNAL = "SIGKILL";
|
|
24
|
+
|
|
25
|
+
const API_CLIENT_FIELD = "client";
|
|
26
|
+
|
|
27
|
+
const CLIENT_PROCESS_FIELD = "process";
|
|
28
|
+
|
|
29
|
+
export const TypeScriptApiLaunch = Context.Reference<{
|
|
30
|
+
readonly tsserverPath?: string;
|
|
31
|
+
readonly requestDeadline: `${number} ${Duration.Unit}`;
|
|
32
|
+
}>("@sshkeda/check/TypeScriptApiLaunch", {
|
|
33
|
+
defaultValue: () => ({ requestDeadline: TYPESCRIPT_REQUEST_DEADLINE }),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const CLIENT_MEMBERS = [
|
|
37
|
+
"connect",
|
|
38
|
+
"apiRequest",
|
|
39
|
+
"close",
|
|
40
|
+
] as const satisfies readonly (keyof ConstructorParameters<typeof InternalAPI>[0])[];
|
|
41
|
+
|
|
42
|
+
const TYPESCRIPT_API_CLIENT = Schema.declare(
|
|
43
|
+
(value): value is ConstructorParameters<typeof InternalAPI>[0] =>
|
|
44
|
+
CLIENT_MEMBERS.every((member) => Predicate.hasProperty(value, member)),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const PROCESS_MEMBERS = ["once", "on", "kill", "stdout"] as const;
|
|
48
|
+
|
|
49
|
+
const isWatchableProcess = Schema.is(
|
|
50
|
+
Schema.declare(
|
|
51
|
+
(
|
|
52
|
+
value,
|
|
53
|
+
): value is {
|
|
54
|
+
readonly stdout: { once(event: "close", listener: () => void): void } | null;
|
|
55
|
+
once(
|
|
56
|
+
event: "exit",
|
|
57
|
+
listener: (code: number | null, signal: NodeJS.Signals | null) => void,
|
|
58
|
+
): void;
|
|
59
|
+
on(event: "error", listener: (error: Error) => void): void;
|
|
60
|
+
kill(signal: NodeJS.Signals): boolean;
|
|
61
|
+
} => PROCESS_MEMBERS.every((member) => Predicate.hasProperty(value, member)),
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const stopWith = (
|
|
66
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
67
|
+
child: { kill(signal: NodeJS.Signals): boolean },
|
|
68
|
+
reason: string,
|
|
69
|
+
) => {
|
|
70
|
+
Deferred.doneUnsafe(stopped, Effect.fail(new TypeScriptApiProcessStopped({ reason })));
|
|
71
|
+
child.kill(TERMINATING_SIGNAL);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const exitReason = (code: number | null, signal: NodeJS.Signals | null, outputClosed: boolean) => {
|
|
75
|
+
if (code !== null) {
|
|
76
|
+
return `the TypeScript API process exited with code ${code}`;
|
|
77
|
+
}
|
|
78
|
+
return outputClosed ? OUTPUT_CLOSED : `the TypeScript API process exited on signal ${signal}`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const watchedProcess = (
|
|
82
|
+
client: ConstructorParameters<typeof InternalAPI>[0],
|
|
83
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
84
|
+
exited: Deferred.Deferred<void>,
|
|
85
|
+
) => {
|
|
86
|
+
const child: unknown = client[CLIENT_PROCESS_FIELD];
|
|
87
|
+
if (!isWatchableProcess(child)) {
|
|
88
|
+
return client.close().then(() => Option.none());
|
|
89
|
+
}
|
|
90
|
+
const output = { closed: false };
|
|
91
|
+
child.once("exit", (code, signal) => {
|
|
92
|
+
stopWith(stopped, child, exitReason(code, signal, output.closed));
|
|
93
|
+
Deferred.doneUnsafe(exited, Effect.void);
|
|
94
|
+
});
|
|
95
|
+
child.on("error", (error) => {
|
|
96
|
+
stopWith(stopped, child, `the TypeScript API process failed: ${error.message}`);
|
|
97
|
+
});
|
|
98
|
+
child.stdout?.once("close", () => {
|
|
99
|
+
output.closed = true;
|
|
100
|
+
child.kill(TERMINATING_SIGNAL);
|
|
101
|
+
});
|
|
102
|
+
return Promise.resolve(Option.some(child));
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const answered = <Answer>(
|
|
106
|
+
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
107
|
+
child: Parameters<typeof stopWith>[1],
|
|
108
|
+
requestDeadline: `${number} ${Duration.Unit}`,
|
|
109
|
+
request: () => Promise<Answer>,
|
|
110
|
+
) =>
|
|
111
|
+
Effect.gen(function* () {
|
|
112
|
+
if (yield* Deferred.isDone(stopped)) {
|
|
113
|
+
return yield* Deferred.await(stopped);
|
|
114
|
+
}
|
|
115
|
+
return yield* Effect.raceFirst(Effect.promise(request), Deferred.await(stopped)).pipe(
|
|
116
|
+
Effect.timeoutOrElse({
|
|
117
|
+
duration: requestDeadline,
|
|
118
|
+
orElse: () =>
|
|
119
|
+
Effect.sync(() => {
|
|
120
|
+
stopWith(
|
|
121
|
+
stopped,
|
|
122
|
+
child,
|
|
123
|
+
`the TypeScript API process did not answer within ${requestDeadline}`,
|
|
124
|
+
);
|
|
125
|
+
}).pipe(Effect.andThen(Deferred.await(stopped))),
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const startedSession = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
131
|
+
Effect.gen(function* () {
|
|
132
|
+
const { requestDeadline, ...spawnOptions } = yield* TypeScriptApiLaunch;
|
|
133
|
+
const api = new API(
|
|
134
|
+
sourceTextOverrides.size === 0
|
|
135
|
+
? spawnOptions
|
|
136
|
+
: { ...spawnOptions, fs: { readFile: (fileName) => sourceTextOverrides.get(fileName) } },
|
|
137
|
+
);
|
|
138
|
+
const client = yield* Schema.decodeUnknownEffect(TYPESCRIPT_API_CLIENT)(
|
|
139
|
+
api[API_CLIENT_FIELD],
|
|
140
|
+
).pipe(
|
|
141
|
+
Effect.mapError(
|
|
142
|
+
() => new TypeScriptApiFailed({ operation: STARTING, reason: CLIENT_CHANGED }),
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
const stopped = yield* Deferred.make<never, TypeScriptApiProcessStopped>();
|
|
146
|
+
const exited = yield* Deferred.make<void>();
|
|
147
|
+
const watched = yield* Effect.tryPromise({
|
|
148
|
+
try: () => client.connect().then(() => watchedProcess(client, stopped, exited)),
|
|
149
|
+
catch: (cause) => new TypeScriptApiFailed({ operation: STARTING, reason: String(cause) }),
|
|
150
|
+
});
|
|
151
|
+
if (Option.isNone(watched)) {
|
|
152
|
+
return yield* new TypeScriptApiFailed({ operation: STARTING, reason: CLIENT_CHANGED });
|
|
153
|
+
}
|
|
154
|
+
const child = watched.value;
|
|
155
|
+
const runPromise = Effect.runPromiseWith(yield* Effect.context());
|
|
156
|
+
const send = client.apiRequest.bind(client);
|
|
157
|
+
client.apiRequest = <Answer>(...request: Parameters<typeof send>) =>
|
|
158
|
+
runPromise(answered(stopped, child, requestDeadline, () => send<Answer>(...request)));
|
|
159
|
+
return { api, client, child, stopped, exited };
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const closedSession = ({
|
|
163
|
+
client,
|
|
164
|
+
child,
|
|
165
|
+
stopped,
|
|
166
|
+
exited,
|
|
167
|
+
}: Effect.Success<ReturnType<typeof startedSession>>) =>
|
|
168
|
+
Effect.gen(function* () {
|
|
169
|
+
stopWith(stopped, child, CLOSED);
|
|
170
|
+
yield* Effect.promise(() => client.close());
|
|
171
|
+
yield* Deferred.await(exited).pipe(Effect.timeoutOption(EXIT_DEADLINE));
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
export const openedApi = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
175
|
+
Effect.acquireRelease(startedSession(sourceTextOverrides), closedSession).pipe(
|
|
176
|
+
Effect.map(({ api }) => api),
|
|
177
|
+
);
|