ensure-running 1.0.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/LICENSE +21 -0
- package/README.md +355 -0
- package/dist/bin/ensure-running.js +959 -0
- package/dist/bin/ensure-running.js.map +1 -0
- package/dist/index.cjs +1184 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +356 -0
- package/dist/index.d.ts +356 -0
- package/dist/index.js +1112 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strips node, runner, and script path tokens from raw {@link process.argv}.
|
|
3
|
+
*/
|
|
4
|
+
declare function normalizeArgv(argv: string[]): string[];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Result of parsing service-specific CLI flags.
|
|
8
|
+
*/
|
|
9
|
+
interface ServiceParseResult<TOptions = unknown> {
|
|
10
|
+
options: TOptions;
|
|
11
|
+
remaining: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Contract for a service that can be ensured before running a command.
|
|
15
|
+
*
|
|
16
|
+
* Custom services in `.er/services` should use {@link defineEnsureService} and `export default`.
|
|
17
|
+
*/
|
|
18
|
+
interface EnsureService<TOptions = unknown> {
|
|
19
|
+
/** Stable service id used on the CLI (e.g. `docker`). */
|
|
20
|
+
readonly id: string;
|
|
21
|
+
/** Optional short aliases (e.g. `d`). */
|
|
22
|
+
readonly aliases?: readonly string[];
|
|
23
|
+
/**
|
|
24
|
+
* Consumes service flags from the start of {@link argv}.
|
|
25
|
+
* Stops at the first token that is not a service flag.
|
|
26
|
+
*/
|
|
27
|
+
parseArgs(argv: string[]): ServiceParseResult<TOptions>;
|
|
28
|
+
/** Runs the ensure/check flow and returns a process exit code. */
|
|
29
|
+
run(options: TOptions): Promise<number>;
|
|
30
|
+
/** Prints service-specific help to stdout. */
|
|
31
|
+
printHelp?(): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Defines an ensure service with compile-time shape checking.
|
|
36
|
+
*
|
|
37
|
+
* Use in `.er/services` files:
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* export default defineEnsureService({
|
|
41
|
+
* id: "postgres",
|
|
42
|
+
* parseArgs(argv) { return { options: {}, remaining: argv }; },
|
|
43
|
+
* async run() { return 0; }
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare function defineEnsureService<TOptions = unknown>(service: EnsureService<TOptions>): EnsureService<TOptions>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Registry of ensure services addressable from the CLI.
|
|
51
|
+
*/
|
|
52
|
+
declare class ServiceRegistry {
|
|
53
|
+
private readonly services;
|
|
54
|
+
/**
|
|
55
|
+
* Registers a service by id and optional aliases.
|
|
56
|
+
*/
|
|
57
|
+
register(service: EnsureService): void;
|
|
58
|
+
/**
|
|
59
|
+
* Resolves a service by id or alias.
|
|
60
|
+
*/
|
|
61
|
+
resolve(name: string): EnsureService | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Returns registered services without alias duplicates.
|
|
64
|
+
*/
|
|
65
|
+
list(): EnsureService[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Separator between service chain and trailing command. */
|
|
69
|
+
declare const CommandSeparator = "--";
|
|
70
|
+
/**
|
|
71
|
+
* Thrown when argv tokens appear after services without {@link CommandSeparator}.
|
|
72
|
+
*/
|
|
73
|
+
declare class CommandSeparatorError extends Error {
|
|
74
|
+
constructor(token: string);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Thrown when {@link CommandSeparator} is present without a trailing command.
|
|
78
|
+
*/
|
|
79
|
+
declare class MissingCommandError extends Error {
|
|
80
|
+
constructor();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* One service invocation parsed from argv.
|
|
84
|
+
*/
|
|
85
|
+
interface ServiceInvocation {
|
|
86
|
+
service: EnsureService;
|
|
87
|
+
options: unknown;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Parsed ensure-running CLI invocation.
|
|
91
|
+
*/
|
|
92
|
+
interface ParsedInvocation {
|
|
93
|
+
services: ServiceInvocation[];
|
|
94
|
+
command?: string[];
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Parses argv into chained service invocations and an optional trailing command.
|
|
98
|
+
* A trailing command requires an explicit {@link CommandSeparator} after services.
|
|
99
|
+
*/
|
|
100
|
+
declare function parseInvocation(argv: string[], registry: ServiceRegistry): ParsedInvocation;
|
|
101
|
+
/**
|
|
102
|
+
* Ensures all services in order, then optionally runs a command.
|
|
103
|
+
*/
|
|
104
|
+
declare function runInvocation(invocation: ParsedInvocation): Promise<number>;
|
|
105
|
+
/**
|
|
106
|
+
* Spawns a command with inherited stdio.
|
|
107
|
+
*/
|
|
108
|
+
declare function runCommand(command: string[]): Promise<number>;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Shared types and default option values.
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Optional logger hooks for progress output.
|
|
115
|
+
*/
|
|
116
|
+
interface DockerLogger {
|
|
117
|
+
info?(message: string): void;
|
|
118
|
+
warn?(message: string): void;
|
|
119
|
+
debug?(message: string): void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Options for {@link ensureDockerRunning}.
|
|
123
|
+
*/
|
|
124
|
+
interface EnsureDockerOptions {
|
|
125
|
+
/** Maximum wait time in ms for the daemon to become ready. Default: 120_000. */
|
|
126
|
+
timeout?: number;
|
|
127
|
+
/** Poll interval in ms between readiness checks. Default: 1_000. */
|
|
128
|
+
interval?: number;
|
|
129
|
+
/** When true, attempt to start Docker if the daemon is not running. Default: true. */
|
|
130
|
+
autoStart?: boolean;
|
|
131
|
+
/** Optional logger for progress messages. */
|
|
132
|
+
logger?: DockerLogger;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Result of {@link detectDocker}.
|
|
136
|
+
*/
|
|
137
|
+
interface DockerDetectionResult {
|
|
138
|
+
installed: boolean;
|
|
139
|
+
running: boolean;
|
|
140
|
+
version?: string;
|
|
141
|
+
executable?: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Default options merged into {@link ensureDockerRunning} calls.
|
|
145
|
+
*/
|
|
146
|
+
declare const DefaultEnsureDockerOptions: Required<Pick<EnsureDockerOptions, "timeout" | "interval" | "autoStart">>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Returns true when the Docker daemon is reachable. Never throws.
|
|
150
|
+
*/
|
|
151
|
+
declare function isDockerRunning(): Promise<boolean>;
|
|
152
|
+
/**
|
|
153
|
+
* Ensures Docker is installed, started, and ready for use.
|
|
154
|
+
*
|
|
155
|
+
* @param options Timeout, polling, auto-start, and logger options.
|
|
156
|
+
*/
|
|
157
|
+
declare function ensureDockerRunning(options?: EnsureDockerOptions): Promise<void>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Detects Docker installation and daemon status.
|
|
161
|
+
*/
|
|
162
|
+
declare function detectDocker(): Promise<DockerDetectionResult>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Stable error codes for Docker-related failures.
|
|
166
|
+
*/
|
|
167
|
+
declare enum DockerErrorCode {
|
|
168
|
+
NOT_INSTALLED = "NOT_INSTALLED",
|
|
169
|
+
NOT_RUNNING = "NOT_RUNNING",
|
|
170
|
+
START_FAILED = "START_FAILED",
|
|
171
|
+
TIMEOUT = "TIMEOUT"
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Base error for all Docker ensure failures.
|
|
175
|
+
*/
|
|
176
|
+
declare class DockerError extends Error {
|
|
177
|
+
readonly code: DockerErrorCode;
|
|
178
|
+
constructor(message: string, code: DockerErrorCode, options?: ErrorOptions);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Thrown when the Docker CLI is not installed or not on PATH.
|
|
182
|
+
*/
|
|
183
|
+
declare class DockerNotInstalledError extends DockerError {
|
|
184
|
+
constructor(message?: string);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Thrown when Docker is installed but the daemon is not reachable and autoStart is false.
|
|
188
|
+
*/
|
|
189
|
+
declare class DockerNotRunningError extends DockerError {
|
|
190
|
+
constructor(message?: string);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Thrown when auto-start was attempted but every strategy failed.
|
|
194
|
+
*/
|
|
195
|
+
declare class DockerStartError extends DockerError {
|
|
196
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Thrown when the daemon did not become ready within the configured timeout.
|
|
200
|
+
*/
|
|
201
|
+
declare class DockerTimeoutError extends DockerError {
|
|
202
|
+
constructor(timeoutMs: number);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Docker service CLI modes.
|
|
207
|
+
*/
|
|
208
|
+
declare enum DockerServiceMode {
|
|
209
|
+
ENSURE = "ENSURE",
|
|
210
|
+
CHECK = "CHECK"
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Parsed docker service CLI options.
|
|
214
|
+
*/
|
|
215
|
+
interface DockerServiceOptions {
|
|
216
|
+
mode: DockerServiceMode;
|
|
217
|
+
timeout: number;
|
|
218
|
+
interval: number;
|
|
219
|
+
autoStart: boolean;
|
|
220
|
+
quiet: boolean;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Parses docker-specific flags from argv without consuming the trailing command.
|
|
224
|
+
*/
|
|
225
|
+
declare function parseDockerServiceArgs(argv: string[]): ServiceParseResult<DockerServiceOptions>;
|
|
226
|
+
/**
|
|
227
|
+
* Runs the docker service and returns a process exit code.
|
|
228
|
+
*/
|
|
229
|
+
declare function runDockerService(options: DockerServiceOptions): Promise<number>;
|
|
230
|
+
/**
|
|
231
|
+
* Prints docker service help.
|
|
232
|
+
*/
|
|
233
|
+
declare function printDockerServiceHelp(): void;
|
|
234
|
+
/**
|
|
235
|
+
* Built-in Docker ensure service.
|
|
236
|
+
*/
|
|
237
|
+
declare const dockerService: EnsureService<DockerServiceOptions>;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Thrown when one or more services fail during {@link ensureRunning}.
|
|
241
|
+
*/
|
|
242
|
+
declare class EnsureRunningError extends Error {
|
|
243
|
+
readonly serviceId: string;
|
|
244
|
+
readonly exitCode: number;
|
|
245
|
+
constructor(serviceId: string, exitCode: number, message?: string);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Supported service ids for the default registry.
|
|
249
|
+
*/
|
|
250
|
+
declare enum EnsureServiceId {
|
|
251
|
+
DOCKER = "docker"
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Docker-specific options for the programmatic API.
|
|
255
|
+
*/
|
|
256
|
+
interface EnsureDockerApiOptions {
|
|
257
|
+
/** When true, only checks daemon reachability and never auto-starts. */
|
|
258
|
+
check?: boolean;
|
|
259
|
+
/** Max wait in ms for daemon readiness after start. */
|
|
260
|
+
timeout?: number;
|
|
261
|
+
/** Poll interval in ms between readiness checks. */
|
|
262
|
+
interval?: number;
|
|
263
|
+
/** Attempt to start Docker when the daemon is down. */
|
|
264
|
+
autoStart?: boolean;
|
|
265
|
+
/** Suppress service progress output. */
|
|
266
|
+
quiet?: boolean;
|
|
267
|
+
/** Optional logger hooks (ignored when `quiet` is true). */
|
|
268
|
+
logger?: EnsureDockerOptions["logger"];
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* One service entry for {@link ensureRunning}.
|
|
272
|
+
*/
|
|
273
|
+
type EnsureServiceInput = EnsureServiceId | "docker" | "d" | {
|
|
274
|
+
service: EnsureServiceId.DOCKER | "docker";
|
|
275
|
+
options?: EnsureDockerApiOptions;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Programmatic ensure-running request.
|
|
279
|
+
*/
|
|
280
|
+
interface EnsureRunningRequest {
|
|
281
|
+
/** Services to run in order. */
|
|
282
|
+
services: EnsureServiceInput[];
|
|
283
|
+
/** Optional command to spawn after all services succeed. */
|
|
284
|
+
command?: string[];
|
|
285
|
+
/** Custom service registry (defaults to built-in + `.er/services`). */
|
|
286
|
+
registry?: ServiceRegistry;
|
|
287
|
+
/** Working directory used to discover `.er/services` when registry is omitted. */
|
|
288
|
+
cwd?: string;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Ensures one or more services are ready. Throws {@link EnsureRunningError} on failure.
|
|
292
|
+
*/
|
|
293
|
+
declare function ensureRunning(services: EnsureServiceInput[]): Promise<void>;
|
|
294
|
+
declare function ensureRunning(request: EnsureRunningRequest): Promise<void>;
|
|
295
|
+
/**
|
|
296
|
+
* Ensures services, then optionally runs a command. Returns a process exit code.
|
|
297
|
+
*/
|
|
298
|
+
declare function runEnsureRunning(request: EnsureRunningRequest): Promise<number>;
|
|
299
|
+
/**
|
|
300
|
+
* Ensures Docker is installed, started, and ready. Throws typed docker errors.
|
|
301
|
+
*/
|
|
302
|
+
declare function ensureDocker(options?: EnsureDockerApiOptions): Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Namespaced helpers for programmatic use.
|
|
305
|
+
*/
|
|
306
|
+
declare const ensure: {
|
|
307
|
+
docker: typeof ensureDocker;
|
|
308
|
+
running: typeof ensureRunning;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Creates a registry with built-in services and project-local `.er/services`.
|
|
313
|
+
*/
|
|
314
|
+
declare function createServiceRegistry(cwd?: string): Promise<ServiceRegistry>;
|
|
315
|
+
/**
|
|
316
|
+
* Built-in services only (sync, for tests and programmatic use without custom loading).
|
|
317
|
+
*/
|
|
318
|
+
declare function createBuiltInServiceRegistry(): ServiceRegistry;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Global CLI modes.
|
|
322
|
+
*/
|
|
323
|
+
declare enum CliMode {
|
|
324
|
+
HELP = "HELP",
|
|
325
|
+
VERSION = "VERSION",
|
|
326
|
+
RUN = "RUN"
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Reads the package version from package.json.
|
|
331
|
+
*/
|
|
332
|
+
declare function readPackageVersion(): string;
|
|
333
|
+
/**
|
|
334
|
+
* Prints top-level CLI usage.
|
|
335
|
+
*/
|
|
336
|
+
declare function printCliHelp(registry: ServiceRegistry): void;
|
|
337
|
+
/**
|
|
338
|
+
* Runs the ensure-running CLI.
|
|
339
|
+
*/
|
|
340
|
+
declare function runCli(argv: string[], registry?: ServiceRegistry): Promise<number>;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Validates a custom service loaded from `.er/services`.
|
|
344
|
+
*/
|
|
345
|
+
declare function assertEnsureService(value: unknown, source: string): EnsureService;
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Finds the nearest `.er/services` directory walking up from startDir.
|
|
349
|
+
*/
|
|
350
|
+
declare function findCustomServicesDir(startDir: string): string | undefined;
|
|
351
|
+
/**
|
|
352
|
+
* Loads custom services from `.er/services` and registers them.
|
|
353
|
+
*/
|
|
354
|
+
declare function loadCustomServices(registry: ServiceRegistry, cwd?: string): Promise<void>;
|
|
355
|
+
|
|
356
|
+
export { CliMode, CommandSeparator, CommandSeparatorError, ServiceRegistry as CoreServiceRegistry, DefaultEnsureDockerOptions, type DockerDetectionResult, DockerError, DockerErrorCode, type DockerLogger, DockerNotInstalledError, DockerNotRunningError, DockerServiceMode, type DockerServiceOptions, DockerStartError, DockerTimeoutError, type EnsureDockerApiOptions, type EnsureDockerOptions, EnsureRunningError, type EnsureRunningRequest, type EnsureService, EnsureServiceId, type EnsureServiceInput, MissingCommandError, type ParsedInvocation, type ServiceInvocation, type ServiceParseResult, ServiceRegistry, assertEnsureService, createBuiltInServiceRegistry, createServiceRegistry, defineEnsureService, detectDocker, dockerService, ensure, ensureDocker, ensureDockerRunning, ensureRunning, findCustomServicesDir, isDockerRunning, loadCustomServices, normalizeArgv, parseDockerServiceArgs, parseInvocation, printCliHelp, printDockerServiceHelp, readPackageVersion, runCli, runCommand, runDockerService, runEnsureRunning, runInvocation };
|