mirror of
https://github.com/yoshiko-pg/difit.git
synced 2026-09-19 05:03:24 +08:00
9e2643cfda
* fix(dev): print shutdown review comments * refactor(dev): only hide internal server url * fix(dev): declare stdout proxy types * fix(dev): hide internal port retry logs * refactor(dev): hide nested script banners * style(dev): use rocket banner for vite startup * fix(dev): disable vite clear screen * fix(dev): preserve repeated startup log lines * fix(dev): keep ctrl-c during compile as clean exit * refactor(dev): move pnpm dev helpers into dev dir * rm dev message * fix(hooks): run checks for scripts changes
75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
const CLI_SERVER_URL_PATTERN = /^🚀 difit server started on (https?:\/\/\S+)$/;
|
|
const PORT_RETRY_PATTERN = /^Port \d+ is busy, trying \d+\.\.\.$/;
|
|
|
|
/**
|
|
* @param {{
|
|
* onServerUrl: (serverUrl: string) => void;
|
|
* onOutput: (output: string) => void;
|
|
* }} options
|
|
*/
|
|
export function createCliStdoutProxy({ onServerUrl, onOutput }) {
|
|
let buffer = '';
|
|
let pendingBlankLines = 0;
|
|
/** @type {string | undefined} */
|
|
let detectedServerUrl;
|
|
|
|
function flushPendingBlankLines() {
|
|
while (pendingBlankLines > 0) {
|
|
onOutput('\n');
|
|
pendingBlankLines -= 1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {string} rawLine
|
|
* @param {boolean} hasTrailingNewline
|
|
*/
|
|
function handleLine(rawLine, hasTrailingNewline) {
|
|
const line = rawLine.endsWith('\r') ? rawLine.slice(0, -1) : rawLine;
|
|
|
|
if (line === '') {
|
|
pendingBlankLines += 1;
|
|
return;
|
|
}
|
|
|
|
const serverUrlMatch = line.match(CLI_SERVER_URL_PATTERN);
|
|
const shouldHideServerUrlLine = !detectedServerUrl && serverUrlMatch !== null;
|
|
|
|
if (shouldHideServerUrlLine) {
|
|
detectedServerUrl = serverUrlMatch[1];
|
|
onServerUrl(detectedServerUrl);
|
|
}
|
|
|
|
const shouldHidePortRetryLine = !detectedServerUrl && PORT_RETRY_PATTERN.test(line);
|
|
|
|
if (shouldHideServerUrlLine || shouldHidePortRetryLine) {
|
|
pendingBlankLines = 0;
|
|
return;
|
|
}
|
|
|
|
flushPendingBlankLines();
|
|
onOutput(hasTrailingNewline ? `${line}\n` : line);
|
|
}
|
|
|
|
return {
|
|
push(chunk) {
|
|
buffer += chunk;
|
|
|
|
const lines = buffer.split('\n');
|
|
buffer = lines.pop() ?? '';
|
|
|
|
for (const line of lines) {
|
|
handleLine(line, true);
|
|
}
|
|
},
|
|
flush() {
|
|
if (buffer.length > 0) {
|
|
handleLine(buffer, false);
|
|
}
|
|
|
|
buffer = '';
|
|
pendingBlankLines = 0;
|
|
},
|
|
};
|
|
}
|