pi-reply-reader 0.1.0 → 0.2.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/CHANGELOG.md +143 -0
- package/README.md +402 -39
- package/dist/src/cli.js +137 -10
- package/dist/src/listener.js +532 -0
- package/dist/src/music.js +452 -0
- package/dist/src/outputs.js +29 -4
- package/dist/src/playback.js +170 -46
- package/dist/src/player.js +265 -0
- package/dist/src/project.js +50 -0
- package/dist/src/reading-lock.js +296 -68
- package/dist/src/settings.js +32 -3
- package/dist/src/speech-text.js +581 -64
- package/examples/reply-reader-desktop-audio.service +23 -10
- package/examples/reply-reader-vps-audio.service +57 -0
- package/extensions/reply-reader.ts +920 -106
- package/package.json +3 -2
- package/src/auto-queue.ts +123 -0
- package/src/cli.ts +131 -10
- package/src/listener.ts +522 -0
- package/src/music.ts +474 -0
- package/src/outputs.ts +37 -5
- package/src/pause-key.ts +105 -0
- package/src/playback.ts +179 -48
- package/src/player.ts +255 -0
- package/src/project.ts +46 -0
- package/src/prompts.ts +142 -0
- package/src/reading-lock.ts +364 -66
- package/src/settings.ts +34 -3
- package/src/speech-text.ts +692 -56
package/dist/src/reading-lock.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { closeSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
|
3
3
|
import { isAbsolute, join } from "node:path";
|
|
4
|
-
|
|
5
|
-
export
|
|
4
|
+
import { isPlayer, sameProcess, startTime, terminatePid } from "./player.js";
|
|
5
|
+
export { PLAYER_MARK } from "./player.js";
|
|
6
|
+
/** How long an explicit action waits for the mutex before acting anyway; an auto-read just tries later. */
|
|
7
|
+
const MUTEX_WAIT_MS = 200;
|
|
8
|
+
/** A mutex file still empty after this long was left by a process that died while taking it. */
|
|
9
|
+
const MUTEX_STALE_MS = 1000;
|
|
6
10
|
/**
|
|
7
11
|
* The machine-wide lock under `$XDG_RUNTIME_DIR`, or undefined when it is unset, relative or unusable: then each
|
|
8
12
|
* session only stops its own readings, as before.
|
|
@@ -21,6 +25,12 @@ export function openReadingLock(env) {
|
|
|
21
25
|
return undefined;
|
|
22
26
|
}
|
|
23
27
|
const file = join(dir, "reading.json");
|
|
28
|
+
/** `{ mark }`: rewritten by every explicit action, so replies queued before it know they were dropped (S8). */
|
|
29
|
+
const marks = join(dir, "waiting.json");
|
|
30
|
+
/** Held for the few microseconds of a check-and-write, so two sessions never both take a free machine (T32). */
|
|
31
|
+
const mutex = join(dir, "reading.lock");
|
|
32
|
+
/** `{ project }`: the project of the last reading that started, by any session or the command (S19). */
|
|
33
|
+
const projects = join(dir, "project.json");
|
|
24
34
|
const read = () => {
|
|
25
35
|
try {
|
|
26
36
|
const entry = JSON.parse(readFileSync(file, "utf8"));
|
|
@@ -34,12 +44,12 @@ export function openReadingLock(env) {
|
|
|
34
44
|
return undefined;
|
|
35
45
|
}
|
|
36
46
|
};
|
|
37
|
-
/** Written whole, then renamed into place, so a reader never sees half
|
|
38
|
-
const
|
|
39
|
-
const temp = `${
|
|
47
|
+
/** Written whole, then renamed into place, so a reader never sees half a file. */
|
|
48
|
+
const put = (path, token, data) => {
|
|
49
|
+
const temp = `${path}.${process.pid}.${token}.tmp`;
|
|
40
50
|
try {
|
|
41
|
-
writeFileSync(temp, JSON.stringify(
|
|
42
|
-
renameSync(temp,
|
|
51
|
+
writeFileSync(temp, JSON.stringify(data), { mode: 0o600 });
|
|
52
|
+
renameSync(temp, path);
|
|
43
53
|
}
|
|
44
54
|
catch {
|
|
45
55
|
try {
|
|
@@ -48,6 +58,79 @@ export function openReadingLock(env) {
|
|
|
48
58
|
catch { }
|
|
49
59
|
}
|
|
50
60
|
};
|
|
61
|
+
const write = (entry) => put(file, entry.token, entry);
|
|
62
|
+
const lineFile = (token) => join(dir, `line-${token}.json`);
|
|
63
|
+
const waitingMark = () => {
|
|
64
|
+
try {
|
|
65
|
+
const mark = JSON.parse(readFileSync(marks, "utf8"))?.mark;
|
|
66
|
+
return typeof mark === "string" ? mark : undefined;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const dropWaiting = () => {
|
|
73
|
+
const mark = randomUUID();
|
|
74
|
+
put(marks, mark, { mark });
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Runs `body` holding the machine's mutex. When it cannot be had in time, `timeout` decides: explicit actions act
|
|
78
|
+
* anyway (as before T32), auto-read tries again later. A mutex left by a dead process is broken.
|
|
79
|
+
*/
|
|
80
|
+
const exclusive = (body, timeout) => {
|
|
81
|
+
const deadline = Date.now() + MUTEX_WAIT_MS;
|
|
82
|
+
for (;;) {
|
|
83
|
+
let fd;
|
|
84
|
+
try {
|
|
85
|
+
fd = openSync(mutex, "wx", 0o600);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// Anything but "held" (a read-only folder, say) means no mutex at all, as before T32.
|
|
89
|
+
if (error.code !== "EEXIST")
|
|
90
|
+
return body();
|
|
91
|
+
if (abandoned()) {
|
|
92
|
+
try {
|
|
93
|
+
unlinkSync(mutex);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Not deletable (a read-only folder, say): it counts as held, so the deadline still bounds the wait.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (Date.now() > deadline)
|
|
101
|
+
return timeout();
|
|
102
|
+
pause(2);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
try {
|
|
107
|
+
writeSync(fd, `${process.pid} ${startTime(process.pid) ?? ""}`);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
closeSync(fd);
|
|
111
|
+
}
|
|
112
|
+
return body();
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
try {
|
|
116
|
+
unlinkSync(mutex);
|
|
117
|
+
}
|
|
118
|
+
catch { }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
/** Whether the mutex was left by a process that is gone (or never wrote its pid). */
|
|
123
|
+
const abandoned = () => {
|
|
124
|
+
try {
|
|
125
|
+
const [pid, start] = readFileSync(mutex, "utf8").split(" ");
|
|
126
|
+
if (pid && Number.isInteger(Number(pid)) && Number(pid) > 0)
|
|
127
|
+
return !sameProcess(Number(pid), start || undefined);
|
|
128
|
+
return Date.now() - statSync(mutex).mtimeMs > MUTEX_STALE_MS;
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
51
134
|
/**
|
|
52
135
|
* Writes `entry` over the current one (always for a new reading; for `/read-stop` only when a reading plays) and
|
|
53
136
|
* stops the reading the current one names, if it still plays. The entry is written first, so the stopped
|
|
@@ -60,43 +143,217 @@ export function openReadingLock(env) {
|
|
|
60
143
|
write(entry);
|
|
61
144
|
if (!live)
|
|
62
145
|
return false;
|
|
63
|
-
if
|
|
146
|
+
// SIGTERM, then SIGKILL if that player ignores it (S13): its own session may be hung or gone.
|
|
147
|
+
if (previous.player !== undefined)
|
|
148
|
+
terminatePid(previous.player, previous.playerStart);
|
|
149
|
+
return true;
|
|
150
|
+
};
|
|
151
|
+
const self = () => ({ pid: process.pid, pidStart: startTime(process.pid), started: Date.now() });
|
|
152
|
+
/** The hold of the reading whose entry `mine` was just written. */
|
|
153
|
+
const held = (mine) => {
|
|
154
|
+
/**
|
|
155
|
+
* Whether the entry was written. Once it was, a missing entry means another reading took over and already ended
|
|
156
|
+
* (a paused reading has no player to kill, so this is how it learns, S15/S16); if it never was (writes fail), the
|
|
157
|
+
* reading keeps to itself as without a lock.
|
|
158
|
+
*/
|
|
159
|
+
const written = read()?.token === mine.token;
|
|
160
|
+
const lost = () => {
|
|
161
|
+
const entry = read();
|
|
162
|
+
return entry === undefined ? written : entry.token !== mine.token;
|
|
163
|
+
};
|
|
164
|
+
/** The entry as it is now (a pause request may have changed it), when still this reading's. */
|
|
165
|
+
const own = () => {
|
|
166
|
+
const entry = read();
|
|
167
|
+
if (entry === undefined)
|
|
168
|
+
return written ? undefined : mine;
|
|
169
|
+
return entry.token === mine.token ? entry : undefined;
|
|
170
|
+
};
|
|
171
|
+
const setPlayer = (pid) => {
|
|
172
|
+
const entry = own();
|
|
173
|
+
if (!entry)
|
|
174
|
+
return false;
|
|
175
|
+
write({ ...entry, player: pid, playerStart: startTime(pid) });
|
|
176
|
+
return true;
|
|
177
|
+
};
|
|
178
|
+
const setPaused = (paused) => {
|
|
179
|
+
const entry = own();
|
|
180
|
+
if (!entry)
|
|
181
|
+
return;
|
|
182
|
+
const { paused: _was, player: _player, playerStart: _start, ...rest } = entry;
|
|
183
|
+
// Paused, no player runs: the entry keeps the machine through the reader's own process (S15).
|
|
184
|
+
write(paused ? { ...rest, paused: true } : entry.player === undefined ? rest : { ...rest, player: entry.player, playerStart: entry.playerStart });
|
|
185
|
+
};
|
|
186
|
+
const release = () => {
|
|
187
|
+
if (read()?.token !== mine.token)
|
|
188
|
+
return;
|
|
64
189
|
try {
|
|
65
|
-
|
|
190
|
+
unlinkSync(file);
|
|
66
191
|
}
|
|
67
192
|
catch { }
|
|
68
|
-
}
|
|
69
|
-
return
|
|
193
|
+
};
|
|
194
|
+
return {
|
|
195
|
+
setPlayer: (pid) => exclusive(() => setPlayer(pid), () => setPlayer(pid)),
|
|
196
|
+
lost,
|
|
197
|
+
release: () => exclusive(release, release),
|
|
198
|
+
paused: () => {
|
|
199
|
+
const entry = read();
|
|
200
|
+
if (entry === undefined || entry.token !== mine.token)
|
|
201
|
+
return undefined;
|
|
202
|
+
return entry.paused === true;
|
|
203
|
+
},
|
|
204
|
+
setPaused: (paused) => exclusive(() => setPaused(paused), () => setPaused(paused)),
|
|
205
|
+
};
|
|
70
206
|
};
|
|
71
|
-
const
|
|
207
|
+
const busy = () => {
|
|
208
|
+
const entry = read();
|
|
209
|
+
return entry !== undefined && playing(entry);
|
|
210
|
+
};
|
|
211
|
+
const kindOf = (kind = {}) => ({
|
|
212
|
+
...(kind.pausable ? { pausable: true } : {}),
|
|
213
|
+
...(kind.prompt ? { prompt: true } : {}),
|
|
214
|
+
});
|
|
215
|
+
/** Ends the reading in another session without touching what waits; true when one was playing or paused. */
|
|
216
|
+
const skip = () => takeOver({ token: randomUUID(), ...self(), stopped: true }, false);
|
|
72
217
|
return {
|
|
73
|
-
claim() {
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
218
|
+
claim(options = {}) {
|
|
219
|
+
const mine = { token: randomUUID(), ...self(), ...kindOf(options) };
|
|
220
|
+
const take = () => {
|
|
221
|
+
if (options.dropWaiting)
|
|
222
|
+
dropWaiting();
|
|
223
|
+
takeOver(mine, true);
|
|
224
|
+
};
|
|
225
|
+
exclusive(take, take);
|
|
226
|
+
return held(mine);
|
|
227
|
+
},
|
|
228
|
+
claimIfFree(mark, kind) {
|
|
229
|
+
return exclusive(() => {
|
|
230
|
+
if (waitingMark() !== mark)
|
|
231
|
+
return "dropped";
|
|
232
|
+
if (busy())
|
|
233
|
+
return "busy";
|
|
234
|
+
const mine = { token: randomUUID(), ...self(), ...kindOf(kind) };
|
|
235
|
+
write(mine);
|
|
236
|
+
return held(mine);
|
|
237
|
+
}, () => "busy");
|
|
238
|
+
},
|
|
239
|
+
swapProject(project) {
|
|
240
|
+
const swap = () => {
|
|
241
|
+
let previous;
|
|
242
|
+
try {
|
|
243
|
+
const saved = JSON.parse(readFileSync(projects, "utf8"))?.project;
|
|
244
|
+
if (typeof saved === "string")
|
|
245
|
+
previous = saved;
|
|
246
|
+
}
|
|
247
|
+
catch { }
|
|
248
|
+
if (previous !== project)
|
|
249
|
+
put(projects, randomUUID(), { project });
|
|
250
|
+
return previous;
|
|
251
|
+
};
|
|
252
|
+
return exclusive(swap, swap);
|
|
253
|
+
},
|
|
254
|
+
busy,
|
|
255
|
+
waitingMark,
|
|
256
|
+
dropWaiting: () => exclusive(dropWaiting, dropWaiting),
|
|
257
|
+
stopOthers: () => {
|
|
258
|
+
const stop = () => {
|
|
259
|
+
dropWaiting();
|
|
260
|
+
return takeOver({ token: randomUUID(), ...self(), stopped: true }, false);
|
|
261
|
+
};
|
|
262
|
+
return exclusive(stop, stop);
|
|
263
|
+
},
|
|
264
|
+
skipOthers: () => exclusive(skip, skip),
|
|
265
|
+
togglePause: () => {
|
|
266
|
+
const toggle = () => {
|
|
78
267
|
const entry = read();
|
|
79
|
-
|
|
268
|
+
if (entry === undefined || !playing(entry))
|
|
269
|
+
return undefined;
|
|
270
|
+
if (entry.prompt)
|
|
271
|
+
return skip() ? "stopped" : undefined;
|
|
272
|
+
if (!entry.pausable)
|
|
273
|
+
return "unsupported";
|
|
274
|
+
if (entry.paused) {
|
|
275
|
+
const { paused: _paused, ...rest } = entry;
|
|
276
|
+
write(rest);
|
|
277
|
+
return "resumed";
|
|
278
|
+
}
|
|
279
|
+
write({ ...entry, paused: true });
|
|
280
|
+
return "paused";
|
|
80
281
|
};
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
282
|
+
return exclusive(toggle, toggle);
|
|
283
|
+
},
|
|
284
|
+
publishLine(token, session, items, tag, asking) {
|
|
285
|
+
if (items.length === 0) {
|
|
286
|
+
try {
|
|
287
|
+
unlinkSync(lineFile(token));
|
|
288
|
+
}
|
|
289
|
+
catch { }
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
put(lineFile(token), token, {
|
|
293
|
+
token,
|
|
294
|
+
session,
|
|
295
|
+
tag: tag || token.slice(-4),
|
|
296
|
+
beat: Date.now(),
|
|
297
|
+
...(asking ? { asking: true } : {}),
|
|
298
|
+
pid: process.pid,
|
|
299
|
+
pidStart: startTime(process.pid),
|
|
300
|
+
items,
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
waitingList() {
|
|
304
|
+
let names;
|
|
305
|
+
try {
|
|
306
|
+
names = readdirSync(dir).filter((name) => /^line-[\w-]+\.json$/.test(name));
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
const mark = waitingMark();
|
|
312
|
+
const all = [];
|
|
313
|
+
/** Each listed session's tag, by token, and the tokens under each label. */
|
|
314
|
+
const tags = new Map();
|
|
315
|
+
const labels = new Map();
|
|
316
|
+
for (const name of names) {
|
|
317
|
+
try {
|
|
318
|
+
const line = JSON.parse(readFileSync(join(dir, name), "utf8"));
|
|
319
|
+
if (typeof line.token !== "string" || typeof line.session !== "string" || !Number.isInteger(line.pid) || !Array.isArray(line.items))
|
|
320
|
+
continue;
|
|
321
|
+
// A session whose process is gone left its line behind: it waits for nothing.
|
|
322
|
+
if (!sameProcess(line.pid, typeof line.pidStart === "string" ? line.pidStart : undefined)) {
|
|
323
|
+
try {
|
|
324
|
+
unlinkSync(join(dir, name));
|
|
325
|
+
}
|
|
326
|
+
catch { }
|
|
327
|
+
continue;
|
|
94
328
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
329
|
+
for (const item of line.items) {
|
|
330
|
+
if (typeof item?.name !== "string" || typeof item.at !== "number")
|
|
331
|
+
continue;
|
|
332
|
+
const itemMark = typeof item.mark === "string" ? item.mark : undefined;
|
|
333
|
+
// Dropped by an explicit action since it was queued (S8): it will never play.
|
|
334
|
+
if (itemMark !== mark)
|
|
335
|
+
continue;
|
|
336
|
+
const beat = typeof line.beat === "number" ? line.beat : undefined;
|
|
337
|
+
all.push({
|
|
338
|
+
name: item.name,
|
|
339
|
+
at: item.at,
|
|
340
|
+
mark: itemMark,
|
|
341
|
+
session: line.session,
|
|
342
|
+
token: line.token,
|
|
343
|
+
beat,
|
|
344
|
+
...(line.asking === true ? { asking: true } : {}),
|
|
345
|
+
});
|
|
346
|
+
tags.set(line.token, typeof line.tag === "string" && line.tag ? line.tag : line.token.slice(-4));
|
|
347
|
+
labels.set(line.session, (labels.get(line.session) ?? new Set()).add(line.token));
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
catch { }
|
|
351
|
+
}
|
|
352
|
+
for (const item of all)
|
|
353
|
+
if ((labels.get(item.session)?.size ?? 0) > 1)
|
|
354
|
+
item.session = `${item.session} #${tags.get(item.token)}`;
|
|
355
|
+
return all.sort((a, b) => a.at - b.at);
|
|
98
356
|
},
|
|
99
|
-
stopOthers: () => takeOver({ token: randomUUID(), ...self(), stopped: true }, false),
|
|
100
357
|
};
|
|
101
358
|
}
|
|
102
359
|
/**
|
|
@@ -108,36 +365,7 @@ function playing(entry) {
|
|
|
108
365
|
return false;
|
|
109
366
|
return entry.player === undefined || isPlayer(entry.player, entry.playerStart);
|
|
110
367
|
}
|
|
111
|
-
/**
|
|
112
|
-
function
|
|
113
|
-
|
|
114
|
-
process.kill(pid, 0);
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
return start === undefined || startTime(pid) === start;
|
|
120
|
-
}
|
|
121
|
-
/** Guards against pid reuse: only a live player we started, of this user, may be signalled. */
|
|
122
|
-
function isPlayer(pid, start) {
|
|
123
|
-
if (!sameProcess(pid, start))
|
|
124
|
-
return false;
|
|
125
|
-
try {
|
|
126
|
-
if (process.getuid && statSync(`/proc/${pid}`).uid !== process.getuid())
|
|
127
|
-
return false;
|
|
128
|
-
return readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").includes(PLAYER_MARK);
|
|
129
|
-
}
|
|
130
|
-
catch {
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/** Field 22 of /proc/<pid>/stat, the process start time; undefined where /proc is missing. */
|
|
135
|
-
function startTime(pid) {
|
|
136
|
-
try {
|
|
137
|
-
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
138
|
-
return stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19];
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
368
|
+
/** Blocks this thread for `ms`; only ever a few milliseconds, while another process holds the mutex. */
|
|
369
|
+
function pause(ms) {
|
|
370
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
143
371
|
}
|
package/dist/src/settings.js
CHANGED
|
@@ -57,11 +57,40 @@ export function loadSpeed(env) {
|
|
|
57
57
|
const { speed } = parsed;
|
|
58
58
|
return inRange(speed) ? { speed } : ignored("speed must be a number from 0.5 to 2.0");
|
|
59
59
|
}
|
|
60
|
+
const AUTO_VALUES = ["on", "questions", "off"];
|
|
60
61
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
62
|
+
* The auto-read mode and output the last session chose (S24), from settings.json `autoRead` and `output`. A missing
|
|
63
|
+
* file, key, or a value of the wrong kind means none saved (an unreadable or invalid file is reported by `loadSpeed`
|
|
64
|
+
* at the first reading). Nothing throws.
|
|
63
65
|
*/
|
|
66
|
+
export function loadSaved(env) {
|
|
67
|
+
const path = configFile(env, SETTINGS);
|
|
68
|
+
if (!path)
|
|
69
|
+
return {};
|
|
70
|
+
let parsed;
|
|
71
|
+
try {
|
|
72
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
78
|
+
return {};
|
|
79
|
+
const { autoRead, output } = parsed;
|
|
80
|
+
return {
|
|
81
|
+
...(typeof autoRead === "string" && AUTO_VALUES.includes(autoRead) ? { autoRead: autoRead } : {}),
|
|
82
|
+
...(typeof output === "string" && output ? { output } : {}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Saves `speed` in settings.json, keeping every other key (see `saveSettings`). */
|
|
64
86
|
export function saveSpeed(env, speed) {
|
|
87
|
+
saveSettings(env, { speed });
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Saves `values` in settings.json, keeping every other key. Throws an Error naming the file and why when it
|
|
91
|
+
* cannot; an existing file that is not a JSON object is left as it is rather than overwritten.
|
|
92
|
+
*/
|
|
93
|
+
export function saveSettings(env, values) {
|
|
65
94
|
const path = configFile(env, SETTINGS);
|
|
66
95
|
if (!path)
|
|
67
96
|
throw new Error(`it was not saved: no config folder (set XDG_CONFIG_HOME or HOME to an absolute path)`);
|
|
@@ -89,7 +118,7 @@ export function saveSpeed(env, speed) {
|
|
|
89
118
|
}
|
|
90
119
|
try {
|
|
91
120
|
mkdirSync(dirname(path), { recursive: true });
|
|
92
|
-
writeFileSync(path, `${JSON.stringify({ ...settings,
|
|
121
|
+
writeFileSync(path, `${JSON.stringify({ ...settings, ...values }, null, 2)}\n`);
|
|
93
122
|
}
|
|
94
123
|
catch (error) {
|
|
95
124
|
throw fail(error.message);
|