Skip to content

Commit 7140c9c

Browse files
committed
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 angular#2038
1 parent 4d338a4 commit 7140c9c

File tree

8 files changed

+118
-51
lines changed

8 files changed

+118
-51
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ install:
4444
- npm --version
4545
# Check the size of caches
4646
- du -sh ./node_modules || true
47-
# Install npm dependecies and ensure that npm cache is not stale
48-
- tools/npm/install-dependencies.sh
47+
# Install npm dependecies
48+
- npm install
4949

5050
before_script:
5151
- mkdir -p $LOGS_DIR

gulpfile.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var madge = require('madge');
1515
var merge = require('merge');
1616
var merge2 = require('merge2');
1717
var path = require('path');
18-
var semver = require('semver');
18+
1919
var watch = require('./tools/build/watch');
2020

2121
var transpile = require('./tools/build/transpile');
@@ -36,6 +36,11 @@ var replace = require('gulp-replace');
3636
var insert = require('gulp-insert');
3737
var shouldLog = require('./tools/build/logging');
3838

39+
require('./tools/check-environment')({
40+
requiredNpmVersion: '>=2.9.0',
41+
requiredNodeVersion: '>=0.12.2'
42+
});
43+
3944
// Make it easy to quiet down portions of the build.
4045
// --logs=all -> log everything (This is the default)
4146
// --logs=quiet -> log nothing
@@ -63,35 +68,6 @@ var angularBuilder = {
6368
}
6469
};
6570

66-
(function checkNodeAndNpmVersions() {
67-
var requiredNpmVersion = '>=2.9.0';
68-
var requiredNodeVersion = '>=0.12.2';
69-
70-
exec('npm --version', function(e, stdout) {
71-
var foundNpmVersion = semver.clean(stdout);
72-
var foundNodeVersion = process.version;
73-
var issues = [];
74-
75-
76-
if (!semver.satisfies(foundNodeVersion, requiredNodeVersion)) {
77-
issues.push('You are running unsupported node version. Found: ' + foundNodeVersion +
78-
' Expected: ' + requiredNodeVersion);
79-
}
80-
81-
if (!semver.satisfies(foundNpmVersion, requiredNpmVersion)) {
82-
issues.push('You are running unsupported npm version. Found: ' + foundNpmVersion +
83-
' Expected: ' + requiredNpmVersion);
84-
}
85-
86-
if (issues.length) {
87-
// TODO: in the future we should error, but let's just display the warning for a few days first
88-
console.warn(Array(80).join('!'));
89-
console.warn('Your environment is not in a good shape. Following issues were found:');
90-
issues.forEach(function(issue) {console.warn(' - ' + issue)});
91-
console.warn(Array(80).join('!'));
92-
}
93-
});
94-
}());
9571

9672
function sequenceComplete(done) {
9773
return function (err) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"url": "https://github.com/angular/angular.git"
2222
},
2323
"scripts": {
24-
"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",
24+
"preinstall": "node tools/npm/check-node-modules --purge",
25+
"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",
2526
"test": "gulp test.all.js && gulp test.all.dart"
2627
},
2728
"dependencies": {

tools/check-environment.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var exec = require('child_process').exec;
2+
var semver = require('semver');
3+
4+
var checkNodeModules = require('./npm/check-node-modules.js');
5+
6+
7+
function checkEnvironment(reqs) {
8+
9+
exec('npm --version', function(e, stdout) {
10+
var foundNpmVersion = semver.clean(stdout);
11+
var foundNodeVersion = process.version;
12+
var issues = [];
13+
14+
15+
if (!semver.satisfies(foundNodeVersion, reqs.requiredNodeVersion)) {
16+
issues.push('You are running unsupported node version. Found: ' + foundNodeVersion +
17+
' Expected: ' + reqs.requiredNodeVersion + '. Use nvm to update your node version.');
18+
}
19+
20+
if (!semver.satisfies(foundNpmVersion, reqs.requiredNpmVersion)) {
21+
issues.push('You are running unsupported npm version. Found: ' + foundNpmVersion +
22+
' Expected: ' + reqs.requiredNpmVersion + '. Run: npm update -g npm');
23+
}
24+
25+
if (!checkNodeModules()) {
26+
issues.push('Your node_modules directory is stale or out of sync with npm-shrinkwrap.json. Run: npm install');
27+
}
28+
29+
if (issues.length) {
30+
console.warn(Array(80).join('!'));
31+
console.warn('Your environment is not in a good shape. Following issues were found:');
32+
issues.forEach(function(issue) {console.warn(' - ' + issue)});
33+
console.warn(Array(80).join('!'));
34+
}
35+
});
36+
}
37+
38+
39+
module.exports = checkEnvironment;

tools/npm/check-node-modules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
var checkNpm = require('./check-node-modules.js');
4+
5+
var purgeIfStale = (process.argv.indexOf('--purge') !== -1)
6+
7+
checkNpm(true, purgeIfStale);

tools/npm/check-node-modules.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json';
7+
var NPM_SHRINKWRAP_CACHED_FILE = 'node_modules/npm-shrinkwrap.cached.json';
8+
var FS_OPTS = {encoding: 'utf-8'};
9+
var PROJECT_ROOT = path.join(__dirname, '../../');
10+
11+
12+
function checkNodeModules(logOutput, purgeIfStale) {
13+
var nodeModulesOK = _checkCache(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE);
14+
15+
if (nodeModulesOK) {
16+
if (logOutput) console.log(':-) npm dependencies are looking good!');
17+
} else {
18+
if (logOutput) console.error(':-( npm dependencies are stale or in an in unknown state!');
19+
if (purgeIfStale) {
20+
if (logOutput) console.log(' purging...');
21+
22+
var nodeModulesPath = path.join(PROJECT_ROOT, 'node_modules');
23+
24+
if (fs.existsSync(nodeModulesPath)) {
25+
// lazy-load fs-extra
26+
var fse = require('fs-extra');
27+
fse.removeSync(nodeModulesPath);
28+
}
29+
}
30+
}
31+
32+
return nodeModulesOK;
33+
}
34+
35+
36+
function _checkCache(markerFile, cacheMarkerFile) {
37+
var absoluteMarkerFilePath = path.join(PROJECT_ROOT, markerFile);
38+
var absoluteCacheMarkerFilePath = path.join(PROJECT_ROOT, cacheMarkerFile);
39+
40+
41+
if (!fs.existsSync(absoluteCacheMarkerFilePath)) return false;
42+
43+
var markerContent = fs.readFileSync(absoluteMarkerFilePath, FS_OPTS);
44+
var cacheMarkerContent = fs.readFileSync(absoluteCacheMarkerFilePath, FS_OPTS);
45+
46+
return markerContent == cacheMarkerContent;
47+
}
48+
49+
50+
module.exports = checkNodeModules;

tools/npm/copy-npm-shrinkwrap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env node
2+
3+
var fse = require('fs-extra');
4+
var path = require('path');
5+
6+
var NPM_SHRINKWRAP_FILE = 'npm-shrinkwrap.json';
7+
var NPM_SHRINKWRAP_CACHED_FILE = 'node_modules/npm-shrinkwrap.cached.json';
8+
var PROJECT_ROOT = path.join(__dirname, '../../');
9+
10+
process.chdir(PROJECT_ROOT);
11+
12+
fse.copySync(NPM_SHRINKWRAP_FILE, NPM_SHRINKWRAP_CACHED_FILE);

tools/npm/install-dependencies.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)