Initial commit
authorNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 23:28:21 +0000 (10:28 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 23:28:21 +0000 (10:28 +1100)
.gitignore [new file with mode: 0644]
.npmignore [new file with mode: 0644]
.yarnignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
README.md [new file with mode: 0644]
disk_build.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..ff3a1bd
--- /dev/null
@@ -0,0 +1,5 @@
+/disk_build-*.tgz
+/node_modules
+/package-lock.json
+/yarn.lock
+/yarn-error.log
diff --git a/.npmignore b/.npmignore
new file mode 100644 (file)
index 0000000..ff3a1bd
--- /dev/null
@@ -0,0 +1,5 @@
+/disk_build-*.tgz
+/node_modules
+/package-lock.json
+/yarn.lock
+/yarn-error.log
diff --git a/.yarnignore b/.yarnignore
new file mode 100644 (file)
index 0000000..ff3a1bd
--- /dev/null
@@ -0,0 +1,5 @@
+/disk_build-*.tgz
+/node_modules
+/package-lock.json
+/yarn.lock
+/yarn-error.log
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..aa360c1
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+SPDX-License-Identifier: MIT
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..19bd520
--- /dev/null
+++ b/README.md
@@ -0,0 +1,56 @@
+# Disk Build helper
+
+An NDCODE project.
+
+## Overview
+
+The `disk_build` package exports a function `disk_build(pathname, build_func)`,
+which given a pathname to an existing source file, will generate a a pathname
+for a corresponding on-disk temporary file, check whether this exists and is
+newer than the original, and rebuild it via a caller-provided function if not.
+
+## Calling API
+
+The interface for the `disk_build`-provided helper function `disk_build()` is:
+
+`await disk_build(pathname, build_func)` &mdash; generates the corresponding
+`built_pathname`, checks if the temporary file `built_pathname` exists and is
+newer than `pathname`, and if not, calls the user-provided callback function
+`build_func(built_pathname)` to rebuild the temporary file before returning.
+
+The interface for the user-provided callback function `build_func()` is:
+
+`await build_func(text, built_pathname)` &mdash; user must create the file
+`built_pathname` and then return, or alternatively an exception can be thrown.
+
+## About diagnostics
+
+A diagnostic will be printed if the temporary file is up-to-date and does not
+need to be rebuilt. No diagnostic is printed if the `build_func()` callback is
+invoked, since it is expected that the user can provide an appropriate one
+(allowing the diagnostic to be suppressed if the source file isn't readable).
+
+## To be implemented
+
+It is intended that dependencies will be tracked in the future, so that an
+on-disk temporary file can be built from several on-disk sources. In that case
+we will write a second temporary file `*.deps` containing a list of the build
+dependencies, so that up-to-dateness can be checked from only the disk status.
+
+## GIT repository
+
+The development version can be cloned, downloaded, or browsed with `gitweb` at:
+https://git.ndcode.org/public/disk_build.git
+
+## License
+
+All of our NPM packages are MIT licensed, please see LICENSE in the repository.
+
+## Contributions
+
+We would greatly welcome your feedback and contributions. The `disk_build` is
+under active development (and is part of a larger project that is also under
+development) and thus the API is considered tentative and subject to change. If
+this is undesirable, you could possibly pin the version in your `package.json`.
+
+Contact: Nick Downing <nick@ndcode.org>
diff --git a/disk_build.js b/disk_build.js
new file mode 100644 (file)
index 0000000..1ec2f6e
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+ * SPDX-License-Identifier: MIT
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+let fs = require('fs')
+let path = require('path')
+let util = require('util')
+
+let fs_stat = util.promisify(fs.stat)
+
+let disk_build = async (pathname, build_func) => {
+  let stats = await fs_stat(pathname)
+
+  let built_pathname = path.posix.resolve(
+    path.posix.dirname(pathname),
+    `.${path.posix.basename(pathname)}`
+  )
+
+  let built_stats
+  try {
+    built_stats = await fs_stat(built_pathname)
+  }
+  catch (err) {
+    if (!(err instanceof Error) || err.code !== 'ENOENT')
+      throw err
+    //built_stats = undefined
+  }
+  if (built_stats === undefined || stats.mtimeMs > built_stats.mtimeMs)
+    await build_func(built_pathname)
+  else
+    console.log('reloading', built_pathname)
+
+  return built_pathname
+}
+
+module.exports = disk_build
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..c24d390
--- /dev/null
@@ -0,0 +1,29 @@
+{
+  "name": "@ndcode/disk_build",
+  "version": "0.1.0",
+  "description": "Helper function for checking and rebuilding disk objects.",
+  "keywords": [
+    "disk",
+    "build",
+    "helper",
+    "modified",
+    "sources",
+    "compile",
+    "compiler"
+  ],
+  "homepage": "https://www.ndcode.org",
+  "repository": {
+    "type": "git",
+    "url": "https://git.ndcode.org/public/disk_build.git"
+  },
+  "bugs": {
+    "email": "nick@ndcode.org"
+  },
+  "main": "disk_build.js",
+  "directories": {},
+  "dependencies": {},
+  "devDependencies": {},
+  "scripts": {},
+  "author": "Nick Downing <nick@ndcode.org>",
+  "license": "MIT"
+}