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

View File

@ -44,8 +44,8 @@ install:
- npm --version
# Check the size of caches
- du -sh ./node_modules || true
# Install npm dependecies and ensure that npm cache is not stale
- tools/npm/install-dependencies.sh
# Install npm dependecies
- npm install
before_script:
- mkdir -p $LOGS_DIR

View File

@ -15,7 +15,7 @@ var madge = require('madge');
var merge = require('merge');
var merge2 = require('merge2');
var path = require('path');
var semver = require('semver');
var watch = require('./tools/build/watch');
var transpile = require('./tools/build/transpile');
@ -36,6 +36,11 @@ var replace = require('gulp-replace');
var insert = require('gulp-insert');
var shouldLog = require('./tools/build/logging');
require('./tools/check-environment')({
requiredNpmVersion: '>=2.9.0',
requiredNodeVersion: '>=0.12.2'
});
// Make it easy to quiet down portions of the build.
// --logs=all -> log everything (This is the default)
// --logs=quiet -> log nothing
@ -63,35 +68,6 @@ var angularBuilder = {
}
};
(function checkNodeAndNpmVersions() {
var requiredNpmVersion = '>=2.9.0';
var requiredNodeVersion = '>=0.12.2';
exec('npm --version', function(e, stdout) {
var foundNpmVersion = semver.clean(stdout);
var foundNodeVersion = process.version;
var issues = [];
if (!semver.satisfies(foundNodeVersion, requiredNodeVersion)) {
issues.push('You are running unsupported node version. Found: ' + foundNodeVersion +
' Expected: ' + requiredNodeVersion);
}
if (!semver.satisfies(foundNpmVersion, requiredNpmVersion)) {
issues.push('You are running unsupported npm version. Found: ' + foundNpmVersion +
' Expected: ' + requiredNpmVersion);
}
if (issues.length) {
// TODO: in the future we should error, but let's just display the warning for a few days first
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('!'));
}
});
}());
function sequenceComplete(done) {
return function (err) {

View File

@ -21,7 +21,8 @@
"url": "https://github.com/angular/angular.git"
},
"scripts": {
"postinstall": "node tools/chromedriverpatch.js && webdriver-manager update && bower install && gulp pubget.dart && tsd reinstall --config modules/angular2/tsd.json && tsd reinstall --config tools/tsd.json",
"preinstall": "node tools/npm/check-node-modules --purge",
"postinstall": "node tools/npm/copy-npm-shrinkwrap && node tools/chromedriverpatch.js && webdriver-manager update && bower install && gulp pubget.dart && tsd reinstall --config modules/angular2/tsd.json && tsd reinstall --config tools/tsd.json",
"test": "gulp test.all.js && gulp test.all.dart"
},
"dependencies": {

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
tools/npm/check-node-modules Executable file
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
tools/npm/check-node-modules.js Executable file
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
tools/npm/copy-npm-shrinkwrap Executable file
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);

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