Skip to content

Commit

Permalink
refactor: further implement
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Feb 8, 2025
1 parent c1e342f commit 91fd1b1
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ exports[`modules/datasource/artifactory/index getReleases parses real data (file
"registryUrl": "https://jfrog.company.com/artifactory",
"releases": [
{
"releaseTimestamp": "2021-07-21T20:08:00.000Z",
"releaseTimestamp": "2021-07-21T19:08:00.000Z",
"version": "1.0.0",
},
{
"releaseTimestamp": "2021-08-23T20:03:00.000Z",
"releaseTimestamp": "2021-08-23T19:03:00.000Z",
"version": "1.0.1",
},
{
"releaseTimestamp": "2021-07-21T20:09:00.000Z",
"releaseTimestamp": "2021-07-21T19:09:00.000Z",
"version": "1.0.2",
},
{
Expand All @@ -29,15 +29,15 @@ exports[`modules/datasource/artifactory/index getReleases parses real data (fold
"registryUrl": "https://jfrog.company.com/artifactory",
"releases": [
{
"releaseTimestamp": "2021-07-21T20:08:00.000Z",
"releaseTimestamp": "2021-07-21T19:08:00.000Z",
"version": "1.0.0",
},
{
"releaseTimestamp": "2021-08-23T20:03:00.000Z",
"releaseTimestamp": "2021-08-23T19:03:00.000Z",
"version": "1.0.1",
},
{
"releaseTimestamp": "2021-07-21T20:09:00.000Z",
"releaseTimestamp": "2021-07-21T19:09:00.000Z",
"version": "1.0.2",
},
{
Expand All @@ -53,17 +53,17 @@ exports[`modules/datasource/artifactory/index getReleases parses real data (merg
"releases": [
{
"registryUrl": "https://jfrog.company.com/artifactory",
"releaseTimestamp": "2021-07-21T20:08:00.000Z",
"releaseTimestamp": "2021-07-21T19:08:00.000Z",
"version": "1.0.0",
},
{
"registryUrl": "https://jfrog.company.com/artifactory",
"releaseTimestamp": "2021-08-23T20:03:00.000Z",
"releaseTimestamp": "2021-08-23T19:03:00.000Z",
"version": "1.0.1",
},
{
"registryUrl": "https://jfrog.company.com/artifactory",
"releaseTimestamp": "2021-07-21T20:09:00.000Z",
"releaseTimestamp": "2021-07-21T19:09:00.000Z",
"version": "1.0.2",
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ a: b
`;

exports[`modules/manager/npm/post-update/index updateYarnBinary() should return .yarnrc.yml content if it has been overwritten 2`] = `
[
{
"contents": "yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
",
"path": "path/to/lockfile/.yarnrc.yml",
"type": "addition",
},
{
{
"path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs": {
"path": "path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs",
"type": "deletion",
},
{
"path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs": {
"contents": "new yarn
",
"isExecutable": true,
"path": "path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs",
"type": "addition",
},
]
`;

exports[`modules/manager/npm/post-update/index updateYarnBinary() should update the Yarn binary 1`] = `
[
{
"path/to/lockfile/.yarnrc.yml": {
"contents": "yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
",
"path": "path/to/lockfile/.yarnrc.yml",
"type": "addition",
},
{
}
`;

exports[`modules/manager/npm/post-update/index updateYarnBinary() should update the Yarn binary 1`] = `
{
"path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs": {
"path": "path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs",
"type": "deletion",
},
{
"path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs": {
"contents": "new yarn
",
"isExecutable": true,
"path": "path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs",
"type": "addition",
},
]
"path/to/lockfile/.yarnrc.yml": {
"contents": "yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
",
"path": "path/to/lockfile/.yarnrc.yml",
"type": "addition",
},
}
`;
16 changes: 8 additions & 8 deletions lib/modules/manager/npm/post-update/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('modules/manager/npm/post-update/index', () => {
git.getFile.mockResolvedValueOnce(oldYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarn);
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand All @@ -307,7 +307,7 @@ describe('modules/manager/npm/post-update/index', () => {
it('should return .yarnrc.yml content if it has been overwritten', async () => {
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarn);
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const existingYarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand All @@ -321,7 +321,7 @@ describe('modules/manager/npm/post-update/index', () => {
it("should not update the Yarn binary if the old .yarnrc.yml doesn't exist", async () => {
git.getFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand All @@ -334,7 +334,7 @@ describe('modules/manager/npm/post-update/index', () => {
it("should not update the Yarn binary if the new .yarnrc.yml doesn't exist", async () => {
git.getFile.mockResolvedValueOnce(oldYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(null as never);
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand All @@ -346,7 +346,7 @@ describe('modules/manager/npm/post-update/index', () => {

it("should return existing .yarnrc.yml if the new one doesn't exist", async () => {
fs.readLocalFile.mockResolvedValueOnce(null as never);
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const existingYarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand All @@ -360,7 +360,7 @@ describe('modules/manager/npm/post-update/index', () => {
git.getFile.mockResolvedValueOnce('');
fs.readLocalFile.mockResolvedValueOnce('');
fs.readLocalFile.mockResolvedValueOnce('');
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
Expand Down Expand Up @@ -458,7 +458,7 @@ describe('modules/manager/npm/post-update/index', () => {
},
additionalFiles,
)
).updatedArtifacts.find((a) => a.path === 'package-lock.json'),
).updatedArtifacts['package-lock.json'],
).toBeUndefined();
});

Expand Down Expand Up @@ -488,7 +488,7 @@ describe('modules/manager/npm/post-update/index', () => {
},
additionalFiles,
)
).updatedArtifacts.find((a) => a.path === 'package-lock.json'),
).updatedArtifacts['package-lock.json'],
).toBeUndefined();
});

Expand Down
56 changes: 27 additions & 29 deletions lib/modules/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export async function writeUpdatedPackageFiles(
// istanbul ignore next
async function updateYarnOffline(
lockFileDir: string,
updatedArtifacts: FileChange[],
updatedArtifacts: Record<string, FileChange>,
): Promise<void> {
try {
const resolvedPaths: string[] = [];
Expand Down Expand Up @@ -289,16 +289,16 @@ async function updateYarnOffline(
const status = await getRepoStatus();
for (const f of status.modified.concat(status.not_added)) {
if (resolvedPaths.some((p) => f.startsWith(p))) {
updatedArtifacts.push({
updatedArtifacts[f] = {
type: 'addition',
path: f,
contents: await readLocalFile(f),
});
};
}
}
for (const f of status.deleted || []) {
if (resolvedPaths.some((p) => f.startsWith(p))) {
updatedArtifacts.push({ type: 'deletion', path: f });
updatedArtifacts[f] = { type: 'deletion', path: f };
}
}
}
Expand All @@ -311,7 +311,7 @@ async function updateYarnOffline(
// exported for testing
export async function updateYarnBinary(
lockFileDir: string,
updatedArtifacts: FileChange[],
updatedArtifacts: Record<string, FileChange>,
existingYarnrcYmlContent: string | undefined | null,
): Promise<string | undefined | null> {
let yarnrcYml = existingYarnrcYmlContent;
Expand All @@ -338,23 +338,21 @@ export async function updateYarnBinary(
logger.debug({ oldYarnPath, newYarnPath }, 'Found updated Yarn binary');

yarnrcYml = yarnrcYml.replace(oldYarnPath, newYarnPath);
updatedArtifacts.push(
{
type: 'addition',
path: yarnrcYmlFilename,
contents: yarnrcYml,
},
{
type: 'deletion',
path: oldYarnFullPath,
},
{
type: 'addition',
path: newYarnFullPath,
contents: await readLocalFile(newYarnFullPath, 'utf8'),
isExecutable: true,
},
);
updatedArtifacts[yarnrcYmlFilename] = {
type: 'addition',
path: yarnrcYmlFilename,
contents: yarnrcYml,
};
updatedArtifacts[oldYarnFullPath] = {
type: 'deletion',
path: oldYarnFullPath,
};
updatedArtifacts[newYarnFullPath] = {
type: 'addition',
path: newYarnFullPath,
contents: await readLocalFile(newYarnFullPath, 'utf8'),
isExecutable: true,
};
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'Error updating Yarn binary');
}
Expand All @@ -367,7 +365,7 @@ export async function getAdditionalFiles(
): Promise<WriteExistingFilesResult> {
logger.trace({ config }, 'getAdditionalFiles');
const artifactErrors: ArtifactError[] = [];
const updatedArtifacts: FileChange[] = [];
const updatedArtifacts: Record<string, FileChange> = {};
if (!packageFiles.npm?.length) {
return { artifactErrors, updatedArtifacts };
}
Expand Down Expand Up @@ -460,13 +458,13 @@ export async function getAdditionalFiles(
logger.debug(`${npmLock} hasn't changed`);
} else {
logger.debug(`${npmLock} needs updating`);
updatedArtifacts.push({
updatedArtifacts[npmLock] = {
type: 'addition',
path: npmLock,
// TODO: can this be undefined? (#22198)

contents: res.lockFile.replace(tokenRe, ''),
});
};
}
}
await resetNpmrcContent(lockFileDir, npmrcContent);
Expand Down Expand Up @@ -547,12 +545,12 @@ export async function getAdditionalFiles(
logger.debug("yarn.lock hasn't changed");
} else {
logger.debug('yarn.lock needs updating');
updatedArtifacts.push({
updatedArtifacts[lockFileName] = {
type: 'addition',
path: lockFileName,
// TODO #22198
contents: res.lockFile!,
});
};
await updateYarnOffline(lockFileDir, updatedArtifacts);
}

Expand Down Expand Up @@ -618,12 +616,12 @@ export async function getAdditionalFiles(
logger.debug("pnpm-lock.yaml hasn't changed");
} else {
logger.debug('pnpm-lock.yaml needs updating');
updatedArtifacts.push({
updatedArtifacts[pnpmShrinkwrap] = {
type: 'addition',
path: pnpmShrinkwrap,
// TODO: can be undefined? (#22198)
contents: res.lockFile!,
});
};
}
}
await resetNpmrcContent(lockFileDir, npmrcContent);
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/npm/post-update/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ArtifactError {

export interface WriteExistingFilesResult {
artifactErrors: ArtifactError[];
updatedArtifacts: FileChange[];
updatedArtifacts: Record<string, FileChange>;
}

export interface GenerateLockFileResult {
Expand Down
15 changes: 15 additions & 0 deletions lib/util/records.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function filterEntries<V>(
input: Record<string, V>,
filterFunc: (entry: [string, V]) => boolean,
): Record<string, V> {
const result: Record<string, V> = {};

for (const entry of Object.entries(input)) {
if (filterFunc(entry)) {
const [key, value] = entry;
result[key] = value;
}
}

return result;
}
2 changes: 1 addition & 1 deletion lib/workers/repository/process/libyear.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('workers/repository/process/libyear', () => {
npm: 1,
regex: 1,
},
// eslint-disable-next-line no-loss-of-precision

totalLibYears: 2,
totalDepsCount: 2,
outdatedDepsCount: 2,
Expand Down
Loading

0 comments on commit 91fd1b1

Please sign in to comment.