Skip to content

Commit

Permalink
refactor: nested npm/update
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 25, 2021
1 parent 256fe01 commit 7a0ade0
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 136 deletions.
15 changes: 0 additions & 15 deletions lib/manager/npm/__snapshots__/update.spec.ts.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/dependency .updateDependency(fileContent, depType, depName, newValue) replaces a github dependency value 1`] = `"{\\"dependencies\\":{\\"gulp\\":\\"gulpjs/gulp#v4.0.0\\"}}"`;

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/dependency .updateDependency(fileContent, depType, depName, newValue) replaces a github fully specified version 1`] = `"{\\"dependencies\\":{\\"n\\":\\"git+https://github.com/owner/n#v1.1.0\\"}}"`;

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/dependency .updateDependency(fileContent, depType, depName, newValue) replaces a github short hash 1`] = `"{\\"dependencies\\":{\\"gulp\\":\\"gulpjs/gulp#0000000\\"}}"`;

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/dependency .updateDependency(fileContent, depType, depName, newValue) replaces a npm package alias 1`] = `"{\\"dependencies\\":{\\"hapi\\":\\"npm:@hapi/hapi@18.3.1\\"}}"`;
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import fs from 'fs';
import upath from 'upath';

import * as npmUpdater from './update';
import * as npmUpdater from '.';

function readFixture(fixture: string) {
return fs.readFileSync(
upath.resolve(__dirname, `./__fixtures__/${fixture}`),
upath.resolve(__dirname, `../../__fixtures__/${fixture}`),
'utf8'
);
}

const input01Content = readFixture('inputs/01.json');
const input01GlobContent = readFixture('inputs/01-glob.json');

describe('workers/branch/package-json', () => {
describe(__dirname, () => {
describe('.updateDependency(fileContent, depType, depName, newValue)', () => {
it('replaces a dependency value', () => {
const upgrade = {
Expand Down Expand Up @@ -200,68 +200,4 @@ describe('workers/branch/package-json', () => {
expect(testContent).toBeNull();
});
});
describe('.bumpPackageVersion()', () => {
const content = JSON.stringify({
name: 'some-package',
version: '0.0.2',
dependencies: { chalk: '2.4.2' },
});
it('mirrors', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'mirror:chalk'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('aborts mirror', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'mirror:a'
);
expect(bumpedContent).toEqual(content);
});
it('increments', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'patch'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('no ops', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.1',
'patch'
);
expect(bumpedContent).toEqual(content);
});
it('updates', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.1',
'minor'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('returns content if bumping errors', async () => {
jest.mock('semver', () => ({
inc: () => {
throw new Error('semver inc');
},
}));
const npmUpdater1 = await import('./update');
const { bumpedContent } = npmUpdater1.bumpPackageVersion(
content,
'0.0.2',
true as any
);
expect(bumpedContent).toEqual(content);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
import equal from 'fast-deep-equal';
import { ReleaseType, inc } from 'semver';
import { logger } from '../../logger';
import { matchAt, replaceAt } from '../../util/string';
import { BumpPackageVersionResult, UpdateDependencyConfig } from '../common';

export function bumpPackageVersion(
content: string,
currentValue: string,
bumpVersion: ReleaseType | string
): BumpPackageVersionResult {
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump package.json version'
);
let newPjVersion: string;
let bumpedContent = content;
try {
if (bumpVersion.startsWith('mirror:')) {
const mirrorPackage = bumpVersion.replace('mirror:', '');
const parsedContent = JSON.parse(content);
newPjVersion =
(parsedContent.dependencies || {})[mirrorPackage] ||
(parsedContent.devDependencies || {})[mirrorPackage] ||
(parsedContent.optionalDependencies || {})[mirrorPackage] ||
(parsedContent.peerDependencies || {})[mirrorPackage];
if (!newPjVersion) {
logger.warn('bumpVersion mirror package not found: ' + mirrorPackage);
return { bumpedContent };
}
} else {
newPjVersion = inc(currentValue, bumpVersion as ReleaseType);
}
logger.debug({ newPjVersion });
bumpedContent = content.replace(
/("version":\s*")[^"]*/,
`$1${newPjVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped package.json version');
}
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
}
return { bumpedContent };
}
import { logger } from '../../../../logger';
import { matchAt, replaceAt } from '../../../../util/string';
import { UpdateDependencyConfig } from '../../../common';

export function updateDependency({
fileContent,
Expand Down
2 changes: 2 additions & 0 deletions lib/manager/npm/update/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { bumpPackageVersion } from './package-version';
export { updateDependency } from './dependency';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/package-version .bumpPackageVersion() increments 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.0.3\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/package-version .bumpPackageVersion() mirrors 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"2.4.2\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;

exports[`/Users/rhys/src/renovatebot/renovate/lib/manager/npm/update/package-version .bumpPackageVersion() updates 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.1.0\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`;
68 changes: 68 additions & 0 deletions lib/manager/npm/update/package-version/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as npmUpdater from '.';

describe(__dirname, () => {
describe('.bumpPackageVersion()', () => {
const content = JSON.stringify({
name: 'some-package',
version: '0.0.2',
dependencies: { chalk: '2.4.2' },
});
it('mirrors', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'mirror:chalk'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('aborts mirror', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'mirror:a'
);
expect(bumpedContent).toEqual(content);
});
it('increments', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.2',
'patch'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('no ops', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.1',
'patch'
);
expect(bumpedContent).toEqual(content);
});
it('updates', () => {
const { bumpedContent } = npmUpdater.bumpPackageVersion(
content,
'0.0.1',
'minor'
);
expect(bumpedContent).toMatchSnapshot();
expect(bumpedContent).not.toEqual(content);
});
it('returns content if bumping errors', async () => {
jest.mock('semver', () => ({
inc: () => {
throw new Error('semver inc');
},
}));
const npmUpdater1 = await import('.');
const { bumpedContent } = npmUpdater1.bumpPackageVersion(
content,
'0.0.2',
true as any
);
expect(bumpedContent).toEqual(content);
});
});
});
53 changes: 53 additions & 0 deletions lib/manager/npm/update/package-version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ReleaseType, inc } from 'semver';
import { logger } from '../../../../logger';
import { BumpPackageVersionResult } from '../../../common';

export function bumpPackageVersion(
content: string,
currentValue: string,
bumpVersion: ReleaseType | string
): BumpPackageVersionResult {
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump package.json version'
);
let newPjVersion: string;
let bumpedContent = content;
try {
if (bumpVersion.startsWith('mirror:')) {
const mirrorPackage = bumpVersion.replace('mirror:', '');
const parsedContent = JSON.parse(content);
newPjVersion =
(parsedContent.dependencies || {})[mirrorPackage] ||
(parsedContent.devDependencies || {})[mirrorPackage] ||
(parsedContent.optionalDependencies || {})[mirrorPackage] ||
(parsedContent.peerDependencies || {})[mirrorPackage];
if (!newPjVersion) {
logger.warn('bumpVersion mirror package not found: ' + mirrorPackage);
return { bumpedContent };
}
} else {
newPjVersion = inc(currentValue, bumpVersion as ReleaseType);
}
logger.debug({ newPjVersion });
bumpedContent = content.replace(
/("version":\s*")[^"]*/,
`$1${newPjVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped package.json version');
}
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
}
return { bumpedContent };
}

0 comments on commit 7a0ade0

Please sign in to comment.