2

My goal is to copy a .asar file using NodeJS's file system. I have a file path and need to copy the .asar file to another destination.

Example of file path:

C:/Users/redacted/Documents/ExampleElectronApp/example.asar

I need to copy this file (archive) to another directory.

Previously I copied this with a walk and copy function but it ended up creating a directory named filename.asar instead of an actual file named filename.asar This function seems to work on all other files and folders correctly (except for .asar archives).

What I have tried

1: Copying the archive using read and write streams.

var fs = require('fs');
fs.createReadStream('C:/Users/redacted/Documents/redacted/node_modules/electron-prebuilt/dist/resources/default_app.asar').pipe(fs.createWriteStream('C:/Users/redacted/Desktop/test.asar'));

This ended up giving an error: Error #1

It says this file is not found but I can assure you, copying the source path does lead me to the .asar file.

2: Using the asar requirement to create a package.

var fs = require('fs');
var asar = require('asar');

asar.createPackage('C:/Users/redacted/Documents/redacted/node_modules/electron-prebuilt/dist/resources/default_app.asar', 'C:/Users/redacted/Desktop/test.asar', function() {
  console.log('done.');
})

While this function does log the correct 'done' message, it seems that the copy was unsucessful. No new files or folders are shown in the destination directory.

Thank you all in advance.

Adam Mikacich
  • 137
  • 1
  • 1
  • 13
  • Isn't windows directory path separator a `\ ` ? Try replacing all `/` by `\\ `. Or use `fs.existsSync(yourPath)` to be sure Node can actually see it. You can even use `fs.readdir` to list the files of a folder. – Seblor Jul 13 '17 at 01:21
  • You don't have any spaces in your filepath, do you? – paulsm4 Jul 13 '17 at 01:22
  • I backslashed \ before all of the spaces inside the path's string. I also tried replacing all / with \\ (besides the space \) and both of these solutions ended up giving the same error. Using `fs.existsSync("path")` returns `true` when I use the source path as "path". Any other suggestions to copy the whole archive? – Adam Mikacich Jul 13 '17 at 01:49

3 Answers3

5

Maybe you can disable the support for asar in fs module, like that:

process.noAsar = true

Here is the document: https://electronjs.org/docs/tutorial/application-packaging#treating-an-asar-archive-as-a-normal-file

Varme
  • 51
  • 1
  • 3
1

The way I ended up succeeding this was by renaming the .asar files then renaming them back after I copied them. Here is my final source for copying .asar files:

var filePathSource = path.join(__dirname, 'default_app.asar')
var filePathTarget = path.join(__dirname, 'test.txt')

var filePathSourceNew = filePathSource.slice(0, -5) + ".txt";
console.log(filePathSourceNew);

fs.rename(filePathSource, filePathSourceNew, function(err) {
    if (err) {
      console.log('ERROR: ' + err);
    } else {
      var stream = fs.createReadStream(filePathSourceNew).pipe(fs.createWriteStream(filePathTarget));
      stream.on('finish', function () {
        fs.rename(filePathSourceNew, filePathSource, function(err) {
            if (err) {
              console.log('ERROR: ' + err);
            } else {
              fs.rename(filePathTarget, filePathTarget.slice(0, -4) + ".asar", function(err) {
                  if (err) {
                    console.log('ERROR: ' + err);
                  } else {
                    console.log("Complete Asar Copy");
                  }
              });
            }
        });
      });
    }
});
Adam Mikacich
  • 137
  • 1
  • 1
  • 13
0

You can use "path" of NODEJS module.

const path = require('path')
filePathSource = path.join(__dirname, 'a', 'b', 'c', 'source.asar') 
filePathTarget = path.join(__dirname, 'a', 'b', 'c', 'target.asar')    
fs.createReadStream(filePathSource).pipe(fs.createWriteStream(filePathTarget));
TonyY
  • 713
  • 7
  • 18
  • This does not answer the question. The question is asking **how to copy a .asar archive.** I already know how to create proper file paths. Please make sure you read my question entirely. – Adam Mikacich Jul 13 '17 at 02:16
  • uh..OK, I think that has the same problem. – TonyY Jul 13 '17 at 02:27
  • Your solution works with any other file but .asar files which is what I am trying to solve. I tested your solution on a css file to make sure my paths were not wrong and it worked. But changed to my asar file in the same directory causes the not found error. I believe this is because windows tries to treat this archive as a directory but then fails because it's actually a file. Any ideas on how to fix this? – Adam Mikacich Jul 13 '17 at 02:33
  • Is this similar to your problem? https://github.com/electron/electron/issues/1676 – TonyY Jul 13 '17 at 02:44
  • 1
    That helped me figure out what I needed to do in order to copy this archive. I ended up renaming the archive with nodejs fs then copying it then renaming it back. – Adam Mikacich Jul 13 '17 at 03:36