11import path from "path" ;
22import fs from "fs/promises" ;
3- import { existsSync as exists } from "fs" ;
43import assert from "assert" ;
54import ora from "ora" ;
65import { mapper } from "@design-sdk/figma-remote" ;
@@ -20,7 +19,17 @@ import { RemoteImageRepositories } from "@design-sdk/figma-remote/asset-reposito
2019
2120setupCache ( axios ) ;
2221
23- const mkdir = ( path : string ) => ! exists ( path ) && fs . mkdir ( path ) ;
22+ const exists = async ( path : string ) => {
23+ try {
24+ await fs . access ( path ) ;
25+ return true ;
26+ } catch ( e ) {
27+ return false ;
28+ }
29+ } ;
30+
31+ const mkdir = async ( path : string ) =>
32+ ! ( await exists ( path ) ) && ( await fs . mkdir ( path ) ) ;
2433
2534interface ReportConfig {
2635 sample : string ;
@@ -61,7 +70,7 @@ async function report() {
6170 console . info ( `Loaded ${ samples . length } samples` ) ;
6271 console . info ( `Configuration used - ${ JSON . stringify ( config , null , 2 ) } ` ) ;
6372
64- mkdir ( coverage_path ) ;
73+ await mkdir ( coverage_path ) ;
6574
6675 const client = Client ( {
6776 paths : {
@@ -73,10 +82,12 @@ async function report() {
7382 const ssworker = new ScreenshotWorker ( { } ) ;
7483 await ssworker . launch ( ) ;
7584
85+ let i = 0 ;
7686 for ( const c of samples ) {
87+ i ++ ;
7788 // create .coverage/:id folder
7889 const coverage_set_path = path . join ( coverage_path , c . id ) ;
79- mkdir ( coverage_set_path ) ;
90+ await mkdir ( coverage_set_path ) ;
8091
8192 const { id : filekey } = c ;
8293 let file ;
@@ -109,17 +120,22 @@ async function report() {
109120 continue ;
110121 }
111122
123+ let ii = 0 ;
112124 for ( const frame of frames ) {
113- const spinner = ora ( `Running coverage for ${ c . id } /${ frame . id } ` ) . start ( ) ;
125+ ii ++ ;
126+
127+ const spinner = ora (
128+ `[${ i } /${ samples . length } ] Running coverage for ${ c . id } /${ frame . id } (${ ii } /${ frames . length } )`
129+ ) . start ( ) ;
114130
115131 // create .coverage/:id/:node folder
116132 const coverage_node_path = path . join ( coverage_set_path , frame . id ) ;
117- mkdir ( coverage_node_path ) ;
133+ await mkdir ( coverage_node_path ) ;
118134
119135 // report.json
120136 const report_file = path . join ( coverage_node_path , "report.json" ) ;
121137 if ( config . skipIfReportExists ) {
122- if ( exists ( report_file ) ) {
138+ if ( await exists ( report_file ) ) {
123139 spinner . succeed ( `Skipping - report for ${ frame . id } already exists` ) ;
124140 continue ;
125141 }
@@ -152,20 +168,31 @@ async function report() {
152168 const image_a = path . join ( coverage_node_path , image_a_rel ) ;
153169 // download the exported image with url
154170 // if the exported is local fs path, then use copy instead
155- if ( exists ( exported ) ) {
156- // copy file with symlink
157- // unlink if exists
158- if ( exists ( image_a ) ) {
159- await fs . unlink ( image_a ) ;
171+ if ( await exists ( exported ) ) {
172+ try {
173+ // copy file with symlink
174+ // rempve if already exists before linking new one
175+ if ( await exists ( image_a ) ) {
176+ await fs . unlink ( image_a ) ;
177+ }
178+ await fs . symlink ( exported , image_a ) ;
179+ } catch ( e ) {
180+ // TODO: symlink still fails with "EEXIST: file already exists, symlink"
181+ // we need to handle this.
182+ // reason? - unknown
160183 }
161- await fs . symlink ( exported , image_a ) ;
162184 } else if ( exported . startsWith ( "http" ) ) {
163185 const dl = await axios . get ( exported , { responseType : "arraybuffer" } ) ;
164186 await fs . writeFile ( image_a , dl . data ) ;
165187 } else {
166188 throw new Error ( `File not found - ${ exported } ` ) ;
167189 }
168190
191+ if ( ! ( await exists ( image_a ) ) ) {
192+ spinner . fail ( `Image A not found - ${ image_a } ` ) ;
193+ continue ;
194+ }
195+
169196 // codegen
170197 const code = await htmlcss (
171198 {
@@ -239,6 +266,13 @@ async function report() {
239266 spinner . fail ( `error on ${ frame . id } : ${ e . message } ` ) ;
240267 }
241268 }
269+
270+ // cleaup
271+ // if the coverage is empty, remove the folder
272+ const files = await fs . readdir ( coverage_set_path ) ;
273+ if ( files . length === 0 ) {
274+ await fs . rmdir ( coverage_set_path ) ;
275+ }
242276 }
243277
244278 // cleaup
0 commit comments