file import { Action , ActionRequest , Target } from "./../../common/Objects/ActionRequest" ; import { ActionRequestResult } from "./../../common/Objects/ActionRequestResult" ; import { ConversionResult } from "./../../common/Objects/ConversionResult" ; import { CSVRow } from "./../../common/Objects/CSVRow" ; import { RepoDetails } from "./../../common/Objects/RepoDetails" ; import { User } from "./../../common/Objects/User" ; import { writeReport } from "./../../common/ReportWriter" ; import { PublishOptions } from "./../../culturize.conf" ; import { mainWindow , showResultWindow , toggleTransformation } from "./../../main" ; import { convertCSVtoWebConfig } from "./../Converter/Converter" ; import { GitRepoManager } from "./../Git/Git" ; import log = require ( "electron-log" ) ; import fs = require ( "fs" ) ; const isGithubUrl = require ( "is-github-url" ) ; import octokit = require ( "@octokit/rest" ) ; import GitUrlParse = require ( "git-url-parse" ) ; 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 publish ( request , repoDetails , defaultBranch , repoPath ) { notifyStep ( "Preparing" ) ; const baseSubdir = PublishOptions . baseSubdir ? PublishOptions . baseSubdir : "" ; if ( baseSubdir !== "" ) { if ( dirRegex . test ( baseSubdir ) ) { request . subdir = baseSubdir + "/" + request . subdir ; } else { log . error ( `Ignored the base subdirectory "${ baseSubdir }" because it is not valid` ) ; } } try { toggleTransformation ( true ) ; log . info ( `Request Data: ${ JSON . stringify ( request ) }` ) ; notifyStep ( "Checking input" ) ; if ( ! checkRequestInput ( request ) ) { throw new Error ( "invalid input" ) ; } const user = request . user ; notifyStep ( "Converting file" ) ; await sleep ( 10 ) ; log . info ( "generation config file for" + request . target ) ; const response = await convertCSVtoWebConfig ( request . csvPath , request . target , request . subdir ) ; log . info ( "Conversion result: " + response . file . length + " characters in the configuration, generated from " + response . numLinesAccepted + " rows (" + response . numLinesRejected + ")" ) ; notifyStep ( "Parsing URL" ) ; const isOwnerOfRepo = ( repoDetails . getOwner ( ) . toUpperCase ( ) === user . userName . toUpperCase ( ) ) ; let repoURL ; repoURL = request . repoUrl ; if ( isOwnerOfRepo ) { notifyStep ( `${ user . userName } owns ${ request . repoUrl }` ) ; } else { notifyStep ( `${ user . userName } does not own the repo ${ request . repoUrl }, make sure you have permissions to
                    push and pull this repo.` ) ; } notifyStep ( "Preparing Git" ) ; log . info ( "Preparing GitRepoManager instance" ) ; const manager = await prepareGitRepoManager ( repoURL , request . branch , user , repoPath , defaultBranch ) ; notifyStep ( "Saving the configuration file to the desired location" ) ; if ( request . target === Target . apache ) { manager . saveStringToFile ( response . file , ".htaccess" , request . subdir ) ; } else { manager . saveStringToFile ( response . file , "nginx_redirect.conf" , request . subdir ) ; } notifyStep ( "Pushing changes" ) ; await manager . pushChanges ( request . commitMsg ) ; 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 , response . rows , reportFilename ) ; notifyStep ( "Done !" ) ; toggleTransformation ( false ) ; sendRequestResult ( new ActionRequestResult ( Action . publish , true , false , null , reportFilename , response . numLinesAccepted , response . numLinesRejected ) ) ; } catch ( error ) { toggleTransformation ( false ) ; log . error ( error as string ) ; sendRequestResult ( new ActionRequestResult ( Action . publish , 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 ( "publish-done" , result ) ; } param { string } repoURL param { string } branch param { string } token param { string } repoPath async function prepareGitRepoManager ( repoURL , branch , user , repoPath , defaultBranch ) { const grm = new GitRepoManager ( repoURL , branch , defaultBranch , user , repoPath ) ; log . info ( "Updating local copy" ) ; try { await grm . updateLocalCopy ( ) ; } catch ( error ) { log . error ( error ) ; throw error ; } return grm ; } param { PublishRequest } request function checkRequestInput ( request ) { const repoUrl = request . repoUrl ; if ( ! isGithubUrl ( repoUrl ) ) { log . error ( '"' + repoUrl + '" is not a valid GitHub repository' ) ; return false ; } const subdir = request . subdir ; if ( ( subdir . length > 0 ) && ( ! dirRegex . test ( subdir ) ) ) { log . error ( '"' + subdir + '" is not a valid path' ) ; return false ; } 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 ; }