|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import { normalize } from '@angular-devkit/core'; |
| 8 | +import * as path from 'path'; |
| 9 | +import * as semver from 'semver'; |
9 | 10 | import { Arguments, Option } from '../models/interface';
|
10 | 11 | import { SchematicCommand } from '../models/schematic-command';
|
11 | 12 | import { findUp } from '../utilities/find-up';
|
12 | 13 | import { getPackageManager } from '../utilities/package-manager';
|
| 14 | +import { |
| 15 | + PackageIdentifier, |
| 16 | + PackageManifest, |
| 17 | + fetchPackageMetadata, |
| 18 | +} from '../utilities/package-metadata'; |
| 19 | +import { findNodeDependencies, readPackageTree } from '../utilities/package-tree'; |
13 | 20 | import { Schema as UpdateCommandSchema } from './update';
|
14 | 21 |
|
| 22 | +const npa = require('npm-package-arg'); |
| 23 | + |
| 24 | +const oldConfigFileNames = [ |
| 25 | + '.angular-cli.json', |
| 26 | + 'angular-cli.json', |
| 27 | +]; |
| 28 | + |
15 | 29 | export class UpdateCommand extends SchematicCommand<UpdateCommandSchema> {
|
16 | 30 | public readonly allowMissingWorkspace = true;
|
17 | 31 |
|
18 |
| - collectionName = '@schematics/update'; |
19 |
| - schematicName = 'update'; |
20 |
| - |
21 |
| - async parseArguments(schematicOptions: string[], schema: Option[]): Promise<Arguments> { |
22 |
| - const args = await super.parseArguments(schematicOptions, schema); |
23 |
| - const maybeArgsLeftovers = args['--']; |
24 |
| - |
25 |
| - if (maybeArgsLeftovers |
26 |
| - && maybeArgsLeftovers.length == 1 |
27 |
| - && maybeArgsLeftovers[0] == '@angular/cli' |
28 |
| - && args.migrateOnly === undefined |
29 |
| - && args.from === undefined) { |
30 |
| - // Check for a 1.7 angular-cli.json file. |
31 |
| - const oldConfigFileNames = [ |
32 |
| - normalize('.angular-cli.json'), |
33 |
| - normalize('angular-cli.json'), |
34 |
| - ]; |
35 |
| - const oldConfigFilePath = findUp(oldConfigFileNames, process.cwd()) |
36 |
| - || findUp(oldConfigFileNames, __dirname); |
37 |
| - |
38 |
| - if (oldConfigFilePath) { |
39 |
| - args.migrateOnly = true; |
40 |
| - args.from = '1.0.0'; |
| 32 | + async parseArguments(_schematicOptions: string[], _schema: Option[]): Promise<Arguments> { |
| 33 | + return {}; |
| 34 | + } |
| 35 | + |
| 36 | + // tslint:disable-next-line:no-big-function |
| 37 | + async run(options: UpdateCommandSchema & Arguments) { |
| 38 | + const packages: PackageIdentifier[] = []; |
| 39 | + for (const request of options['--'] || []) { |
| 40 | + try { |
| 41 | + const packageIdentifier: PackageIdentifier = npa(request); |
| 42 | + |
| 43 | + // only registry identifiers are supported |
| 44 | + if (!packageIdentifier.registry) { |
| 45 | + this.logger.error(`Package '${request}' is not a registry package identifer.`); |
| 46 | + |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + if (packages.some(v => v.name === packageIdentifier.name)) { |
| 51 | + this.logger.error(`Duplicate package '${packageIdentifier.name}' specified.`); |
| 52 | + |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + // If next option is used and no specifier supplied, use next tag |
| 57 | + if (options.next && !packageIdentifier.rawSpec) { |
| 58 | + packageIdentifier.fetchSpec = 'next'; |
| 59 | + } |
| 60 | + |
| 61 | + packages.push(packageIdentifier); |
| 62 | + } catch (e) { |
| 63 | + this.logger.error(e.message); |
| 64 | + |
| 65 | + return 1; |
41 | 66 | }
|
42 | 67 | }
|
43 | 68 |
|
44 |
| - // Move `--` to packages. |
45 |
| - if (args.packages == undefined && args['--']) { |
46 |
| - args.packages = args['--']; |
47 |
| - delete args['--']; |
48 |
| - } |
| 69 | + if (options.all && packages.length > 0) { |
| 70 | + this.logger.error('Cannot specify packages when using the "all" option.'); |
49 | 71 |
|
50 |
| - return args; |
51 |
| - } |
| 72 | + return 1; |
| 73 | + } else if (options.all && options.migrateOnly) { |
| 74 | + this.logger.error('Cannot use "all" option with "migrate-only" option.'); |
| 75 | + |
| 76 | + return 1; |
| 77 | + } else if (!options.migrateOnly && (options.from || options.to)) { |
| 78 | + this.logger.error('Can only use "from" or "to" options with "migrate-only" option.'); |
| 79 | + |
| 80 | + return 1; |
| 81 | + } |
52 | 82 |
|
53 |
| - async run(options: UpdateCommandSchema & Arguments) { |
54 | 83 | const packageManager = getPackageManager(this.workspace.root);
|
| 84 | + this.logger.info(`Using package manager: '${packageManager}'`); |
| 85 | + |
| 86 | + // Special handling for Angular CLI 1.x migrations |
| 87 | + if (options.migrateOnly === undefined && options.from === undefined) { |
| 88 | + if (!options.all && packages.length === 1 && packages[0].name === '@angular/cli') { |
| 89 | + const oldConfigFilePath = findUp(oldConfigFileNames, process.cwd()); |
| 90 | + if (oldConfigFilePath) { |
| 91 | + options.migrateOnly = true; |
| 92 | + options.from = '1.0.0'; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + this.logger.info('Collecting installed dependencies...'); |
| 98 | + |
| 99 | + const packageTree = await readPackageTree(this.workspace.root); |
| 100 | + const rootDependencies = findNodeDependencies(packageTree); |
| 101 | + |
| 102 | + this.logger.info(`Found ${Object.keys(rootDependencies).length} dependencies.`); |
| 103 | + |
| 104 | + if (options.all || packages.length === 0) { |
| 105 | + // Either update all packages or show status |
| 106 | + return this.runSchematic({ |
| 107 | + collectionName: '@schematics/update', |
| 108 | + schematicName: 'update', |
| 109 | + dryRun: !!options.dryRun, |
| 110 | + showNothingDone: false, |
| 111 | + additionalOptions: { |
| 112 | + force: options.force || false, |
| 113 | + next: options.next || false, |
| 114 | + packageManager, |
| 115 | + packages: options.all ? Object.keys(rootDependencies) : [], |
| 116 | + }, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + if (options.migrateOnly) { |
| 121 | + if (!options.from) { |
| 122 | + this.logger.error('"from" option is required when using the "migrate-only" option.'); |
| 123 | + |
| 124 | + return 1; |
| 125 | + } else if (packages.length !== 1) { |
| 126 | + this.logger.error( |
| 127 | + 'A single package must be specified when using the "migrate-only" option.', |
| 128 | + ); |
| 129 | + |
| 130 | + return 1; |
| 131 | + } |
| 132 | + |
| 133 | + if (options.next) { |
| 134 | + this.logger.warn('"next" option has no effect when using "migrate-only" option.'); |
| 135 | + } |
| 136 | + |
| 137 | + const packageName = packages[0].name; |
| 138 | + let packageNode = rootDependencies[packageName]; |
| 139 | + if (typeof packageNode === 'string') { |
| 140 | + this.logger.error('Package found in package.json but is not installed.'); |
| 141 | + |
| 142 | + return 1; |
| 143 | + } else if (!packageNode) { |
| 144 | + // Allow running migrations on transitively installed dependencies |
| 145 | + // There can technically be nested multiple versions |
| 146 | + // TODO: If multiple, this should find all versions and ask which one to use |
| 147 | + const child = packageTree.children.find(c => c.name === packageName); |
| 148 | + if (child) { |
| 149 | + // A link represents a symlinked package so use the actual in this case |
| 150 | + packageNode = child.isLink ? child.target : child; |
| 151 | + } |
| 152 | + |
| 153 | + if (!packageNode) { |
| 154 | + this.logger.error('Package is not installed.'); |
| 155 | + |
| 156 | + return 1; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + const updateMetadata = packageNode.package['ng-update']; |
| 161 | + let migrations = updateMetadata && updateMetadata.migrations; |
| 162 | + if (migrations === undefined) { |
| 163 | + this.logger.error('Package does not provide migrations.'); |
| 164 | + |
| 165 | + return 1; |
| 166 | + } else if (typeof migrations !== 'string') { |
| 167 | + this.logger.error('Package contains a malformed migrations field.'); |
| 168 | + |
| 169 | + return 1; |
| 170 | + } |
| 171 | + |
| 172 | + // if not non-relative, add package name |
| 173 | + if (migrations.startsWith('.') || migrations.startsWith('/')) { |
| 174 | + migrations = path.join(packageName, migrations); |
| 175 | + } |
| 176 | + |
| 177 | + return this.runSchematic({ |
| 178 | + collectionName: '@schematics/update', |
| 179 | + schematicName: 'migrate', |
| 180 | + dryRun: !!options.dryRun, |
| 181 | + force: false, |
| 182 | + showNothingDone: false, |
| 183 | + additionalOptions: { |
| 184 | + package: packageName, |
| 185 | + collection: migrations, |
| 186 | + from: options.from, |
| 187 | + to: options.to || packageNode.package.version, |
| 188 | + }, |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + const requests: PackageIdentifier[] = []; |
| 193 | + |
| 194 | + // Validate packages actually are part of the workspace |
| 195 | + for (const pkg of packages) { |
| 196 | + const node = rootDependencies[pkg.name]; |
| 197 | + if (!node) { |
| 198 | + this.logger.error(`Package '${pkg.name}' is not a dependency.`); |
| 199 | + |
| 200 | + return 1; |
| 201 | + } |
| 202 | + |
| 203 | + // If a specific version is requested and matches the installed version, skip. |
| 204 | + if (pkg.type === 'version' && |
| 205 | + typeof node === 'object' && |
| 206 | + node.package.version === pkg.fetchSpec) { |
| 207 | + this.logger.info(`Package '${pkg.name}' is already at '${pkg.fetchSpec}'.`); |
| 208 | + continue; |
| 209 | + } |
| 210 | + |
| 211 | + requests.push(pkg); |
| 212 | + } |
| 213 | + |
| 214 | + if (requests.length === 0) { |
| 215 | + return 0; |
| 216 | + } |
| 217 | + |
| 218 | + this.logger.info('Fetching dependency metadata from registry...'); |
| 219 | + for (const requestIdentifier of requests) { |
| 220 | + let metadata; |
| 221 | + try { |
| 222 | + // Metadata requests are internally cached; multiple requests for same name |
| 223 | + // does not result in additional network traffic |
| 224 | + metadata = await fetchPackageMetadata(requestIdentifier.name, this.logger); |
| 225 | + } catch (e) { |
| 226 | + this.logger.error(`Error fetching metadata for '${requestIdentifier.name}': ` + e.message); |
| 227 | + |
| 228 | + return 1; |
| 229 | + } |
| 230 | + |
| 231 | + // Try to find a package version based on the user requested package specifier |
| 232 | + // registry specifier types are either version, range, or tag |
| 233 | + let manifest: PackageManifest | undefined; |
| 234 | + if (requestIdentifier.type === 'version') { |
| 235 | + manifest = metadata.versions.get(requestIdentifier.fetchSpec); |
| 236 | + } else if (requestIdentifier.type === 'range') { |
| 237 | + const maxVersion = semver.maxSatisfying( |
| 238 | + Array.from(metadata.versions.keys()), |
| 239 | + requestIdentifier.fetchSpec, |
| 240 | + ); |
| 241 | + if (maxVersion) { |
| 242 | + manifest = metadata.versions.get(maxVersion); |
| 243 | + } |
| 244 | + } else if (requestIdentifier.type === 'tag') { |
| 245 | + manifest = metadata.tags[requestIdentifier.fetchSpec]; |
| 246 | + } |
| 247 | + |
| 248 | + if (!manifest) { |
| 249 | + this.logger.error( |
| 250 | + `Package specified by '${requestIdentifier.raw}' does not exist within the registry.`, |
| 251 | + ); |
| 252 | + |
| 253 | + return 1; |
| 254 | + } |
| 255 | + } |
55 | 256 |
|
56 | 257 | return this.runSchematic({
|
57 |
| - collectionName: this.collectionName, |
58 |
| - schematicName: this.schematicName, |
59 |
| - schematicOptions: options['--'], |
| 258 | + collectionName: '@schematics/update', |
| 259 | + schematicName: 'update', |
60 | 260 | dryRun: !!options.dryRun,
|
61 |
| - force: false, |
62 | 261 | showNothingDone: false,
|
63 |
| - additionalOptions: { packageManager }, |
| 262 | + additionalOptions: { |
| 263 | + force: options.force || false, |
| 264 | + packageManager, |
| 265 | + packages: requests.map(p => p.toString()), |
| 266 | + }, |
64 | 267 | });
|
65 | 268 | }
|
66 | 269 | }
|
0 commit comments