@samuelfaj/distill 0.1.21 → 0.1.31
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/bin/distill.js +115 -1
- package/package.json +1 -1
package/bin/distill.js
CHANGED
|
@@ -35,9 +35,119 @@ function resolveBinaryPath() {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
const PROGRESS_PREFIX = "__DISTILL_PROGRESS__:";
|
|
39
|
+
const PROGRESS_FRAMES = ["-", "\\", "|", "/"];
|
|
40
|
+
const PROGRESS_DOT_FRAMES = ["", ".", "..", "...", "..", "."];
|
|
41
|
+
const PROGRESS_LABELS = {
|
|
42
|
+
collecting: "distill: waiting",
|
|
43
|
+
summarizing: "distill: summarizing"
|
|
44
|
+
};
|
|
45
|
+
|
|
38
46
|
const binPath = resolveBinaryPath();
|
|
47
|
+
const progressWriter = process.stderr.isTTY ? process.stderr : process.stdout.isTTY ? process.stdout : null;
|
|
48
|
+
let progressPhase = "collecting";
|
|
49
|
+
let progressFrame = 0;
|
|
50
|
+
let progressTimer = null;
|
|
51
|
+
let progressVisible = false;
|
|
52
|
+
let childStderrBuffer = "";
|
|
53
|
+
|
|
54
|
+
function renderProgress() {
|
|
55
|
+
if (!progressWriter) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const frame = PROGRESS_FRAMES[progressFrame % PROGRESS_FRAMES.length];
|
|
60
|
+
const dots =
|
|
61
|
+
PROGRESS_DOT_FRAMES[
|
|
62
|
+
Math.floor(progressFrame / PROGRESS_FRAMES.length) % PROGRESS_DOT_FRAMES.length
|
|
63
|
+
];
|
|
64
|
+
progressFrame += 1;
|
|
65
|
+
progressWriter.write(
|
|
66
|
+
`\r\u001b[2K${frame} ${PROGRESS_LABELS[progressPhase] || PROGRESS_LABELS.collecting}${dots}`
|
|
67
|
+
);
|
|
68
|
+
progressVisible = true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function startProgress() {
|
|
72
|
+
if (!progressWriter || progressTimer) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
renderProgress();
|
|
77
|
+
progressTimer = setInterval(renderProgress, 120);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function stopProgress() {
|
|
81
|
+
if (progressTimer) {
|
|
82
|
+
clearInterval(progressTimer);
|
|
83
|
+
progressTimer = null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (progressVisible && progressWriter) {
|
|
87
|
+
progressWriter.write("\r\u001b[2K");
|
|
88
|
+
progressVisible = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function handleChildStderrLine(line) {
|
|
93
|
+
if (!line) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!line.startsWith(PROGRESS_PREFIX)) {
|
|
98
|
+
stopProgress();
|
|
99
|
+
process.stderr.write(`${line}\n`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (line === `${PROGRESS_PREFIX}stop`) {
|
|
104
|
+
stopProgress();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (line.startsWith(`${PROGRESS_PREFIX}phase:`)) {
|
|
109
|
+
progressPhase = line.slice(`${PROGRESS_PREFIX}phase:`.length) || "collecting";
|
|
110
|
+
progressFrame = 0;
|
|
111
|
+
renderProgress();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function flushChildStderr(force = false) {
|
|
116
|
+
if (!force && !childStderrBuffer.includes("\n")) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const parts = childStderrBuffer.split("\n");
|
|
121
|
+
childStderrBuffer = force ? "" : parts.pop() || "";
|
|
122
|
+
|
|
123
|
+
for (const line of parts) {
|
|
124
|
+
handleChildStderrLine(line);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (force && childStderrBuffer) {
|
|
128
|
+
handleChildStderrLine(childStderrBuffer);
|
|
129
|
+
childStderrBuffer = "";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
39
133
|
const child = spawn(binPath, process.argv.slice(2), {
|
|
40
|
-
stdio: "inherit"
|
|
134
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
135
|
+
env: {
|
|
136
|
+
...process.env,
|
|
137
|
+
DISTILL_PROGRESS_PROTOCOL: "stderr"
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
startProgress();
|
|
142
|
+
|
|
143
|
+
child.stdout.on("data", (chunk) => {
|
|
144
|
+
stopProgress();
|
|
145
|
+
process.stdout.write(chunk);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
child.stderr.on("data", (chunk) => {
|
|
149
|
+
childStderrBuffer += chunk.toString("utf8");
|
|
150
|
+
flushChildStderr();
|
|
41
151
|
});
|
|
42
152
|
|
|
43
153
|
const forwardSignal = (signal) => {
|
|
@@ -51,11 +161,15 @@ const forwardSignal = (signal) => {
|
|
|
51
161
|
});
|
|
52
162
|
|
|
53
163
|
child.on("error", (error) => {
|
|
164
|
+
stopProgress();
|
|
54
165
|
console.error(`[distill] Failed to launch native binary: ${error.message}`);
|
|
55
166
|
process.exit(1);
|
|
56
167
|
});
|
|
57
168
|
|
|
58
169
|
child.on("exit", (code, signal) => {
|
|
170
|
+
flushChildStderr(true);
|
|
171
|
+
stopProgress();
|
|
172
|
+
|
|
59
173
|
if (signal) {
|
|
60
174
|
process.removeAllListeners(signal);
|
|
61
175
|
process.kill(process.pid, signal);
|