Skip to content

Commit be50130

Browse files
authoredMay 13, 2022
Merge pull request #44 from Tohaker/feature/greater-package-version
Add option to restrict publishing to greater package version only
2 parents e59377e + 45b397d commit be50130

File tree

7 files changed

+376
-165
lines changed

7 files changed

+376
-165
lines changed
 

‎README.md

+69-84
Large diffs are not rendered by default.

‎src/action/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Access, Options } from "../options";
44

55
/**
66
* The main entry point of the GitHub Action
7+
*
78
* @internal
89
*/
910
async function main(): Promise<void> {
@@ -17,24 +18,37 @@ async function main(): Promise<void> {
1718
token: getInput("token", { required: true }),
1819
registry: getInput("registry", { required: true }),
1920
package: getInput("package", { required: true }),
20-
checkVersion: getInput("check-version", { required: true }).toLowerCase() === "true",
21+
checkVersion:
22+
getInput("check-version", { required: true }).toLowerCase() === "true",
2123
tag: getInput("tag"),
2224
access: getInput("access") as Access,
2325
dryRun: getInput("dry-run").toLowerCase() === "true",
26+
greaterVersionOnly: getInput("greater-version-only").toLowerCase() === "true",
2427
debug: debugHandler,
2528
};
2629

2730
// Publish to NPM
2831
let results = await npmPublish(options);
2932

3033
if (results.type === "none") {
31-
console.log(`\n📦 ${results.package} v${results.version} is already published to ${options.registry}`);
34+
console.log(
35+
`\n📦 ${results.package} v${results.version} is already published to ${options.registry}`
36+
);
37+
}
38+
if (results.type === "lower") {
39+
console.log(
40+
`\n📦 ${results.package} v${results.version} is lower than the version published to ${options.registry}`
41+
);
3242
}
3343
else if (results.dryRun) {
34-
console.log(`\n📦 ${results.package} v${results.version} was NOT actually published to ${options.registry} (dry run)`);
44+
console.log(
45+
`\n📦 ${results.package} v${results.version} was NOT actually published to ${options.registry} (dry run)`
46+
);
3547
}
3648
else {
37-
console.log(`\n📦 Successfully published ${results.package} v${results.version} to ${options.registry}`);
49+
console.log(
50+
`\n📦 Successfully published ${results.package} v${results.version} to ${options.registry}`
51+
);
3852
}
3953

4054
// Set the GitHub Actions output variables

‎src/normalize-options.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface NormalizedOptions {
1313
access?: Access;
1414
dryRun: boolean;
1515
checkVersion: boolean;
16+
greaterVersionOnly: boolean;
1617
quiet: boolean;
1718
debug: Debug;
1819
}
@@ -22,7 +23,10 @@ export interface NormalizedOptions {
2223
* @internal
2324
*/
2425
export function normalizeOptions(options: Options): NormalizedOptions {
25-
let registryURL = typeof options.registry === "string" ? new URL(options.registry) : options.registry;
26+
let registryURL =
27+
typeof options.registry === "string"
28+
? new URL(options.registry)
29+
: options.registry;
2630

2731
return {
2832
token: options.token || "",
@@ -31,7 +35,12 @@ export function normalizeOptions(options: Options): NormalizedOptions {
3135
tag: options.tag || "latest",
3236
access: options.access,
3337
dryRun: options.dryRun || false,
34-
checkVersion: options.checkVersion === undefined ? true : Boolean(options.checkVersion),
38+
checkVersion:
39+
options.checkVersion === undefined ? true : Boolean(options.checkVersion),
40+
greaterVersionOnly:
41+
options.greaterVersionOnly === undefined
42+
? false
43+
: Boolean(options.greaterVersionOnly),
3544
quiet: options.quiet || false,
3645
debug: options.debug || (() => undefined),
3746
};

‎src/npm-publish.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,33 @@ 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.greaterVersionOnly && 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+
// The version should be marked as lower if we disallow decrementing the version
39+
type:
40+
(options.greaterVersionOnly && cmp === -1 && "lower") || diff || "none",
2941
version: manifest.version.raw,
3042
oldVersion: publishedVersion.raw,
3143
tag: options.tag,
32-
access: options.access || (manifest.name.startsWith("@") ? "restricted" : "public"),
33-
dryRun: options.dryRun
44+
access:
45+
options.access ||
46+
(manifest.name.startsWith("@") ? "restricted" : "public"),
47+
dryRun: options.dryRun,
3448
};
3549

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

‎src/options.ts

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ export interface Options {
6464
*/
6565
quiet?: boolean;
6666

67+
/**
68+
* Only publish the package if the version number in package.json
69+
* is greater than the latest on NPM.
70+
*
71+
* Defaults to `false`
72+
*/
73+
greaterVersionOnly?: boolean;
74+
6775
/**
6876
* A function to call to log debug messages.
6977
*

‎src/results.ts

+1-1
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

0 commit comments

Comments
 (0)
Failed to load comments.