UNPKG

alinex-fs

Version:

Extension of nodes filesystem tools.

169 lines (153 loc) 4.1 kB
/* Touch Files ================================================= The touch methods allow the following options: - `reference` - file path used as reference - `time` - time to use - `mtime` - use this time (defauklts to current time) - `noCreate` - don't create file if not existing - `noAccess` - don't change access time of the file - `noModified` - don't change modified time of the file * `options` optional specific settings like: - `noCreate` - don't create file if it already exists - `time` - timw to set - `mtime` - modification timw to set - `reference` - use this file's time - `noAccess` - (boolean) set access time - `noModified` - (boolean) set modified time` __Example:__ ``` coffee fs = require 'alinex-fs' fs.touch "/tmp/testfile", (err) -> * everything done, go on ``` ``` coffee fs = require 'alinex-fs' fs.touch "/tmp/testfile", noCreate: true , (err) -> * everything done, go on ``` */ (function() { var fs, touch, touchSync; fs = require('fs'); /* @param {String} file to be changed @param {Object} [options] see description above @param {function(<Error>)} cb callback with `Error` if sometzhing went wrong */ touch = module.exports.touch = function(file, options, cb) { var atime, mtime; if (options == null) { options = {}; } if (cb == null) { cb = function() {}; } if (typeof options === 'function') { cb = options; options = {}; } if (options.time == null) { options.time = new Date(); } atime = mtime = options.time; if (options.mtime) { mtime = options.mtime; } if (options.reference) { return fs.stat(options.reference, function(err, stats) { if (err) { return cb(err); } return touch(file, { time: stats.atime, mtime: stats.mtime, noCreate: options.noCreate }, cb); }); } if (options.noAccess || options.noModified) { if (options.noAccess) { atime = null; } if (options.noModified) { atime = null; } return fs.stat(file, function(err, stats) { if (err) { return cb(err); } return touch(file, { time: atime != null ? atime : stats.atime, mtime: mtime != null ? mtime : stats.mtime, noCreate: options.noCreate }, cb); }); } return fs.exists(file, function(exists) { if (exists || options.noCreate) { return cb(); } return fs.open(file, 'a', function(err, fd) { if (err) { return cb(err); } return fs.close(fd, function(err) { if (err) { return cb(err); } return fs.utimes(file, atime, mtime, cb); }); }); }); }; /* @param {String} file to be changed @param {Object} [options] see description above @throws {Error} if sometzhing went wrong */ touchSync = module.exports.touchSync = function(file, options) { var atime, mtime, stats; if (options == null) { options = {}; } if (options.time == null) { options.time = new Date(); } atime = mtime = options.time; if (options.mtime) { mtime = options.mtime; } if (options.reference) { stats = fs.statSync(options.reference); return touchSync(file, { time: stats.atime, mtime: stats.mtime, noCreate: options.noCreate }); } if (options.noAccess || options.noModified) { if (options.noAccess) { atime = null; } if (options.noModified) { atime = null; } stats = fs.statSync(file); return touchSync(file, { time: atime != null ? atime : stats.atime, mtime: mtime != null ? mtime : stats.mtime, noCreate: options.noCreate }); } if (fs.existsSync(file) || options.noCreate) { return; } fs.closeSync(fs.openSync(file, 'a')); return fs.utimesSync(file, atime, mtime); }; }).call(this); //# sourceMappingURL=touch.map