sindresorhus / trash

Move files and directories to the trash
MIT License
2.58k stars 79 forks source link

Dynamic requirements fail #48

Closed 5punk closed 8 years ago

5punk commented 8 years ago

First of all, love your module. It's nice and robust, has made our lives easier.

We are trying to package our app in a node binary, saw users can use it without node. Unfortunately due the dynamic requires in your app, the trash functionality doesn't work. When we bundle our code for production, it gets built to a binary.

Here: https://github.com/sindresorhus/trash/blob/master/index.js#L22-L24 Here: https://github.com/sindresorhus/trash/blob/master/lib/macos.js#L10 etc.

https://github.com/jaredallard/nexe#doesnt-support-dynamic-require-statements

Can you add the source code as part of the package so it can support compiled binaries?

Thanks.

sindresorhus commented 8 years ago

Here: https://github.com/sindresorhus/trash/blob/master/lib/macos.js#L10

What do you want done here?

5punk commented 8 years ago

@sindresorhus By any chance would you have the source code to that binary?

One of the ways we handle external file requirements is

Then, external file requirements / executions will work :)

sindresorhus commented 8 years ago

By any chance would you have the source code to that binary?

Yes, it's linked to from the readme: https://github.com/sindresorhus/macos-trash

One of the ways we handle external file requirements is Check if file exists in path If not, write file to path

Please clarify. I have no idea what this means.

5punk commented 8 years ago

When this package is a dependency to a project that is built into a binary, __dirname is built into /Users/5punk/projects/ProjectTest/node_modules/trash/bin/macos-trash

So when I give this built binary package to you, macos-trash is not found because it is still trying to search for the binary at /Users/5punk/projects/ProjectTest/node_modules/trash/bin/macos-trash

So a workaround is to check if the file exists in a generic path, if it doesn't exist, write the source code to the generic path.

const BINARY_PATH = isWindows ? '\AppData\trash\macos-trash' : '/opt/trash/macos-trash';

if (!fileExists(path.resolve(BINARY_PATH)) {
  fs.writeFileSync(BINARY_PATH, TRASH_SOURCE_CODE.toString());
  // make it or built it next (clang)
}

// Now feel free to use it :)
var bin = BINARY_PATH;

better yet, just download the release package (.zip) from https://github.com/sindresorhus/recycle-bin/ https://github.com/sindresorhus/macos-trash

and then, run the build script and now you've binaries at the location you want. :)

sindresorhus commented 8 years ago

I'm willing to move the require statements to the top as that's a better practice anyways, but I'm not interested in adding lots of workarounds for this. nexe should just do what Electron does and monkeypatch fs and child_process so it just works transparently: https://github.com/electron/electron/blob/master/docs/tutorial/application-packaging.md#node-api