@cvr/linear 0.1.0 → 0.3.0

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,192 +1,493 @@
1
- import { Context, Effect, Layer, Option, Ref } from "effect"
2
- import { LinearClient, Issue, User, Team } from "@linear/sdk"
3
- import { LinearApiError, InvalidTokenError, TokenNotFoundError } from "../lib/errors.js"
4
- import { ConfigService } from "./Config.js"
1
+ import { Context, Effect, Layer } from "effect";
2
+ import { LinearClient } from "@linear/sdk";
3
+ import type { Issue, Project, Team, User } from "@linear/sdk";
4
+ import { LinearApiError, InvalidTokenError, TokenNotFoundError } from "../lib/errors.js";
5
+ import { ConfigService } from "./Config.js";
5
6
 
6
7
  export interface IssueFilter {
7
- readonly teamId?: string
8
- readonly state?: "triage" | "backlog" | "unstarted" | "started" | "completed" | "canceled"
9
- readonly assigneeId?: string
10
- readonly limit?: number
8
+ readonly teamId?: string;
9
+ readonly state?: "triage" | "backlog" | "unstarted" | "started" | "completed" | "canceled";
10
+ readonly assigneeId?: string;
11
+ readonly limit?: number;
11
12
  }
12
13
 
13
- export class LinearService extends Context.Tag("@linear/LinearService")<
14
+ export interface IssueDetailsOptions {
15
+ readonly comments: boolean;
16
+ readonly children: boolean;
17
+ readonly relations: boolean;
18
+ }
19
+
20
+ export interface IssueReference {
21
+ readonly id: string;
22
+ readonly identifier: string;
23
+ readonly title: string;
24
+ readonly url: string;
25
+ }
26
+
27
+ export interface IssueDetails extends IssueReference {
28
+ readonly description?: string;
29
+ readonly branchName: string;
30
+ readonly priority: {
31
+ readonly value: number;
32
+ readonly label: string;
33
+ };
34
+ readonly state?: {
35
+ readonly id: string;
36
+ readonly name: string;
37
+ readonly type: string;
38
+ };
39
+ readonly team?: {
40
+ readonly id: string;
41
+ readonly key: string;
42
+ readonly name: string;
43
+ };
44
+ readonly assignee?: {
45
+ readonly id: string;
46
+ readonly name: string;
47
+ readonly email: string;
48
+ };
49
+ readonly project?: {
50
+ readonly id: string;
51
+ readonly name: string;
52
+ readonly url: string;
53
+ };
54
+ readonly parent?: IssueReference;
55
+ readonly labels: ReadonlyArray<{ readonly id: string; readonly name: string }>;
56
+ readonly children?: ReadonlyArray<IssueReference>;
57
+ readonly comments?: ReadonlyArray<{
58
+ readonly id: string;
59
+ readonly body: string;
60
+ readonly createdAt: string;
61
+ readonly url: string;
62
+ readonly author?: { readonly id: string; readonly name: string };
63
+ }>;
64
+ readonly relations?: ReadonlyArray<{
65
+ readonly id: string;
66
+ readonly type: string;
67
+ readonly direction: "outbound" | "inbound";
68
+ readonly issue: IssueReference;
69
+ }>;
70
+ }
71
+
72
+ export class LinearService extends Context.Service<
14
73
  LinearService,
