Skip to content

Commit 8a60e21

Browse files
daniseijoarcanis
authored andcommitted
feat(version): Allow for prereleases flags and prerelease identifiers (#7336)
* Complete semver versioning flags with prereleases and preid * Update CHANGELOG
1 parent 2f5c937 commit 8a60e21

File tree

4 files changed

+128
-8
lines changed

4 files changed

+128
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Adds prereleases flags and prerelease identifier to `yarn version`.
8+
9+
[#7336](https://github.com/yarnpkg/yarn/pull/7336) - [**Daniel Seijo**](https://github.com/daniseijo)
10+
711
- Fixes audits when used with `yarn add` & `yarn upgrade`
812

913
[#7326](https://github.com/yarnpkg/yarn/pull/7326) - [**David Sanders**](https://github.com/dsanders11)
@@ -41,7 +45,7 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
4145
- Adds support for the npm enterprise URLs when computing the offline mirror filenames.
4246

4347
[#7200](https://github.com/yarnpkg/yarn/pull/7200) - [**John Millikin**](https://john-millikin.com)
44-
48+
4549
- Tweaks the lockfile parser logic to parse a few extra cases
4650

4751
[#7210](https://github.com/yarnpkg/yarn/pull/7210) - [**Maël Nison**](https://twitter.com/arcanis)
@@ -119,7 +123,7 @@ The 1.15.1 doesn't exist due to a release hiccup.
119123
- Packages won't be auto-unplugged anymore if `ignore-scripts` is set in the yarnrc file
120124

121125
[#6983](https://github.com/yarnpkg/yarn/pull/6983) - [**Micha Reiser**](https://github.com/MichaReiser)
122-
126+
123127
- Enables displaying Emojis on [Terminus](https://github.com/Eugeny/terminus) by default
124128

125129
[#7093](https://github.com/yarnpkg/yarn/pull/7093) - [**David Refoua**](https://github.com/DRSDavidSoft)

__tests__/commands/version.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,101 @@ test('run version with --patch flag and make sure patch version is incremented',
182182
expect(pkg.version).toEqual('1.0.1');
183183
});
184184
});
185+
186+
test('run version with --premajor flag and make sure premajor version is incremented', (): Promise<void> => {
187+
return runRun([], {gitTagVersion, premajor: true}, 'no-args', async (config): ?Promise<void> => {
188+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
189+
expect(pkg.version).toEqual('2.0.0-0');
190+
});
191+
});
192+
193+
test('run version with --premajor flag with preid and make sure premajor version is incremented', (): Promise<void> => {
194+
return runRun([], {gitTagVersion, premajor: true, preid: 'alpha'}, 'no-args', async (config): ?Promise<void> => {
195+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
196+
expect(pkg.version).toEqual('2.0.0-alpha.0');
197+
});
198+
});
199+
200+
test('run version with --preminor flag and make sure preminor version is incremented', (): Promise<void> => {
201+
return runRun([], {gitTagVersion, preminor: true}, 'no-args', async (config): ?Promise<void> => {
202+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
203+
expect(pkg.version).toEqual('1.1.0-0');
204+
});
205+
});
206+
207+
test('run version with --preminor flag with preid and make sure preminor version is incremented', (): Promise<void> => {
208+
return runRun([], {gitTagVersion, preminor: true, preid: 'alpha'}, 'no-args', async (config): ?Promise<void> => {
209+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
210+
expect(pkg.version).toEqual('1.1.0-alpha.0');
211+
});
212+
});
213+
214+
test('run version with --prepatch flag and make sure prepatch version is incremented', (): Promise<void> => {
215+
return runRun([], {gitTagVersion, prepatch: true}, 'no-args', async (config): ?Promise<void> => {
216+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
217+
expect(pkg.version).toEqual('1.0.1-0');
218+
});
219+
});
220+
221+
test('run version with --prepatch flag with preid and make sure prepatch version is incremented', (): Promise<void> => {
222+
return runRun([], {gitTagVersion, prepatch: true, preid: 'alpha'}, 'no-args', async (config): ?Promise<void> => {
223+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
224+
expect(pkg.version).toEqual('1.0.1-alpha.0');
225+
});
226+
});
227+
228+
test('run version with --prerelease flag and make sure prerelease version is incremented', (): Promise<void> => {
229+
return runRun([], {gitTagVersion, prerelease: true}, 'no-args', async (config): ?Promise<void> => {
230+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
231+
expect(pkg.version).toEqual('1.0.1-0');
232+
});
233+
});
234+
235+
test('run version with --prerelease flag with preid and make sure prerelease version is incremented', (): Promise<
236+
void,
237+
> => {
238+
return runRun([], {gitTagVersion, prerelease: true, preid: 'alpha'}, 'no-args', async (config): ?Promise<void> => {
239+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
240+
expect(pkg.version).toEqual('1.0.1-alpha.0');
241+
});
242+
});
243+
244+
test('run version with --new-version prerelease flag and make sure prerelease version is incremented', (): Promise<
245+
void,
246+
> => {
247+
return runRun([], {gitTagVersion, newVersion: 'prerelease'}, 'no-args', async (config): ?Promise<void> => {
248+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
249+
expect(pkg.version).toEqual('1.0.1-0');
250+
});
251+
});
252+
253+
test('run version with --new-version and preid flags and make sure prerelease version is incremented', (): Promise<
254+
void,
255+
> => {
256+
return runRun([], {gitTagVersion, newVersion: 'prerelease', preid: 'beta'}, 'no-args', async (config): ?Promise<
257+
void,
258+
> => {
259+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
260+
expect(pkg.version).toEqual('1.0.1-beta.0');
261+
});
262+
});
263+
264+
test('run version with --new-version and preid flags and make sure premajor version is incremented', (): Promise<
265+
void,
266+
> => {
267+
return runRun([], {gitTagVersion, newVersion: 'premajor', preid: 'beta'}, 'no-args', async (config): ?Promise<
268+
void,
269+
> => {
270+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
271+
expect(pkg.version).toEqual('2.0.0-beta.0');
272+
});
273+
});
274+
275+
test('run version with main release and --new-version and preid flags and make sure identifier is ignored', (): Promise<
276+
void,
277+
> => {
278+
return runRun([], {gitTagVersion, newVersion: 'major', preid: 'beta'}, 'no-args', async (config): ?Promise<void> => {
279+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
280+
expect(pkg.version).toEqual('2.0.0');
281+
});
282+
});

flow-typed/npm/semver_v5.1.x.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ declare module 'semver' {
4141
// Functions
4242
declare function clean(v: string, loose?: boolean): string | null;
4343
declare function valid(v: string, loose?: boolean): string | null;
44-
declare function inc(v: string, release: string, loose?: boolean): string | null;
44+
declare function inc(v: string, release: string, loose?: boolean, identifier?: string): string | null;
45+
declare function inc(v: string, release: string, identifier: string): string | null;
4546
declare function major(v: string, loose?: boolean): number;
4647
declare function minor(v: string, loose?: boolean): number;
4748
declare function patch(v: string, loose?: boolean): number;

src/cli/commands/version.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const semver = require('semver');
1414
const path = require('path');
1515

1616
const NEW_VERSION_FLAG = '--new-version [version]';
17-
function isValidNewVersion(oldVersion: string, newVersion: string, looseSemver: boolean): boolean {
18-
return !!(semver.valid(newVersion, looseSemver) || semver.inc(oldVersion, newVersion, looseSemver));
17+
function isValidNewVersion(oldVersion: string, newVersion: string, looseSemver: boolean, identifier?: string): boolean {
18+
return !!(semver.valid(newVersion, looseSemver) || semver.inc(oldVersion, newVersion, looseSemver, identifier));
1919
}
2020

2121
export function setFlags(commander: Object) {
@@ -24,6 +24,11 @@ export function setFlags(commander: Object) {
2424
commander.option('--major', 'auto-increment major version number');
2525
commander.option('--minor', 'auto-increment minor version number');
2626
commander.option('--patch', 'auto-increment patch version number');
27+
commander.option('--premajor', 'auto-increment premajor version number');
28+
commander.option('--preminor', 'auto-increment preminor version number');
29+
commander.option('--prepatch', 'auto-increment prepatch version number');
30+
commander.option('--prerelease', 'auto-increment prerelease version number');
31+
commander.option('--preid [preid]', 'add a custom identifier to the prerelease');
2732
commander.option('--message [message]', 'message');
2833
commander.option('--no-git-tag-version', 'no git tag version');
2934
commander.option('--no-commit-hooks', 'bypass git hooks when committing new version');
@@ -44,6 +49,10 @@ export async function setVersion(
4449
const pkgLoc = pkg._loc;
4550
const scripts = map();
4651
let newVersion = flags.newVersion;
52+
let identifier = undefined;
53+
if (flags.preid) {
54+
identifier = flags.preid;
55+
}
4756
invariant(pkgLoc, 'expected package location');
4857

4958
if (args.length && !newVersion) {
@@ -76,7 +85,7 @@ export async function setVersion(
7685
}
7786

7887
// get new version
79-
if (newVersion && !isValidNewVersion(oldVersion, newVersion, config.looseSemver)) {
88+
if (newVersion && !isValidNewVersion(oldVersion, newVersion, config.looseSemver, identifier)) {
8089
throw new MessageError(reporter.lang('invalidVersion'));
8190
}
8291

@@ -88,6 +97,14 @@ export async function setVersion(
8897
newVersion = semver.inc(oldVersion, 'minor');
8998
} else if (flags.patch) {
9099
newVersion = semver.inc(oldVersion, 'patch');
100+
} else if (flags.premajor) {
101+
newVersion = semver.inc(oldVersion, 'premajor', identifier);
102+
} else if (flags.preminor) {
103+
newVersion = semver.inc(oldVersion, 'preminor', identifier);
104+
} else if (flags.prepatch) {
105+
newVersion = semver.inc(oldVersion, 'prepatch', identifier);
106+
} else if (flags.prerelease) {
107+
newVersion = semver.inc(oldVersion, 'prerelease', identifier);
91108
}
92109
}
93110

@@ -117,15 +134,15 @@ export async function setVersion(
117134
};
118135
}
119136

120-
if (isValidNewVersion(oldVersion, newVersion, config.looseSemver)) {
137+
if (isValidNewVersion(oldVersion, newVersion, config.looseSemver, identifier)) {
121138
break;
122139
} else {
123140
newVersion = null;
124141
reporter.error(reporter.lang('invalidSemver'));
125142
}
126143
}
127144
if (newVersion) {
128-
newVersion = semver.inc(oldVersion, newVersion, config.looseSemver) || newVersion;
145+
newVersion = semver.inc(oldVersion, newVersion, config.looseSemver, identifier) || newVersion;
129146
}
130147
invariant(newVersion, 'expected new version');
131148

0 commit comments

Comments
 (0)