Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active July 8, 2016 00:33
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 Noitidart/7810121036595cdc735de2936a7952da to your computer and use it in GitHub Desktop.
Save Noitidart/7810121036595cdc735de2936a7952da to your computer and use it in GitHub Desktop.
_ff-addon-snippet-writeThenDir -
// https://gist.github.com/Noitidart/7810121036595cdc735de2936a7952da -rev2
function writeThenDir(aPlatPath, aContents, aDirFrom, aOptions={}) {
// tries to writeAtomic
// if it fails due to dirs not existing, it creates the dir
// then writes again
// if fail again for whatever reason it throws
var cOptionsDefaults = {
encoding: 'utf-8',
noOverwrite: false
// tmpPath: aPlatPath + '.tmp'
};
aOptions = Object.assign(cOptionsDefaults, aOptions);
var do_write = function() {
OS.File.writeAtomic(aPlatPath, aContents, aOptions); // doing unixMode:0o4777 here doesn't work, i have to `OS.File.setPermissions(path_toFile, {unixMode:0o4777})` after the file is made
};
try {
do_write();
} catch (OSFileError) {
if (OSFileError.becauseNoSuchFile) { // this happens when directories dont exist to it
OS.File.makeDir(OS.Path.dirname(aPlatPath), {from:aDirFrom});
do_write(); // if it fails this time it will throw outloud
} else {
throw OSFileError;
}
}
}
@Noitidart
Copy link
Author

README

Rev1

  • Taken from NativeShot v1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment