1- import type { FullConfig } from "@allurereport/core" ;
21import { readConfig } from "@allurereport/core" ;
32import { serve } from "@allurereport/static-server" ;
43import { Command , Option } from "clipanion" ;
54import { glob } from "glob" ;
65import { 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" ;
89import { join } from "node:path" ;
910import { cwd as processCwd } from "node:process" ;
1011import { 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