buffer last line without new line end

This commit is contained in:
Christopher Homberger
2024-12-01 11:41:36 +01:00
parent 303ba421b8
commit d78e1c19fe

View File

@@ -423,113 +423,124 @@ export class Act {
} catch (error: any) { } } catch (error: any) { }
}); });
const handleIO = async (data: any) => { const handleIO = () => {
const lines: string[] = data.toString().split('\n').filter((line: string) => line !== ''); let lastline: string = "";
for await (const line of lines) { return async (data: any) => {
const dateString = new Date().toString(); let xdata: string = data.toString();
let lines: string[] = xdata.split('\n').filter((line: string) => line !== '');
if (lastline?.length > 0) {
lines[0] = lastline + lines[0];
lastline = "";
}
if (!xdata.endsWith("\n")) {
lastline = lines.pop() || "";
}
for await (const line of lines) {
const dateString = new Date().toString();
let message: string; let message: string;
try { try {
const parsedMessage = JSON.parse(line); const parsedMessage = JSON.parse(line);
if (parsedMessage.msg) { if (typeof parsedMessage.msg === 'string') {
message = `${parsedMessage.job ? `[${parsedMessage.job}] ` : ``}${parsedMessage.msg}`; message = `${parsedMessage.job ? `[${parsedMessage.job}] ` : ``}${parsedMessage.msg}`;
} else { } else {
message = line; message = line;
} }
// Update job and step status in workspace history // Update job and step status in workspace history
if (parsedMessage.jobID) { if (parsedMessage.jobID) {
let jobName: string = parsedMessage.jobID; let jobName: string = parsedMessage.jobID;
try { try {
if (parsedMessage.jobID in commandArgs.workflow.yaml.jobs && commandArgs.workflow.yaml.jobs[parsedMessage.jobID].name) { if (parsedMessage.jobID in commandArgs.workflow.yaml.jobs && commandArgs.workflow.yaml.jobs[parsedMessage.jobID].name) {
jobName = commandArgs.workflow.yaml.jobs[parsedMessage.jobID].name; jobName = commandArgs.workflow.yaml.jobs[parsedMessage.jobID].name;
}
} catch (error: any) { }
let jobIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs!
.findIndex(job => job.name === jobName);
if (jobIndex < 0) {
// Add new job with setup step
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs!.push({
name: jobName,
status: HistoryStatus.Running,
date: {
start: dateString
},
steps: [
{
id: "--setup-job", // Special id for setup job
name: 'Setup Job',
status: HistoryStatus.Running,
date: {
start: dateString
}
}
]
});
jobIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs!.length - 1;
} }
} catch (error: any) { }
let jobIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs! const isCompleteJobStep = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.length > 1;
.findIndex(job => job.name === jobName); if (parsedMessage.stepID || isCompleteJobStep) {
if (jobIndex < 0) { let stepName: string;
let stepId: string;
if (!parsedMessage.stepID && isCompleteJobStep) {
stepName = 'Complete Job';
stepId = "--complete-job"; // Special Id for complete job
} else {
stepName = parsedMessage.stage !== 'Main' ? `${parsedMessage.stage} ${parsedMessage.step}` : parsedMessage.step;
stepId = parsedMessage.stepID[0];
}
// Add new job with setup step if (this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].status === HistoryStatus.Running) {
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs!.push({ // TODO: How to know if setup job step failed?
name: jobName, this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].status = HistoryStatus.Success;
status: HistoryStatus.Running, this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].date.end = dateString;
date: { }
start: dateString
}, let stepIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!
steps: [ .findIndex(step => step.id === stepId && step.name === stepName);
{ if (stepIndex < 0) {
id: "--setup-job", // Special id for setup job // Add new step
name: 'Setup Job', this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.push({
id: stepId,
name: stepName,
status: HistoryStatus.Running, status: HistoryStatus.Running,
date: { date: {
start: dateString start: dateString
} }
} });
] stepIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.length - 1;
}); }
jobIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs!.length - 1;
}
const isCompleteJobStep = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.length > 1; if (parsedMessage.stepResult) {
if (parsedMessage.stepID || isCompleteJobStep) { this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![stepIndex].status =
let stepName: string; parsedMessage.stepResult === 'success' ? HistoryStatus.Success : HistoryStatus.Failed;
let stepId: string; this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![stepIndex].date.end = dateString;
if (!parsedMessage.stepID && isCompleteJobStep) { }
stepName = 'Complete Job';
stepId = "--complete-job"; // Special Id for complete job
} else {
stepName = parsedMessage.stage !== 'Main' ? `${parsedMessage.stage} ${parsedMessage.step}` : parsedMessage.step;
stepId = parsedMessage.stepID[0];
} }
if (this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].status === HistoryStatus.Running) { if (parsedMessage.jobResult) {
// TODO: How to know if setup job step failed? this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].status =
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].status = HistoryStatus.Success; parsedMessage.jobResult === 'success' ? HistoryStatus.Success : HistoryStatus.Failed;
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![0].date.end = dateString; this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].date.end =
} dateString;
let stepIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!
.findIndex(step => step.id === stepId && step.name === stepName);
if (stepIndex < 0) {
// Add new step
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.push({
id: stepId,
name: stepName,
status: HistoryStatus.Running,
date: {
start: dateString
}
});
stepIndex = this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps!.length - 1;
}
if (parsedMessage.stepResult) {
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![stepIndex].status =
parsedMessage.stepResult === 'success' ? HistoryStatus.Success : HistoryStatus.Failed;
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].steps![stepIndex].date.end = dateString;
} }
} }
} catch (error: any) {
if (parsedMessage.jobResult) { message = line;
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].status =
parsedMessage.jobResult === 'success' ? HistoryStatus.Success : HistoryStatus.Failed;
this.historyManager.workspaceHistory[commandArgs.path][historyIndex].jobs![jobIndex].date.end =
dateString;
}
} }
} catch (error: any) {
message = line;
}
if (userOptions.includes(Option.Json)) { if (userOptions.includes(Option.Json)) {
message = line; message = line;
} }
writeEmitter.fire(`${message.trimEnd()}\r\n`); writeEmitter.fire(`${message.trimEnd()}\r\n`);
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
}
await this.storageManager.update(StorageKey.WorkspaceHistory, this.historyManager.workspaceHistory);
} }
await this.storageManager.update(StorageKey.WorkspaceHistory, this.historyManager.workspaceHistory);
}; };
let shell = env.shell; let shell = env.shell;
@@ -561,8 +572,8 @@ export class Act {
} }
} }
); );
exec.stdout.on('data', handleIO); exec.stdout.on('data', handleIO());
exec.stderr.on('data', handleIO); exec.stderr.on('data', handleIO());
exec.on('exit', async (code, signal) => { exec.on('exit', async (code, signal) => {
const dateString = new Date().toString(); const dateString = new Date().toString();