Skip to content

Commit

Permalink
feat(core): special case the github npm registry when migrating
Browse files Browse the repository at this point in the history
ISSUES CLOSED: nrwl#10223
  • Loading branch information
gioragutt committed May 16, 2022
1 parent 43d5e62 commit 3876f09
Showing 1 changed file with 74 additions and 46 deletions.
120 changes: 74 additions & 46 deletions packages/nx/src/command-line/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../utils/package-json';
import {
createTempNpmDirectory,
detectPackageManager,
getPackageManagerCommand,
packageRegistryPack,
packageRegistryView,
Expand Down Expand Up @@ -487,72 +488,99 @@ function versions(root: string, from: Record<string, string>) {

// testing-fetch-start
function createFetcher() {
const migrationsCache: Record<
string,
Promise<ResolvedMigrationConfiguration>
> = {};
const resolvedVersionCache: Record<string, Promise<string>> = {};
const migrationsCache: Record<string, ResolvedMigrationConfiguration> = {};
const resolvedVersionCache: Record<string, string> = {};

function fetchMigrations(
packageName,
packageVersion,
setCache: (packageName: string, packageVersion: string) => void
): Promise<ResolvedMigrationConfiguration> {
const cacheKey = packageName + '-' + packageVersion;
return Promise.resolve(resolvedVersionCache[cacheKey])
.then((cachedResolvedVersion) => {
if (cachedResolvedVersion) {
return cachedResolvedVersion;
}
function getMigrationCache(packageName: string, packageVersion: string) {
return migrationsCache[`${packageName}-${packageVersion}`];
}

resolvedVersionCache[cacheKey] = resolvePackageVersionUsingRegistry(
packageName,
packageVersion
);
return resolvedVersionCache[cacheKey];
})
.then((resolvedVersion) => {
if (
resolvedVersion !== packageVersion &&
migrationsCache[`${packageName}-${resolvedVersion}`]
) {
return migrationsCache[`${packageName}-${resolvedVersion}`];
}
setCache(packageName, resolvedVersion);
return getPackageMigrationsUsingRegistry(packageName, resolvedVersion);
})
.catch(() => {
logger.info(`Fetching ${packageName}@${packageVersion}`);
function setMigrationCache(
packageName: string,
packageVersion: string,
migrations: ResolvedMigrationConfiguration
) {
migrationsCache[`${packageName}-${packageVersion}`] = migrations;
}

const packageRegistryCache: Record<string, string> = {};

const pm = detectPackageManager();

return getPackageMigrationsUsingInstall(packageName, packageVersion);
});
async function getRegistryOfPackage(packageName: string) {
const configKeyPrefix = packageName.startsWith('@')
? `${packageName.substring(0, packageName.indexOf('/'))}:`
: '';

if (!packageRegistryCache[packageName]) {
const { stdout } = await execAsync(
`${pm} config get ${configKeyPrefix}registry`
);

packageRegistryCache[configKeyPrefix] = stdout.trim();
}
return packageRegistryCache[configKeyPrefix];
}

return function nxMigrateFetcher(
async function fetchMigrations(
packageName: string,
packageVersion: string
): Promise<ResolvedMigrationConfiguration> {
if (migrationsCache[`${packageName}-${packageVersion}`]) {
return migrationsCache[`${packageName}-${packageVersion}`];
if (
(await getRegistryOfPackage(packageName)) ===
'https://npm.pkg.github.com/'
) {
/**
* The github npm package registry does not yet support custom fields,
* meaning that `npm view` will not return the nx migration information,
* so we have to fallback to getting the information with a full install.
*/
logger.info(`Fetching ${packageName}@${packageVersion}`);
return getPackageMigrationsUsingInstall(packageName, packageVersion);
}

try {
const cacheKey = `${packageName}-${packageVersion}`;

resolvedVersionCache[cacheKey] ??=
await resolvePackageVersionUsingRegistry(packageName, packageVersion);

const resolvedVersion = resolvedVersionCache[cacheKey];

if (
resolvedVersion !== packageVersion &&
getMigrationCache(packageName, resolvedVersion)
) {
return getMigrationCache(packageName, resolvedVersion);
}

return getPackageMigrationsUsingRegistry(packageName, resolvedVersion);
} catch {
logger.info(`Fetching ${packageName}@${packageVersion}`);
return getPackageMigrationsUsingInstall(packageName, packageVersion);
}
}

let resolvedVersion: string = packageVersion;
let migrations: Promise<ResolvedMigrationConfiguration>;
function setCache(packageName: string, packageVersion: string) {
migrationsCache[packageName + '-' + packageVersion] = migrations;
return async function nxMigrateFetcher(
packageName: string,
packageVersion: string
): Promise<ResolvedMigrationConfiguration> {
if (getMigrationCache(packageName, packageVersion)) {
return getMigrationCache(packageName, packageVersion);
}
migrations = fetchMigrations(packageName, packageVersion, setCache).then(

let migrations = await fetchMigrations(packageName, packageVersion).then(
(result) => {
if (result.schematics) {
result.generators = result.schematics;
delete result.schematics;
}
resolvedVersion = result.version;
return result;
}
);

setCache(packageName, packageVersion);
setMigrationCache(packageName, packageVersion, migrations);
setMigrationCache(packageName, migrations.version, migrations);

return migrations;
};
Expand Down

0 comments on commit 3876f09

Please sign in to comment.