|
| 1 | +""" |
| 2 | + Script that sets up the Angular snapshot github builds. We set up the snapshot builds by |
| 3 | + overwriting the versions in the "package.json" and taking advantage of Yarn's resolutions |
| 4 | + feature. Yarn resolutions will be used to flatten nested Angular packages because by default |
| 5 | + Yarn does not flatten any dependency. See: |
| 6 | +
|
| 7 | + node_modules/compiler@snapshot |
| 8 | + node_modules/compiler-cli@snapshot |
| 9 | + |
| 10 | +
|
| 11 | + Note that we cannot just use Yarn's `--flat` option because that would mean that it tries |
| 12 | + to flatten **all** dependencies and could cause unexpected results. We **only** want to |
| 13 | + explicitly flatten out all `@angular/*` dependencies. This can be achieved with resolutions. |
| 14 | + Read more here: https://yarnpkg.com/lang/en/docs/package-json/#toc-resolutions |
| 15 | +""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import argparse |
| 20 | + |
| 21 | +def collect_angular_deps(obj, result): |
| 22 | + for item in obj: |
| 23 | + if (item.startswith('@angular/')): |
| 24 | + result.append(item) |
| 25 | + |
| 26 | +project_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../')) |
| 27 | +package_json_path = os.path.join(project_dir, 'package.json') |
| 28 | + |
| 29 | +parser = argparse.ArgumentParser(description='Setup Angular snapshot packages') |
| 30 | +parser.add_argument('--tag', required = True) |
| 31 | +parsed_args = parser.parse_args() |
| 32 | + |
| 33 | +with open(package_json_path, 'r') as package_json: |
| 34 | + data = json.load(package_json) |
| 35 | + angular_pkgs = [] |
| 36 | + |
| 37 | + collect_angular_deps(data['dependencies'], angular_pkgs) |
| 38 | + collect_angular_deps(data['devDependencies'], angular_pkgs) |
| 39 | + |
| 40 | + if not 'resolutions' in data: |
| 41 | + data['resolutions'] = {} |
| 42 | + |
| 43 | + print('Setting up snapshot builds for:') |
| 44 | + |
| 45 | + for pkg in angular_pkgs: |
| 46 | + new_url = "github:angular/%s-builds#%s" % (pkg.split('/')[1], parsed_args.tag) |
| 47 | + |
| 48 | + print(" %s -> %s" % (pkg, new_url)) |
| 49 | + |
| 50 | + # Add resolutions for each package in the format "**/{PACKAGE}" so that all |
| 51 | + # nested versions of that specific Angular package will have the same version. |
| 52 | + data['resolutions']['**/%s' % pkg] = new_url |
| 53 | + |
| 54 | + # Since the resolutions only cover the version of all nested installs, we also need |
| 55 | + # to explicitly set the version for the package listed in the project "package.json". |
| 56 | + data['dependencies'][pkg] = new_url |
| 57 | + |
| 58 | + # In case this dependency was previously a dev dependency, just remove it because we |
| 59 | + # re-added it as a normal dependency for simplicity. |
| 60 | + if pkg in data['devDependencies']: |
| 61 | + del data['devDependencies'][pkg] |
| 62 | + |
| 63 | + new_package_json = json.dumps(data, indent = 2) |
| 64 | + |
| 65 | + with open(package_json_path, 'w') as package_json_out: |
| 66 | + package_json_out.write(new_package_json) |
| 67 | + |
| 68 | + print('') |
| 69 | + print('Successfully added the "resolutions" to the "package.json".') |
| 70 | + print('Please run "yarn install" in the project to update the lock file.') |
0 commit comments