Skip to content

Commit 63f31bc

Browse files
committed
Adding comparison check for decrementing version
1 parent 265fdaf commit 63f31bc

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

src/action/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ async function main(): Promise<void> {
3535
`\n📦 ${results.package} v${results.version} is already published to NPM`
3636
);
3737
}
38+
else if (options.greaterVersion && results.type === "lower") {
39+
console.log(
40+
`\n📦 ${results.package} v${results.version} is lower than the version published to NPM`
41+
);
42+
}
3843
else if (results.dryRun) {
3944
console.log(
4045
`\n📦 ${results.package} v${results.version} was NOT actually published to NPM (dry run)`

src/npm-publish.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@ export async function npmPublish(opts: Options = {}): Promise<Results> {
1818
// Determine if/how the version has changed
1919
let diff = semver.diff(manifest.version, publishedVersion);
2020

21-
if (diff || !options.checkVersion) {
21+
// Compare both versions to see if it's changed
22+
let cmp = semver.compare(manifest.version, publishedVersion);
23+
24+
let shouldPublish =
25+
!options.checkVersion ||
26+
// compare returns 1 if manifest is higher than published
27+
(options.greaterVersion && cmp === 1) ||
28+
// compare returns 0 if the manifest is the same as published
29+
cmp !== 0;
30+
31+
if (shouldPublish) {
2232
// Publish the new version to NPM
2333
await npm.publish(manifest, options);
2434
}
2535

2636
let results: Results = {
2737
package: manifest.name,
28-
type: diff || "none",
38+
type: (cmp === -1 && "lower") || diff || "none",
2939
version: manifest.version.raw,
3040
oldVersion: publishedVersion.raw,
3141
tag: options.tag,
32-
access: options.access || (manifest.name.startsWith("@") ? "restricted" : "public"),
33-
dryRun: options.dryRun
42+
access:
43+
options.access ||
44+
(manifest.name.startsWith("@") ? "restricted" : "public"),
45+
dryRun: options.dryRun,
3446
};
3547

3648
options.debug("OUTPUT:", results);

src/results.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Results {
1010
/**
1111
* The type of version change that occurred
1212
*/
13-
type: ReleaseType | "none";
13+
type: ReleaseType | "lower" | "none";
1414

1515
/**
1616
* The name of the NPM package that was published

test/specs/action/success.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,64 @@ describe("GitHub Action - success tests", () => {
123123
npm.assert.ran(4);
124124
});
125125

126+
it("should not publish a new version to NPM if the version is lower and greaterVersion flag is set", () => {
127+
files.create([
128+
{
129+
path: "workspace/package.json",
130+
contents: { name: "my-lib", version: "0.1.0" },
131+
},
132+
]);
133+
134+
npm.mock({
135+
args: ["config", "get", "userconfig"],
136+
stdout: `${paths.npmrc}${EOL}`,
137+
});
138+
139+
npm.mock({
140+
args: ["view", "my-lib", "version"],
141+
stdout: `1.0.0${EOL}`,
142+
});
143+
144+
npm.mock({
145+
args: ["config", "get", "userconfig"],
146+
stdout: `${paths.npmrc}${EOL}`,
147+
});
148+
149+
npm.mock({
150+
args: ["publish"],
151+
env: { INPUT_TOKEN: "my-secret-token" },
152+
stdout: `my-lib 0.1.0${EOL}`,
153+
});
154+
155+
let cli = exec.action({
156+
env: {
157+
INPUT_TOKEN: "my-secret-token",
158+
"INPUT_GREATER-VERSION": "true",
159+
},
160+
});
161+
162+
expect(cli).to.have.stderr("");
163+
expect(cli).stdout.to.include("my-lib 0.1.0");
164+
expect(cli).stdout.to.include(
165+
"my-lib v0.1.0 is lower than the version published to NPM"
166+
);
167+
expect(cli).stdout.to.include("::set-output name=type::lower");
168+
expect(cli).stdout.to.include("::set-output name=version::0.1.0");
169+
expect(cli).stdout.to.include("::set-output name=old-version::1.0.0");
170+
expect(cli).stdout.to.include("::set-output name=tag::latest");
171+
expect(cli).stdout.to.include("::set-output name=access::public");
172+
expect(cli).stdout.to.include("::set-output name=dry-run::false");
173+
expect(cli).to.have.exitCode(0);
174+
175+
files.assert.contents(
176+
"home/.npmrc",
177+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
178+
`registry=https://registry.npmjs.org/${EOL}`
179+
);
180+
181+
npm.assert.ran(4);
182+
});
183+
126184
it("should publish a new version to NPM if no package exists", async () => {
127185
files.create([
128186
{

0 commit comments

Comments
 (0)