From e23b5d7136db8f7245f1befadca2b3fa0a58ae51 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Mon, 22 May 2017 17:41:47 +0200 Subject: [PATCH] Check if 'fs.constants.O_CREAT' exists for node v5.* and lower --- lib/resource/physical/PhysicalFile.js | 19 ++++++++++++------- src/resource/physical/PhysicalFile.ts | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/resource/physical/PhysicalFile.js b/lib/resource/physical/PhysicalFile.js index 42fe0737..79f1c74c 100644 --- a/lib/resource/physical/PhysicalFile.js +++ b/lib/resource/physical/PhysicalFile.js @@ -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; diff --git a/src/resource/physical/PhysicalFile.ts b/src/resource/physical/PhysicalFile.ts index 6a39e218..31f8891b 100644 --- a/src/resource/physical/PhysicalFile.ts +++ b/src/resource/physical/PhysicalFile.ts @@ -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) {