Skip to content

Commit 48e7075

Browse files
clydinpull[bot]
authored andcommitted
refactor(@schematics/update): remove direct rxjs dependency
1 parent a9bf0e5 commit 48e7075

File tree

6 files changed

+90
-111
lines changed

6 files changed

+90
-111
lines changed

packages/schematics/update/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ ts_library(
3838
"@npm//@types/npm-package-arg",
3939
"@npm//@types/semver",
4040
"@npm//npm-package-arg",
41-
"@npm//rxjs",
4241
"@npm//semver",
4342
],
4443
)

packages/schematics/update/migrate/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { virtualFs } from '@angular-devkit/core';
99
import { HostTree } from '@angular-devkit/schematics';
1010
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
11-
import { map } from 'rxjs/operators';
11+
import { map } from 'rxjs/operators'; // tslint:disable-line: no-implicit-dependencies
1212
import { _coerceVersionNumber } from './index';
1313

1414

packages/schematics/update/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"ini": "1.3.5",
1818
"npm-package-arg": "^8.0.0",
1919
"pacote": "9.5.12",
20-
"rxjs": "6.6.2",
2120
"semver": "7.3.2",
2221
"semver-intersect": "1.4.0"
2322
}

packages/schematics/update/update/index.ts

Lines changed: 81 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import {
1515
} from '@angular-devkit/schematics';
1616
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
1717
import * as npa from 'npm-package-arg';
18-
import { Observable, from as observableFrom, of } from 'rxjs';
19-
import { map, mergeMap, reduce, switchMap } from 'rxjs/operators';
2018
import * as semver from 'semver';
2119
import { getNpmPackageJson } from './npm';
2220
import { NpmRepositoryPackageJson } from './npm-package-json';
@@ -240,7 +238,7 @@ function _performUpdate(
240238
logger: logging.LoggerApi,
241239
migrateOnly: boolean,
242240
migrateExternal: boolean,
243-
): Observable<void> {
241+
): void {
244242
const packageJsonContent = tree.read('/package.json');
245243
if (!packageJsonContent) {
246244
throw new SchematicsException('Could not find a package.json. Are you in a Node project?');
@@ -347,8 +345,6 @@ function _performUpdate(
347345
(global as any).externalMigrations = externalMigrations;
348346
}
349347
}
350-
351-
return of<void>(undefined);
352348
}
353349

354350
function _migrateOnly(
@@ -358,12 +354,12 @@ function _migrateOnly(
358354
to?: string,
359355
) {
360356
if (!info) {
361-
return of<void>();
357+
return;
362358
}
363359

364360
const target = info.installed;
365361
if (!target || !target.updateMetadata.migrations) {
366-
return of<void>(undefined);
362+
return;
367363
}
368364

369365
const collection = (
@@ -379,8 +375,6 @@ function _migrateOnly(
379375
to: to || target.version,
380376
}),
381377
);
382-
383-
return of<void>(undefined);
384378
}
385379

