file import { BrowserWindow } from "electron" ; import { Action , ActionRequest } from "./../../common/Objects/ActionRequest" ; import { ActionRequestResult } from "./../../common/Objects/ActionRequestResult" ; import { ConversionResult } from "./../../common/Objects/ConversionResult" ; import { CSVRow } from "./../../common/Objects/CSVRow" ; import { User } from "./../../common/Objects/User" ; import { writeReport } from "./../../common/ReportWriter" ; import { PublishOptions } from "./../../culturize.conf" ; import { mainWindow , showResultWindow , toggleTransformation , validationAborted } from "./../../main" ; import { createArrayFromCSV } from "./../Parser/Parser" ; import log = require ( "electron-log" ) ; import fs = require ( "fs" ) ; import path = require ( "path" ) ; import { shell } from "electron" ; /**
 * This is the regular expression used to check the
 * validity of the subdirectory given by the user
 * (and the baseSubdir of PublishOptions)
 */ const dirRegex = /^([\w-]+)(((\/)([\w-]+))+)?$/ ; param { number } ms const sleep = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ; async param { PublishRequest } request param { string } path export async function validate ( request ) { notifyStep ( "Preparing" ) ; try { toggleTransformation ( true ) ; log . info ( `Request Data: ${ JSON . stringify ( request ) }` ) ; notifyStep ( "Checking input parameters" ) ; if ( ! checkRequestInput ( request ) ) { throw new Error ( "invalid input" ) ; } notifyStep ( "Parsing CSV" ) ; let numAccepted = 0 ; let numRejected = 0 ; const rows = await createArrayFromCSV ( request . csvPath , ( ) => { numAccepted ++ ; } , ( row ) => { numRejected ++ ; } , true ) ; notifyStep ( "Checking URLs" ) ; const resultWindow = showResultWindow ( ) ; await resultWindow . loadFile ( __dirname + "/../../../static/report.html" ) ; resultWindow . webContents . send ( "start-validation" ) ; await checkURLs ( rows , resultWindow ) ; if ( validationAborted ) { notifyStep ( "Validation aborted" ) ; toggleTransformation ( false ) ; sendRequestResult ( new ActionRequestResult ( Action . validate , false , true ) ) ; return ; } numAccepted = 0 ; numRejected = 0 ; for ( const row of rows ) { if ( row . valid && row . urlWorking && row . duplicateOf === - 1 ) { numAccepted += 1 ; } else { numRejected += 1 ; } } notifyStep ( "Writing report" ) ; const reportFilename = path . join ( path . dirname ( request . csvPath ) , path . basename ( request . csvPath ) + "-" + request . timestamp . replace ( /:/g , "" ) . replace ( / / , "_" ) + "-report.html" ) ; writeReport ( request . action , rows , reportFilename ) ; notifyStep ( "Done !" ) ; toggleTransformation ( false ) ; sendRequestResult ( new ActionRequestResult ( Action . validate , true , false , null , reportFilename , numAccepted , numRejected ) ) ; } catch ( error ) { toggleTransformation ( false ) ; log . error ( error as string ) ; sendRequestResult ( new ActionRequestResult ( Action . validate , false , false , error as string ) ) ; } } param { string } stepDesc function notifyStep ( stepDesc ) { log . info ( stepDesc ) ; mainWindow . webContents . send ( "update-action-step" , stepDesc ) ; } param { ActionRequestResult } result function sendRequestResult ( result ) { mainWindow . webContents . send ( "validate-done" , result ) ; } param { PublishRequest } request function checkRequestInput ( request ) { const repoUrl = request . repoUrl ; const csvPath = request . csvPath ; if ( ! fs . existsSync ( csvPath ) ) { log . error ( "The file \"" + path + "\" does not exists." ) ; return false ; } if ( ! csvPath . endsWith ( ".csv" ) ) { log . error ( "The file \"" + path + "\" is not a .csv file." ) ; return false ; } return true ; } param { CSVRow [ ] } rows async function checkURLs ( rows , resultWindow ) { let numAccepted = 0 ; let numRejected = 0 ; for ( const row of rows ) { if ( validationAborted ) { console . log ( "abort" ) ; break ; } await row . checkURL ( ) ; if ( row . valid && row . urlWorking && row . duplicateOf === - 1 ) { numAccepted += 1 ; } else { numRejected += 1 ; } const data = { html : row . createHTMLRow ( ) , accepted : numAccepted , rejected : numRejected , } ; if ( ! validationAborted ) { resultWindow . webContents . send ( "new-data" , data ) ; } } }