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 override with outputFile? #195

Closed
brunano21 opened this issue Nov 20, 2015 · 4 comments
Closed

How to override with outputFile? #195

brunano21 opened this issue Nov 20, 2015 · 4 comments

Comments

@brunano21
Copy link

I'm using the outputFile function but I would like to override the file if it already exists. However, seems it is not possible to pass an option to the function.
How to accomplish that thus?

Edit: Even if it should override an existing file, this doesnt happen.

@brunano21 brunano21 changed the title How override with outputFile? How to override with outputFile? Nov 20, 2015
@thomas-riccardi
Copy link

By default outputFile does overwrite the file.
What is missing is documentation on the 3rd parameter: options, which seems to be forwarded down to nodejs native fs, where we can pass a flag, to not override (ie append):
fs.outputFile('/tmp/foo', 'bar', {flag: 'a'}) will append to the /tmp/foo file, instead of override.

See #197.

@jprichardson
Copy link
Owner

@brunano21 as @triccardi-systran stated, the whole point of outputFile is to behave exactly like writeFile except that it creates the directory if it doesn't exist. Can you paste a snippet of what you're trying to do along with your expectations? Thanks.

@brunano21
Copy link
Author

@jprichardson
Hi,
my snippet is the following:

function _takeScreenshot (object, action) {
    var self = this;
    var remote = self._remote;
    var path = null;
    var filename = null;

    path = "./tests-screenshots/" + self._testName + "/ref";
    filename = ("000" + (self._totalSteps - self._testSteps.length - 1)).slice(-3) + ".png";

    return remote
        .takeScreenshot()
        .then(function (data) {

            fse.ensureDirSync(path);

            fse.outputFile(path + "/" + filename, data);
            return remote;
        });
};

Unfortunately the screenshot is saved only if the file does not exist. If it does, it's not overwritten.

@jprichardson
Copy link
Owner

The problem is that you are not properly interfacing this library with promises.

If you your snippet to work, you must either do the following:

  1. Use the sync version of fse.outputFileSync().
  2. Wrap the async version of fse.outputFile() into a promise.
  3. Use fs-extra-promise: https://github.com/overlookmotel/fs-extra-promise

Demonstrating 1:

var fse = require('fs-extra')

...

return remote
  .takeScreenshot()
  .then(function (data) {
    fse.outputFileSync(path + "/" + filename, data)
      return remote;
    });

Demonstrating 2:

var fse = require('fs-extra')

...

return remote
  .takeScreenshot()
  .then(function (data) {
    return new Promise(function (resolve, reject) {
      fse.outputFile(path + "/" + filename, data, function(err) {
         if (err) return reject(err)
         else resolve(remote)
      })
    })

Demonstrating 3:

var fse = require('fs-extra-promise')

...

return remote
  .takeScreenshot()
  .then(function (data) {
      return fse.outputFileAsync(path + "/" + filename, data)
    })

Hope that helps.

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

3 participants