diff --git a/lib/manager/npm/__snapshots__/update.spec.ts.snap b/lib/manager/npm/__snapshots__/update.spec.ts.snap deleted file mode 100644 index a3b4198523efe405bf2bd8e42bd2b0e2c2583902..0000000000000000000000000000000000000000 --- a/lib/manager/npm/__snapshots__/update.spec.ts.snap +++ /dev/null @@ -1,15 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`workers/branch/package-json .bumpPackageVersion() increments 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.0.3\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`; - -exports[`workers/branch/package-json .bumpPackageVersion() mirrors 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"2.4.2\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`; - -exports[`workers/branch/package-json .bumpPackageVersion() updates 1`] = `"{\\"name\\":\\"some-package\\",\\"version\\":\\"0.1.0\\",\\"dependencies\\":{\\"chalk\\":\\"2.4.2\\"}}"`; - -exports[`workers/branch/package-json .updateDependency(fileContent, depType, depName, newValue) replaces a github dependency value 1`] = `"{\\"dependencies\\":{\\"gulp\\":\\"gulpjs/gulp#v4.0.0\\"}}"`; - -exports[`workers/branch/package-json .updateDependency(fileContent, depType, depName, newValue) replaces a github fully specified version 1`] = `"{\\"dependencies\\":{\\"n\\":\\"git+https://github.com/owner/n#v1.1.0\\"}}"`; - -exports[`workers/branch/package-json .updateDependency(fileContent, depType, depName, newValue) replaces a github short hash 1`] = `"{\\"dependencies\\":{\\"gulp\\":\\"gulpjs/gulp#0000000\\"}}"`; - -exports[`workers/branch/package-json .updateDependency(fileContent, depType, depName, newValue) replaces a npm package alias 1`] = `"{\\"dependencies\\":{\\"hapi\\":\\"npm:@hapi/hapi@18.3.1\\"}}"`; diff --git a/lib/manager/npm/update/dependency/__snapshots__/index.spec.ts.snap b/lib/manager/npm/update/dependency/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..2e7bf0257dfc95daed0faced9d2eed5fd307785f --- /dev/null +++ b/lib/manager/npm/update/dependency/__snapshots__/index.spec.ts.snap @@ -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\\"}}"`; diff --git a/lib/manager/npm/update.spec.ts b/lib/manager/npm/update/dependency/index.spec.ts similarity index 75% rename from lib/manager/npm/update.spec.ts rename to lib/manager/npm/update/dependency/index.spec.ts index 9611d0c713d8c7785172753900c505d92bea4504..12c1ef19fc718e68e2e8b7e46f4e11c90442354b 100644 --- a/lib/manager/npm/update.spec.ts +++ b/lib/manager/npm/update/dependency/index.spec.ts @@ -1,11 +1,11 @@ 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' ); } @@ -13,7 +13,7 @@ function readFixture(fixture: string) { 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 = { @@ -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); - }); - }); }); diff --git a/lib/manager/npm/update.ts b/lib/manager/npm/update/dependency/index.ts similarity index 72% rename from lib/manager/npm/update.ts rename to lib/manager/npm/update/dependency/index.ts index 1852ed981cc275b81b1644e0d9cd79f999191d54..9e44af0f64d902f8c1175cf8efdfe1804268efd5 100644 --- a/lib/manager/npm/update.ts +++ b/lib/manager/npm/update/dependency/index.ts @@ -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, diff --git a/lib/manager/npm/update/index.ts b/lib/manager/npm/update/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..23c3f54e1209c57447e91fdd8e9bf1f04ac4c706 --- /dev/null +++ b/lib/manager/npm/update/index.ts @@ -0,0 +1,2 @@ +export { bumpPackageVersion } from './package-version'; +export { updateDependency } from './dependency'; diff --git a/lib/manager/npm/update/package-version/__snapshots__/index.spec.ts.snap b/lib/manager/npm/update/package-version/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..c18e1c9f7a02213d75cc0ad9ad55a23387faa377 --- /dev/null +++ b/lib/manager/npm/update/package-version/__snapshots__/index.spec.ts.snap @@ -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\\"}}"`; diff --git a/lib/manager/npm/update/package-version/index.spec.ts b/lib/manager/npm/update/package-version/index.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ca10ee366a6af3ac17e08953faea3d2359bb5a7 --- /dev/null +++ b/lib/manager/npm/update/package-version/index.spec.ts @@ -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); + }); + }); +}); diff --git a/lib/manager/npm/update/package-version/index.ts b/lib/manager/npm/update/package-version/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c327aa4d2ab9ee9dcd9dfd4b44f59f864b6adb3 --- /dev/null +++ b/lib/manager/npm/update/package-version/index.ts @@ -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 }; +}