Repository: gulpjs/vinyl-fs
Pull request #172 (2016-06-07 07:49:27)

Introduce module for constants
As discussed [here](https://github.com/gulpjs/vinyl-fs/issues/158).

phated: Needs rebase

Patch:
diff --git a/lib/constants.js b/lib/constants.js
new file mode 100644
index 0000000..942d9b8
--- /dev/null
+++ b/lib/constants.js
@@ -0,0 +1,7 @@
+'use strict';
+
+module.exports = {
+  MASK_MODE: parseInt('7777', 8),
+  DEFAULT_FILE_MODE: parseInt('0666', 8),
+  DEFAULT_DIR_MODE: parseInt('0777', 8),
+};
diff --git a/lib/dest/write-contents/write-stream.js b/lib/dest/write-contents/write-stream.js
index 8683bec..04c38c1 100644
--- a/lib/dest/write-contents/write-stream.js
+++ b/lib/dest/write-contents/write-stream.js
@@ -12,17 +12,24 @@ function writeStream(file, written) {
   };
 
   var outStream = fs.createWriteStream(file.path, opt);
+  var fd = null;
 
   file.contents.once('error', complete);
   file.contents.once('end', readStreamEnd);
   outStream.once('error', complete);
   outStream.once('finish', complete);
+  outStream.once('open', onOpen);
 
   // Streams are piped with end disabled, this prevents the
   // WriteStream from closing the file descriptor after all
   // data is written.
   file.contents.pipe(outStream, { end: false });
 
+  // Obtain the file descriptor from the "open" event.
+  function onOpen(openFd) {
+    fd = openFd;
+  }
+
   function readStreamEnd() {
     readStream(file, complete);
   }
@@ -41,16 +48,17 @@ function writeStream(file, written) {
     file.contents.removeListener('end', readStreamEnd);
     outStream.removeListener('error', complete);
     outStream.removeListener('finish', complete);
+    outStream.removeListener('open', onOpen);
 
     if (streamErr) {
       return end(streamErr);
     }
 
-    if (typeof outStream.fd !== 'number') {
+    if (typeof fd !== 'number') {
       return end();
     }
 
-    fo.updateMetadata(outStream.fd, file, end);
+    fo.updateMetadata(fd, file, end);
   }
 }
 
diff --git a/lib/file-operations.js b/lib/file-operations.js
index 10ef8db..6ce4e83 100644
--- a/lib/file-operations.js
+++ b/lib/file-operations.js
@@ -6,10 +6,8 @@ var assign = require('object-assign');
 var isEqual = require('lodash.isequal');
 var isValidDate = require('vali-date');
 
-// TODO shared module
-var MASK_MODE = parseInt('7777', 8);
-var DEFAULT_FILE_MODE = parseInt('0666', 8);
-var DEFAULT_DIR_MODE = parseInt('0777', 8);
+var constants = require('./constants');
+
 var APPEND_MODE_REGEXP = /a/;
 
 function closeFd(propagatedErr, fd, callback) {
@@ -32,7 +30,7 @@ function getModeDiff(fsMode, vinylMode) {
   var modeDiff = 0;
 
   if (typeof vinylMode === 'number') {
-    modeDiff = (vinylMode ^ fsMode) & MASK_MODE;
+    modeDiff = (vinylMode ^ fsMode) & constants.MASK_MODE;
   }
 
   return modeDiff;
@@ -178,7 +176,7 @@ function writeFile(filepath, data, options, callback) {
   }
 
   // Default the same as node
-  var mode = options.mode || DEFAULT_FILE_MODE;
+  var mode = options.mode || constants.DEFAULT_FILE_MODE;
   var flag = options.flag || 'w';
   var position = APPEND_MODE_REGEXP.test(flag) ? null : 0;
 
@@ -203,7 +201,7 @@ function mkdirp(dirpath, mode, callback) {
     mode = undefined;
   }
 
-  var m = mode || DEFAULT_DIR_MODE;
+  var m = mode || constants.DEFAULT_DIR_MODE;
   var cb = callback || function() {};
   dirpath = path.resolve(dirpath);
 


Test patch:
diff --git a/test/dest-modes.js b/test/dest-modes.js
index 8700486..fa735a0 100644
--- a/test/dest-modes.js
+++ b/test/dest-modes.js
@@ -10,6 +10,7 @@ var expect = require('expect');
 var through = require('through2');
 
 var vfs = require('../');
+var constants = require('../lib/constants');
 
 function wipeOut() {
   this.timeout(20000);
@@ -331,14 +332,17 @@ describe('.dest() with custom modes', function() {
       return;
     }
 
-
     var inputBase = path.join(__dirname, './fixtures');
     var inputPath = path.join(__dirname, './fixtures/wow/suchempty');
     var expectedBase = path.join(__dirname, './out-fixtures/wow');
     var expectedPath = path.join(__dirname, './out-fixtures/wow/suchempty');
     // NOTE: Darwin does not set setgid
-    var expectedDirMode = (isDarwin ? parseInt('777', 8) : parseInt('2777', 8)) & ~process.umask();
-    var expectedFileMode = parseInt('677', 8) & ~process.umask();
+    var expectedDirMode = constants.DEFAULT_DIR_MODE;
+    if (!isDarwin) {
+      expectedDirMode |= parseInt('2000', 8);
+    }
+    expectedDirMode &= ~process.umask();
+    var expectedFileMode = constants.DEFAULT_FILE_MODE & ~process.umask();
 
     var firstFile = new File({
       base: inputBase,