From 98617b4273e01fef966464c002166614ef533f15 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Sun, 20 Oct 2019 22:55:45 +0200 Subject: [PATCH] fix: update version at monorepo.mainVersionFile (#351) --- .../lib/util/__tests__/updateVersion.spec.js | 2 +- .../shipjs-lib/src/lib/util/updateVersion.js | 8 +++-- .../prepare/__tests__/updateVersion.spec.js | 5 ++- .../__tests__/updateVersionMonorepo.spec.js | 34 +++++++++++++++---- .../shipjs/src/step/prepare/updateVersion.js | 2 +- .../src/step/prepare/updateVersionMonorepo.js | 7 ++-- packages/shipjs/src/step/release/validate.js | 4 +-- 7 files changed, 46 insertions(+), 16 deletions(-) diff --git a/packages/shipjs-lib/src/lib/util/__tests__/updateVersion.spec.js b/packages/shipjs-lib/src/lib/util/__tests__/updateVersion.spec.js index 10ec2649..e11ab0de 100644 --- a/packages/shipjs-lib/src/lib/util/__tests__/updateVersion.spec.js +++ b/packages/shipjs-lib/src/lib/util/__tests__/updateVersion.spec.js @@ -7,7 +7,7 @@ describe('updateVersion', () => { it('update version correctly', () => { silentExec('./tests/bootstrap-examples/empty.sh version-updating'); const dir = resolve('sandbox/version-updating'); - updateVersion('0.9.9', dir); + updateVersion({ nextVersion: '0.9.9', dir }); expect(getCurrentVersion('sandbox/version-updating')).toBe('0.9.9'); }); }); diff --git a/packages/shipjs-lib/src/lib/util/updateVersion.js b/packages/shipjs-lib/src/lib/util/updateVersion.js index 554d621c..6afae930 100644 --- a/packages/shipjs-lib/src/lib/util/updateVersion.js +++ b/packages/shipjs-lib/src/lib/util/updateVersion.js @@ -1,8 +1,12 @@ import { readFileSync, writeFileSync } from 'fs'; import { resolve } from 'path'; -export default function updateVersion(nextVersion, dir = '.') { - const filePath = resolve(dir, 'package.json'); +export default function updateVersion({ + nextVersion, + dir = '.', + fileName = 'package.json', +}) { + const filePath = resolve(dir, fileName); const json = JSON.parse(readFileSync(filePath).toString()); json.version = nextVersion; writeFileSync(filePath, `${JSON.stringify(json, null, 2)}\n`); diff --git a/packages/shipjs/src/step/prepare/__tests__/updateVersion.spec.js b/packages/shipjs/src/step/prepare/__tests__/updateVersion.spec.js index c5dc6ea2..b83dd0d2 100644 --- a/packages/shipjs/src/step/prepare/__tests__/updateVersion.spec.js +++ b/packages/shipjs/src/step/prepare/__tests__/updateVersion.spec.js @@ -15,7 +15,10 @@ describe('updateVersion', () => { dryRun: false, }); expect(updateVersionPackageJson).toHaveBeenCalledTimes(1); - expect(updateVersionPackageJson).toHaveBeenCalledWith('1.2.3', '.'); + expect(updateVersionPackageJson).toHaveBeenCalledWith({ + dir: '.', + nextVersion: '1.2.3', + }); expect(versionUpdated.mock.calls[0][0]).toMatchInlineSnapshot(` Object { "dir": ".", diff --git a/packages/shipjs/src/step/prepare/__tests__/updateVersionMonorepo.spec.js b/packages/shipjs/src/step/prepare/__tests__/updateVersionMonorepo.spec.js index 9b229d0c..1eec4601 100644 --- a/packages/shipjs/src/step/prepare/__tests__/updateVersionMonorepo.spec.js +++ b/packages/shipjs/src/step/prepare/__tests__/updateVersionMonorepo.spec.js @@ -13,22 +13,38 @@ describe('updateVersionMonorepo', () => { updateVersionMonorepo({ config: { versionUpdated, - monorepo: { packagesToBump: ['packages/*'] }, + monorepo: { + packagesToBump: ['packages/*'], + mainVersionFile: 'lerna.json', + }, }, dir: '.', nextVersion: '1.2.3', }); - expect(updateVersion).toHaveBeenCalledTimes(2); + expect(updateVersion).toHaveBeenCalledTimes(3); expect(updateVersion.mock.calls[0]).toMatchInlineSnapshot(` Array [ - "1.2.3", - "packages/a", + Object { + "dir": ".", + "fileName": "lerna.json", + "nextVersion": "1.2.3", + }, ] `); expect(updateVersion.mock.calls[1]).toMatchInlineSnapshot(` Array [ - "1.2.3", - "packages/b", + Object { + "dir": "packages/a", + "nextVersion": "1.2.3", + }, + ] + `); + expect(updateVersion.mock.calls[2]).toMatchInlineSnapshot(` + Array [ + Object { + "dir": "packages/b", + "nextVersion": "1.2.3", + }, ] `); expect(versionUpdated).toHaveBeenCalledTimes(1); @@ -50,7 +66,10 @@ describe('updateVersionMonorepo', () => { mockPrint(print, output); updateVersionMonorepo({ config: { - monorepo: { packagesToBump: ['packages/*'] }, + monorepo: { + packagesToBump: ['packages/*'], + mainVersionFile: 'lerna.json', + }, }, dryRun: true, }); @@ -58,6 +77,7 @@ describe('updateVersionMonorepo', () => { Array [ "› Updating the versions on the monorepo.", "Your configuration: [\\"packages/*\\"]", + "Main version file: lerna.json", "Actual packages to bump:", "-> packages/a/package.json", "-> packages/b/package.json", diff --git a/packages/shipjs/src/step/prepare/updateVersion.js b/packages/shipjs/src/step/prepare/updateVersion.js index 9a9322d8..984463e3 100644 --- a/packages/shipjs/src/step/prepare/updateVersion.js +++ b/packages/shipjs/src/step/prepare/updateVersion.js @@ -11,7 +11,7 @@ export default async ({ config, nextVersion, dir, dryRun }) => print(`-> execute ${info('versionUpdated()')} callback.`); return; } - updateVersion(nextVersion, dir); + updateVersion({ nextVersion, dir }); await versionUpdated({ version: nextVersion, dir, diff --git a/packages/shipjs/src/step/prepare/updateVersionMonorepo.js b/packages/shipjs/src/step/prepare/updateVersionMonorepo.js index dd33df0e..56d5b014 100644 --- a/packages/shipjs/src/step/prepare/updateVersionMonorepo.js +++ b/packages/shipjs/src/step/prepare/updateVersionMonorepo.js @@ -9,11 +9,12 @@ export default async ({ config, nextVersion, dir, dryRun }) => async () => { const { versionUpdated, - monorepo: { packagesToBump }, + monorepo: { mainVersionFile, packagesToBump }, } = config; const packageList = expandPackageList(packagesToBump, dir); if (dryRun) { print(`Your configuration: ${JSON.stringify(packagesToBump)}`); + print(`Main version file: ${mainVersionFile}`); print(`Actual packages to bump:`); packageList.forEach(packageDir => print(`-> ${info(`${packageDir}/package.json`)}`) @@ -21,9 +22,11 @@ export default async ({ config, nextVersion, dir, dryRun }) => print(`-> execute ${info('versionUpdated()')} callback.`); return; } + + updateVersion({ nextVersion, dir, fileName: mainVersionFile }); packageList.forEach(packageDir => { print(`-> ${info(`${packageDir}/package.json`)}`); - updateVersion(nextVersion, packageDir); + updateVersion({ nextVersion, dir: packageDir }); }); await versionUpdated({ version: nextVersion, diff --git a/packages/shipjs/src/step/release/validate.js b/packages/shipjs/src/step/release/validate.js index a677a19a..513c2304 100644 --- a/packages/shipjs/src/step/release/validate.js +++ b/packages/shipjs/src/step/release/validate.js @@ -17,8 +17,8 @@ export default ({ config, dir }) => } = config; const commitMessage = getLatestCommitMessage(dir); const currentVersion = - monorepo && monorepo.readVersionFrom - ? getCurrentVersion(dir, monorepo.readVersionFrom) + monorepo && monorepo.mainVersionFile + ? getCurrentVersion(dir, monorepo.mainVersionFile) : getCurrentVersion(dir); const currentBranch = getCurrentBranch(dir); const validationResult = shouldRelease({