|
| 1 | +/*! |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var async = require('async'); |
| 20 | +var david = require('david'); |
| 21 | +var glob = require('globby'); |
| 22 | +var is = require('is'); |
| 23 | +var packageJson = require('package-json'); |
| 24 | +var path = require('path'); |
| 25 | +var semver = require('semver'); |
| 26 | + |
| 27 | +require('shelljs/global'); |
| 28 | + |
| 29 | +/*! Developer Documentation |
| 30 | + * |
| 31 | + * This file will dig through all of the sub-module's package.json's, check for |
| 32 | + * any compatible updates, and run `npm install` to update them. |
| 33 | + */ |
| 34 | + |
| 35 | +/** |
| 36 | + * Dependencies not to update. |
| 37 | + * |
| 38 | + * @type {string[]} |
| 39 | + */ |
| 40 | +var BLACKLIST = [ |
| 41 | + 'dns-zonefile' |
| 42 | +]; |
| 43 | + |
| 44 | +/** |
| 45 | + * The minimum version of Node that we support. This is compared against each |
| 46 | + * depdendency's package.json.engines.node to confirm the newest version is |
| 47 | + * compatible. |
| 48 | + * |
| 49 | + * @const {string} |
| 50 | + */ |
| 51 | +var MIN_SUPPORTED_NODE_VERSION = |
| 52 | + require('../package.json').engines.node.replace(/^\D*/, ''); |
| 53 | + |
| 54 | +var pkgs = glob.sync('packages/*/package.json') |
| 55 | + .map(function(packageJsonPath) { |
| 56 | + return { |
| 57 | + cwd: path.dirname(packageJsonPath), |
| 58 | + json: require(path.join('..', packageJsonPath)), |
| 59 | + depsToUpdate: [] // Updated in `populateDepsToUpdate` |
| 60 | + }; |
| 61 | + }); |
| 62 | + |
| 63 | +async.eachLimit(pkgs, 5, populateDepsToUpdate, function(err) { |
| 64 | + if (err) { |
| 65 | + console.error('Could not update dependencies', err); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + async.eachLimit(pkgs, 5, updatePackage, function(err) { |
| 70 | + if (err) { |
| 71 | + console.error('Could not update dependencies', err); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + console.log('Dependencies updated!'); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +function updatePackage(pkg, callback) { |
| 80 | + if (!is.empty(pkg.depsToUpdate)) { |
| 81 | + var updatedDeps = pkg.depsToUpdate.map(function(depToUpdate) { |
| 82 | + return depToUpdate.name + '@^' + depToUpdate.stable; |
| 83 | + }); |
| 84 | + |
| 85 | + exec('npm install --save ' + updatedDeps.join(' '), { |
| 86 | + cwd: pkg.cwd |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + callback(); |
| 91 | +} |
| 92 | + |
| 93 | +function shouldUpdateDep(dependency, callback) { |
| 94 | + if (BLACKLIST.indexOf(dependency.name) > -1) { |
| 95 | + setImmediate(function() { |
| 96 | + callback(null, false); |
| 97 | + }); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + packageJson(dependency.name, dependency.stable).then(function(json) { |
| 102 | + var minCompatibleNodeVersion = json.engines && json.engines.node; |
| 103 | + |
| 104 | + if (!minCompatibleNodeVersion) { |
| 105 | + callback(null, true); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + var isCompatible = semver.satisfies( |
| 110 | + MIN_SUPPORTED_NODE_VERSION, |
| 111 | + minCompatibleNodeVersion |
| 112 | + ); |
| 113 | + |
| 114 | + callback(null, isCompatible); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +function populateDepsToUpdate(pkg, callback) { |
| 119 | + david.getUpdatedDependencies(pkg.json, function(err, updatedDeps) { |
| 120 | + if (err) { |
| 121 | + callback(err); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (is.empty(updatedDeps)) { |
| 126 | + callback(); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + for (var updatedDep in updatedDeps) { |
| 131 | + updatedDeps[updatedDep].name = updatedDep; |
| 132 | + } |
| 133 | + |
| 134 | + async.filter(updatedDeps, shouldUpdateDep, function(err, depsToUpdate) { |
| 135 | + if (err) { |
| 136 | + callback(err); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + pkg.depsToUpdate = depsToUpdate; |
| 141 | + |
| 142 | + callback(); |
| 143 | + }); |
| 144 | + }); |
| 145 | +} |
0 commit comments