Skip to content

Commit

Permalink
fix: update version at monorepo.mainVersionFile (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Oct 20, 2019
1 parent 107255a commit 98617b4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
8 changes: 6 additions & 2 deletions packages/shipjs-lib/src/lib/util/updateVersion.js
Original file line number Diff line number Diff line change
@@ -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`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": ".",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -50,14 +66,18 @@ describe('updateVersionMonorepo', () => {
mockPrint(print, output);
updateVersionMonorepo({
config: {
monorepo: { packagesToBump: ['packages/*'] },
monorepo: {
packagesToBump: ['packages/*'],
mainVersionFile: 'lerna.json',
},
},
dryRun: true,
});
expect(output).toMatchInlineSnapshot(`
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",
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/step/prepare/updateVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions packages/shipjs/src/step/prepare/updateVersionMonorepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ 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`)}`)
);
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,
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/step/release/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 98617b4

Please sign in to comment.