Last active
April 1, 2016 17:46
-
-
Save joeybaker/7bec7d3bdb5d77b1997bb8b7ecaec5e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From c39e95ae6d41939e73568bea5782228d8590b953 Mon Sep 17 00:00:00 2001 | |
From: Joey Baker <[email protected]> | |
Date: Thu, 31 Mar 2016 11:17:44 -0700 | |
Subject: [PATCH] Add: bin/should-install | |
Small script that warns if the dependencies are different from the last | |
time the script was run. | |
--- | |
bin/lib/should-install.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++ | |
bin/should-install | 22 +++++++++++++++++ | |
2 files changed, 83 insertions(+) | |
create mode 100644 bin/lib/should-install.js | |
create mode 100755 bin/should-install | |
diff --git a/bin/lib/should-install.js b/bin/lib/should-install.js | |
new file mode 100644 | |
index 0000000..0a47247 | |
--- /dev/null | |
+++ b/bin/lib/should-install.js | |
@@ -0,0 +1,61 @@ | |
+import {createWriteStream, readFile} from 'fs' | |
+import md5 from 'md5' | |
+import mkdirp from 'mkdirp' | |
+import path from 'path' | |
+import {parallel} from 'async' | |
+ | |
+const ENOENT = 'ENOENT' | |
+const CWD = process.cwd() | |
+const CACHE_DIR = path.join(CWD, '.cache') | |
+const CACHE_PATH = path.join(CACHE_DIR, 'should-install.json.cache') | |
+const PKG_PATH = path.join(CWD, 'package.json') | |
+ | |
+const getDepsHash = (done) => { | |
+ readFile(PKG_PATH, (err, pkg) => { | |
+ if (err) return void done(err) | |
+ | |
+ const json = JSON.parse(pkg.toString()) | |
+ const hash = md5(json.dependencies + json.devDependencies) | |
+ done(null, hash) | |
+ }) | |
+} | |
+ | |
+const readCache = (done) => { | |
+ readFile(CACHE_PATH, (err, cache) => { | |
+ if (err) { | |
+ if (err.code === ENOENT) done(null, {}) | |
+ else done(err) | |
+ } | |
+ else done(null, JSON.parse(cache.toString())) | |
+ }) | |
+} | |
+ | |
+const writeCache = (cache, done) => { | |
+ createWriteStream(CACHE_PATH) | |
+ .on('error', done) | |
+ .on('finish', done) | |
+ .end(JSON.stringify(cache)) | |
+} | |
+ | |
+export default ({checkpoint}, complete) => { | |
+ parallel({ | |
+ cacheDirExists: (done) => { | |
+ mkdirp(CACHE_DIR, done) | |
+ } | |
+ , depsHash: getDepsHash | |
+ , cache: readCache | |
+ }, (err, {depsHash, cache}) => { | |
+ if (err) return void complete(err) | |
+ | |
+ const currentDiffersFromCache = depsHash !== cache.depsHash | |
+ | |
+ if (checkpoint) { | |
+ writeCache({depsHash}, () => { | |
+ complete(null, currentDiffersFromCache) | |
+ }) | |
+ } | |
+ else { | |
+ complete(null, currentDiffersFromCache) | |
+ } | |
+ }) | |
+} | |
diff --git a/bin/should-install b/bin/should-install | |
new file mode 100755 | |
index 0000000..76a3e8c | |
--- /dev/null | |
+++ b/bin/should-install | |
@@ -0,0 +1,22 @@ | |
+#!/usr/bin/env node | |
+ | |
+const argv = require('yargs') | |
+ .usage('checks a cache of package.json and the git HEAD to see if npm should install') | |
+ .option('checkpoint', { | |
+ alias: 'c' | |
+ , describe: 'if this is a checkpoint, the cache will be updated and used for the next comparison' | |
+ , default: false | |
+ }) | |
+ .help('h') | |
+ .alias('h', 'help') | |
+ .epilog('License: Artistic-2.0') | |
+ .version(require('../package.json').version) | |
+ .showHelpOnFail(false, 'Specify --help for available options') | |
+ .argv | |
+ | |
+require('babel/register') | |
+require('./lib/should-install.js')(argv, (err, shouldInstall) => { | |
+ if (err) throw err | |
+ else if (shouldInstall) process.exit(1) | |
+ else process.exit(0) | |
+}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ "update-should-install-cache": "echo $(./bin/should-install --checkpoint)", | |
+ "postinstall": "if [ \"${NODE_ENV-}\" = \"development\" ]; then npm run -s update-should-install-cache; fi", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment