@dbx-tools/cli-service 0.9.166 → 0.9.173
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/README.md +7 -1
- package/index.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -2
- package/lib/src/_config.d.ts +49 -2
- package/lib/src/_config.js +11 -3
- package/lib/src/_host.d.ts +33 -0
- package/lib/src/_host.js +53 -26
- package/lib/src/_paths.d.ts +1 -0
- package/lib/src/_paths.js +2 -1
- package/lib/src/_runtime.js +49 -27
- package/lib/src/_startup.d.ts +4 -1
- package/lib/src/_startup.js +71 -2
- package/lib/src/service.d.ts +3 -0
- package/lib/src/service.js +64 -33
- package/package.json +4 -4
- package/src/_config.ts +15 -4
- package/src/_host.ts +66 -29
- package/src/_paths.ts +2 -0
- package/src/_runtime.ts +57 -33
- package/src/_startup.ts +108 -1
- package/src/service.ts +98 -32
package/src/service.ts
CHANGED
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
* @module
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import { randomUUID } from "node:crypto";
|
|
13
13
|
import { existsSync } from "node:fs";
|
|
14
|
-
import { rm } from "node:fs/promises";
|
|
14
|
+
import { readdir, rm } from "node:fs/promises";
|
|
15
15
|
import { createRequire } from "node:module";
|
|
16
16
|
import { dirname, extname, join, resolve } from "node:path";
|
|
17
17
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
18
18
|
|
|
19
|
-
import { type BinContext, ensure } from "@dbx-tools/core";
|
|
19
|
+
import { type BinContext, ensure, withFileLock } from "@dbx-tools/core";
|
|
20
20
|
import { sleep } from "@dbx-tools/shared-core";
|
|
21
21
|
import { z } from "zod";
|
|
22
22
|
import { compileWithBun } from "./_compile.ts";
|
|
@@ -33,7 +33,13 @@ import {
|
|
|
33
33
|
installServicePythonPackage,
|
|
34
34
|
installServiceRuntime,
|
|
35
35
|
} from "./_runtime.ts";
|
|
36
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
installStartup,
|
|
38
|
+
removeStartup,
|
|
39
|
+
startStartup,
|
|
40
|
+
stopStartup,
|
|
41
|
+
type ServiceLaunch,
|
|
42
|
+
} from "./_startup.ts";
|
|
37
43
|
import {
|
|
38
44
|
CliServiceDefinitionSchema,
|
|
39
45
|
type CliServiceCommand,
|
|
@@ -80,6 +86,8 @@ export interface CliServiceRuntimeOptions {
|
|
|
80
86
|
readonly homeDirectory?: string;
|
|
81
87
|
/** Temporary directory used for the local control socket. */
|
|
82
88
|
readonly temporaryDirectory?: string;
|
|
89
|
+
/** Current user ID used for platform login registration. */
|
|
90
|
+
readonly userId?: number;
|
|
83
91
|
/** Shared dbx-tools home containing installed service binaries. */
|
|
84
92
|
readonly globalHomeDirectory?: string;
|
|
85
93
|
/** Package-provided Bun executable override. */
|
|
@@ -181,6 +189,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
181
189
|
environment: options.environment ?? defaults.environment,
|
|
182
190
|
homeDirectory: options.homeDirectory ?? defaults.homeDirectory,
|
|
183
191
|
temporaryDirectory: options.temporaryDirectory ?? defaults.temporaryDirectory,
|
|
192
|
+
userId: options.userId ?? defaults.userId,
|
|
184
193
|
};
|
|
185
194
|
this.globalHomeDirectory =
|
|
186
195
|
options.globalHomeDirectory ?? join(this.runtime.homeDirectory, ".dbx-tools");
|
|
@@ -201,6 +210,16 @@ export class CliService implements CliServiceLifecycle {
|
|
|
201
210
|
if (options.pythonProject && !this.definition.pythonPackage) {
|
|
202
211
|
throw new Error(`${this.definition.name} does not define a Python package`);
|
|
203
212
|
}
|
|
213
|
+
const paths = resolveServicePaths(this.definition, this.runtime);
|
|
214
|
+
await withFileLock(["cli-service-install", paths.directory], () =>
|
|
215
|
+
this.installLocked(paths, options),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private async installLocked(
|
|
220
|
+
paths: ReturnType<typeof resolveServicePaths>,
|
|
221
|
+
options: CliServiceInstallOptions,
|
|
222
|
+
): Promise<void> {
|
|
204
223
|
const owner = resolveServicePackage(this.definition.packageName);
|
|
205
224
|
const dependencies = owner.dependencies();
|
|
206
225
|
const external = new Set(externalRuntimePackages(dependencies));
|
|
@@ -209,23 +228,46 @@ export class CliService implements CliServiceLifecycle {
|
|
|
209
228
|
this.globalHomeDirectory,
|
|
210
229
|
Object.fromEntries(Object.entries(dependencies).filter(([name]) => external.has(name))),
|
|
211
230
|
);
|
|
212
|
-
const
|
|
213
|
-
const
|
|
231
|
+
const version = this.definition.version ?? owner.version;
|
|
232
|
+
const artifactId = `${safeToken(version)}-${randomUUID()}`;
|
|
233
|
+
const artifactDirectory = join(paths.directory, "versions", artifactId);
|
|
234
|
+
const artifactBinDirectory = join(
|
|
235
|
+
this.globalHomeDirectory,
|
|
236
|
+
"bin",
|
|
237
|
+
"services",
|
|
238
|
+
this.binaryName(this.definition, "service"),
|
|
239
|
+
"versions",
|
|
240
|
+
artifactId,
|
|
241
|
+
);
|
|
242
|
+
const installedDefinition = await this.installDefinition(
|
|
243
|
+
owner,
|
|
244
|
+
artifactDirectory,
|
|
245
|
+
artifactBinDirectory,
|
|
246
|
+
options,
|
|
247
|
+
);
|
|
214
248
|
const host = await this.ensureCompiledBinary(
|
|
215
249
|
this.binaryName(installedDefinition, "service"),
|
|
216
250
|
this.hostEntrypoint,
|
|
217
251
|
resolve(dirname(this.hostEntrypoint), ".."),
|
|
252
|
+
[],
|
|
253
|
+
artifactBinDirectory,
|
|
218
254
|
);
|
|
219
|
-
await this.ensureTrayBinary();
|
|
255
|
+
await this.ensureTrayBinary(artifactBinDirectory);
|
|
220
256
|
await this.stop();
|
|
221
|
-
|
|
257
|
+
const installed = { ...installedDefinition, hostExecutable: host.path };
|
|
258
|
+
await writeServiceDefinition(paths.configFile, installed);
|
|
222
259
|
await installStartup(
|
|
223
|
-
|
|
260
|
+
installed,
|
|
224
261
|
paths,
|
|
225
262
|
this.runtime,
|
|
226
263
|
this.launch(host.path, paths.configFile),
|
|
227
264
|
);
|
|
228
265
|
if (options.start ?? true) await this.start();
|
|
266
|
+
await Promise.all([
|
|
267
|
+
removeOtherArtifacts(dirname(artifactDirectory), artifactId),
|
|
268
|
+
removeOtherArtifacts(dirname(artifactBinDirectory), artifactId),
|
|
269
|
+
rm(join(paths.directory, "python"), { recursive: true, force: true }),
|
|
270
|
+
]);
|
|
229
271
|
}
|
|
230
272
|
|
|
231
273
|
/** Start the installed service when it is not already running. */
|
|
@@ -238,22 +280,23 @@ export class CliService implements CliServiceLifecycle {
|
|
|
238
280
|
|
|
239
281
|
const installedDefinition = await readServiceDefinition(paths.configFile);
|
|
240
282
|
const launch = this.launch(
|
|
241
|
-
|
|
283
|
+
installedDefinition.hostExecutable ??
|
|
284
|
+
this.binaryContext(this.binaryName(installedDefinition, "service")).path,
|
|
242
285
|
paths.configFile,
|
|
243
286
|
);
|
|
244
|
-
const child =
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
287
|
+
const child = await startStartup(installedDefinition, paths, this.runtime, launch);
|
|
288
|
+
const started = waitForState(
|
|
289
|
+
async () => (await requestServiceControl(paths.controlAddress, "status")) !== undefined,
|
|
290
|
+
true,
|
|
291
|
+
DEFAULT_START_TIMEOUT_MILLISECONDS,
|
|
292
|
+
`${this.definition.name} did not start`,
|
|
293
|
+
);
|
|
294
|
+
if (!child) {
|
|
295
|
+
await started;
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
250
298
|
await Promise.race([
|
|
251
|
-
|
|
252
|
-
async () => (await requestServiceControl(paths.controlAddress, "status")) !== undefined,
|
|
253
|
-
true,
|
|
254
|
-
DEFAULT_START_TIMEOUT_MILLISECONDS,
|
|
255
|
-
`${this.definition.name} did not start`,
|
|
256
|
-
),
|
|
299
|
+
started,
|
|
257
300
|
new Promise<never>((_resolve, reject) => {
|
|
258
301
|
child.once("error", reject);
|
|
259
302
|
child.once("exit", (code, signal) => {
|
|
@@ -271,7 +314,10 @@ export class CliService implements CliServiceLifecycle {
|
|
|
271
314
|
async stop(): Promise<void> {
|
|
272
315
|
const paths = resolveServicePaths(this.definition, this.runtime);
|
|
273
316
|
const response = await requestServiceControl(paths.controlAddress, "stop");
|
|
274
|
-
if (!response)
|
|
317
|
+
if (!response) {
|
|
318
|
+
await stopStartup(this.definition, this.runtime);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
275
321
|
await waitForState(
|
|
276
322
|
async () => (await requestServiceControl(paths.controlAddress, "status")) !== undefined,
|
|
277
323
|
false,
|
|
@@ -295,7 +341,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
295
341
|
async uninstall(): Promise<void> {
|
|
296
342
|
const paths = resolveServicePaths(this.definition, this.runtime);
|
|
297
343
|
await this.stop();
|
|
298
|
-
await removeStartup(paths);
|
|
344
|
+
await removeStartup(this.definition, paths, this.runtime);
|
|
299
345
|
await rm(paths.directory, { recursive: true, force: true });
|
|
300
346
|
}
|
|
301
347
|
|
|
@@ -325,7 +371,8 @@ export class CliService implements CliServiceLifecycle {
|
|
|
325
371
|
|
|
326
372
|
private async installDefinition(
|
|
327
373
|
owner: ServicePackage,
|
|
328
|
-
|
|
374
|
+
artifactDirectory: string,
|
|
375
|
+
artifactBinDirectory: string,
|
|
329
376
|
options: CliServiceInstallOptions,
|
|
330
377
|
): Promise<CliServiceDefinition> {
|
|
331
378
|
const definition = CliServiceDefinitionSchema.parse({
|
|
@@ -346,7 +393,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
346
393
|
: `${pythonPackage.name}==${pythonPackage.version}`;
|
|
347
394
|
pythonEnvironment.PYTHON = await this.pythonInstaller(
|
|
348
395
|
this.uvExecutable,
|
|
349
|
-
join(
|
|
396
|
+
join(artifactDirectory, "python"),
|
|
350
397
|
[primarySpecifier, ...pythonPackage.dependencies],
|
|
351
398
|
pythonPackage.python,
|
|
352
399
|
this.runtime.platform,
|
|
@@ -360,6 +407,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
360
407
|
definition.command,
|
|
361
408
|
"command",
|
|
362
409
|
pythonEnvironment,
|
|
410
|
+
artifactBinDirectory,
|
|
363
411
|
)
|
|
364
412
|
: undefined;
|
|
365
413
|
const menu: CliServiceMenuItem[] = [];
|
|
@@ -374,6 +422,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
374
422
|
item.command,
|
|
375
423
|
`menu-${index + 1}`,
|
|
376
424
|
pythonEnvironment,
|
|
425
|
+
artifactBinDirectory,
|
|
377
426
|
),
|
|
378
427
|
}
|
|
379
428
|
: item,
|
|
@@ -393,6 +442,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
393
442
|
command: CliServiceCommand,
|
|
394
443
|
purpose: string,
|
|
395
444
|
runtimeEnvironment: Readonly<Record<string, string>>,
|
|
445
|
+
artifactBinDirectory: string,
|
|
396
446
|
): Promise<CliServiceCommand> {
|
|
397
447
|
const environment = { ...command.environment, ...runtimeEnvironment };
|
|
398
448
|
if (command.executable) {
|
|
@@ -407,6 +457,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
407
457
|
resolve(entrypoint),
|
|
408
458
|
owner.directory,
|
|
409
459
|
externalRuntimePackages(owner.dependencies()),
|
|
460
|
+
artifactBinDirectory,
|
|
410
461
|
);
|
|
411
462
|
return {
|
|
412
463
|
executable: installed.path,
|
|
@@ -421,6 +472,7 @@ export class CliService implements CliServiceLifecycle {
|
|
|
421
472
|
entrypoint: string,
|
|
422
473
|
workingDirectory: string,
|
|
423
474
|
external: readonly string[] = [],
|
|
475
|
+
binDirectory?: string,
|
|
424
476
|
): Promise<BinContext> {
|
|
425
477
|
return ensure(
|
|
426
478
|
name,
|
|
@@ -430,16 +482,15 @@ export class CliService implements CliServiceLifecycle {
|
|
|
430
482
|
return pathToFileURL(output).href;
|
|
431
483
|
},
|
|
432
484
|
{
|
|
433
|
-
destination: this.binaryContext(name),
|
|
485
|
+
destination: this.binaryContext(name, binDirectory),
|
|
434
486
|
skipVersionCheck: true,
|
|
435
|
-
force: true,
|
|
436
487
|
},
|
|
437
488
|
);
|
|
438
489
|
}
|
|
439
490
|
|
|
440
|
-
private async ensureTrayBinary(): Promise<BinContext> {
|
|
491
|
+
private async ensureTrayBinary(artifactBinDirectory: string): Promise<BinContext> {
|
|
441
492
|
const name = trayExecutableName(this.runtime.platform);
|
|
442
|
-
const binDir = join(
|
|
493
|
+
const binDir = join(artifactBinDirectory, "traybin");
|
|
443
494
|
return ensure(name, pathToFileURL(this.trayExecutable).href, {
|
|
444
495
|
destination: {
|
|
445
496
|
root: this.globalHomeDirectory,
|
|
@@ -450,8 +501,8 @@ export class CliService implements CliServiceLifecycle {
|
|
|
450
501
|
});
|
|
451
502
|
}
|
|
452
503
|
|
|
453
|
-
private binaryContext(name: string): BinContext {
|
|
454
|
-
const binDir = join(this.globalHomeDirectory, "bin");
|
|
504
|
+
private binaryContext(name: string, directory?: string): BinContext {
|
|
505
|
+
const binDir = directory ?? join(this.globalHomeDirectory, "bin");
|
|
455
506
|
return {
|
|
456
507
|
root: this.globalHomeDirectory,
|
|
457
508
|
binDir,
|
|
@@ -465,6 +516,21 @@ export class CliService implements CliServiceLifecycle {
|
|
|
465
516
|
}
|
|
466
517
|
}
|
|
467
518
|
|
|
519
|
+
async function removeOtherArtifacts(directory: string, current: string): Promise<void> {
|
|
520
|
+
let entries;
|
|
521
|
+
try {
|
|
522
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
523
|
+
} catch (error) {
|
|
524
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
525
|
+
throw error;
|
|
526
|
+
}
|
|
527
|
+
await Promise.all(
|
|
528
|
+
entries
|
|
529
|
+
.filter((entry) => entry.name !== current)
|
|
530
|
+
.map((entry) => rm(join(directory, entry.name), { recursive: true, force: true })),
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
|
|
468
534
|
/** Preserve a package's extras while replacing its registry name with a local project path. */
|
|
469
535
|
function _localPythonSpecifier(project: string, packageName: string): string {
|
|
470
536
|
const directory = resolve(project);
|