@dbx-tools/core 0.9.214 → 0.11.2
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/index.ts +6 -5
- package/lib/index.d.ts +6 -5
- package/lib/index.js +5 -5
- package/lib/src/bin.d.ts +2 -0
- package/lib/src/bin.js +13 -13
- package/lib/src/brand-files.d.ts +4 -0
- package/lib/src/brand-files.js +16 -2
- package/lib/src/exec.d.ts +4 -1
- package/lib/src/exec.js +3 -1
- package/lib/src/file.d.ts +26 -6
- package/lib/src/file.js +67 -3
- package/lib/src/project-utils.js +4 -7
- package/package.json +3 -3
- package/src/bin.ts +13 -10
- package/src/brand-files.ts +22 -1
- package/src/exec.ts +7 -2
- package/src/file.ts +101 -2
- package/src/project-utils.ts +4 -5
package/src/file.ts
CHANGED
|
@@ -4,11 +4,100 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import {
|
|
9
|
+
Stats,
|
|
10
|
+
mkdirSync,
|
|
11
|
+
mkdtempSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
renameSync,
|
|
14
|
+
rmSync,
|
|
15
|
+
statSync as nodeStatSync,
|
|
16
|
+
writeFileSync,
|
|
17
|
+
} from "node:fs";
|
|
18
|
+
import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
9
21
|
|
|
10
22
|
const recordCache = new Map<string, Record<string, unknown> | undefined>();
|
|
11
23
|
|
|
24
|
+
/** Options for reading a JSON file asynchronously. */
|
|
25
|
+
export interface ReadJsonFileOptions {
|
|
26
|
+
/** Abort the underlying file read. */
|
|
27
|
+
readonly signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Options for writing formatted JSON to a host file. */
|
|
31
|
+
export interface WriteJsonFileOptions {
|
|
32
|
+
/** Replace the destination through a temporary sibling file. Defaults to false. */
|
|
33
|
+
readonly atomic?: boolean;
|
|
34
|
+
/** Create the destination directory recursively. Defaults to false. */
|
|
35
|
+
readonly createDirectory?: boolean;
|
|
36
|
+
/** File mode applied when a new file is created. */
|
|
37
|
+
readonly mode?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Read and parse one JSON file. */
|
|
41
|
+
export async function readJsonFile(
|
|
42
|
+
path: string,
|
|
43
|
+
options: ReadJsonFileOptions = {},
|
|
44
|
+
): Promise<unknown> {
|
|
45
|
+
return JSON.parse(await readFile(path, { encoding: "utf8", signal: options.signal }));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Read and parse one JSON file synchronously. */
|
|
49
|
+
export function readJsonFileSync(path: string): unknown {
|
|
50
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Write one JSON value with two-space indentation and a trailing newline. */
|
|
54
|
+
export async function writeJsonFile(
|
|
55
|
+
path: string,
|
|
56
|
+
value: unknown,
|
|
57
|
+
options: WriteJsonFileOptions = {},
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
if (options.createDirectory) await mkdir(dirname(path), { recursive: true });
|
|
60
|
+
const temporary = options.atomic ? temporaryPath(path) : undefined;
|
|
61
|
+
try {
|
|
62
|
+
await writeFile(temporary ?? path, jsonFileText(value), {
|
|
63
|
+
encoding: "utf8",
|
|
64
|
+
...(options.mode === undefined ? {} : { mode: options.mode }),
|
|
65
|
+
});
|
|
66
|
+
if (temporary) await rename(temporary, path);
|
|
67
|
+
} finally {
|
|
68
|
+
if (temporary) await rm(temporary, { force: true });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Write one JSON value synchronously with two-space indentation and a trailing newline. */
|
|
73
|
+
export function writeJsonFileSync(
|
|
74
|
+
path: string,
|
|
75
|
+
value: unknown,
|
|
76
|
+
options: WriteJsonFileOptions = {},
|
|
77
|
+
): void {
|
|
78
|
+
if (options.createDirectory) mkdirSync(dirname(path), { recursive: true });
|
|
79
|
+
const temporary = options.atomic ? temporaryPath(path) : undefined;
|
|
80
|
+
try {
|
|
81
|
+
writeFileSync(temporary ?? path, jsonFileText(value), {
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
...(options.mode === undefined ? {} : { mode: options.mode }),
|
|
84
|
+
});
|
|
85
|
+
if (temporary) renameSync(temporary, path);
|
|
86
|
+
} finally {
|
|
87
|
+
if (temporary) rmSync(temporary, { force: true });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Create an operating-system temporary directory with the supplied name prefix. */
|
|
92
|
+
export function temporaryDirectory(prefix = "dbx-tools-"): Promise<string> {
|
|
93
|
+
return mkdtemp(join(tmpdir(), prefix));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Create an operating-system temporary directory synchronously. */
|
|
97
|
+
export function temporaryDirectorySync(prefix = "dbx-tools-"): string {
|
|
98
|
+
return mkdtempSync(join(tmpdir(), prefix));
|
|
99
|
+
}
|
|
100
|
+
|
|
12
101
|
/**
|
|
13
102
|
* Return the host-relative path when `candidate` is `root` or a descendant,
|
|
14
103
|
* otherwise `undefined`. Equality returns `""`; callers requiring a strict
|
|
@@ -56,3 +145,13 @@ export function cachedRecord<T extends Record<string, unknown>>(
|
|
|
56
145
|
recordCache.set(key, value);
|
|
57
146
|
return value;
|
|
58
147
|
}
|
|
148
|
+
|
|
149
|
+
function jsonFileText(value: unknown): string {
|
|
150
|
+
const serialized = JSON.stringify(value, null, 2);
|
|
151
|
+
if (serialized === undefined) throw new TypeError("JSON file value is not serializable");
|
|
152
|
+
return `${serialized}\n`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function temporaryPath(path: string): string {
|
|
156
|
+
return `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
157
|
+
}
|
package/src/project-utils.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { Stats, readFileSync } from "node:fs";
|
|
|
13
13
|
import { homedir } from "node:os";
|
|
14
14
|
import { basename, dirname, join, resolve } from "node:path";
|
|
15
15
|
import {
|
|
16
|
+
parse,
|
|
16
17
|
parseRecord,
|
|
17
18
|
type UrlBuilder,
|
|
18
19
|
urlBuilder,
|
|
@@ -397,15 +398,13 @@ export function resolveProjectGhAccount(
|
|
|
397
398
|
projectGhAccountCache.set(key, null);
|
|
398
399
|
return undefined;
|
|
399
400
|
}
|
|
400
|
-
|
|
401
|
+
const parsed = parse<{
|
|
401
402
|
hosts?: Record<
|
|
402
403
|
string,
|
|
403
404
|
Array<{ active?: boolean; host?: string; login?: string; state?: string }>
|
|
404
405
|
>;
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
parsed = JSON.parse(status) as typeof parsed;
|
|
408
|
-
} catch {
|
|
406
|
+
}>(status);
|
|
407
|
+
if (!parsed) {
|
|
409
408
|
projectGhAccountCache.set(key, null);
|
|
410
409
|
return undefined;
|
|
411
410
|
}
|