build: make 'npm install' work reliably and issue build warning when node_modules look stale

This is done a in bit kludgy way on purpose so that it works on Windows and CI.

Works around npm/npm#8452.

Closes #2038
This commit is contained in:
Igor Minar
2015-06-05 23:37:09 -07:00
parent 4d338a4f5c
commit 7140c9cc34
8 changed files with 118 additions and 51 deletions
+39
View File
@@ -0,0 +1,39 @@
var exec = require('child_process').exec;
var semver = require('semver');
var checkNodeModules = require('./npm/check-node-modules.js');
function checkEnvironment(reqs) {
exec('npm --version', function(e, stdout) {
var foundNpmVersion = semver.clean(stdout);
var foundNodeVersion = process.version;
var issues = [];
if (!semver.satisfies(foundNodeVersion, reqs.requiredNodeVersion)) {
issues.push('You are running unsupported node version. Found: ' + foundNodeVersion +
' Expected: ' + reqs.requiredNodeVersion + '. Use nvm to update your node version.');
}
if (!semver.satisfies(foundNpmVersion, reqs.requiredNpmVersion)) {
issues.push('You are running unsupported npm version. Found: ' + foundNpmVersion +
' Expected: ' + reqs.requiredNpmVersion + '. Run: npm update -g npm');
}
if (!checkNodeModules()) {
issues.push('Your node_modules directory is stale or out of sync with npm-shrinkwrap.json. Run: npm install');
}
if (issues.length) {
console.warn(Array(80).join('!'));
console.warn('Your environment is not in a good shape. Following issues were found:');
issues.forEach(function(issue) {console.warn(' - ' + issue)});
console.warn(Array(80).join('!'));
}
});
}
module.exports = checkEnvironment;
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env node
var checkNpm = require('./check-node-modules.js');
var purgeIfStale = (process.argv.indexOf('--purge') !== -1)
checkNpm(true, purgeIfStale);
+50
View File
@@ -0,0 +1,50 @@
"use strict";
var fs = require('fs');
var path = require('path');
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json';
var NPM_SHRINKWRAP_CACHED_FILE = 'node_modules/npm-shrinkwrap.cached.json';
var FS_OPTS = {encoding: 'utf-8'};
var PROJECT_ROOT = path.join(__dirname, '../../');
function checkNodeModules(logOutput, purgeIfStale) {
var nodeModulesOK = _checkCache(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE);
if (nodeModulesOK) {
if (logOutput) console.log(':-) npm dependencies are looking good!');
} else {
if (logOutput) console.error(':-( npm dependencies are stale or in an in unknown state!');
if (purgeIfStale) {
if (logOutput) console.log(' purging...');
var nodeModulesPath = path.join(PROJECT_ROOT, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
// lazy-load fs-extra
var fse = require('fs-extra');
fse.removeSync(nodeModulesPath);
}
}
}
return nodeModulesOK;
}
function _checkCache(markerFile, cacheMarkerFile) {
var absoluteMarkerFilePath = path.join(PROJECT_ROOT, markerFile);
var absoluteCacheMarkerFilePath = path.join(PROJECT_ROOT, cacheMarkerFile);
if (!fs.existsSync(absoluteCacheMarkerFilePath)) return false;
var markerContent = fs.readFileSync(absoluteMarkerFilePath, FS_OPTS);
var cacheMarkerContent = fs.readFileSync(absoluteCacheMarkerFilePath, FS_OPTS);
return markerContent == cacheMarkerContent;
}
module.exports = checkNodeModules;
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env node
var fse = require('fs-extra');
var path = require('path');
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json';
var NPM_SHRINKWRAP_CACHED_FILE = 'node_modules/npm-shrinkwrap.cached.json';
var PROJECT_ROOT = path.join(__dirname, '../../');
process.chdir(PROJECT_ROOT);
fse.copySync(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE);
-18
View File
@@ -1,18 +0,0 @@
#!/bin/bash
set -e
SHRINKWRAP_FILE=npm-shrinkwrap.json
SHRINKWRAP_CACHED_FILE=node_modules/npm-shrinkwrap.cached.json
if diff -q $SHRINKWRAP_FILE $SHRINKWRAP_CACHED_FILE; then
echo 'No shrinkwrap changes detected. npm install will be skipped...';
echo 'Running npm postinstall'
npm run postinstall
else
echo 'Blowing away node_modules and reinstalling npm dependencies...'
rm -rf node_modules
npm install
cp $SHRINKWRAP_FILE $SHRINKWRAP_CACHED_FILE
echo 'npm install successful!'
fi