[Pkg-javascript-commits] [node-touch] 01/03: Imported Upstream version 0.0.3

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Sat Mar 7 21:16:30 UTC 2015


This is an automated email from the git hooks/post-receive script.

sebastic pushed a commit to branch master
in repository node-touch.

commit 161de88c3927fb3782094796f25b3dda0d5650d3
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date:   Sat Mar 7 21:30:50 2015 +0100

    Imported Upstream version 0.0.3
---
 LICENSE        |  15 ++++++
 README.md      |  39 +++++++++++++++
 bin/touch.js   |  31 ++++++++++++
 package.json   |  15 ++++++
 test/sanity.js |  31 ++++++++++++
 touch.js       | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 285 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..05eeeb8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+The ISC License
+
+Copyright (c) Isaac Z. Schlueter
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7fb7ec4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# node-touch
+
+For all your node touching needs.
+
+## Installing
+
+```bash
+npm install touch
+```
+
+## CLI Usage:
+
+See `man touch`
+
+## API Usage:
+
+```javascript
+var touch = require("touch")
+```
+
+Gives you the following functions:
+
+* `touch(filename, options, cb)`
+* `touch.sync(filename, options)`
+* `touch.ftouch(fd, options, cb)`
+* `touch.ftouchSync(fd, options)`
+
+## Options
+
+* `force` like `touch -f` Boolean
+* `time` like `touch -t <date>` Can be a Date object, or any parseable
+  Date string, or epoch ms number.
+* `atime` like `touch -a` Can be either a Boolean, or a Date.
+* `mtime` like `touch -m` Can be either a Boolean, or a Date.
+* `ref` like `touch -r <file>` Must be path to a file.
+* `nocreate` like `touch -c` Boolean
+
+If neither `atime` nor `mtime` are set, then both values are set.  If
+one of them is set, then the other is not.
diff --git a/bin/touch.js b/bin/touch.js
new file mode 100644
index 0000000..931ca4d
--- /dev/null
+++ b/bin/touch.js
@@ -0,0 +1,31 @@
+var touch = require("../touch")
+  , fs = require("fs")
+  , path = require("path")
+  , nopt = require("nopt")
+  , types = { atime: Boolean
+            , mtime: Boolean
+            , time: Date
+            , ref: path
+            , nocreate: Boolean
+            , force: Boolean }
+  , shorthands = { a: "--atime"
+                 , m: "--mtime"
+                 , r: "--ref"
+                 , t: "--time"
+                 , c: "--nocreate"
+                 , f: "--force" }
+
+var options = nopt(types, shorthands)
+
+var files = options.argv.remain
+delete options.argv
+
+files.forEach(function (file) {
+  touch(file, options, function (er) {
+    if (er) {
+      console.error("bad touch!")
+      throw er
+    }
+    console.error(file, fs.statSync(file))
+  })
+})
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..14e4d8c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,15 @@
+{
+  "author": "Isaac Z. Schlueter <i at izs.me> (http://blog.izs.me/)",
+  "name": "touch",
+  "description": "like touch(1) in node",
+  "version": "0.0.3",
+  "repository": "git://github.com/isaacs/node-touch.git",
+  "main": "touch.js",
+  "engines": {
+    "node": ">=0.6"
+  },
+  "dependencies": {
+    "nopt": "~1.0.10"
+  },
+  "license": "ISC"
+}
diff --git a/test/sanity.js b/test/sanity.js
new file mode 100644
index 0000000..d7d8a9b
--- /dev/null
+++ b/test/sanity.js
@@ -0,0 +1,31 @@
+var fs = require("fs")
+var touch = require("../touch.js")
+
+function _ (fn) { return function (er) {
+  if (er) throw er
+  fn()
+}}
+
+touch.sync("sync")
+touch("async", _(function () {
+  console.log("async", fs.statSync("async"))
+  console.log("sync", fs.statSync("sync"))
+
+  setTimeout(function () {
+    touch.sync("sync")
+    touch("async", _(function () {
+      console.log("async", fs.statSync("async"))
+      console.log("sync", fs.statSync("sync"))
+      setTimeout(function () {
+        touch.sync("sync")
+        touch("async", _(function () {
+          console.log("async", fs.statSync("async"))
+          console.log("sync", fs.statSync("sync"))
+          fs.unlinkSync("sync")
+          fs.unlinkSync("async")
+        }))
+      }, 1000)
+    }))
+  }, 1000)
+}))
+
diff --git a/touch.js b/touch.js
new file mode 100644
index 0000000..e2bb3ec
--- /dev/null
+++ b/touch.js
@@ -0,0 +1,154 @@
+var fs = require("fs")
+  , cons = require("constants")
+
+module.exports = touch
+touch.touchSync = touch.sync = function (f, options) {
+  return touch(f, options)
+}
+
+touch.ftouch = ftouch
+touch.ftouchSync = function (fd, options) {
+  return ftouch(fd, options)
+}
+
+function validOpts (options) {
+  options = Object.create(options || {})
+
+  // {mtime: true}, {ctime: true}
+  // If set to something else, then treat as epoch ms value
+  var now = new Date(options.time || Date.now())
+  if (!options.atime && !options.mtime) {
+    options.atime = options.mtime = now
+  } else if (true === options.atime) {
+    options.atime = now
+  } else if (true === options.mtime) {
+    options.mtime = now
+  }
+
+  var oflags = 0
+  if (!options.force) {
+    oflags = oflags | cons.O_RDWR
+  }
+  if (!options.nocreate) {
+    oflags = oflags | cons.O_CREAT
+  }
+
+  options.oflags = oflags
+  return options
+}
+
+function optionsRef (then, arg, options, cb) {
+  if (!options.ref) return then(arg, options, cb)
+
+  return cb
+       ? fs.stat(options.ref, optionsRefcb(then, arg, options, cb))
+       : optionsRefcb(then, arg, options)(null, fs.statSync(options.ref))
+}
+
+function optionsRefcb (then, arg, options, cb) { return function (er, s) {
+  if (er) {
+    er.path = er.file = options.ref
+    return cb(er)
+  }
+  options.atime = options.atime && s.atime.getTime()
+  options.mtime = options.mtime && s.mtime.getTime()
+
+  // so we don't keep doing this.
+  options.ref = null
+
+  return then(arg, options, cb)
+}}
+
+function touch (f, options, cb) {
+  if (typeof options === "function") cb = options, options = null
+  options = validOpts(options)
+  return optionsRef(touch_, f, validOpts(options), cb)
+}
+
+function touch_ (f, options, cb) {
+  return openThenF(f, options, cb)
+}
+
+function openThenF (f, options, cb) {
+  options.closeAfter = true
+  return cb
+       ? fs.open(f, options.oflags, openThenFcb(options, cb))
+       : openThenFcb(options)(null, fs.openSync(f, options.oflags))
+}
+
+function openThenFcb (options, cb) { return function (er, fd) {
+  if (er) {
+    if (fd && options.closeAfter) fs.close(fd, function () {})
+    return cb(er)
+  }
+  return ftouch(fd, options, cb)
+}}
+
+function ftouch (fd, options, cb) {
+  if (typeof options === "function") cb = options, options = null
+  return optionsRef(ftouch_, fd, validOpts(options), cb)
+}
+
+function ftouch_ (fd, options, cb) {
+  // still not set.  leave as what the file already has.
+  return fstatThenFutimes(fd, options, cb)
+}
+
+function fstatThenFutimes (fd, options, cb) {
+  if (options.atime && options.mtime) return thenFutimes(fd, options, cb)
+
+  return cb
+       ? fs.fstat(fd, fstatThenFutimescb(fd, options, cb))
+       : fstatThenFutimescb(fd, options)(null, fs.fstatSync(fd))
+}
+
+function fstatThenFutimescb (fd, options, cb) { return function (er, s) {
+  if (er) {
+    if (options.closeAfter) fs.close(fd, function () {})
+    return cb(er)
+  }
+  options.atime = options.atime || s.atime.getTime()
+  options.mtime = options.mtime || s.mtime.getTime()
+  return thenFutimes(fd, options, cb)
+}}
+
+function thenFutimes (fd, options, cb) {
+  if (typeof options.atime === "object") {
+    options.atime = options.atime.getTime()
+  }
+  if (typeof options.mtime === "object") {
+    options.mtime = options.mtime.getTime()
+  }
+
+  var a = parseInt(options.atime / 1000, 10)
+    , m = parseInt(options.mtime / 1000, 10)
+  return cb
+       ? fs.futimes(fd, a, m, thenFutimescb(fd, options, cb))
+       : thenFutimescb(fd, options)(null, fs.futimesSync(fd, a, m))
+}
+
+function thenFutimescb (fd, options, cb) { return function (er, res) {
+  if (er) {
+    if (options.closeAfter) fs.close(fd, function () {})
+    return cb(er)
+  }
+  return finish(fd, options, res, cb)
+}}
+
+function finish (fd, options, res, cb) {
+  return options.closeAfter ? finishClose(fd, options, res, cb)
+       : cb ? cb(null, res)
+       : res
+}
+
+function finishClose (fd, options, res, cb) {
+  return cb
+       ? fs.close(fd, finishClosecb(res, options, cb))
+       : finishClosecb(res, options)(null, fs.closeSync(fd))
+}
+
+function finishClosecb (res, options, cb) { return function (er) {
+  if (er) return cb(er)
+  options.closeAfter = null
+  return finish(null, options, res, cb)
+}}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-touch.git



More information about the Pkg-javascript-commits mailing list