15
74
  {
16
- readonly getViewer: Effect.Effect<User, LinearApiError | InvalidTokenError | TokenNotFoundError>
17
- readonly getTeams: Effect.Effect<readonly Team[], LinearApiError | TokenNotFoundError>
18
- readonly getIssues: (filter: IssueFilter) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>
19
- readonly getIssue: (id: string) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>
20
- readonly getMyIssues: (filter?: { state?: string }) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>
21
- readonly updateIssueState: (issueId: string, stateId: string) => Effect.Effect<void, LinearApiError | TokenNotFoundError>
75
+ readonly getViewer: Effect.Effect<
76
+ User,
77
+ LinearApiError | InvalidTokenError | TokenNotFoundError
78
+ >;
79
+ readonly getTeams: Effect.Effect<readonly Team[], LinearApiError | TokenNotFoundError>;
80
+ readonly getProjects: Effect.Effect<readonly Project[], LinearApiError | TokenNotFoundError>;
81
+ readonly getIssues: (
82
+ filter: IssueFilter,
83
+ ) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
84
+ readonly getIssue: (id: string) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
85
+ readonly getIssueDetails: (
86
+ id: string,
87
+ options: IssueDetailsOptions,
88
+ ) => Effect.Effect<IssueDetails, LinearApiError | TokenNotFoundError>;
89
+ readonly getMyIssues: (filter?: {
90
+ state?: string;
91
+ }) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
92
+ readonly updateIssueState: (
93
+ issueId: string,
94
+ stateId: string,
95
+ ) => Effect.Effect<void, LinearApiError | TokenNotFoundError>;
22
96
  readonly createIssue: (input: {
23
- title: string
24
- teamId: string
25
- description?: string
26
- }) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>
97
+ title: string;
98
+ teamId: string;
99
+ description?: string;
100
+ parentId?: string;
101
+ projectId?: string;
102
+ priority?: number;
103
+ }) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
104
+ readonly createComment: (
105
+ issueId: string,
106
+ body: string,
107
+ ) => Effect.Effect<
108
+ { readonly id: string; readonly url: string },
109
+ LinearApiError | TokenNotFoundError
110
+ >;
111
+ readonly rawQuery: (
112
+ query: string,
113
+ variables: Readonly<Record<string, unknown>>,
114
+ ) => Effect.Effect<unknown, LinearApiError | TokenNotFoundError>;
27
115
  }
28
- >() {
116
+ >()("@cvr/linear/services/Linear/LinearService") {
29
117
  static readonly layer = Layer.effect(
30
118
  LinearService,
31
119
  Effect.gen(function* () {
32
- const configService = yield* ConfigService
33
-
34
- // Cache the client once token is loaded
35
- const clientRef = yield* Ref.make<LinearClient | null>(null)
120
+ const configService = yield* ConfigService;
36
121
 
37
- // Get or create client lazily
38
- const getClient = Effect.gen(function* () {
39
- const cached = yield* Ref.get(clientRef)
40
- if (cached) return cached
41
-
42
- const token = yield* configService.getToken
43
- const client = new LinearClient({ apiKey: token })
44
- yield* Ref.set(clientRef, client)
45
- return client
46
- })
122
+ // Lazy client creation - token is loaded when first API call is made
123
+ const getClient = configService.getToken.pipe(
124
+ Effect.catchTag("ConfigError", (e) => TokenNotFoundError.make({ message: e.message })),
125
+ Effect.map((token) => new LinearClient({ apiKey: token })),
126
+ Effect.cached,
127
+ Effect.runSync,
128
+ );
47
129
 
48
130
  const getViewer = Effect.gen(function* () {
49
- const client = yield* getClient
50
- const viewer = yield* Effect.tryPromise({
131
+ const client = yield* getClient;
132
+ return yield* Effect.tryPromise({
51
133
  try: () => client.viewer,
52
134
  catch: (e) => {
53
- const msg = String(e)
135
+ const msg = String(e);
54
136
  if (msg.includes("Authentication") || msg.includes("401")) {
55
- return InvalidTokenError.make({ message: "Invalid API token" })
137
+ return InvalidTokenError.make({ message: "Invalid API token" });
56
138
  }
57
- return LinearApiError.make({ message: msg })
139
+ return LinearApiError.make({ message: msg });
58
140
  },
59
- })
60
- return viewer
61
- })
141
+ });
142
+ }).pipe(Effect.withSpan("LinearService.getViewer"));
62
143
 
63
144
  const getTeams = Effect.gen(function* () {
64
- const client = yield* getClient
65
- const teams = yield* Effect.tryPromise({
66
- try: async () => {
67
- const connection = await client.teams()
68
- return connection.nodes
69
- },
145
+ const client = yield* getClient;
146
+ const connection = yield* Effect.tryPromise({
147
+ try: () => client.teams(),
70
148
  catch: (e) => LinearApiError.make({ message: String(e) }),
71
- })
72
- return teams
73
- })
149
+ });
150
+ return connection.nodes;
151
+ }).pipe(Effect.withSpan("LinearService.getTeams"));
74
152
 
75
- const getIssues = (filter: IssueFilter) =>
153
+ const getProjects = Effect.gen(function* () {
154
+ const client = yield* getClient;
155
+ const connection = yield* Effect.tryPromise({
156
+ try: () => client.projects(),
157
+ catch: (error) => LinearApiError.make({ message: String(error) }),
158
+ });
159
+ return connection.nodes;
160
+ }).pipe(Effect.withSpan("LinearService.getProjects"));
161
+
162
+ const getIssues = Effect.fn("LinearService.getIssues")((filter: IssueFilter) =>
76
163
  Effect.gen(function* () {
77
- const client = yield* getClient
78
- const issues = yield* Effect.tryPromise({
79
- try: async () => {
80
- const connection = await client.issues({
164
+ const client = yield* getClient;
165
+ const connection = yield* Effect.tryPromise({
166
+ try: () =>
167
+ client.issues({
81
168
  first: filter.limit ?? 50,
82
169
  filter: {
83
- team: filter.teamId ? { id: { eq: filter.teamId } } : undefined,
84
- state: filter.state ? { type: { eq: filter.state } } : undefined,
85
- assignee: filter.assigneeId
86
- ? { id: { eq: filter.assigneeId } }
87
- : undefined,
170
+ team: filter.teamId !== undefined ? { id: { eq: filter.teamId } } : undefined,
171
+ state: filter.state !== undefined ? { type: { eq: filter.state } } : undefined,
172
+ assignee:
173
+ filter.assigneeId !== undefined ? { id: { eq: filter.assigneeId } } : undefined,
88
174
  },
89
- })
90
- return connection.nodes
91
- },
175
+ }),
92
176
  catch: (e) => LinearApiError.make({ message: String(e) }),
93
- })
94
- return issues
95
- })
177
+ });
178
+ return connection.nodes;
179
+ }),
180
+ );
96
181
 
