PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/jscs/node_modules/vow-fs/lib/fs.js

https://gitlab.com/TurnInternational/simon
JavaScript | 364 lines | 193 code | 33 blank | 138 comment | 17 complexity | fc39df6bea2439d26803d13b609943cb MD5 | raw file
  1. /**
  2. * @module vow-fs
  3. * @author Filatov Dmitry <dfilatov@yandex-team.ru>
  4. * @version 0.3.5
  5. * @license
  6. * Dual licensed under the MIT and GPL licenses:
  7. * * http://www.opensource.org/licenses/mit-license.php
  8. * * http://www.gnu.org/licenses/gpl.html
  9. */
  10. var vow = require('vow'),
  11. Queue = require('vow-queue'),
  12. openFilesQueue = new Queue(),
  13. fs = require('fs'),
  14. path = require('path'),
  15. os = require('os'),
  16. uuid = require('uuid'),
  17. glob = require('glob'),
  18. slice = Array.prototype.slice,
  19. promisify = function(nodeFn) {
  20. return function() {
  21. var defer = vow.defer(),
  22. args = slice.call(arguments);
  23. args.push(function(err) {
  24. err? defer.reject(err) : defer.resolve(arguments[1]);
  25. });
  26. try {
  27. nodeFn.apply(fs, args);
  28. }
  29. catch(err) {
  30. defer.reject(err);
  31. }
  32. return defer.promise();
  33. };
  34. },
  35. tmpDir = os.tmpdir || os.tmpDir || function() { return '/tmp'; },
  36. makeDir = promisify(fs.mkdir),
  37. removeDir = promisify(fs.rmdir),
  38. lstat = promisify(fs.lstat),
  39. emfileTimeout = 1,
  40. emfileFixWrapper = function(method, weight) {
  41. var wrapper = function() {
  42. var callArgs = arguments;
  43. return openFilesQueue.enqueue(function() {
  44. return method.apply(vfs, callArgs).then(
  45. function(res) {
  46. emfileTimeout = Math.max(1, emfileTimeout / 2);
  47. return res;
  48. },
  49. function(err) {
  50. if(err.code === 'EMFILE') {
  51. emfileTimeout++;
  52. return vow.delay(null, emfileTimeout).then(function() {
  53. return wrapper.apply(vfs, callArgs);
  54. });
  55. }
  56. else {
  57. throw err;
  58. }
  59. });
  60. }, weight);
  61. };
  62. return wrapper;
  63. },
  64. undef;
  65. var vfs = module.exports = {
  66. /**
  67. * Reads file by given path
  68. * @param {String} path
  69. * @param {String} [encoding]
  70. * @returns {vow:Promise}
  71. */
  72. read : emfileFixWrapper(promisify(fs.readFile)),
  73. /**
  74. * Writes data to file by given path
  75. * @param {String} path
  76. * @param {String|Buffer} data
  77. * @param {String} [encoding]
  78. * @returns {vow:Promise}
  79. */
  80. write : emfileFixWrapper(promisify(fs.writeFile)),
  81. /**
  82. * Appends data to file by given path
  83. * @param {String} path
  84. * @param {String|Buffer} data
  85. * @param {String} [encoding]
  86. * @returns {vow:Promise}
  87. */
  88. append : emfileFixWrapper(promisify(fs.appendFile)),
  89. /**
  90. * Removes file at given path
  91. * @param {String} pathToRemove
  92. * @returns {vow:Promise}
  93. */
  94. remove : promisify(fs.unlink),
  95. /**
  96. * Copies file from sourcePath to targetPath
  97. * @param {String} sourcePath
  98. * @param {String} targetPath
  99. * @returns {vow:Promise}
  100. */
  101. copy : emfileFixWrapper(function(sourcePath, targetPath) {
  102. return this.isFile(sourcePath).then(function(isFile) {
  103. if(!isFile) {
  104. var err = Error();
  105. err.errno = 28;
  106. err.code = 'EISDIR';
  107. err.path = sourcePath;
  108. throw err;
  109. }
  110. var defer = vow.defer(),
  111. sourceStream = fs.createReadStream(sourcePath),
  112. errFn = function(err) {
  113. defer.reject(err);
  114. };
  115. sourceStream
  116. .on('error', errFn)
  117. .on('open', function() {
  118. var targetStream = fs.createWriteStream(targetPath);
  119. sourceStream.pipe(
  120. targetStream
  121. .on('error', errFn)
  122. .on('close', function() {
  123. defer.resolve();
  124. }));
  125. });
  126. return defer.promise();
  127. });
  128. }, 2),
  129. /**
  130. * Moves from sourcePath to targetPath
  131. * @param {String} sourcePath
  132. * @param {String} targetPath
  133. * @returns {vow:Promise}
  134. */
  135. move : promisify(fs.rename),
  136. /**
  137. * Extracts fs.Stats about a given path
  138. * @param {String} path
  139. * @returns {vow:Promise}
  140. */
  141. stat : promisify(fs.stat),
  142. /**
  143. * Tests whether or not the given path exists
  144. * @param {String} path
  145. * @returns {vow:Promise}
  146. */
  147. exists : fs.exists?
  148. function(path) {
  149. var defer = vow.defer();
  150. fs.exists(path, function(exists) {
  151. defer.resolve(exists);
  152. });
  153. return defer.promise();
  154. } :
  155. function(path) {
  156. var defer = vow.defer();
  157. fs.stat(path, function(err) {
  158. defer.resolve(!err);
  159. });
  160. return defer.promise();
  161. },
  162. /**
  163. * Creates a hard link from the sourcePath to targetPath
  164. * @param {String} sourcePath
  165. * @param {String} targetPath
  166. * @returns {vow:Promise}
  167. */
  168. link : promisify(fs.link),
  169. /**
  170. * Creates a relative symbolic link from the sourcePath to targetPath
  171. * @param {String} sourcePath
  172. * @param {String} targetPath
  173. * @param {String} [type=file] can be either 'dir', 'file', or 'junction'
  174. * @returns {vow:Promise}
  175. */
  176. symLink : promisify(fs.symlink),
  177. /**
  178. * Changes the owner for a given path using Unix user-id and group-id numbers
  179. * @param {String} path
  180. * @param uid
  181. * @param gid
  182. * @returns {vow:Promise}
  183. */
  184. chown : promisify(fs.chown),
  185. /**
  186. * Changes the Unix mode for a path. Returns a promise
  187. * @param {String} path
  188. * @param mode
  189. * @returns {vow:Promise}
  190. */
  191. chmod : promisify(fs.chmod),
  192. /**
  193. * Normalizes a given path to absolute path
  194. * @param {String} path
  195. * @returns {vow:Promise}
  196. */
  197. absolute : promisify(fs.realpath),
  198. /**
  199. * Checks whether the given path is a file
  200. * @param {String} path
  201. * @returns {vow:Promise}
  202. */
  203. isFile : function(path) {
  204. return this.stat(path).then(function(stats) {
  205. return stats.isFile();
  206. });
  207. },
  208. /**
  209. * Checks whether the given path is a directory
  210. * @param {String} path
  211. * @returns {vow:Promise}
  212. */
  213. isDir : function(path) {
  214. return this.stat(path).then(function(stats) {
  215. return stats.isDirectory();
  216. });
  217. },
  218. /**
  219. * Checks whether the given path is a socket
  220. * @param {String} path
  221. * @returns {vow:Promise}
  222. */
  223. isSocket : function(path) {
  224. return this.stat(path).then(function(stats) {
  225. return stats.isSocket();
  226. });
  227. },
  228. /**
  229. * Checks whether the given path is a symbolic link
  230. * @param {String} path
  231. * @returns {vow:Promise}
  232. */
  233. isSymLink : function(path) {
  234. return lstat(path).then(function(stats) {
  235. return stats.isSymbolicLink();
  236. });
  237. },
  238. /**
  239. * Makes a temporary file
  240. * @param {Object} options
  241. * @param {String} [options.prefix]
  242. * @param {String} [options.dir=os.tmpdir()]
  243. * @param {String} [options.ext=tmp]
  244. * @returns {vow:Promise}
  245. */
  246. makeTmpFile : function(options) {
  247. options || (options = {});
  248. var filePath = path.join(
  249. options.dir || tmpDir(),
  250. (options.prefix || '') + uuid.v4() + (options.ext || '.tmp'));
  251. return vfs.write(filePath, '').then(function() {
  252. return filePath;
  253. });
  254. },
  255. /**
  256. * Reads the contents of a directory by given path
  257. * @param {String} path
  258. * @returns {vow:Promise}
  259. */
  260. listDir : emfileFixWrapper(promisify(fs.readdir)),
  261. /**
  262. * Makes a directory at a given path, recursively creating any branches that doesn't exist
  263. * @param {String} dirPath
  264. * @param [mode=0777]
  265. * @param [failIfExist=false]
  266. * @returns {vow:Promise}
  267. */
  268. makeDir : function(dirPath, mode, failIfExist) {
  269. if(typeof mode === 'boolean') {
  270. failIfExist = mode;
  271. mode = undef;
  272. }
  273. var dirName = path.dirname(dirPath),
  274. onFailed = function(e) {
  275. if(e.code !== 'EEXIST' || failIfExist) {
  276. throw e;
  277. }
  278. return vfs.isDir(dirPath).then(function(isDir) {
  279. if(!isDir) {
  280. throw e;
  281. }
  282. });
  283. };
  284. return vfs.exists(dirName).then(function(exists) {
  285. if(exists) {
  286. return makeDir(dirPath, mode).fail(onFailed);
  287. }
  288. else {
  289. failIfExist = false;
  290. return vfs.makeDir(dirName, mode).then(function() {
  291. return makeDir(dirPath, mode).fail(onFailed);
  292. });
  293. }
  294. });
  295. },
  296. /**
  297. * Removes directory
  298. * @param {String} dirPath
  299. * @returns {vow:Promise}
  300. */
  301. removeDir : function(dirPath) {
  302. return vfs.listDir(dirPath)
  303. .then(function(list) {
  304. return list.length && vow.all(
  305. list.map(function(file) {
  306. var fullPath = path.join(dirPath, file);
  307. return vfs.isFile(fullPath).then(function(isFile) {
  308. return isFile?
  309. vfs.remove(fullPath) :
  310. vfs.removeDir(fullPath);
  311. });
  312. }));
  313. })
  314. .then(function() {
  315. return removeDir(dirPath);
  316. });
  317. },
  318. /**
  319. * Matches files using the patterns, see https://github.com/isaacs/node-glob for details
  320. * @param {String} pattern to be matched
  321. * @param {Object} [options] options
  322. * @returns {vow:Promise}
  323. */
  324. glob : promisify(glob),
  325. options : function(opts) {
  326. if(typeof opts.openFileLimit !== 'undefined') {
  327. openFilesQueue.setParams({ weightLimit : opts.openFileLimit });
  328. }
  329. }
  330. };
  331. openFilesQueue.start();