Skip to content

Commit

Permalink
Check if 'fs.constants.O_CREAT' exists for node v5.* and lower
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 22, 2017
1 parent 099d40b commit e23b5d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
19 changes: 12 additions & 7 deletions lib/resource/physical/PhysicalFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ var PhysicalFile = (function (_super) {
callback(null, IResource_1.ResourceType.File);
};
PhysicalFile.prototype.create = function (callback) {
fs.open(this.realPath, fs.constants.O_CREAT, function (e, fd) {
if (e)
callback(e);
else
fs.close(fd, function (e) {
if (!fs.constants || !fs.constants.O_CREAT) {
fs.writeFile(this.realPath, '', callback);
}
else {
fs.open(this.realPath, fs.constants.O_CREAT, function (e, fd) {
if (e)
callback(e);
});
});
else
fs.close(fd, function (e) {
callback(e);
});
});
}
};
PhysicalFile.prototype.delete = function (callback) {
var _this = this;
Expand Down
21 changes: 14 additions & 7 deletions src/resource/physical/PhysicalFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ export class PhysicalFile extends PhysicalResource
// ****************************** Actions ****************************** //
create(callback : SimpleCallback)
{
fs.open(this.realPath, fs.constants.O_CREAT, (e, fd) => {
if(e)
callback(e);
else
fs.close(fd, (e) => {
if(!fs.constants || !fs.constants.O_CREAT)
{ // node v5.* and lower
fs.writeFile(this.realPath, '', callback);
}
else
{ // node v6.* and higher
fs.open(this.realPath, fs.constants.O_CREAT, (e, fd) => {
if(e)
callback(e);
});
})
else
fs.close(fd, (e) => {
callback(e);
});
})
}
}
delete(callback : SimpleCallback)
{
Expand Down

0 comments on commit e23b5d7

Please sign in to comment.