97
- const getMyIssues = (filter?: { state?: string }) =>
182
+ const getMyIssues = Effect.fn("LinearService.getMyIssues")((filter?: { state?: string }) =>
98
183
  Effect.gen(function* () {
99
- const client = yield* getClient
100
- const issues = yield* Effect.tryPromise({
101
- try: async () => {
102
- const viewer = await client.viewer
103
- const connection = await viewer.assignedIssues({
184
+ const client = yield* getClient;
185
+ const viewer = yield* Effect.tryPromise({
186
+ try: () => client.viewer,
187
+ catch: (e) => LinearApiError.make({ message: String(e) }),
188
+ });
189
+ const connection = yield* Effect.tryPromise({
190
+ try: () =>
191
+ viewer.assignedIssues({
104
192
  first: 50,
105
- filter: filter?.state
106
- ? { state: { type: { eq: filter.state } } }
107
- : undefined,
108
- })
109
- return connection.nodes
110
- },
193
+ filter:
194
+ filter?.state !== undefined
195
+ ? { state: { type: { eq: filter.state } } }
196
+ : undefined,
197
+ }),
111
198
  catch: (e) => LinearApiError.make({ message: String(e) }),
112
- })
113
- return issues
114
- })
199
+ });
200
+ return connection.nodes;
201
+ }),
202
+ );
115
203
 
116
- const getIssue = (id: string) =>
204
+ const getIssue = Effect.fn("LinearService.getIssue")((id: string) =>
117
205
  Effect.gen(function* () {
118
- const client = yield* getClient
119
- const issue = yield* Effect.tryPromise({
206
+ const client = yield* getClient;
207
+ return yield* Effect.tryPromise({
120
208
  try: () => client.issue(id),
121
209
  catch: (e) => LinearApiError.make({ message: String(e) }),
122
- })
123
- return issue
124
- })
210
+ });
211
+ }),
212
+ );
125
213
 
