|
| 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 | +} |
0 commit comments