Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions report-app/report-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '../runner/reporting/report-local-disk';

const app = express();
const reportsLoaderPromise = getReportLoader();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I had it set up this way with a lazy promise was that the CLI's dev server was a bit flaky if the the boot-up sequence for the Express server is asynchronous. I think we may have to keep that so it shows up reliably.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with flaky?

I saw some flakiness/or delay of the server when booting up, but that was because we printed the "Server is ready at: localhost:XX" too early. I've fixed this by leveraging the Express "listen" callback

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was happening either if I had the server already running and I restarted it, or if I go to localhost:4200 too quickly. The CLI would throw some error since the server hadn't booted up yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen anything like this (after adding the listen Express hook). How do we proceed?

We need to await so that we can hook up endpoints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's proceed as is and we can think of something else if it shows up again.

const reportsLoader = await getReportLoader();
const options = getOptions();
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
Expand All @@ -23,7 +23,7 @@ let localDataPromise: Promise<FetchedLocalReports> | null = null;
// Endpoint for fetching all available report groups.
app.get('/api/reports', async (_, res) => {
const [remoteGroups, localData] = await Promise.all([
reportsLoaderPromise.then((loader) => loader.getGroupsList()),
reportsLoader.getGroupsList(),
resolveLocalData(options.reportsRoot),
]);
const results = remoteGroups.slice();
Expand All @@ -44,8 +44,7 @@ app.get('/api/reports/:id', async (req, res) => {
if (localData.has(id)) {
result = [localData.get(id)!.run];
} else {
const loader = await reportsLoaderPromise;
result = await loader.getGroupedReports(id);
result = await reportsLoader.getGroupedReports(id);
}

res.json(result);
Expand All @@ -68,15 +67,21 @@ app.use('/**', (req, res, next) => {
.catch(next);
});

// Support custom endpoints by advanced users.
await reportsLoader.configureEndpoints?.(app);

if (isMainModule(import.meta.url)) {
app.listen(options.port);
app.listen(options.port, () => {
console.log(`Server listening on port: ${options.port}`);
});
}

export const reqHandler = createNodeRequestHandler(app);

interface ReportLoader {
getGroupedReports: (groupId: string) => Promise<{ group: string }[]>;
getGroupsList: () => Promise<{ id: string }[]>;
configureEndpoints?: (expressApp: typeof app) => Promise<void>;
}

/** Gets the server options from the command line. */
Expand Down
9 changes: 1 addition & 8 deletions report-app/src/app/pages/report-viewer/report-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,7 @@ export class ReportViewer {
});

protected getScreenshotUrl(result: AssessmentResult): string | null {
for (let i = result.attemptDetails.length - 1; i > -1; i--) {
const screenshotUrl =
result.attemptDetails[i].buildResult.screenshotPngUrl;
if (screenshotUrl) {
return screenshotUrl;
}
}
return null;
return result.build.screenshotPngUrl ?? null;
}

protected isLoading = this.reportsFetcher.isLoadingSingleReport;
Expand Down
27 changes: 17 additions & 10 deletions runner/report-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ async function handler(cliArgs: Arguments<Options>): Promise<void> {
);
}

console.log(
formatTitleCard(
[
`View your reports at http://localhost:${cliArgs.port}`,
`Reports are served from ${relative(process.cwd(), reportsDir)}`,
].join('\n')
)
);

await executeCommand(
'node report-app/server/server.mjs',
join(import.meta.dirname, '..'),
environmentVariables,
{ forwardStderrToParent: true, forwardStdoutToParent: true }
{
forwardStderrToParent: true,
forwardStdoutToParent: true,
notifyWhenMatchingStdout: {
pattern: /Server listening on port:/,
notifyFn: () => {
console.log(
formatTitleCard(
[
`View your reports at http://localhost:${cliArgs.port}`,
`Reports are served from ${relative(process.cwd(), reportsDir)}`,
].join('\n')
)
);
},
},
}
);
}
9 changes: 9 additions & 0 deletions runner/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function executeCommand(
opts: {
forwardStderrToParent?: boolean;
forwardStdoutToParent?: boolean;
notifyWhenMatchingStdout?: { notifyFn: () => void; pattern: RegExp };
abortSignal?: AbortSignal;
} = {}
): Promise<string> {
Expand All @@ -28,6 +29,7 @@ export function executeCommand(

let stdout = '';
let stderr = '';
let notifyWhenMatchingStdout = opts.notifyWhenMatchingStdout;

proc.on('error', (err) => {
reject(err);
Expand All @@ -38,6 +40,13 @@ export function executeCommand(
if (opts.forwardStdoutToParent) {
process.stdout.write(c);
}
if (
notifyWhenMatchingStdout &&
notifyWhenMatchingStdout.pattern.test(stdout)
) {
notifyWhenMatchingStdout.notifyFn();
notifyWhenMatchingStdout = undefined;
}
});
proc.stderr!.on('data', (c) => {
stderr += c;
Expand Down
Loading