@@ -4,12 +4,13 @@ import fs from 'fs';
44import path from 'path' ;
55import cors from 'cors' ;
66import url from 'url' ;
7+ import { acceptOne } from './scripts/accept-new-reference-files.common'
78
89const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
910
1011export interface ServerOptions {
11- port : number ,
12- openPage : string
12+ port : number ;
13+ openPage : string ;
1314}
1415
1516let app : express . Express ;
@@ -21,16 +22,9 @@ export default function server(options: ServerOptions) {
2122 app = express ( ) ;
2223
2324 app . use ( cors ( ) ) ;
24-
25- const exposedFolders = [
26- 'dist' ,
27- 'src' ,
28- 'font' ,
29- 'img' ,
30- 'playground' ,
31- 'playground-template' ,
32- 'test-data'
33- ] ;
25+ app . use ( express . json ( ) ) ;
26+
27+ const exposedFolders = [ 'dist' , 'src' , 'font' , 'img' , 'playground' , 'playground-template' , 'test-data' ] ;
3428
3529 for ( const exposedFolder of exposedFolders ) {
3630 app . use ( '/' + exposedFolder , express . static ( exposedFolder ) ) ;
@@ -41,15 +35,15 @@ export default function server(options: ServerOptions) {
4135 const response : any = [ ] ;
4236
4337 async function crawl ( d : string , name : string ) {
44- console . log ( 'Crawling ' , d ) ;
38+ // console.log('Crawling ', d);
4539 const dir = await fs . promises . opendir ( d ) ;
4640 try {
4741 while ( true ) {
4842 const entry = await dir . read ( ) ;
4943 if ( ! entry ) {
5044 break ;
5145 } else if ( entry . isDirectory ( ) && entry . name !== '.' && entry . name !== '..' ) {
52- await crawl ( path . join ( d , entry . name ) , name + '/' + entry . name )
46+ await crawl ( path . join ( d , entry . name ) , name + '/' + entry . name ) ;
5347 } else if ( entry . isFile ( ) ) {
5448 if ( entry . name . endsWith ( '.new.png' ) ) {
5549 response . push ( {
@@ -60,38 +54,64 @@ export default function server(options: ServerOptions) {
6054 }
6155 }
6256 }
63- }
64- finally {
57+ } finally {
6558 await dir . close ( ) ;
6659 }
6760 }
6861
6962 const testDataPath = path . join ( __dirname , 'test-data' ) ;
70- console . log ( 'will crawl: ' , testDataPath )
63+ // console.log('will crawl: ', testDataPath);
7164 await crawl ( testDataPath , 'test-data' ) ;
7265
7366 res . json ( response ) ;
67+ } catch ( e ) {
68+ res . json ( {
69+ message : ( e as Error ) . message ,
70+ stack : ( e as Error ) . stack
71+ } ) ;
7472 }
75- catch ( e ) {
73+ } ) ;
74+
75+ app . post ( '/accept-test-result' , async ( req , res ) => {
76+ try {
77+ const body = req . body ;
78+ // basic validation that nothing bad happens
79+ if ( typeof body . originalFile !== 'string' ) {
80+ res . sendStatus ( 400 ) ;
81+ return ;
82+ }
83+ const newFile = path . normalize ( path . resolve ( path . join ( __dirname , body . newFile ) ) ) ;
84+ const testDataPath = path . normalize ( path . resolve ( path . join ( __dirname , 'test-data' ) ) ) ;
85+
86+ if ( ! newFile . startsWith ( testDataPath ) ) {
87+ res . sendStatus ( 400 ) ;
88+ return ;
89+ }
90+
91+ await acceptOne ( newFile ) ;
92+ res . json ( {
93+ message : 'Accepted'
94+ } ) ;
95+ } catch ( e ) {
7696 res . json ( {
7797 message : ( e as Error ) . message ,
7898 stack : ( e as Error ) . stack
7999 } ) ;
80100 }
81- } )
101+ } ) ;
82102
83103 app . listen ( options . port , ( ) => {
84104 console . log ( 'Server listening on port ' + options . port ) ;
85105 } ) ;
86106
87- let first = true
107+ let first = true ;
88108 return {
89109 name : 'server' ,
90110 generateBundle ( ) {
91111 if ( first ) {
92- first = false
93- opener ( `http://localhost:${ options . port } ` + options . openPage )
112+ first = false ;
113+ opener ( `http://localhost:${ options . port } ` + options . openPage ) ;
94114 }
95115 }
96116 } ;
97- }
117+ }
0 commit comments