Initial commit
authorNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:05:34 +0000 (15:05 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:05:34 +0000 (15:05 +1100)
.gitignore [new file with mode: 0644]
build_cache.js [new file with mode: 0644]
package.json [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..9cf709a
--- /dev/null
@@ -0,0 +1 @@
+/build_cache-*.tgz
diff --git a/build_cache.js b/build_cache.js
new file mode 100644 (file)
index 0000000..bd4b78c
--- /dev/null
@@ -0,0 +1,39 @@
+var fs = require('fs')
+var util = require('util')
+
+var statAsync = util.promisify(fs.stat)
+
+var BuildCache = function() {
+  if (!this instanceof BuildCache)
+    throw Error('BuildCache is a constructor')
+  this.map = new Map()
+}
+
+BuildCache.prototype.get = async function(key) {
+  var result = this.map.get(key)
+  if (result === undefined)
+    return undefined
+  var deps = result.deps
+  var time = result.time
+  for (var i = 0; i < deps.length; ++i) {
+    stats = await statAsync(deps[i])
+    if (stats.mtimeMs > time) {
+      this.map.delete(key)
+      return undefined
+    }
+  }
+  return result.value
+}
+
+BuildCache.prototype.set = function(key, value, deps) {
+  this.map.set(
+    key,
+    {
+      value: value,
+      deps: deps === undefined ? [key] : deps,
+      time: Date.now()
+    }
+  )
+}
+
+module.exports = BuildCache
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..4ef4093
--- /dev/null
@@ -0,0 +1,14 @@
+{
+  "name": "build_cache",
+  "version": "1.0.0",
+  "description": "Simple caching scheme that checks for modified sources",
+  "main": "build_cache.js",
+  "directories": {},
+  "dependencies": {
+    "fs": "^0.0.1-security"
+  },
+  "devDependencies": {},
+  "scripts": {},
+  "author": "Nick Downing",
+  "license": "GPL-3.0"
+}