Skip to content

Instantly share code, notes, and snippets.

@interfacekun
Forked from benloong/copyContent.js
Created April 1, 2019 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interfacekun/d6ca30e3b59a32e26aa8f7d31591fb3b to your computer and use it in GitHub Desktop.
Save interfacekun/d6ca30e3b59a32e26aa8f7d31591fb3b to your computer and use it in GitHub Desktop.
Copy file out of ASAR archive for Electron app
var fs = require("fs");
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() ) {
fsj.file( destOutsideAsarArchive , {content: 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