Skip to content

Commit 9f95ccb

Browse files
committed
review fixes
1 parent 4b37496 commit 9f95ccb

3 files changed

Lines changed: 174 additions & 154 deletions

File tree

packages/cli/src/commands/open.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { FullConfig } from "@allurereport/core";
21
import { readConfig } from "@allurereport/core";
32
import { serve } from "@allurereport/static-server";
43
import { Command, Option } from "clipanion";
54
import { glob } from "glob";
65
import { randomUUID } from "node:crypto";
7-
import { mkdir } from "node:fs/promises";
6+
import { existsSync } from "node:fs";
7+
import { mkdtemp, rm } from "node:fs/promises";
8+
import { tmpdir } from "node:os";
89
import { join } from "node:path";
910
import { cwd as processCwd } from "node:process";
1011
import { generate } from "./commons/generate.js";
@@ -22,8 +23,8 @@ export class OpenCommand extends Command {
2223
});
2324

2425
resultsDir = Option.String({
25-
required: true,
26-
name: "Pattern to match test results directories in the current working directory (default: ./**/allure-results)",
26+
name: "Pattern to match test results directories in the current working directory (default: ./allure-results)",
27+
required: false,
2728
});
2829

2930
config = Option.String("--config,-c", {
@@ -40,43 +41,53 @@ export class OpenCommand extends Command {
4041

4142
async execute() {
4243
const cwd = this.cwd ?? processCwd();
44+
const targetFullPath = join(cwd, this.resultsDir ?? "allure-report");
45+
const summaryFiles = existsSync(targetFullPath)
46+
? await glob(join(targetFullPath, "**", "summary.json"), {
47+
mark: true,
48+
nodir: false,
49+
absolute: true,
50+
dot: true,
51+
windowsPathsNoEscape: true,
52+
cwd,
53+
})
54+
: [];
4355

44-
const isGlobPatternGiven = this.resultsDir.includes("*") || this.resultsDir.includes("?");
45-
const summaryFilesGlob = isGlobPatternGiven ? this.resultsDir : join(this.resultsDir, "**", "summary.json");
46-
const summaryFiles = await glob(summaryFilesGlob, {
47-
mark: true,
48-
nodir: false,
49-
absolute: true,
50-
dot: true,
51-
windowsPathsNoEscape: true,
52-
cwd,
53-
});
54-
let config: FullConfig;
56+
if (summaryFiles.length > 0) {
57+
const config = await readConfig(cwd, this.config, {
58+
port: this.port,
59+
});
5560

56-
// there's no generated report to serve, so we generate it first in a temp directory
57-
if (!summaryFiles.length) {
58-
config = await readConfig(cwd, this.config, {
61+
await serve({
62+
port: config.port ? parseInt(config.port, 10) : undefined,
63+
servePath: targetFullPath,
64+
open: true,
65+
});
66+
} else {
67+
const tmpDir = await mkdtemp(join(tmpdir(), `allure-report-${randomUUID()}`));
68+
const config = await readConfig(cwd, this.config, {
5969
port: this.port,
60-
output: join(cwd, ".allure", randomUUID()),
70+
output: tmpDir,
6171
});
6272

63-
await mkdir(config.output, { recursive: true });
6473
await generate({
65-
resultsDir: this.resultsDir,
74+
resultsDir: targetFullPath,
6675
cwd,
6776
config,
6877
});
69-
} else {
70-
config = await readConfig(cwd, this.config, {
71-
port: this.port,
72-
output: this.resultsDir,
78+
79+
// clean up temp report directory on ctrl-c
80+
process.on("SIGINT", async () => {
81+
await rm(config.output, { recursive: true });
82+
83+
process.exit(0);
7384
});
74-
}
7585

76-
await serve({
77-
port: config.port ? parseInt(config.port, 10) : undefined,
78-
servePath: config.output,
79-
open: true,
80-
});
86+
await serve({
87+
port: config.port ? parseInt(config.port, 10) : undefined,
88+
servePath: config.output,
89+
open: true,
90+
});
91+
}
8192
}
8293
}

0 commit comments

Comments
 (0)