Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to allow external packages to use original-fs #2035

Closed
Gregg8 opened this issue Jun 23, 2015 · 6 comments
Closed

How to allow external packages to use original-fs #2035

Gregg8 opened this issue Jun 23, 2015 · 6 comments

Comments

@Gregg8
Copy link

Gregg8 commented Jun 23, 2015

Updated description in next comment...

@Gregg8
Copy link
Author

Gregg8 commented Jun 23, 2015

Within Electron we are attempting to use a library (adm-zip) which uses fs. This library ends up using Electron's version of fs and not original-fs.

But (for various reasons) that's a problem. We want adm-zip to get the original-fs version of fs.

Is there any way to temporarily reinstate/rebind fs to original-fs when we call the library?

Further background: We're using adm-zip to unzip a file which contains a .asar file and the Electron fs fails on that file.

@zcbenz
Copy link
Member

zcbenz commented Jun 25, 2015

You can do it by monkey-patch:

fs = require('fs')
originalFs = require('original-fs')
for name, method of originalFs
  fs[name] = method

@zcbenz zcbenz closed this as completed Jun 25, 2015
@Gregg8
Copy link
Author

Gregg8 commented Jun 26, 2015

That did the trick, many thanks.

@marwanhilmi
Copy link

Apologies to revive a closed issue, @zcbenz, I tried your suggestion but get an error:
TypeError: Cannot assign to read only property 'F_OK' of #<Object> when attempting to do fs[name] = method, am I missing something obvious?

If I do a monkey-patch like this - what is a good practice to ensure you aren't breaking something else? Do you need to revert the monkey-patch after including the modules that need original-fs? For example, if I do something like:

// monkey patch fs
var rimraf = require('rimraf') // needs 'original-fs'
// should I revert monkey patch now or is it safe to continue?

Any suggestions for best practice around this? This has definitely been problematic for me - I've been forking modules and adding var fs = process.versions.electron ? require('original-fs') : require('fs') which is obviously not ideal.

@Gregg8
Copy link
Author

Gregg8 commented Jul 11, 2015

My project is in ClojureScript, not pure JS otherwise I would provide my source code, but what I do is patch just before the calls to my external lib then unpatch once they're done. Seems the safest thing to do.

@arnaudlamy
Copy link

You can do it by monkey-patch:

fs = require('fs')
originalFs = require('original-fs')
for name, method of originalFs
  fs[name] = method

Hello,

I'm facing the same issue and I'm a beginner. Can you help on how to apply your patch?

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants