Last active
July 8, 2016 00:33
-
-
Save Noitidart/7810121036595cdc735de2936a7952da to your computer and use it in GitHub Desktop.
_ff-addon-snippet-writeThenDir -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Rev1