126
- const updateIssueState = (issueId: string, stateId: string) =>
127
- Effect.gen(function* () {
128
- const client = yield* getClient
129
- yield* Effect.tryPromise({
130
- try: () => client.updateIssue(issueId, { stateId }),
131
- catch: (e) => LinearApiError.make({ message: String(e) }),
132
- })
133
- })
134
-
135
- const createIssue = (input: {
136
- title: string
137
- teamId: string
138
- description?: string
139
- }) =>
140
- Effect.gen(function* () {
141
- const client = yield* getClient
142
- const result = yield* Effect.tryPromise({
143
- try: async () => {
144
- const payload = await client.createIssue({
145
- title: input.title,
146
- teamId: input.teamId,
147
- description: input.description,
148
- })
149
- const issue = await payload.issue
150
- if (!issue) throw new Error("Failed to create issue")
151
- return issue
152
- },
153
- catch: (e) => LinearApiError.make({ message: String(e) }),
154
- })
155
- return result
156
- })
214
+ const getIssueDetails = Effect.fn("LinearService.getIssueDetails")(
215
+ (id: string, options: IssueDetailsOptions) =>
216
+ Effect.gen(function* () {
217
+ const issue = yield* getIssue(normalizeIssueId(id));
218
+ const [state, team, assignee, project, parent, labels] = yield* Effect.all([
219
+ resolveLinearFetch(issue.state),
220
+ resolveLinearFetch(issue.team),
221
+ resolveLinearFetch(issue.assignee),
222
+ resolveLinearFetch(issue.project),
223
+ resolveLinearFetch(issue.parent),
224
+ loadConnection(() => issue.labels()),
225
+ ]);
226
+
227
+ const children = options.children
228
+ ? yield* loadConnection(() => issue.children())
229
+ : undefined;
230
+ const comments = options.comments
231
+ ? yield* loadConnection(() => issue.comments())
232
+ : undefined;
233
+ const outboundRelations = options.relations
234
+ ? yield* loadConnection(() => issue.relations())
235
+ : undefined;
236
+ const inboundRelations = options.relations
237
+ ? yield* loadConnection(() => issue.inverseRelations())
238
+ : undefined;
239
+
240
+ const detailedComments =
241
+ comments === undefined
242
+ ? undefined
243
+ : yield* Effect.forEach(
244
+ comments,
245
+ (comment) =>
246
+ resolveLinearFetch(comment.user).pipe(
247
+ Effect.map((author) => ({
248
+ id: comment.id,
249
+ body: comment.body,
250
+ createdAt: comment.createdAt.toISOString(),
251
+ url: comment.url,
252
+ author:
253
+ author === undefined ? undefined : { id: author.id, name: author.name },
254
+ })),
255
+ ),
256
+ { concurrency: 4 },
257
+ );
258
+
259
+ const relations =
260
+ outboundRelations === undefined || inboundRelations === undefined
261
+ ? undefined
262
+ : yield* Effect.forEach(
263
+ outboundRelations
264
+ .map(
265
+ (
266
+ relation,
267
+ ): {
268
+ readonly relation: typeof relation;
269
+ readonly direction: "outbound" | "inbound";
270
+ } => ({ relation, direction: "outbound" }),
271
+ )
272
+ .concat(
273
+ inboundRelations.map((relation) => ({
274
+ relation,
275
+ direction: "inbound" as const,
276
+ })),
277
+ ),
278
+ ({ direction, relation }) =>
279
+ resolveLinearFetch(
280
+ direction === "outbound" ? relation.relatedIssue : relation.issue,
281
+ ).pipe(
282
+ Effect.flatMap((relatedIssue) =>
283
+ relatedIssue === undefined
284
+ ? Effect.succeed(undefined)
285
+ : Effect.succeed({
286
+ id: relation.id,
287
+ type: relation.type,
288
+ direction,
289
+ issue: toIssueReference(relatedIssue),
290
+ }),
291
+ ),
292
+ ),
293
+ { concurrency: 4 },
294
+ ).pipe(
295
+ Effect.map((items) =>
296
+ items.filter((item): item is NonNullable<typeof item> => item !== undefined),
297
+ ),
298
+ );
299
+
300
+ return {
301
+ id: issue.id,
302
+ identifier: issue.identifier,
303
+ title: issue.title,
304
+ url: issue.url,
305
+ description: issue.description ?? undefined,
306
+ branchName: issue.branchName,
307
+ priority: { value: issue.priority, label: issue.priorityLabel },
308
+ state:
309
+ state === undefined
310
+ ? undefined
311
+ : { id: state.id, name: state.name, type: state.type },
312
+ team:
313
+ team === undefined ? undefined : { id: team.id, key: team.key, name: team.name },
314
+ assignee:
315
+ assignee === undefined
316
+ ? undefined
317
+ : { id: assignee.id, name: assignee.name, email: assignee.email },
318
+ project:
319
+ project === undefined
320
+ ? undefined
321
+ : { id: project.id, name: project.name, url: project.url },
322
+ parent: parent === undefined ? undefined : toIssueReference(parent),
323
+ labels: labels.map((label) => ({ id: label.id, name: label.name })),
324
+ children: children?.map(toIssueReference),
325
+ comments: detailedComments,
326
+ relations,
327
+ } satisfies IssueDetails;
328
+ }),
329
+ );
330
+
331
+ const updateIssueState = Effect.fn("LinearService.updateIssueState")(
332
+ (issueId: string, stateId: string) =>
333
+ Effect.gen(function* () {
334
+ const client = yield* getClient;
335
+ yield* Effect.tryPromise({
336
+ try: () => client.updateIssue(issueId, { stateId }),
337
+ catch: (e) => LinearApiError.make({ message: String(e) }),
338
+ });
339
+ }),
340
+ );
341
+
342
+ const createIssue = Effect.fn("LinearService.createIssue")(
343
+ (input: {
344
+ title: string;
345
+ teamId: string;
346
+ description?: string;
347
+ parentId?: string;
348
+ projectId?: string;
349
+ priority?: number;
350
+ }) =>
351
+ Effect.gen(function* () {
352
+ const client = yield* getClient;
353
+ const payload = yield* Effect.tryPromise({
354
+ try: () =>
355
+ client.createIssue({
356
+ title: input.title,
357
+ teamId: input.teamId,
358
+ description: input.description,
359
+ parentId: input.parentId,
360
+ projectId: input.projectId,
361
+ priority: input.priority,
362
+ }),
363
+ catch: (e) => LinearApiError.make({ message: String(e) }),
364
+ });
365
+ const issueFetch = payload.issue;
366
+ if (issueFetch === undefined) {
367
+ return yield* LinearApiError.make({ message: "Failed to create issue" });
368
+ }
369
+ return yield* Effect.tryPromise({
370
+ try: () => issueFetch,
371
+ catch: (e) => LinearApiError.make({ message: String(e) }),
372
+ });
373
+ }),
374
+ );
375
+
376
+ const createComment = Effect.fn("LinearService.createComment")(
377
+ (issueId: string, body: string) =>
378
+ Effect.gen(function* () {
379
+ const client = yield* getClient;
380
+ const payload = yield* Effect.tryPromise({
381
+ try: () => client.createComment({ issueId: normalizeIssueId(issueId), body }),
382
+ catch: (error) => LinearApiError.make({ message: String(error) }),
383
+ });
384
+ const comment = yield* resolveLinearFetch(payload.comment);
385
+ return comment === undefined
386
+ ? yield* LinearApiError.make({ message: "Linear did not return the created comment" })
387
+ : { id: comment.id, url: comment.url };
388
+ }),
389
+ );
390
+
391
+ const rawQuery = Effect.fn("LinearService.rawQuery")(
392
+ (query: string, variables: Readonly<Record<string, unknown>>) =>
393
+ Effect.gen(function* () {
394
+ const client = yield* getClient;
395
+ const response = yield* Effect.tryPromise({
396
+ try: () =>
397
+ client.client.rawRequest<unknown, Record<string, unknown>>(query, variables),
398
+ catch: (error) => LinearApiError.make({ message: String(error) }),
399
+ });
400
+ return response.data;
401
+ }),
402
+ );
157
403
 
