88 * When running `npm run build` or `npm run build:main`, this file is compiled to
99 * `./src/main.js` using webpack. This gives us some performance wins.
1010 */
11- import path , { resolve } from 'path' ;
11+ import path from 'path' ;
1212import { app , BrowserWindow , shell , ipcMain , safeStorage } from 'electron' ;
1313import { autoUpdater } from 'electron-updater' ;
1414import log from 'electron-log' ;
1515import MenuBuilder from './menu' ;
1616import { resolveHtmlPath } from './util' ;
1717import youtubeDl from 'youtube-dl-exec' ;
18- import * as dotenv from 'dotenv' ; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
19- dotenv . config ( ) ;
2018import Store from 'electron-store' ;
2119import ElectronGoogleOAuth2 from '@getstation/electron-google-oauth2' ;
22- // import { upload } from 'youtube-videos-uploader'
23- // import { Video } from 'youtube-videos-uploader/dist/types';
24-
20+ require ( 'dotenv' ) . config ( ) ;
2521const fs = require ( 'fs' ) ;
2622const store = new Store ( ) ;
2723const isDev = require ( 'electron-is-dev' ) ;
2824
2925// initialize the Youtube API library
30- const { google} = require ( 'googleapis' ) ;
26+ const { google } = require ( 'googleapis' ) ;
3127const youtube = google . youtube ( 'v3' ) ;
3228const readline = require ( 'readline' ) ;
3329
@@ -42,32 +38,32 @@ class AppUpdater {
4238let mainWindow : BrowserWindow | null = null ;
4339
4440// Deep linked url
45- let deeplinkingUrl : any
41+ let deeplinkingUrl : any ;
4642
4743// Force Single Instance Application
48- const gotTheLock = app . requestSingleInstanceLock ( )
44+ const gotTheLock = app . requestSingleInstanceLock ( ) ;
4945if ( gotTheLock ) {
50- console . log ( " Got the lock" )
51- console . log ( " process.argv" , process . argv )
46+ console . log ( ' Got the lock' ) ;
47+ console . log ( ' process.argv' , process . argv ) ;
5248 app . on ( 'second-instance' , ( e , argv ) => {
5349 // Someone tried to run a second instance, we should focus our window.
54- console . log ( " second instance" )
50+ console . log ( ' second instance' ) ;
5551 // Protocol handler for win32
5652 // argv: An array of the second instance’s (command line / deep linked) arguments
5753 if ( process . platform == 'win32' ) {
5854 // Keep only command line / deep linked arguments
59- deeplinkingUrl = argv . slice ( 1 )
55+ deeplinkingUrl = argv . slice ( 1 ) ;
6056 }
61- logEverywhere ( 'app.makeSingleInstance# ' + deeplinkingUrl )
57+ logEverywhere ( 'app.makeSingleInstance# ' + deeplinkingUrl ) ;
6258
6359 if ( mainWindow ) {
64- if ( mainWindow . isMinimized ( ) ) mainWindow . restore ( )
65- mainWindow . focus ( )
60+ if ( mainWindow . isMinimized ( ) ) mainWindow . restore ( ) ;
61+ mainWindow . focus ( ) ;
6662 }
67- } )
63+ } ) ;
6864} else {
69- console . log ( " Did not get the lock" )
70- app . quit ( )
65+ console . log ( ' Did not get the lock' ) ;
66+ app . quit ( ) ;
7167}
7268
7369ipcMain . handle ( 'create-folder' , async ( event , arg ) => {
@@ -76,9 +72,9 @@ ipcMain.handle('create-folder', async (event, arg) => {
7672 } ) ;
7773} ) ;
7874
79- ipcMain . handle ( 'open-folder' , async ( event , arg ) => {
80- shell . openPath ( process . cwd ( ) + path . sep + 'downloadedVODs' + path . sep + arg )
81- } )
75+ ipcMain . handle ( 'open-folder' , async ( event , arg ) => {
76+ shell . openPath ( process . cwd ( ) + path . sep + 'downloadedVODs' + path . sep + arg ) ;
77+ } ) ;
8278
8379ipcMain . handle ( 'retrieve-video-information' , async ( event , arg ) => {
8480 return youtubeDl ( arg . vodUrl , {
@@ -95,69 +91,71 @@ ipcMain.handle('download-video', async (event, arg) => {
9591 return youtubeDl ( arg . vodUrl , {
9692 // @ts -ignore
9793 downloadSections : '*' + arg . startTime + '-' + arg . endTime ,
98- output : './downloadedVODs/' + arg . tournamentName + "/" + arg . title + '.mp4' ,
94+ output : './downloadedVODs/' + arg . tournamentName + '/' + arg . title + '.mp4' ,
9995 } ) ;
10096} ) ;
10197
102- ipcMain . handle ( 'get-tournament-folders' , async ( event , arg ) => {
103- return fs . readdirSync ( arg ) . filter ( ( file : any ) => fs . statSync ( arg + '/' + file ) . isDirectory ( ) )
104- } )
105-
106- ipcMain . handle ( 'open-google-login' , async ( event , arg ) => {
107- // shell.openExternal("http://localhost:3000/")
98+ ipcMain . handle ( 'get-tournament-folders' , async ( event , arg ) => {
99+ return fs
100+ . readdirSync ( arg )
101+ . filter ( ( file : any ) => fs . statSync ( arg + '/' + file ) . isDirectory ( ) ) ;
102+ } ) ;
108103
109- // const myApiOauth = new ElectronGoogleOAuth2(
110- // ['https://www.googleapis.com/auth/youtube.upload'],
111- // { successRedirectURL: 'tournamentvodclipper://' }
112- // );
104+ ipcMain . handle ( 'open-google-login' , async ( event , arg ) => {
105+ const myApiOauth = new ElectronGoogleOAuth2 (
106+ process . env . GOOGLE_CLIENT_ID ! ,
107+ process . env . GOOGLE_CLIENT_SECRET ! ,
108+ [ 'https://www.googleapis.com/auth/youtube.upload' ] ,
109+ { successRedirectURL : 'tournamentvodclipper://' }
110+ ) ;
113111
114- // return myApiOauth.openAuthWindowAndGetTokens()
115- } )
112+ return myApiOauth . openAuthWindowAndGetTokens ( ) ;
113+ } ) ;
116114
117- ipcMain . handle ( 'upload-videos' , async ( event , args ) => {
118- // const credentials = { email: args.email, recoveryemail: args.recoveryemail, pass: safeStorage.decryptString(Buffer.from(store.get('ytPassword') as Buffer))}
119- // const videoList: Video[] = []
115+ ipcMain . handle ( 'upload-videos' , async ( event , args ) => {
120116 fs . readdirSync ( args . path ) . forEach ( ( videoName : any ) => {
121117 const fileSize = fs . statSync ( args . path + videoName ) . size ;
122118 if ( path . extname ( videoName ) . toLowerCase ( ) === '.mp4' ) {
123- console . log ( 'videoName' , videoName )
124- return youtube . videos . insert (
125- {
126- part : 'id,snippet,status' ,
127- notifySubscribers : false ,
128- requestBody : {
129- snippet : {
130- title : videoName . replace ( / \. [ ^ / . ] + $ / , "" ) ,
131- description : args . description ,
119+ console . log ( 'videoName' , videoName ) ;
120+ return youtube . videos
121+ . insert (
122+ {
123+ part : 'id,snippet,status' ,
124+ notifySubscribers : false ,
125+ requestBody : {
126+ snippet : {
127+ title : videoName . replace ( / \. [ ^ / . ] + $ / , '' ) ,
128+ description : args . description ,
129+ } ,
130+ status : {
131+ privacyStatus : 'unlisted' ,
132+ } ,
132133 } ,
133- status : {
134- privacyStatus : 'unlisted' ,
134+ media : {
135+ body : fs . createReadStream ( args . path + videoName ) ,
135136 } ,
137+ access_token : args . accessToken ,
136138 } ,
137- media : {
138- body : fs . createReadStream ( args . path + videoName ) ,
139- } ,
140- access_token : args . accessToken
141- } ,
142- {
143- // Use the `onUploadProgress` event from Axios to track the
144- // number of bytes uploaded to this point.
145- onUploadProgress : ( evt : any ) => {
146- const progress = ( evt . bytesRead / fileSize ) * 100 ;
147- readline . clearLine ( process . stdout , 0 ) ;
148- readline . cursorTo ( process . stdout , 0 , null ) ;
149- process . stdout . write ( `${ Math . round ( progress ) } % complete` ) ;
150- } ,
151- }
152- ) . then ( ( response : any ) => {
153- console . log ( "response" , response )
154- } ) . catch ( ( error : any ) => {
155- return error
156- } )
139+ {
140+ // Use the `onUploadProgress` event from Axios to track the
141+ // number of bytes uploaded to this point.
142+ onUploadProgress : ( evt : any ) => {
143+ const progress = ( evt . bytesRead / fileSize ) * 100 ;
144+ readline . clearLine ( process . stdout , 0 ) ;
145+ readline . cursorTo ( process . stdout , 0 , null ) ;
146+ process . stdout . write ( `${ Math . round ( progress ) } % complete` ) ;
147+ } ,
148+ }
149+ )
150+ . then ( ( response : any ) => {
151+ console . log ( 'response' , response ) ;
152+ } )
153+ . catch ( ( error : any ) => {
154+ return error ;
155+ } ) ;
157156 }
158- } )
159- // return upload(credentials, videoList)
160- } )
157+ } ) ;
158+ } ) ;
161159
162160ipcMain . handle ( 'get-api-key' , async ( event , arg ) => {
163161 if ( isDev ) {
@@ -177,17 +175,19 @@ ipcMain.on('electron-store-set', async (event, key, val) => {
177175ipcMain . on ( 'electron-store-delete' , async ( event , val ) => {
178176 store . delete ( val ) ;
179177} ) ;
180- ipcMain . on ( 'electron-store-set-secret' , async ( event , key , val ) => {
181- store . set ( key , safeStorage . encryptString ( val ) )
182- } )
178+ ipcMain . on ( 'electron-store-set-secret' , async ( event , key , val ) => {
179+ store . set ( key , safeStorage . encryptString ( val ) ) ;
180+ } ) ;
183181ipcMain . on ( 'electron-store-get-secret' , async ( event , val ) => {
184- let encryptedVal = store . get ( val )
182+ let encryptedVal = store . get ( val ) ;
185183 if ( encryptedVal ) {
186- event . returnValue = safeStorage . decryptString ( Buffer . from ( encryptedVal as Buffer ) ) ;
184+ event . returnValue = safeStorage . decryptString (
185+ Buffer . from ( encryptedVal as Buffer )
186+ ) ;
187187 } else {
188- event . returnValue = ''
188+ event . returnValue = '' ;
189189 }
190- } )
190+ } ) ;
191191
192192ipcMain . on ( 'ipc-example' , async ( event , arg ) => {
193193 const msgTemplate = ( pingPong : string ) => `IPC test: ${ pingPong } ` ;
@@ -280,32 +280,34 @@ const createWindow = async () => {
280280
281281// Log both at dev console and at running node console instance
282282function logEverywhere ( s : any ) {
283- console . log ( s )
283+ console . log ( s ) ;
284284 if ( mainWindow && mainWindow . webContents ) {
285- mainWindow . webContents . executeJavaScript ( `console.log("${ s } ")` )
285+ mainWindow . webContents . executeJavaScript ( `console.log("${ s } ")` ) ;
286286 }
287287}
288288
289289/**
290290 * Add event listeners...
291- */
291+ */
292292
293293if ( isDev && process . platform === 'win32' ) {
294294 // Set the path of electron.exe and your app.
295295 // These two additional parameters are only available on windows.
296296 // Setting this is required to get this working in dev mode.
297- app . setAsDefaultProtocolClient ( 'tournamentvodclipper' , process . execPath , [ process . argv [ 1 ] ] ) ;
297+ app . setAsDefaultProtocolClient ( 'tournamentvodclipper' , process . execPath , [
298+ process . argv [ 1 ] ,
299+ ] ) ;
298300} else {
299301 app . setAsDefaultProtocolClient ( 'tournamentvodclipper' ) ;
300302}
301303
302- app . on ( 'open-url' , function ( event , url ) {
303- console . log ( " open-url called" )
304- event . preventDefault ( )
305- deeplinkingUrl = url
306- logEverywhere ( 'open-url# ' + deeplinkingUrl )
307- mainWindow ?. webContents . send ( 'login-success' , { deeplinkingUrl } )
308- } )
304+ app . on ( 'open-url' , function ( event , url ) {
305+ console . log ( ' open-url called' ) ;
306+ event . preventDefault ( ) ;
307+ deeplinkingUrl = url ;
308+ logEverywhere ( 'open-url# ' + deeplinkingUrl ) ;
309+ mainWindow ?. webContents . send ( 'login-success' , { deeplinkingUrl } ) ;
310+ } ) ;
309311
310312app . on ( 'window-all-closed' , ( ) => {
311313 // Respect the OSX convention of having the application in memory even
0 commit comments