386380
function _getUpdateMetadata(
@@ -508,7 +502,7 @@ function _usageMessage(
508502
if (packagesToUpdate.length == 0) {
509503
logger.info('We analyzed your package.json and everything seems to be in order. Good work!');
510504

511-
return of<void>(undefined);
505+
return;
512506
}
513507

514508
logger.info(
@@ -536,7 +530,7 @@ function _usageMessage(
536530
logger.info(' ' + fields.map((x, i) => x.padEnd(pads[i])).join(''));
537531
});
538532

539-
return of<void>(undefined);
533+
return;
540534
}
541535

542536

@@ -860,7 +854,7 @@ export default function(options: UpdateSchema): Rule {
860854
options.to = _formatVersion(options.to);
861855
const usingYarn = options.packageManager === 'yarn';
862856

863-
return (tree: Tree, context: SchematicContext) => {
857+
return async (tree: Tree, context: SchematicContext) => {
864858
const logger = context.logger;
865859
const npmDeps = new Map(_getAllDependencies(tree).filter(([name, specifier]) => {
866860
try {
@@ -874,95 +868,87 @@ export default function(options: UpdateSchema): Rule {
874868
}));
875869
const packages = _buildPackageList(options, npmDeps, logger);
876870

877-
return observableFrom(npmDeps.keys()).pipe(
878-
// Grab all package.json from the npm repository. This requires a lot of HTTP calls so we
879-
// try to parallelize as many as possible.
880-
mergeMap(depName => getNpmPackageJson(
881-
depName,
882-
logger,
883-
{ registryUrl: options.registry, usingYarn, verbose: options.verbose },
884-
)),
885-
886-
// Build a map of all dependencies and their packageJson.
887-
reduce<Partial<NpmRepositoryPackageJson>, Map<string, NpmRepositoryPackageJson>>(
888-
(acc, npmPackageJson) => {
889-
// If the package was not found on the registry. It could be private, so we will just
890-
// ignore. If the package was part of the list, we will error out, but will simply ignore
891-
// if it's either not requested (so just part of package.json. silently) or if it's a
892-
// `--all` situation. There is an edge case here where a public package peer depends on a
893-
// private one, but it's rare enough.
894-
if (!npmPackageJson.name) {
895-
if (npmPackageJson.requestedName && packages.has(npmPackageJson.requestedName)) {
896-
if (options.all) {
897-
logger.warn(`Package ${JSON.stringify(npmPackageJson.requestedName)} was not `
898-
+ 'found on the registry. Skipping.');
899-
} else {
900-
throw new SchematicsException(
901-
`Package ${JSON.stringify(npmPackageJson.requestedName)} was not found on the `
902-
+ 'registry. Cannot continue as this may be an error.');
903-
}
871+
// Grab all package.json from the npm repository. This requires a lot of HTTP calls so we
872+
// try to parallelize as many as possible.
873+
const allPackageMetadata = await Promise.all(Array.from(npmDeps.keys()).map(depName => getNpmPackageJson(
874+
depName,
875+
logger,
876+
{ registryUrl: options.registry, usingYarn, verbose: options.verbose },
877+
)));
878+
879+
// Build a map of all dependencies and their packageJson.
880+
const npmPackageJsonMap = allPackageMetadata.reduce(
881+
(acc, npmPackageJson) => {
882+
// If the package was not found on the registry. It could be private, so we will just
883+
// ignore. If the package was part of the list, we will error out, but will simply ignore
884+
// if it's either not requested (so just part of package.json. silently) or if it's a
885+
// `--all` situation. There is an edge case here where a public package peer depends on a
886+
// private one, but it's rare enough.
887+
if (!npmPackageJson.name) {
888+
if (npmPackageJson.requestedName && packages.has(npmPackageJson.requestedName)) {
889+
if (options.all) {
890+
logger.warn(`Package ${JSON.stringify(npmPackageJson.requestedName)} was not `
891+
+ 'found on the registry. Skipping.');
892+
} else {
893+
throw new SchematicsException(
894+
`Package ${JSON.stringify(npmPackageJson.requestedName)} was not found on the `
895+
+ 'registry. Cannot continue as this may be an error.');
904896
}
905-
} else {
906-
// If a name is present, it is assumed to be fully populated
907-
acc.set(npmPackageJson.name, npmPackageJson as NpmRepositoryPackageJson);
908897
}
898+
} else {
899+
// If a name is present, it is assumed to be fully populated
900+
acc.set(npmPackageJson.name, npmPackageJson as NpmRepositoryPackageJson);
901+
}
909902

910-
return acc;
911-
},
912-
new Map<string, NpmRepositoryPackageJson>(),
913-
),
914-
915-
map(npmPackageJsonMap => {
916-
// Augment the command line package list with packageGroups and forward peer dependencies.
917-
// Each added package may uncover new package groups and peer dependencies, so we must
918-
// repeat this process until the package list stabilizes.
919-
let lastPackagesSize;
920-
do {
921-
lastPackagesSize = packages.size;
922-
npmPackageJsonMap.forEach((npmPackageJson) => {
923-
_addPackageGroup(tree, packages, npmDeps, npmPackageJson, logger);
924-
_addPeerDependencies(tree, packages, npmDeps, npmPackageJson, npmPackageJsonMap, logger);
925-
});
926-
} while (packages.size > lastPackagesSize);
927-
928-
// Build the PackageInfo for each module.
929-
const packageInfoMap = new Map<string, PackageInfo>();
930-
npmPackageJsonMap.forEach((npmPackageJson) => {
931-
packageInfoMap.set(
932-
npmPackageJson.name,
933-
_buildPackageInfo(tree, packages, npmDeps, npmPackageJson, logger),
934-
);
935-
});
903+
return acc;
904+
},
905+
new Map<string, NpmRepositoryPackageJson>(),
906+
);
936907

937-
return packageInfoMap;
938-
}),
939-
940-
switchMap(infoMap => {
941-
// Now that we have all the information, check the flags.
942-
if (packages.size > 0) {
943-
if (options.migrateOnly && options.from && options.packages) {
944-
return _migrateOnly(
945-
infoMap.get(options.packages[0]),
946-
context,
947-
options.from,
948-
options.to,
949-
);
950-
}
908+
// Augment the command line package list with packageGroups and forward peer dependencies.
909+
// Each added package may uncover new package groups and peer dependencies, so we must
910+
// repeat this process until the package list stabilizes.
911+
let lastPackagesSize;
912+
do {
913+
lastPackagesSize = packages.size;
914+
npmPackageJsonMap.forEach((npmPackageJson) => {
915+
_addPackageGroup(tree, packages, npmDeps, npmPackageJson, logger);
916+
_addPeerDependencies(tree, packages, npmDeps, npmPackageJson, npmPackageJsonMap, logger);
917+
});
918+
} while (packages.size > lastPackagesSize);
919+
920+
// Build the PackageInfo for each module.
921+
const packageInfoMap = new Map<string, PackageInfo>();
922+
npmPackageJsonMap.forEach((npmPackageJson) => {
923+
packageInfoMap.set(
924+
npmPackageJson.name,
925+
_buildPackageInfo(tree, packages, npmDeps, npmPackageJson, logger),
926+
);
927+
});
951928

952-
const sublog = new logging.LevelCapLogger(
953-
'validation',
954-
logger.createChild(''),
955-
'warn',
956-
);
957-
_validateUpdatePackages(infoMap, !!options.force, !!options.next, sublog);
929+
// Now that we have all the information, check the flags.
930+
if (packages.size > 0) {
931+
if (options.migrateOnly && options.from && options.packages) {
932+
_migrateOnly(
933+
packageInfoMap.get(options.packages[0]),
934+
context,
935+
options.from,
936+
options.to,
937+
);
958938

959-
return _performUpdate(tree, context, infoMap, logger, !!options.migrateOnly, !!options.migrateExternal);
960-
} else {
961-
return _usageMessage(options, infoMap, logger);
962-
}
963-
}),
939+
return;
940+
}
964941

965-
switchMap(() => of(tree)),
966-
);
942+
const sublog = new logging.LevelCapLogger(
943+
'validation',
944+
logger.createChild(''),
945+
'warn',
946+
);
947+
_validateUpdatePackages(packageInfoMap, !!options.force, !!options.next, sublog);
948+
949+
_performUpdate(tree, context, packageInfoMap, logger, !!options.migrateOnly, !!options.migrateExternal);
950+
} else {
951+
_usageMessage(options, packageInfoMap, logger);
952+
}
967953
};
968954
}

packages/schematics/update/update/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import { normalize, virtualFs } from '@angular-devkit/core';
1111
import { HostTree, SchematicsException } from '@angular-devkit/schematics';
1212
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
13-
import { EMPTY } from 'rxjs';
14-
import { catchError, map } from 'rxjs/operators';
13+
import { EMPTY } from 'rxjs'; // tslint:disable-line: no-implicit-dependencies
14+
import { catchError, map } from 'rxjs/operators'; // tslint:disable-line: no-implicit-dependencies
1515
import * as semver from 'semver';
1616
import { angularMajorCompatGuarantee } from './index';
1717

packages/schematics/update/update/npm.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import { logging } from '@angular-devkit/core';
99
import { existsSync, readFileSync } from 'fs';
1010
import { homedir } from 'os';
1111
import * as path from 'path';
12-
import { EMPTY, Observable, from } from 'rxjs';
13-
import { catchError, shareReplay } from 'rxjs/operators';
1412
import { NpmRepositoryPackageJson } from './npm-package-json';
1513

1614
const ini = require('ini');
1715
const lockfile = require('@yarnpkg/lockfile');
1816
const pacote = require('pacote');
1917

20-
const npmPackageJsonCache = new Map<string, Observable<NpmRepositoryPackageJson>>();
18+
const npmPackageJsonCache = new Map<string, Promise<Partial<NpmRepositoryPackageJson>>>();
2119
let npmrc: { [key: string]: string };
2220

2321

@@ -108,7 +106,7 @@ export function getNpmPackageJson(
108106
usingYarn?: boolean;
109107
verbose?: boolean;
110108
},
111-
): Observable<Partial<NpmRepositoryPackageJson>> {
109+
): Promise<Partial<NpmRepositoryPackageJson>> {
112110
const cachedResponse = npmPackageJsonCache.get(packageName);
113111
if (cachedResponse) {
114112
return cachedResponse;
@@ -136,14 +134,11 @@ export function getNpmPackageJson(
136134
);
137135

138136
// TODO: find some way to test this
139-
const response = from(resultPromise).pipe(
140-
shareReplay(),
141-
catchError(err => {
142-
logger.warn(err.message || err);
137+
const response = resultPromise.catch((err) => {
138+
logger.warn(err.message || err);
143139

144-
return EMPTY;
145-
}),
146-
);
140+
return { requestedName: packageName };
141+
});
147142
npmPackageJsonCache.set(packageName, response);
148143

149144
return response;

0 commit comments

Comments
 (0)