- 
      
 - 
        
Save benloong/586d6c899c2d84cea557d358311ae9d7 to your computer and use it in GitHub Desktop.  
    Copy file out of ASAR archive for Electron app
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var fs = require("fs"); | |
| var path = require("path") | |
| var app = require("electron").remote.app; | |
| // var fsj = require("fs-jetpack"); | |
| // example usage : copyFileOutsideOfElectronAsar( "myFolderInsideTheAsarFile", app.getPath("temp") + "com.bla.bla" | |
| var copyFileOutsideOfElectronAsar = function (sourceInAsarArchive, destOutsideAsarArchive) { | |
| if (fs.existsSync(app.getAppPath() + "/" + sourceInAsarArchive)) { | |
| // file will be copied | |
| if (fs.statSync(app.getAppPath() + "/" + sourceInAsarArchive).isFile()) { | |
| let file = destOutsideAsarArchive | |
| let dir = path.dirname(file); | |
| if (!fs.existsSync(dir)) { | |
| fs.mkdirSync(dir, { recursive: true }); | |
| } | |
| fs.writeFileSync(file, fs.readFileSync(app.getAppPath() + "/" + sourceInAsarArchive)); | |
| } | |
| // dir is browsed | |
| else if (fs.statSync(app.getAppPath() + "/" + sourceInAsarArchive).isDirectory()) { | |
| fs.readdirSync(app.getAppPath() + "/" + sourceInAsarArchive).forEach(function (fileOrFolderName) { | |
| copyFileOutsideOfElectronAsar(sourceInAsarArchive + "/" + fileOrFolderName, destOutsideAsarArchive + "/" + fileOrFolderName); | |
| }); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment