@livestore/utils-dev 0.4.0-dev.9 → 0.4.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.
- package/dist/.tsbuildinfo +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.d.ts +10 -5
- package/dist/node/DockerComposeService/DockerComposeService.d.ts.map +1 -1
- package/dist/node/DockerComposeService/DockerComposeService.js +47 -42
- package/dist/node/DockerComposeService/DockerComposeService.js.map +1 -1
- package/dist/node/DockerComposeService/DockerComposeService.test.js +2 -2
- package/dist/node/DockerComposeService/DockerComposeService.test.js.map +1 -1
- package/dist/node/FileLogger.d.ts.map +1 -1
- package/dist/node/FileLogger.js +3 -3
- package/dist/node/FileLogger.js.map +1 -1
- package/dist/node/cmd-log.d.ts +21 -0
- package/dist/node/cmd-log.d.ts.map +1 -0
- package/dist/node/cmd-log.js +47 -0
- package/dist/node/cmd-log.js.map +1 -0
- package/dist/node/cmd.d.ts +12 -4
- package/dist/node/cmd.d.ts.map +1 -1
- package/dist/node/cmd.js +207 -29
- package/dist/node/cmd.js.map +1 -1
- package/dist/node/cmd.test.d.ts +2 -0
- package/dist/node/cmd.test.d.ts.map +1 -0
- package/dist/node/cmd.test.js +103 -0
- package/dist/node/cmd.test.js.map +1 -0
- package/dist/node/mod.d.ts +3 -2
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +63 -18
- package/dist/node/mod.js.map +1 -1
- package/dist/node/workspace.d.ts +22 -0
- package/dist/node/workspace.d.ts.map +1 -0
- package/dist/node/workspace.js +26 -0
- package/dist/node/workspace.js.map +1 -0
- package/dist/node-vitest/Vitest.d.ts.map +1 -1
- package/dist/node-vitest/Vitest.js +11 -11
- package/dist/node-vitest/Vitest.js.map +1 -1
- package/dist/node-vitest/Vitest.test.d.ts +8 -0
- package/dist/node-vitest/Vitest.test.d.ts.map +1 -1
- package/dist/node-vitest/Vitest.test.js +11 -7
- package/dist/node-vitest/Vitest.test.js.map +1 -1
- package/dist/wrangler/WranglerDevServer.d.ts +20 -3
- package/dist/wrangler/WranglerDevServer.d.ts.map +1 -1
- package/dist/wrangler/WranglerDevServer.js +23 -10
- package/dist/wrangler/WranglerDevServer.js.map +1 -1
- package/dist/wrangler/WranglerDevServer.test.js +3 -3
- package/dist/wrangler/WranglerDevServer.test.js.map +1 -1
- package/package.json +74 -21
- package/src/node/DockerComposeService/DockerComposeService.test.ts +5 -2
- package/src/node/DockerComposeService/DockerComposeService.ts +105 -90
- package/src/node/DockerComposeService/test-fixtures/docker-compose.yml +1 -1
- package/src/node/FileLogger.ts +4 -3
- package/src/node/cmd-log.ts +84 -0
- package/src/node/cmd.test.ts +134 -0
- package/src/node/cmd.ts +380 -62
- package/src/node/mod.ts +69 -21
- package/src/node/workspace.ts +46 -0
- package/src/node-vitest/Vitest.test.ts +12 -7
- package/src/node-vitest/Vitest.ts +28 -24
- package/src/wrangler/WranglerDevServer.test.ts +5 -3
- package/src/wrangler/WranglerDevServer.ts +54 -13
- package/src/wrangler/fixtures/wrangler.toml +1 -1
- package/dist/.tsbuildinfo.json +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { omitUndefineds } from '@livestore/utils'
|
|
1
|
+
import { objectToString, omitUndefineds } from '@livestore/utils'
|
|
2
2
|
import {
|
|
3
3
|
Command,
|
|
4
4
|
type CommandExecutor,
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
Stream,
|
|
13
13
|
} from '@livestore/utils/effect'
|
|
14
14
|
|
|
15
|
-
export class DockerComposeError extends Schema.TaggedError<DockerComposeError>()('DockerComposeError', {
|
|
15
|
+
export class DockerComposeError extends Schema.TaggedError<DockerComposeError>('~@livestore/utils-dev/DockerComposeError')('DockerComposeError', {
|
|
16
16
|
cause: Schema.Defect,
|
|
17
17
|
note: Schema.String,
|
|
18
18
|
}) {}
|
|
@@ -20,6 +20,8 @@ export class DockerComposeError extends Schema.TaggedError<DockerComposeError>()
|
|
|
20
20
|
export interface DockerComposeArgs {
|
|
21
21
|
readonly cwd: string
|
|
22
22
|
readonly serviceName?: string
|
|
23
|
+
/** Unique project name to isolate this compose instance. If not provided, a random one is generated. */
|
|
24
|
+
readonly projectName?: string
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export interface StartOptions {
|
|
@@ -52,20 +54,27 @@ export interface DockerComposeOperations {
|
|
|
52
54
|
readonly logs: (
|
|
53
55
|
options?: LogsOptions,
|
|
54
56
|
) => Stream.Stream<string, DockerComposeError | PlatformError.PlatformError, Scope.Scope>
|
|
57
|
+
/** The unique project name used to isolate this compose instance */
|
|
58
|
+
readonly projectName: string
|
|
55
59
|
}
|
|
56
60
|
|
|
61
|
+
const generateProjectName = (): string => `ls-test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
|
62
|
+
|
|
57
63
|
export class DockerComposeService extends Effect.Service<DockerComposeService>()('DockerComposeService', {
|
|
58
64
|
scoped: (args: DockerComposeArgs) =>
|
|
59
65
|
Effect.gen(function* () {
|
|
60
66
|
const { cwd, serviceName } = args
|
|
67
|
+
const projectName = args.projectName ?? generateProjectName()
|
|
61
68
|
|
|
62
69
|
const commandExecutorContext = yield* Effect.context<CommandExecutor.CommandExecutor>()
|
|
63
70
|
|
|
71
|
+
const baseComposeArgs = ['-p', projectName]
|
|
72
|
+
|
|
64
73
|
const pull = Effect.gen(function* () {
|
|
65
74
|
yield* Effect.log(`Pulling Docker Compose images in ${cwd}`)
|
|
66
75
|
|
|
67
76
|
// TODO (@IMax153) Refactor the effect command related code below as there is probably a much more elegant way to accomplish what we want here in a more effect idiomatic way.
|
|
68
|
-
const pullCommand = Command.make('docker', 'compose', 'pull').pipe(
|
|
77
|
+
const pullCommand = Command.make('docker', 'compose', ...baseComposeArgs, 'pull').pipe(
|
|
69
78
|
Command.workingDirectory(cwd),
|
|
70
79
|
Command.stdout('pipe'),
|
|
71
80
|
Command.stderr('pipe'),
|
|
@@ -118,61 +127,60 @@ export class DockerComposeService extends Effect.Service<DockerComposeService>()
|
|
|
118
127
|
Effect.scoped,
|
|
119
128
|
)
|
|
120
129
|
|
|
121
|
-
const start = (options: StartOptions = {})
|
|
122
|
-
|
|
123
|
-
const { detached = true, healthCheck } = options
|
|
130
|
+
const start = Effect.fn('startDockerCompose')(function* (options: StartOptions = {}) {
|
|
131
|
+
const { detached = true, healthCheck } = options
|
|
124
132
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
133
|
+
// Build start command
|
|
134
|
+
const startArgs = ['docker', 'compose', ...baseComposeArgs, 'up']
|
|
135
|
+
if (detached === true) startArgs.push('-d')
|
|
136
|
+
if (serviceName !== undefined) startArgs.push(serviceName)
|
|
129
137
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
)
|
|
138
|
+
const command = yield* Command.make(startArgs[0]!, ...startArgs.slice(1)).pipe(
|
|
139
|
+
Command.workingDirectory(cwd),
|
|
140
|
+
Command.env(options.env ?? {}),
|
|
141
|
+
Command.stderr('inherit'),
|
|
142
|
+
Command.stdout('inherit'),
|
|
143
|
+
Command.start,
|
|
144
|
+
Effect.mapError(
|
|
145
|
+
(cause) =>
|
|
146
|
+
new DockerComposeError({
|
|
147
|
+
cause,
|
|
148
|
+
note: `Failed to start Docker Compose services in ${cwd}`,
|
|
149
|
+
}),
|
|
150
|
+
),
|
|
151
|
+
Effect.provide(commandExecutorContext),
|
|
152
|
+
)
|
|
146
153
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
154
|
+
// Wait for command completion
|
|
155
|
+
yield* command.exitCode.pipe(
|
|
156
|
+
Effect.flatMap((exitCode) =>
|
|
157
|
+
exitCode === 0
|
|
158
|
+
? Effect.void
|
|
159
|
+
: Effect.fail(
|
|
160
|
+
new DockerComposeError({
|
|
161
|
+
cause: new Error(`Docker compose exited with code ${exitCode}`),
|
|
162
|
+
note: `Docker Compose failed to start with exit code ${exitCode}. Env: ${JSON.stringify(options.env)}`,
|
|
163
|
+
}),
|
|
164
|
+
),
|
|
165
|
+
),
|
|
166
|
+
Effect.provide(commandExecutorContext),
|
|
167
|
+
)
|
|
161
168
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
// Perform health check if requested
|
|
170
|
+
if (healthCheck !== undefined) {
|
|
171
|
+
yield* performHealthCheck(healthCheck).pipe(Effect.provide(commandExecutorContext))
|
|
172
|
+
}
|
|
166
173
|
|
|
167
|
-
|
|
168
|
-
|
|
174
|
+
yield* Effect.log(`Docker Compose services started successfully in ${cwd}`)
|
|
175
|
+
})
|
|
169
176
|
|
|
170
177
|
const stop = Effect.gen(function* () {
|
|
171
178
|
yield* Effect.log(`Stopping Docker Compose services in ${cwd}`)
|
|
172
179
|
|
|
173
|
-
const stopCommand =
|
|
174
|
-
|
|
175
|
-
|
|
180
|
+
const stopCommand =
|
|
181
|
+
serviceName !== undefined
|
|
182
|
+
? Command.make('docker', 'compose', ...baseComposeArgs, 'stop', serviceName)
|
|
183
|
+
: Command.make('docker', 'compose', ...baseComposeArgs, 'stop')
|
|
176
184
|
|
|
177
185
|
yield* stopCommand.pipe(
|
|
178
186
|
Command.workingDirectory(cwd),
|
|
@@ -197,22 +205,21 @@ export class DockerComposeService extends Effect.Service<DockerComposeService>()
|
|
|
197
205
|
Effect.gen(function* () {
|
|
198
206
|
const { follow = false, tail, since } = options
|
|
199
207
|
|
|
200
|
-
const
|
|
201
|
-
if (follow)
|
|
202
|
-
if (tail)
|
|
203
|
-
if (since)
|
|
204
|
-
if (serviceName)
|
|
208
|
+
const logsArgs = ['docker', 'compose', ...baseComposeArgs, 'logs']
|
|
209
|
+
if (follow === true) logsArgs.push('-f')
|
|
210
|
+
if (tail !== undefined) logsArgs.push('--tail', tail.toString())
|
|
211
|
+
if (since !== undefined) logsArgs.push('--since', since)
|
|
212
|
+
if (serviceName !== undefined) logsArgs.push(serviceName)
|
|
205
213
|
|
|
206
|
-
const command = yield* Command.make(
|
|
214
|
+
const command = yield* Command.make(logsArgs[0]!, ...logsArgs.slice(1)).pipe(
|
|
207
215
|
Command.workingDirectory(cwd),
|
|
208
216
|
Command.start,
|
|
209
|
-
Effect.
|
|
210
|
-
|
|
217
|
+
Effect.mapError(
|
|
218
|
+
(cause) =>
|
|
211
219
|
new DockerComposeError({
|
|
212
220
|
cause,
|
|
213
221
|
note: `Failed to read Docker Compose logs in ${cwd}`,
|
|
214
222
|
}),
|
|
215
|
-
),
|
|
216
223
|
),
|
|
217
224
|
Effect.provide(commandExecutorContext),
|
|
218
225
|
)
|
|
@@ -230,40 +237,49 @@ export class DockerComposeService extends Effect.Service<DockerComposeService>()
|
|
|
230
237
|
)
|
|
231
238
|
}).pipe(Stream.unwrapScoped)
|
|
232
239
|
|
|
233
|
-
const down = (options?: {
|
|
240
|
+
const down = Effect.fn('downDockerCompose')(function* (options?: {
|
|
234
241
|
readonly env?: Record<string, string>
|
|
235
242
|
readonly volumes?: boolean
|
|
236
243
|
readonly removeOrphans?: boolean
|
|
237
|
-
})
|
|
238
|
-
Effect.
|
|
239
|
-
yield* Effect.log(`Tearing down Docker Compose services in ${cwd}`)
|
|
244
|
+
}) {
|
|
245
|
+
yield* Effect.log(`Tearing down Docker Compose services in ${cwd}`)
|
|
240
246
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
247
|
+
const downArgs = ['docker', 'compose', ...baseComposeArgs, 'down']
|
|
248
|
+
if (options?.volumes === true) downArgs.push('-v')
|
|
249
|
+
if (options?.removeOrphans === true) downArgs.push('--remove-orphans')
|
|
250
|
+
if (serviceName !== undefined) downArgs.push(serviceName)
|
|
245
251
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
252
|
+
yield* Command.make(downArgs[0]!, ...downArgs.slice(1)).pipe(
|
|
253
|
+
Command.workingDirectory(cwd),
|
|
254
|
+
Command.env(options?.env ?? {}),
|
|
255
|
+
Command.exitCode,
|
|
256
|
+
Effect.flatMap((exitCode: number) =>
|
|
257
|
+
exitCode === 0
|
|
258
|
+
? Effect.void
|
|
259
|
+
: Effect.fail(
|
|
260
|
+
new DockerComposeError({
|
|
261
|
+
cause: new Error(`Docker compose down exited with code ${exitCode}`),
|
|
262
|
+
note: `Failed to tear down Docker Compose services`,
|
|
263
|
+
}),
|
|
264
|
+
),
|
|
265
|
+
),
|
|
266
|
+
Effect.provide(commandExecutorContext),
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
yield* Effect.log(`Docker Compose services torn down successfully`)
|
|
270
|
+
})
|
|
262
271
|
|
|
263
|
-
|
|
264
|
-
|
|
272
|
+
// Register cleanup finalizer to ensure containers are removed when scope closes
|
|
273
|
+
yield* Effect.addFinalizer(() =>
|
|
274
|
+
down({ volumes: true, removeOrphans: true }).pipe(
|
|
275
|
+
Effect.tap(() => Effect.log(`Docker Compose cleanup completed for project ${projectName}`)),
|
|
276
|
+
Effect.catchAll((error) =>
|
|
277
|
+
Effect.log('Docker Compose cleanup failed for project', projectName, objectToString(error)),
|
|
278
|
+
),
|
|
279
|
+
),
|
|
280
|
+
)
|
|
265
281
|
|
|
266
|
-
return { pull, start, stop, down, logs }
|
|
282
|
+
return { pull, start, stop, down, logs, projectName }
|
|
267
283
|
}),
|
|
268
284
|
}) {}
|
|
269
285
|
|
|
@@ -287,17 +303,16 @@ const performHealthCheck = ({
|
|
|
287
303
|
|
|
288
304
|
const healthCheck = checkHealth.pipe(
|
|
289
305
|
Effect.repeat({
|
|
290
|
-
while: (healthy: boolean) =>
|
|
306
|
+
while: (healthy: boolean) => healthy === false,
|
|
291
307
|
schedule: Schedule.fixed(interval),
|
|
292
308
|
}),
|
|
293
309
|
Effect.timeout(timeout),
|
|
294
|
-
Effect.
|
|
295
|
-
|
|
310
|
+
Effect.mapError(
|
|
311
|
+
() =>
|
|
296
312
|
new DockerComposeError({
|
|
297
313
|
cause: new Error('Health check timeout'),
|
|
298
314
|
note: `Health check failed for ${url} after ${Duration.toMillis(timeout)}ms`,
|
|
299
315
|
}),
|
|
300
|
-
),
|
|
301
316
|
),
|
|
302
317
|
)
|
|
303
318
|
|
|
@@ -320,7 +335,7 @@ export const startDockerComposeServicesScoped = (
|
|
|
320
335
|
|
|
321
336
|
// Start the services
|
|
322
337
|
yield* dockerCompose.start({
|
|
323
|
-
...omitUndefineds({ healthCheck: args.healthCheck ? args.healthCheck : undefined }),
|
|
338
|
+
...omitUndefineds({ healthCheck: args.healthCheck !== undefined ? args.healthCheck : undefined }),
|
|
324
339
|
})
|
|
325
340
|
|
|
326
341
|
// Add cleanup finalizer to the current scope
|
package/src/node/FileLogger.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fs from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
3
|
import util from 'node:util'
|
|
4
|
+
|
|
4
5
|
import {
|
|
5
6
|
Cause,
|
|
6
7
|
Effect,
|
|
@@ -120,7 +121,7 @@ export const prettyLoggerTty = (options: {
|
|
|
120
121
|
readonly formatDate: (date: Date) => string
|
|
121
122
|
readonly onLog?: (str: string) => void
|
|
122
123
|
}) => {
|
|
123
|
-
const color = options.colors ? withColor : withColorNoop
|
|
124
|
+
const color = options.colors === true ? withColor : withColorNoop
|
|
124
125
|
return Logger.make<unknown, string>(({ annotations, cause, date, fiberId, logLevel, message: message_, spans }) => {
|
|
125
126
|
let str = ''
|
|
126
127
|
|
|
@@ -141,7 +142,7 @@ export const prettyLoggerTty = (options: {
|
|
|
141
142
|
` ${color(logLevel.label, ...logLevelColors[logLevel._tag])}` +
|
|
142
143
|
` (${FiberId.threadName(fiberId)})`
|
|
143
144
|
|
|
144
|
-
if (List.isCons(spans)) {
|
|
145
|
+
if (List.isCons(spans) === true) {
|
|
145
146
|
const now = date.getTime()
|
|
146
147
|
const render = LogSpan.render(now)
|
|
147
148
|
for (const span of spans) {
|
|
@@ -162,7 +163,7 @@ export const prettyLoggerTty = (options: {
|
|
|
162
163
|
log(firstLine)
|
|
163
164
|
// if (!processIsBun) console.group()
|
|
164
165
|
|
|
165
|
-
if (
|
|
166
|
+
if (Cause.isEmpty(cause) === false) {
|
|
166
167
|
logIndented(Cause.pretty(cause, { renderErrorCause: true }))
|
|
167
168
|
}
|
|
168
169
|
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { isNotUndefined } from '@livestore/utils'
|
|
5
|
+
import { Effect } from '@livestore/utils/effect'
|
|
6
|
+
|
|
7
|
+
export type TCmdLoggingOptions = {
|
|
8
|
+
readonly logDir?: string
|
|
9
|
+
readonly logFileName?: string
|
|
10
|
+
readonly logRetention?: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Prepares logging directories, archives previous canonical log and prunes archives.
|
|
15
|
+
* Returns the canonical current log path if logging is enabled, otherwise undefined.
|
|
16
|
+
*/
|
|
17
|
+
export const prepareCmdLogging: (options: TCmdLoggingOptions) => Effect.Effect<string | undefined> = Effect.fn(
|
|
18
|
+
'cmd.logging.prepare',
|
|
19
|
+
)(function* ({ logDir, logFileName = 'dev.log', logRetention = 50 }: TCmdLoggingOptions) {
|
|
20
|
+
if (logDir == null || logDir === '') return undefined as string | undefined
|
|
21
|
+
|
|
22
|
+
const logsDir = logDir
|
|
23
|
+
const archiveDir = path.join(logsDir, 'archive')
|
|
24
|
+
const currentLogPath = path.join(logsDir, logFileName)
|
|
25
|
+
|
|
26
|
+
// Ensure directories exist
|
|
27
|
+
yield* Effect.sync(() => fs.mkdirSync(archiveDir, { recursive: true }))
|
|
28
|
+
|
|
29
|
+
// Archive previous log if present
|
|
30
|
+
if (fs.existsSync(currentLogPath) === true) {
|
|
31
|
+
const safeIso = new Date().toISOString().replaceAll(':', '-')
|
|
32
|
+
const archivedBase = `${path.parse(logFileName).name}-${safeIso}.log`
|
|
33
|
+
const archivedLog = path.join(archiveDir, archivedBase)
|
|
34
|
+
yield* Effect.try(() => fs.renameSync(currentLogPath, archivedLog)).pipe(
|
|
35
|
+
Effect.catchAll(() =>
|
|
36
|
+
Effect.try(() => {
|
|
37
|
+
fs.copyFileSync(currentLogPath, archivedLog)
|
|
38
|
+
fs.truncateSync(currentLogPath, 0)
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
Effect.ignore,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
// Prune archives to retain only the newest N
|
|
45
|
+
yield* Effect.try(() => fs.readdirSync(archiveDir)).pipe(
|
|
46
|
+
Effect.map((names) => names.filter((n) => n.endsWith('.log'))),
|
|
47
|
+
Effect.map((names) =>
|
|
48
|
+
names
|
|
49
|
+
.map((name) => ({ name, mtimeMs: fs.statSync(path.join(archiveDir, name)).mtimeMs }))
|
|
50
|
+
.sort((a, b) => b.mtimeMs - a.mtimeMs),
|
|
51
|
+
),
|
|
52
|
+
Effect.flatMap((entries) =>
|
|
53
|
+
Effect.forEach(entries.slice(logRetention), (e) =>
|
|
54
|
+
Effect.try(() => fs.unlinkSync(path.join(archiveDir, e.name))).pipe(Effect.ignore),
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
Effect.ignore,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return currentLogPath
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Given a command input, applies logging by piping output through `tee` to the
|
|
66
|
+
* canonical log file. Returns the transformed input and whether a shell is required.
|
|
67
|
+
*/
|
|
68
|
+
export const applyLoggingToCommand: (
|
|
69
|
+
commandInput: string | (string | undefined)[],
|
|
70
|
+
options: TCmdLoggingOptions,
|
|
71
|
+
) => Effect.Effect<{ input: string | string[]; subshell: boolean; logPath?: string }> = Effect.fn('cmd.logging.apply')(
|
|
72
|
+
function* (commandInput, options) {
|
|
73
|
+
const asArray = Array.isArray(commandInput)
|
|
74
|
+
const parts = asArray === true ? commandInput.filter(isNotUndefined) : undefined
|
|
75
|
+
|
|
76
|
+
const logPath = yield* prepareCmdLogging(options)
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
input: asArray === true ? ((parts as string[]) ?? []) : commandInput,
|
|
80
|
+
subshell: false,
|
|
81
|
+
...(logPath !== undefined ? { logPath } : {}),
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
)
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { CommandExecutor, Duration, Effect, Layer } from '@livestore/utils/effect'
|
|
4
|
+
import { PlatformNode } from '@livestore/utils/node'
|
|
5
|
+
import { Vitest } from '@livestore/utils-dev/node-vitest'
|
|
6
|
+
import { expect } from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { cmd } from './cmd.ts'
|
|
9
|
+
import { CurrentWorkingDirectory } from './workspace.ts'
|
|
10
|
+
|
|
11
|
+
const withNode = Vitest.makeWithTestCtx({
|
|
12
|
+
makeLayer: () => Layer.mergeAll(PlatformNode.NodeContext.layer, CurrentWorkingDirectory.live),
|
|
13
|
+
timeout: 20_000,
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
Vitest.describe('cmd helper', () => {
|
|
17
|
+
const ansiRegex = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g')
|
|
18
|
+
|
|
19
|
+
Vitest.scopedLive('runs tokenized string without shell', (test) =>
|
|
20
|
+
Effect.gen(function* () {
|
|
21
|
+
const exit = yield* cmd('printf ok')
|
|
22
|
+
expect(exit).toBe(CommandExecutor.ExitCode(0))
|
|
23
|
+
}).pipe(withNode(test)),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
Vitest.scopedLive('runs array input', (test) =>
|
|
27
|
+
Effect.gen(function* () {
|
|
28
|
+
const exit = yield* cmd(['printf', 'ok'])
|
|
29
|
+
expect(exit).toBe(CommandExecutor.ExitCode(0))
|
|
30
|
+
}).pipe(withNode(test)),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
/** TODO(#1020): Investigate CI timeout flakiness for cmd tests that spawn many child processes */
|
|
34
|
+
Vitest.scopedLive(
|
|
35
|
+
'supports logging with archive + retention',
|
|
36
|
+
(test) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const workspace = process.env.WORKSPACE_ROOT!
|
|
39
|
+
const logsDir = path.join(workspace, 'tmp', 'cmd-tests', String(Date.now()))
|
|
40
|
+
|
|
41
|
+
// first run
|
|
42
|
+
const exit1 = yield* cmd('printf first', { logDir: logsDir })
|
|
43
|
+
expect(exit1).toBe(CommandExecutor.ExitCode(0))
|
|
44
|
+
const current = path.join(logsDir, 'dev.log')
|
|
45
|
+
expect(fs.existsSync(current)).toBe(true)
|
|
46
|
+
const firstLog = fs.readFileSync(current, 'utf8')
|
|
47
|
+
const firstStdoutLines = firstLog.split('\n').filter((line) => line.includes('[stdout]'))
|
|
48
|
+
expect(firstStdoutLines.length).toBeGreaterThan(0)
|
|
49
|
+
for (const line of firstStdoutLines) {
|
|
50
|
+
expect(line).toContain('[stdout] first')
|
|
51
|
+
expect(line).toContain('INFO')
|
|
52
|
+
expect(line).toContain('printf first')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// second run — archives previous
|
|
56
|
+
const exit2 = yield* cmd('printf second', { logDir: logsDir })
|
|
57
|
+
expect(exit2).toBe(CommandExecutor.ExitCode(0))
|
|
58
|
+
const archiveDir = path.join(logsDir, 'archive')
|
|
59
|
+
const archives = fs.readdirSync(archiveDir).filter((f) => f.endsWith('.log'))
|
|
60
|
+
expect(archives.length).toBe(1)
|
|
61
|
+
const archivedPath = path.join(archiveDir, archives[0]!)
|
|
62
|
+
const archivedLog = fs.readFileSync(archivedPath, 'utf8')
|
|
63
|
+
const archivedStdoutLines = archivedLog.split('\n').filter((line) => line.includes('[stdout]'))
|
|
64
|
+
expect(archivedStdoutLines.length).toBeGreaterThan(0)
|
|
65
|
+
for (const line of archivedStdoutLines) {
|
|
66
|
+
expect(line).toContain('[stdout] first')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const secondLog = fs.readFileSync(current, 'utf8')
|
|
70
|
+
const secondStdoutLines = secondLog.split('\n').filter((line) => line.includes('[stdout]'))
|
|
71
|
+
expect(secondStdoutLines.length).toBeGreaterThan(0)
|
|
72
|
+
for (const line of secondStdoutLines) {
|
|
73
|
+
expect(line).toContain('[stdout] second')
|
|
74
|
+
expect(line).toContain('INFO')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// generate many archives to exercise retention (keep 50)
|
|
78
|
+
for (let i = 0; i < 60; i++) {
|
|
79
|
+
// Use small unique payloads
|
|
80
|
+
yield* cmd(['printf', String(i)], { logDir: logsDir })
|
|
81
|
+
}
|
|
82
|
+
const archivesAfter = fs.readdirSync(archiveDir).filter((f) => f.endsWith('.log'))
|
|
83
|
+
expect(archivesAfter.length).toBeLessThanOrEqual(50)
|
|
84
|
+
}).pipe(withNode(test)),
|
|
85
|
+
{ timeout: 30_000, retry: 3 },
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
Vitest.scopedLive('streams stdout and stderr with logger formatting', (test) =>
|
|
89
|
+
Effect.gen(function* () {
|
|
90
|
+
const workspace = process.env.WORKSPACE_ROOT!
|
|
91
|
+
const logsDir = path.join(workspace, 'tmp', 'cmd-tests', `format-${Date.now()}`)
|
|
92
|
+
|
|
93
|
+
const exit = yield* cmd(['node', '-e', "console.log('out'); console.error('err')"], {
|
|
94
|
+
logDir: logsDir,
|
|
95
|
+
})
|
|
96
|
+
expect(exit).toBe(CommandExecutor.ExitCode(0))
|
|
97
|
+
|
|
98
|
+
const current = path.join(logsDir, 'dev.log')
|
|
99
|
+
const logContent = fs.readFileSync(current, 'utf8')
|
|
100
|
+
expect(logContent).toMatch(/\[stdout] out/)
|
|
101
|
+
expect(logContent).toMatch(/\[stderr] err/)
|
|
102
|
+
|
|
103
|
+
const relevantLines = logContent
|
|
104
|
+
.split('\n')
|
|
105
|
+
.map((line) => line.trim())
|
|
106
|
+
.filter((line) => line.includes('[stdout]') || line.includes('[stderr]'))
|
|
107
|
+
|
|
108
|
+
expect(relevantLines.length).toBeGreaterThanOrEqual(2)
|
|
109
|
+
|
|
110
|
+
for (const line of relevantLines) {
|
|
111
|
+
const stripped = line.replace(ansiRegex, '')
|
|
112
|
+
expect(stripped.startsWith('[')).toBe(true)
|
|
113
|
+
expect(stripped).toMatch(/(INFO|WARN)/)
|
|
114
|
+
expect(stripped).toMatch(/\[(stdout|stderr)]/)
|
|
115
|
+
}
|
|
116
|
+
}).pipe(withNode(test)),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
Vitest.scopedLive('cleans up logged child process when interrupted', (test) =>
|
|
120
|
+
Effect.gen(function* () {
|
|
121
|
+
const workspace = process.env.WORKSPACE_ROOT!
|
|
122
|
+
const logsDir = path.join(workspace, 'tmp', 'cmd-tests', `timeout-${Date.now()}`)
|
|
123
|
+
|
|
124
|
+
const result = yield* cmd(['node', '-e', 'setTimeout(() => {}, 5000)'], {
|
|
125
|
+
logDir: logsDir,
|
|
126
|
+
stdout: 'pipe',
|
|
127
|
+
stderr: 'pipe',
|
|
128
|
+
}).pipe(Effect.timeoutOption(Duration.millis(200)))
|
|
129
|
+
|
|
130
|
+
expect(result._tag).toBe('None')
|
|
131
|
+
expect(fs.existsSync(path.join(logsDir, 'dev.log'))).toBe(true)
|
|
132
|
+
}).pipe(withNode(test)),
|
|
133
|
+
)
|
|
134
|
+
})
|