11import path from "path" ;
22import { exec , execSync } from "child_process" ;
3+ import electronLog from 'electron-log' ;
34import fs from "fs-extra" ;
45import { app , BrowserWindow , ipcMain } from "electron" ;
56import { format } from "date-fns" ;
67import { getHtml , getAuthCmd , getExecuteFile , getExecutePath } from "./utils" ;
78import { connectDataBase , findRecord , insertRecord } from "./sqlHelper" ;
89import sharp from 'sharp'
910process . env [ "ELECTRON_DISABLE_SECURITY_WARNINGS" ] = "true" ;
10- let templateFilePath = path . join ( process . cwd ( ) , "resources" , "command" ) ;
11-
12-
13- if ( import . meta. env . DEV ) {
14- templateFilePath = path . join ( process . cwd ( ) , "command" ) ;
15- }
16- console . log ( templateFilePath , "templateFilePath" ) ;
17-
18- const ytDlp = path . join (
19- process . cwd ( ) ,
20- "command" ,
21- getExecutePath ( ) ,
22- getExecuteFile ( "yt-dlp" )
23- ) ;
24-
25- const ytDlpPath = `${ getAuthCmd ( ) } ${ ytDlp } ` ;
26-
27- const ffmpeg = path . join (
28- process . cwd ( ) ,
29- "command" ,
30- getExecutePath ( ) ,
31- getExecuteFile ( "ffmpeg" )
32- ) ;
33-
34- const ffmpegPath = `${ getAuthCmd ( ) } ${ ffmpeg } ` ;
35-
36- const removeDuplicateImages = path . join (
37- process . cwd ( ) ,
38- "command" ,
39- getExecutePath ( ) ,
40- getExecuteFile ( "RemoveDuplicateImages" )
41- ) ;
42-
43- const removeDuplicateImagesPath = `${ getAuthCmd ( ) } ${ removeDuplicateImages } ` ;
4411
4512// The built directory structure
4613//
@@ -107,8 +74,55 @@ function createWindow() {
10774 // win.loadFile('dist/index.html')
10875 win . loadFile ( path . join ( process . env . DIST , "index.html" ) ) ;
10976 }
77+
78+ // 重写console.log方法
79+ const originalConsoleLog = console . log ;
80+ console . log = ( ...args ) => {
81+ originalConsoleLog ( ...args ) ;
82+ win ?. webContents . send ( 'main-process-log' , args ) ;
83+ } ;
84+
85+ // 重写electron-log的日志方法
86+ [ 'error' , 'warn' , 'info' , 'verbose' , 'debug' , 'silly' ] . forEach ( level => {
87+ const originalLog = electronLog [ level ] ;
88+ electronLog [ level ] = ( ...args ) => {
89+ originalLog ( ...args ) ;
90+ win ?. webContents . send ( 'main-process-log' , [ level , ...args ] ) ;
91+ } ;
92+ } ) ;
11093}
11194
95+ let templateFilePath = path . join ( process . cwd ( ) , "resources" , "command" ) ;
96+
97+ if ( ! import . meta. env . PROD ) {
98+ templateFilePath = path . join ( process . cwd ( ) , "command" ) ;
99+ }
100+ console . log ( templateFilePath , "templateFilePath" ) ;
101+
102+ const ytDlp = path . join (
103+ templateFilePath ,
104+ getExecutePath ( ) ,
105+ getExecuteFile ( "yt-dlp" )
106+ ) ;
107+
108+ const ytDlpPath = `${ getAuthCmd ( ) } ${ ytDlp } ` ;
109+
110+ const ffmpeg = path . join (
111+ templateFilePath ,
112+ getExecutePath ( ) ,
113+ getExecuteFile ( "ffmpeg" )
114+ ) ;
115+
116+ const ffmpegPath = `${ getAuthCmd ( ) } ${ ffmpeg } ` ;
117+
118+ const removeDuplicateImages = path . join (
119+ templateFilePath ,
120+ getExecutePath ( ) ,
121+ getExecuteFile ( "RemoveDuplicateImages" )
122+ ) ;
123+
124+ const removeDuplicateImagesPath = `${ getAuthCmd ( ) } ${ removeDuplicateImages } ` ;
125+
112126app . on ( "window-all-closed" , ( ) => {
113127 app . quit ( ) ;
114128 win = null ;
@@ -120,8 +134,7 @@ ipcMain.on("call-yt-dlp-video", async(event,videoUrl: string) => {
120134 let record : any = await findRecord ( videoUrl ) ;
121135 console . log ( record , "record-------------------" )
122136 let locationPath = path . join (
123- process . cwd ( ) ,
124- "command" ,
137+ templateFilePath ,
125138 record . FolderDate
126139 ) ;
127140
@@ -176,8 +189,7 @@ ipcMain.on("call-yt-dlp", async (event, videoUrl, isDownloadVideo) => {
176189 const createInfo = createMetadata ( videoUrl ) ;
177190
178191 let locationPath = path . join (
179- process . cwd ( ) ,
180- "command" ,
192+ templateFilePath ,
181193 createInfo . folderDate
182194 ) ;
183195
@@ -268,8 +280,7 @@ ipcMain.on(
268280 const startTimeName = everyStartTime . replace ( / [ . : , - ] / g, "" ) ;
269281 console . log ( startTimeName , "startTimeName" )
270282 const imagePath = path . join (
271- process . cwd ( ) ,
272- "command" ,
283+ templateFilePath ,
273284 folderDate ,
274285 startTimeName
275286 ) ;
@@ -309,13 +320,17 @@ ipcMain.on("call-get-duration", (event, folderDate) => {
309320 event . reply ( "reply-duration" , packageJson . duration ) ;
310321} )
311322
323+ ipcMain . on ( "call-execute-path" , ( event ) => {
324+ event . reply ( "reply-execute-path" , process . cwd ( ) ) ;
325+ } )
326+
312327/**
313328 * 图片压缩
314329 */
315330ipcMain . on ( "call-image-compress" , ( event , folderDate , everyStartTime , list ) => {
316331 console . log ( "call-image-compress" , list )
317332 const startTimeName = everyStartTime . replace ( / [ . : , - ] / g, "" ) ;
318- const locationPath = path . join ( process . cwd ( ) , "command" , folderDate , startTimeName ) ;
333+ const locationPath = path . join ( templateFilePath , folderDate , startTimeName ) ;
319334 list . forEach ( async ( item : any , index : number ) => {
320335 console . log ( item , "fileName" )
321336 let fileName = item . split ( '.' ) [ 0 ] ;
@@ -382,6 +397,7 @@ const removeSimilarImages = (imagePath: string, multiple: number) => {
382397/**
383398 * 在指定目录下查找元数据json文件
384399 * @param directoryPath
400+ * @param type
385401 * @returns
386402 */
387403const findJsonFilesInDirectorySync = (
@@ -425,12 +441,19 @@ const createMetadata = (url: string) => {
425441 const folderDate = format ( new Date ( ) , "yyyy-MM-dd-HH-mm-ss" ) ;
426442 console . log ( folderDate , "date-folderDate" ) ;
427443
428- const locationPath = path . join ( process . cwd ( ) , "command" , folderDate ) ;
444+ const locationPath = path . join ( templateFilePath , folderDate ) ;
429445 let cmd = "" ;
430446 cmd = ` ${ ytDlpPath } ${ url } -P ${ locationPath } --write-info-json --skip-download -o "%(id)s.%(ext)s"` ;
431447
432448 console . log ( cmd , "cmd-123" ) ;
433- execSync ( cmd ) ;
449+ try
450+ {
451+ execSync ( cmd ) ;
452+ }
453+ catch ( e ) {
454+ console . log ( "执行cmd-123失败" , e ) ;
455+ }
456+ console . log ( "cmd-123执行完毕" ) ;
434457 const jsonFile : string | undefined =
435458 findJsonFilesInDirectorySync ( locationPath ) ;
436459
0 commit comments