<commit_before>const React = require('react');
const app = window.require('electron').remote; 
const dialog = app.dialog;
const fork = window.require('child_process').fork;
const sudo = window.require('sudo-prompt');
const ipc = window.require('node-ipc');
const fs = window.require('fs');

// React Components
import Block from './Block';
import Progress from './progress';
import Info from './info';

var IPCserverStarted;   // Promise, returns socket on resolution


class MainPage extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            progress: {
                value: 0,
                infoText: 'Go ahead! click USB Mass Storage button to begin'
            },
            buttonState: {
                ums: true,
                img: false,
                flash: false
            }
        };
        this.umsClick = this.umsClick.bind(this);
        this.selectImage = this.selectImage.bind(this);
        this.writeImage = this.writeImage.bind(this);

        // IPC Server
        window.process.env.IPC_SERVER_ID = `BB-server-${window.process.pid}`;
        window.process.env.IPC_CLIENT_ID = `BB-client-${window.process.pid}`;

        ipc.config.id = window.process.env.IPC_SERVER_ID;
        ipc.config.silent = true;
        ipc.serve();

        ipc.server.on('message', function(data){
            console.log(data);
            try{
                var status = JSON.parse(data);
            }
            catch(e){ }
            if(status && status.description) this.setState((prevState)=>{
                return {
                    progress: {
                        value: status.complete,
                        infoText: status.description
                    },
                    buttonState:{
                        ums: prevState.buttonState.ums,
                        img: prevState.buttonState.img,
                        flash: prevState.buttonState.flash
                    }
                }
            });

            if(status && status.done) this.setState((prevState)=>{
                return {
                    progress: {
                        value: 0,
                        infoText: 'Select Image for flashing'
                    },
                    buttonState:{
                        ums: false,
                        img: true,
                        flash: false
                    }
                }
            });
        }.bind(this));
    }

        

    umsClick(){
        // Promise, starts IPC server and returns socket for client on resolution
        IPCserverStarted = new Promise((resolve, reject)=>{

            ipc.server.on('start', () => {
                
                const child = fork('./lib/elevate.js', [],{
                    silent: true, // in order for the stdin, stdout and stderr to get piped back to the parent process
                    env: window.process.env
                });
                child.stdout.on('data', (data)=>{
                    console.log(`${data}`);
                });

                child.stderr.on('data', (data)=>{
                    console.log(`${data}`);
                    child.kill();
                    reject();
                });

                child.on('close', (code) => {
                    console.log(`child process exited with code ${code}`);
                });

                ipc.server.on('script', function(data, socket){
                    resolve(socket);
                });
            });

            ipc.server.start();
        });
        
        IPCserverStarted.then((socket)=>{
            ipc.server.emit(
                socket,
                'script',
                {
                    id: ipc.config.id,
                    script: 'usbMassStorage'
                }
            );
        });
    }

    selectImage(){
        dialog.showOpenDialog({title: 'Select OS image for Flashing' , filters:[{name: 'OS Images', extensions: ['img', 'xz']}]},(filePath) => {
            if(filePath === undefined){
                console.log("No file selected");
                return;
            }
            console.log(filePath);
        });
    }

    writeImage(){
        IPCserverStarted.then((socket)=>{
            ipc.server.emit(
                socket,
                'script',
                {
                    id: ipc.config.id,
                    script: 'imageWrite'
                }
            );
        });
    }<commit_msg>complete 'Select Image' button functionality<commit_after>const React = require('react');
const app = window.require('electron').remote; 
const dialog = app.dialog;
const fork = window.require('child_process').fork;
const sudo = window.require('sudo-prompt');
const ipc = window.require('node-ipc');
const fs = window.require('fs');
const drivelist = window.require('drivelist');
const path = window.require('path');

// React Components
import Block from './Block';
import Progress from './progress';
import Info from './info';

var IPCserverStarted;   // Promise, returns socket on resolution
var imagePath;


class MainPage extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            progress: {
                value: 0,
                infoText: 'Go ahead! click USB Mass Storage button to begin'
            },
            buttonState: {
                ums: true,
                img: false,
                flash: false
            }
        };
        this.umsClick = this.umsClick.bind(this);
        this.selectImage = this.selectImage.bind(this);
        this.writeImage = this.writeImage.bind(this);

        // IPC Server
        window.process.env.IPC_SERVER_ID = `BB-server-${window.process.pid}`;
        window.process.env.IPC_CLIENT_ID = `BB-client-${window.process.pid}`;

        ipc.config.id = window.process.env.IPC_SERVER_ID;
        ipc.config.silent = true;
        ipc.serve();

        ipc.server.on('message', function(data){
            console.log(data);
            try{
                var status = JSON.parse(data);
            }
            catch(e){ }
            if(status && status.description) this.setState((prevState)=>{
                return {
                    progress: {
                        value: status.complete,
                        infoText: status.description
                    },
                    buttonState:{
                        ums: prevState.buttonState.ums,
                        img: prevState.buttonState.img,
                        flash: prevState.buttonState.flash
                    }
                }
            });

            if(status && status.done) this.setState((prevState)=>{
                return {
                    progress: {
                        value: 0,
                        infoText: 'Select Image for flashing'
                    },
                    buttonState:{
                        ums: false,
                        img: true,
                        flash: false
                    }
                }
            });
        }.bind(this));
    }

        

    umsClick(){
        // Promise, starts IPC server and returns socket for client on resolution
        IPCserverStarted = new Promise((resolve, reject)=>{

            ipc.server.on('start', () => {
                
                const child = fork('./lib/elevate.js', [],{
                    silent: true, // in order for the stdin, stdout and stderr to get piped back to the parent process
                    env: window.process.env
                });
                child.stdout.on('data', (data)=>{
                    console.log(`${data}`);
                });

                child.stderr.on('data', (data)=>{
                    console.log(`${data}`);
                    child.kill();
                    reject();
                });

                child.on('close', (code) => {
                    console.log(`child process exited with code ${code}`);
                });

                ipc.server.on('script', function(data, socket){
                    resolve(socket);
                });
            });

            ipc.server.start();
        });
        
        IPCserverStarted.then((socket)=>{
            ipc.server.emit(
                socket,
                'script',
                {
                    id: ipc.config.id,
                    script: 'usbMassStorage'
                }
            );
        });
    }

    selectImage(){
        dialog.showOpenDialog({title: 'Select OS image for Flashing' , filters:[{name: 'OS Images', extensions: ['img', 'xz']}]},(filePath) => {
            if(filePath === undefined){
                this.setState((prevState)=>{
                    return {
                        progress: {
                            value: 0,
                            infoText: 'No Image file is selected'
                        },
                        buttonState:{
                            ums: false,
                            img: true,
                            flash: false
                        }
                    }
                });
                return;
            }
            else{
                imagePath = filePath[0];
                this.setState((prevState)=>{
                    return {
                        progress: {
                            value: 0,
                            infoText: 'Selected Image: ' + path.basename(imagePath)
                        },
                        buttonState:{
                            ums: false,
                            img: true,
                            flash: true
                        }
                    }
                });
            }
        });
    }

    writeImage(){
        IPCserverStarted.then((socket)=>{
            ipc.server.emit(
                socket,
                'script',
                {
                    id: ipc.config.id,
                    script: 'imageWrite'
                }
            );
        });
    }
{'fraction_non_alphanumeric': 0.11521652761223679, 'fraction_numerical': 0.0006952721493841876, 'mean_word_length': 1.0842475677913475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861786', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2364, 'n_words': 501}