158
404
  return LinearService.of({
159
405
  getViewer,
160
406
  getTeams,
407
+ getProjects,
161
408
  getIssues,
162
409
  getMyIssues,
163
410
  getIssue,
411
+ getIssueDetails,
164
412
  updateIssueState,
165
413
  createIssue,
166
- })
167
- })
168
- )
169
-
170
- static readonly testLayer = (options?: {
171
- viewer?: User
172
- teams?: readonly Team[]
173
- issues?: readonly Issue[]
414
+ createComment,
415
+ rawQuery,
416
+ });
417
+ }),
418
+ );
419
+
420
+ static readonly layerTest = (options?: {
421
+ viewer?: User;
422
+ teams?: readonly Team[];
423
+ issues?: readonly Issue[];
174
424
  }) =>
175
425
  Layer.succeed(LinearService, {
176
- getViewer: options?.viewer
177
- ? Effect.succeed(options.viewer)
178
- : Effect.fail(LinearApiError.make({ message: "No viewer in test" })),
426
+ getViewer:
427
+ options?.viewer !== undefined
428
+ ? Effect.succeed(options.viewer)
429
+ : Effect.fail(LinearApiError.make({ message: "No viewer in test" })),
179
430
  getTeams: Effect.succeed(options?.teams ?? []),
431
+ getProjects: Effect.succeed([]),
180
432
  getIssues: (_filter) => Effect.succeed(options?.issues ?? []),
181
433
  getMyIssues: (_filter) => Effect.succeed(options?.issues ?? []),
182
434
  getIssue: (id) => {
183
- const issue = options?.issues?.find((i) => i.id === id || (i as any).identifier === id)
184
- return issue
435
+ const issue = options?.issues?.find((i) => i.id === id || i.identifier === id);
436
+ return issue !== undefined
185
437
  ? Effect.succeed(issue)
186
- : Effect.fail(LinearApiError.make({ message: `Issue ${id} not found` }))
438
+ : Effect.fail(LinearApiError.make({ message: `Issue ${id} not found` }));
439
+ },
440
+ getIssueDetails: (id, _options) => {
441
+ const issue = options?.issues?.find(
442
+ (candidate) => candidate.id === id || candidate.identifier === id,
443
+ );
444
+ return issue === undefined
445
+ ? Effect.fail(LinearApiError.make({ message: `Issue ${id} not found` }))
446
+ : Effect.succeed({
447
+ id: issue.id,
448
+ identifier: issue.identifier,
449
+ title: issue.title,
450
+ url: issue.url,
451
+ description: issue.description ?? undefined,
452
+ branchName: issue.branchName,
453
+ priority: { value: issue.priority, label: issue.priorityLabel },
454
+ labels: [],
455
+ });
187
456
  },
188
457
  updateIssueState: (_issueId, _stateId) => Effect.void,
189
458
  createIssue: (_input) =>
190
459
  Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
191
- })
460
+ createComment: (_issueId, _body) =>
461
+ Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
462
+ rawQuery: (_query, _variables) =>
463
+ Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
464
+ });
192
465
  }
