Skip to content

Commit 4b1eaf5

Browse files
author
Sebastian Florek
committed
Add option to update and check npm/bower dependencies with gulp
1 parent 5128fbb commit 4b1eaf5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

build/dependencies.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2015 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import gulp from 'gulp';
16+
import gulputil from 'gulp-util';
17+
import ncu from 'npm-check-updates';
18+
import path from 'path';
19+
import through from 'through2';
20+
21+
import conf from './conf';
22+
23+
/**
24+
* Updates npm dependencies.
25+
*/
26+
gulp.task('update-npm-dependencies', function() {
27+
return gulp.src([path.join(conf.paths.base, 'package.json')]).pipe(updateDependencies('npm'));
28+
});
29+
30+
/**
31+
* Checks npm dependencies which need to be updated.
32+
*/
33+
gulp.task('check-npm-dependencies', function() {
34+
return gulp.src([path.join(conf.paths.base, 'package.json')]).pipe(checkDependencies('npm'));
35+
});
36+
37+
/**
38+
* Updates bower dependencies.
39+
*/
40+
gulp.task('update-bower-dependencies', function() {
41+
return gulp.src([path.join(conf.paths.base, 'bower.json')]).pipe(updateDependencies('bower'));
42+
});
43+
44+
/**
45+
* Checks bower dependencies which need to be updated.
46+
*/
47+
gulp.task('check-bower-dependencies', function() {
48+
return gulp.src([path.join(conf.paths.base, 'bower.json')]).pipe(checkDependencies('bower'));
49+
});
50+
51+
/**
52+
* Updates dependencies of given package manager by updating related package/bower json file.
53+
*
54+
* @param {string} packageManager
55+
* @return {stream}
56+
*/
57+
function updateDependencies(packageManager) {
58+
return through.obj(function(file, codec, cb) {
59+
let relativePath = path.relative(process.cwd(), file.path);
60+
61+
ncu.run({
62+
packageFile: relativePath,
63+
packageManager: packageManager,
64+
cli: true,
65+
upgradeAll: true,
66+
args: [],
67+
})
68+
.then(cb);
69+
});
70+
}
71+
72+
/**
73+
* Checks and lists outdated dependencies if there are any.
74+
*
75+
* @param {string} packageManager
76+
* @return {stream}
77+
*/
78+
function checkDependencies(packageManager) {
79+
return through.obj(function(file, codec, cb) {
80+
let relativePath = path.relative(process.cwd(), file.path);
81+
82+
ncu.run({
83+
packageFile: relativePath,
84+
packageManager: packageManager,
85+
})
86+
.then(function(toUpgrade) {
87+
let dependenciesStr = Object.keys(toUpgrade)
88+
.map((key) => { return `${key}: ${toUpgrade[key]}\n`; })
89+
.join('');
90+
91+
if (dependenciesStr.length !== 0) {
92+
gulputil.log(
93+
gulputil.colors.yellow(
94+
`Dependencies needed to update:\n${dependenciesStr}\n` +
95+
'Run: \'gulp update-npm-dependencies\', then \'npm install\' to update' +
96+
' dependencies.\n'));
97+
}
98+
99+
cb();
100+
});
101+
});
102+
}

gulpfile.babel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import './build/check';
2222
import './build/cluster';
2323
import './build/backend';
2424
import './build/build';
25+
import './build/dependencies';
2526
import './build/deploy';
2627
import './build/index';
2728
import './build/script';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"karma-sauce-launcher": "^0.3.0",
6161
"karma-sourcemap-loader": "~0.3.6",
6262
"lodash": "~4.1.0",
63+
"npm-check-updates": "^2.5.8",
6364
"proxy-middleware": "~0.15.0",
6465
"q": "~1.4.1",
6566
"semver": "~5.1.0",

0 commit comments

Comments
 (0)