|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Copyright 2016, Google, Inc. |
| 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 | +const fs = require(`fs`); |
| 17 | +const path = require(`path`); |
| 18 | + |
| 19 | +const PROJECT_ROOT = path.join(__dirname, `..`); |
| 20 | + |
| 21 | +// Get subdirectories with a `package.json` file |
| 22 | +const directories = fs.readdirSync(PROJECT_ROOT) |
| 23 | + .filter((dir) => fs.existsSync(path.join(PROJECT_ROOT, dir, `package.json`))); |
| 24 | + |
| 25 | +// Dedupe package.json dependencies |
| 26 | +// WARNING: This will fail if two different versions of the same package are required. |
| 27 | +const pkgSet = {}; |
| 28 | +let pkgJson; |
| 29 | +directories.forEach((dir) => { |
| 30 | + pkgJson = JSON.parse(fs.readFileSync(path.join(PROJECT_ROOT, dir, `package.json`))); |
| 31 | + Object.assign(pkgSet, pkgJson.dependencies); |
| 32 | +}); |
| 33 | + |
| 34 | +// Update root-level package.json (by shelling to npm) |
| 35 | +const spawn = require('child_process').spawn; |
| 36 | +spawn(`yarn`, [`add`].concat(Object.keys(pkgSet).map(pkg => `${pkg}@${pkgSet[pkg]}`))); |
0 commit comments