466
+
467
+ const normalizeIssueId = (input: string): string => {
468
+ const match = input.match(/\/issue\/([A-Za-z]+-\d+)(?:\/|$)/);
469
+ return match?.[1] ?? input;
470
+ };
471
+
472
+ const toIssueReference = (issue: Issue): IssueReference => ({
473
+ id: issue.id,
474
+ identifier: issue.identifier,
475
+ title: issue.title,
476
+ url: issue.url,
477
+ });
478
+
479
+ const resolveLinearFetch = <T>(
480
+ fetch: PromiseLike<T> | undefined,
481
+ ): Effect.Effect<T | undefined, LinearApiError> =>
482
+ fetch === undefined
483
+ ? Effect.succeed<T | undefined>(undefined)
484
+ : Effect.tryPromise({
485
+ try: () => Promise.resolve(fetch),
486
+ catch: (error) => LinearApiError.make({ message: String(error) }),
487
+ });
488
+
489
+ const loadConnection = <T>(load: () => PromiseLike<{ readonly nodes: ReadonlyArray<T> }>) =>
490
+ Effect.tryPromise({
491
+ try: () => Promise.resolve(load()),
492
+ catch: (error) => LinearApiError.make({ message: String(error) }),
493
+ }).pipe(Effect.map((connection) => connection.nodes));
@@ -1,34 +1,27 @@
1
- import { Context, Effect, Layer } from "effect"
1
+ import { Console, Context, Effect, Layer, Terminal } from "effect";
2
2
 
3
3
  /**
4
4
  * Simple stdin reading service that works with Bun compiled binaries.
5
5
  * Uses Node's readline API which Bun supports.
6
6
  */
7
- export class StdinService extends Context.Tag("StdinService")<
7
+ export class StdinService extends Context.Service<
8
8
  StdinService,
9
9
  {
10
- readonly readLine: (prompt: string) => Effect.Effect<string>
10
+ readonly readLine: (prompt: string) => Effect.Effect<string, Terminal.QuitError>;
11
11
  }
12
- >() {
13
- static readonly layer = Layer.succeed(StdinService, {
14
- readLine: (prompt) =>
15
- Effect.async<string>((resume) => {
16
- // Use Node's readline API (Bun compatible)
17
- const readline = require("node:readline")
18
- const rl = readline.createInterface({
19
- input: process.stdin,
20
- output: process.stdout,
21
- })
12
+ >()("@cvr/linear/services/Stdin/StdinService") {
13
+ static readonly layer = Layer.effect(
14
+ StdinService,
15
+ Effect.gen(function* () {
16
+ const terminal = yield* Terminal.Terminal;
17
+ return StdinService.of({
18
+ readLine: (prompt) => Console.log(prompt).pipe(Effect.andThen(terminal.readLine)),
19
+ });
20
+ }),
21
+ );
22
22
 
23
- rl.question(prompt, (answer: string) => {
24
- rl.close()
25
- resume(Effect.succeed(answer))
26
- })
27
- }),
28
- })
29
-
30
- static readonly testLayer = (input: string) =>
23
+ static readonly layerTest = (input: string) =>
31
24
  Layer.succeed(StdinService, {
32
25
  readLine: (_prompt) => Effect.succeed(input),
33
- })
26
+ });
34
27
  }