Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: separate npm extract functions #24455

Merged
merged 7 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
200 changes: 200 additions & 0 deletions lib/modules/manager/npm/extract/common/dependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import is from '@sindresorhus/is';
import validateNpmPackageName from 'validate-npm-package-name';
import { logger } from '../../../../../logger';
import { regEx } from '../../../../../util/regex';
import { GithubTagsDatasource } from '../../../../datasource/github-tags';
import { NpmDatasource } from '../../../../datasource/npm';
import * as nodeVersioning from '../../../../versioning/node';
import { api, isValid, isVersion } from '../../../../versioning/npm';
import type { PackageDependency } from '../../../types';

const RE_REPOSITORY_GITHUB_SSH_FORMAT = regEx(
/(?:git@)github.com:([^/]+)\/([^/.]+)(?:\.git)?/
);

export function parseDepName(depType: string, key: string): string {
if (depType !== 'resolutions') {
return key;
}

const [, depName] = regEx(/((?:@[^/]+\/)?[^/@]+)$/).exec(key) ?? [];
return depName;
}

export function extractDependency(
depType: string,
depName: string,
input: string
): PackageDependency {
const dep: PackageDependency = {};
if (!validateNpmPackageName(depName).validForOldPackages) {
dep.skipReason = 'invalid-name';
return dep;
}
if (typeof input !== 'string') {
dep.skipReason = 'invalid-value';
return dep;
}
dep.currentValue = input.trim();
if (depType === 'engines' || depType === 'packageManager') {
if (depName === 'node') {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = 'nodejs/node';
dep.versioning = nodeVersioning.id;
} else if (depName === 'yarn') {
dep.datasource = NpmDatasource.id;
dep.commitMessageTopic = 'Yarn';
const major =
isVersion(dep.currentValue) && api.getMajor(dep.currentValue);
if (major && major > 1) {
dep.packageName = '@yarnpkg/cli';
}
} else if (depName === 'npm') {
dep.datasource = NpmDatasource.id;
dep.commitMessageTopic = 'npm';
} else if (depName === 'pnpm') {
dep.datasource = NpmDatasource.id;
dep.commitMessageTopic = 'pnpm';
} else if (depName === 'vscode') {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = 'microsoft/vscode';
} else {
dep.skipReason = 'unknown-engines';
}
if (!isValid(dep.currentValue)) {
dep.skipReason = 'unspecified-version';
}
return dep;
}

// support for volta
if (depType === 'volta') {
if (depName === 'node') {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = 'nodejs/node';
dep.versioning = nodeVersioning.id;
} else if (depName === 'yarn') {
dep.datasource = NpmDatasource.id;
dep.commitMessageTopic = 'Yarn';
const major =
isVersion(dep.currentValue) && api.getMajor(dep.currentValue);
if (major && major > 1) {
dep.packageName = '@yarnpkg/cli';
}
} else if (depName === 'npm') {
dep.datasource = NpmDatasource.id;
} else if (depName === 'pnpm') {
dep.datasource = NpmDatasource.id;
dep.commitMessageTopic = 'pnpm';
} else {
dep.skipReason = 'unknown-volta';
}
if (!isValid(dep.currentValue)) {
dep.skipReason = 'unspecified-version';
}
return dep;
}

if (dep.currentValue.startsWith('npm:')) {
dep.npmPackageAlias = true;
const valSplit = dep.currentValue.replace('npm:', '').split('@');
if (valSplit.length === 2) {
dep.packageName = valSplit[0];
dep.currentValue = valSplit[1];
} else if (valSplit.length === 3) {
dep.packageName = valSplit[0] + '@' + valSplit[1];
dep.currentValue = valSplit[2];
} else {
logger.debug('Invalid npm package alias: ' + dep.currentValue);
}
}
if (dep.currentValue.startsWith('file:')) {
dep.skipReason = 'file';
return dep;
}

if (isValid(dep.currentValue)) {
dep.datasource = NpmDatasource.id;
if (dep.currentValue === '') {
dep.skipReason = 'empty';
}
return dep;
}
const hashSplit = dep.currentValue.split('#');
if (hashSplit.length !== 2) {
dep.skipReason = 'unspecified-version';
return dep;
}
const [depNamePart, depRefPart] = hashSplit;

let githubOwnerRepo: string;
let githubOwner: string;
let githubRepo: string;
const matchUrlSshFormat = RE_REPOSITORY_GITHUB_SSH_FORMAT.exec(depNamePart);
if (matchUrlSshFormat === null) {
githubOwnerRepo = depNamePart
.replace(regEx(/^github:/), '')
.replace(regEx(/^git\+/), '')
.replace(regEx(/^https:\/\/github\.com\//), '')
.replace(regEx(/\.git$/), '');
const githubRepoSplit = githubOwnerRepo.split('/');
if (githubRepoSplit.length !== 2) {
dep.skipReason = 'unspecified-version';
return dep;
}
[githubOwner, githubRepo] = githubRepoSplit;
} else {
githubOwner = matchUrlSshFormat[1];
githubRepo = matchUrlSshFormat[2];
githubOwnerRepo = `${githubOwner}/${githubRepo}`;
}
const githubValidRegex = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/i; // TODO #12872 lookahead
if (
!githubValidRegex.test(githubOwner) ||
!githubValidRegex.test(githubRepo)
) {
dep.skipReason = 'unspecified-version';
return dep;
}
if (isVersion(depRefPart)) {
dep.currentRawValue = dep.currentValue;
dep.currentValue = depRefPart;
dep.datasource = GithubTagsDatasource.id;
dep.packageName = githubOwnerRepo;
dep.pinDigests = false;
} else if (
regEx(/^[0-9a-f]{7}$/).test(depRefPart) ||
regEx(/^[0-9a-f]{40}$/).test(depRefPart)
) {
dep.currentRawValue = dep.currentValue;
dep.currentValue = null;
dep.currentDigest = depRefPart;
dep.datasource = GithubTagsDatasource.id;
dep.packageName = githubOwnerRepo;
} else {
dep.skipReason = 'unversioned-reference';
return dep;
}
dep.sourceUrl = `https://github.com/${githubOwnerRepo}`;
dep.gitRef = true;
return dep;
}

export function getExtractedConstraints(
deps: PackageDependency[]
): Record<string, string> {
const extractedConstraints: Record<string, string> = {};
const constraints = ['node', 'yarn', 'npm', 'pnpm', 'vscode'];
for (const dep of deps) {
if (
!dep.skipReason &&
(dep.depType === 'engines' || dep.depType === 'packageManager') &&
dep.depName &&
constraints.includes(dep.depName) &&
is.string(dep.currentValue)
) {
extractedConstraints[dep.depName] = dep.currentValue;
}
}
return extractedConstraints;
}
11 changes: 11 additions & 0 deletions lib/modules/manager/npm/extract/common/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { PackageDependency } from '../../../types';
import type { NpmManagerData } from '../../types';

export function setNodeCommitTopic(
dep: PackageDependency<NpmManagerData>
): void {
// This is a special case for Node.js to group it together with other managers
if (dep.depName === 'node') {
dep.commitMessageTopic = 'Node.js';
}
}
46 changes: 46 additions & 0 deletions lib/modules/manager/npm/extract/common/overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import is from '@sindresorhus/is';
import type { PackageDependency } from '../../../types';
import type { NpmManagerData } from '../../types';
import { extractDependency } from './dependency';
import { setNodeCommitTopic } from './node';

/**
* Used when there is a json object as a value in overrides block.
* @param parents
* @param child
* @returns PackageDependency array
*/
export function extractOverrideDepsRec(
parents: string[],
child: NpmManagerData
): PackageDependency[] {
const deps: PackageDependency[] = [];
if (!child || is.emptyObject(child)) {
return deps;
}
for (const [overrideName, versionValue] of Object.entries(child)) {
if (is.string(versionValue)) {
// special handling for "." override depenency name
// "." means the constraint is applied to the parent dep
const currDepName =
overrideName === '.' ? parents[parents.length - 1] : overrideName;
const dep: PackageDependency<NpmManagerData> = {
depName: currDepName,
depType: 'overrides',
managerData: { parents: parents.slice() }, // set parents for dependency
};
setNodeCommitTopic(dep);
deps.push({
...dep,
...extractDependency('overrides', currDepName, versionValue),
});
} else {
// versionValue is an object, run recursively.
parents.push(overrideName);
const depsOfObject = extractOverrideDepsRec(parents, versionValue);
deps.push(...depsOfObject);
}
}
parents.pop();
return deps;
}
116 changes: 116 additions & 0 deletions lib/modules/manager/npm/extract/common/package-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import is from '@sindresorhus/is';
import { CONFIG_VALIDATION } from '../../../../../constants/error-messages';
import { logger } from '../../../../../logger';
import { regEx } from '../../../../../util/regex';
import type { PackageDependency, PackageFileContent } from '../../../types';
import type { NpmManagerData } from '../../types';
import type { NpmPackage, NpmPackageDependency } from '../types';
import {
extractDependency,
getExtractedConstraints,
parseDepName,
} from './dependency';
import { setNodeCommitTopic } from './node';
import { extractOverrideDepsRec } from './overrides';

export function extractPackageJson(
packageJson: NpmPackage,
packageFile: string
): PackageFileContent<NpmManagerData> | null {
logger.trace(`npm.extractPackageJson(${packageFile})`);
const deps: PackageDependency[] = [];

if (packageJson._id && packageJson._args && packageJson._from) {
logger.debug({ packageFile }, 'Ignoring vendorised package.json');
return null;
}
if (packageFile !== 'package.json' && packageJson.renovate) {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = packageFile;
error.validationError =
'Nested package.json must not contain Renovate configuration. Please use `packageRules` with `matchFileNames` in your main config instead.';
throw error;
}
const packageJsonName = packageJson.name;
logger.debug(
`npm file ${packageFile} has name ${JSON.stringify(packageJsonName)}`
);
const packageFileVersion = packageJson.version;

const depTypes = {
dependencies: 'dependency',
devDependencies: 'devDependency',
optionalDependencies: 'optionalDependency',
peerDependencies: 'peerDependency',
engines: 'engine',
volta: 'volta',
resolutions: 'resolutions',
packageManager: 'packageManager',
overrides: 'overrides',
};

for (const depType of Object.keys(depTypes) as (keyof typeof depTypes)[]) {
let dependencies = packageJson[depType];
if (dependencies) {
try {
if (depType === 'packageManager') {
const match = regEx('^(?<name>.+)@(?<range>.+)$').exec(
dependencies as string
);
// istanbul ignore next
if (!match?.groups) {
break;
}
dependencies = { [match.groups.name]: match.groups.range };
}
for (const [key, val] of Object.entries(
dependencies as NpmPackageDependency
)) {
const depName = parseDepName(depType, key);
let dep: PackageDependency = {
depType,
depName,
};
if (depName !== key) {
dep.managerData = { key };
}
if (depType === 'overrides' && !is.string(val)) {
// TODO: fix type #22198
deps.push(
...extractOverrideDepsRec(
[depName],
val as unknown as NpmManagerData
)
);
} else {
// TODO: fix type #22198
dep = { ...dep, ...extractDependency(depType, depName, val!) };
setNodeCommitTopic(dep);
dep.prettyDepType = depTypes[depType];
deps.push(dep);
}
}
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ fileName: packageFile, depType, err },
'Error parsing package.json'
);
return null;
}
}
}

const extractedConstraints = getExtractedConstraints(deps);

return {
deps,
extractedConstraints,
packageFileVersion,
managerData: {
packageJsonName,
hasPackageManager: is.nonEmptyStringAndNotWhitespace(
packageJson.packageManager
),
},
};
}
3 changes: 2 additions & 1 deletion lib/modules/manager/npm/extract/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Fixtures } from '../../../../../test/fixtures';
import { fs } from '../../../../../test/util';
import { logger } from '../../../../logger';
import type { ExtractConfig } from '../../types';
import { postExtract } from './post';
import * as npmExtract from '.';

jest.mock('../../../../util/fs');
Expand Down Expand Up @@ -952,7 +953,7 @@ describe('modules/manager/npm/extract/index', () => {

describe('.postExtract()', () => {
it('runs', async () => {
await expect(npmExtract.postExtract([])).resolves.not.toThrow();
await expect(postExtract([])).resolves.not.toThrow();
});
});
});