@@ -2,20 +2,14 @@ const { dialog, BrowserWindow, app } = require("electron");
22const path = require ( "path" ) ;
33const util = require ( "util" ) ;
44const execFile = util . promisify ( require ( "child_process" ) . execFile ) ;
5-
6- const Promise = require ( "bluebird" ) ;
7- const fs = Promise . promisifyAll ( require ( "fs-extra" ) ) ;
5+ const fs = require ( "fs-extra" ) ;
86
97let loadedFilePath = null ;
108let images = [ ] ;
119
12- const getResourcePath = ( ) => {
13- if ( app . isPackaged ) {
14- return process . resourcesPath ;
15- } else {
16- return "." ;
17- }
18- } ;
10+ const imageExt = [ ".png" , ".jpg" ] ;
11+
12+ const resourcePath = app . isPackaged ? process . resourcesPath : "." ;
1913
2014const getFiles = async ( path = "./" ) => {
2115 const entries = await fs . readdir ( path , { withFileTypes : true } ) ;
@@ -31,30 +25,35 @@ const getFiles = async (path = "./") => {
3125} ;
3226
3327const loadRcc = async ( filePath ) => {
34- const localPath = `${ path . resolve ( getResourcePath ( ) , "rcc" ) } ` ;
28+ const localPath = `${ path . resolve ( resourcePath , "rcc" ) } ` ;
3529
3630 // clear previous images
3731 images = [ ] ;
3832
3933 // delete res directory
4034 try {
41- await fs . rmdir ( ` ${ localPath } / qresource` , { recursive : true } ) ;
35+ await fs . rmdir ( path . join ( localPath , " qresource" ) , { recursive : true } ) ;
4236 } catch { }
4337
44- await fs . copyFile ( filePath , ` ${ localPath } / res.rcc` ) ;
38+ await fs . copyFile ( filePath , path . join ( localPath , " res.rcc" ) ) ;
4539
46- const result = await execFile ( ` ${ localPath } / rcc.exe` , [ "--reverse" ] , {
40+ await execFile ( path . join ( localPath , " rcc.exe" ) , [ "--reverse" ] , {
4741 cwd : `${ localPath } /` ,
4842 } ) ;
4943
5044 // get directory content
51- const files = await getFiles ( `${ localPath } /qresource/res/res.rcc` ) ;
45+ const files = await getFiles (
46+ path . join ( localPath , "qresource" , "res" , "res.rcc" )
47+ ) ;
48+
5249 for ( const file of files ) {
53- const ext = path . extname ( file . path ) ;
5450 images . push ( {
5551 name : path . parse ( file . name ) . name ,
56- path : path . relative ( `${ localPath } /qresource/res/res.rcc` , file . path ) ,
57- isImage : ext === ".png" || ext === ".jpg" ,
52+ path : path . relative (
53+ path . join ( localPath , "qresource" , "res" , "res.rcc" ) ,
54+ file . path
55+ ) ,
56+ isImage : imageExt . includes ( path . extname ( file . path ) ) ,
5857 data : Buffer . from ( await fs . readFile ( file . path , "binary" ) , "binary" ) ,
5958 } ) ;
6059 }
@@ -63,23 +62,23 @@ const loadRcc = async (filePath) => {
6362 images . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
6463
6564 // cleanup
66- await fs . rmdir ( ` ${ localPath } / qresource` , { recursive : true } ) ;
67- await fs . rm ( ` ${ localPath } / res.rcc` ) ;
65+ await fs . rmdir ( path . join ( localPath , " qresource" ) , { recursive : true } ) ;
66+ await fs . rm ( path . join ( localPath , " res.rcc" ) ) ;
6867
6968 loadedFilePath = filePath ;
7069
7170 BrowserWindow . getAllWindows ( ) [ 0 ] . webContents . send ( "populate-list" , images ) ;
7271} ;
7372
7473const extractToPng = async ( directoryPath ) => {
75- if ( images . length === 0 ) {
74+ if ( ! images . length ) {
7675 dialog . showErrorBox ( "Error" , "Nothing to extract." ) ;
7776 return ;
7877 }
7978
8079 for ( const image of images ) {
8180 if ( image . isImage ) {
82- await fs . outputFileAsync ( `${ directoryPath } /${ image . path } ` , image . data ) ;
81+ await fs . outputFile ( `${ directoryPath } /${ image . path } ` , image . data ) ;
8382 }
8483 }
8584
@@ -89,14 +88,12 @@ const extractToPng = async (directoryPath) => {
8988 } ) ;
9089} ;
9190
92- const saveRcc = async ( filePath ) => {
91+ const saveRcc = async ( filePath = loadedFilePath ) => {
9392 if ( images . length === 0 ) {
9493 return ;
9594 }
9695
97- filePath = filePath || loadedFilePath ;
98-
99- const localPath = `${ path . resolve ( getResourcePath ( ) , "rcc" ) } ` ;
96+ const localPath = path . resolve ( resourcePath , "rcc" ) ;
10097
10198 // create .qrc file
10299 let data = `<!DOCTYPE RCC><RCC version="1.0">\n<qresource>\n` ;
@@ -107,15 +104,15 @@ const saveRcc = async (filePath) => {
107104
108105 data += `</qresource>\n</RCC>` ;
109106
110- await fs . outputFileAsync ( ` ${ localPath } / res/ res.qrc` , data ) ;
107+ await fs . outputFile ( path . join ( localPath , " res" , " res.qrc" ) , data ) ;
111108
112109 // dump images
113110 for ( const image of images ) {
114- await fs . outputFileAsync ( ` ${ localPath } / res/ ${ image . path } ` , image . data ) ;
111+ await fs . outputFile ( path . join ( localPath , " res" , image . path ) , image . data ) ;
115112 }
116113
117- const result = await execFile (
118- ` ${ localPath } / rcc.exe` ,
114+ await execFile (
115+ path . join ( localPath , " rcc.exe" ) ,
119116 [
120117 "--format-version" ,
121118 "1" ,
@@ -129,10 +126,12 @@ const saveRcc = async (filePath) => {
129126 }
130127 ) ;
131128
132- await fs . move ( "./rcc/res/res_output.rcc" , ` ${ filePath } ` , { overwrite : true } ) ;
129+ await fs . move ( "./rcc/res/res_output.rcc" , filePath , { overwrite : true } ) ;
133130
134131 // cleanup
135- await fs . rmdir ( `${ localPath } /res` , { recursive : true } ) ;
132+ await fs . rmdir ( path . join ( localPath , "res" ) , {
133+ recursive : true ,
134+ } ) ;
136135
137136 dialog . showMessageBox ( null , {
138137 message : `Rcc saved successfully.` ,
0 commit comments