Skip to content

Add option to restrict publishing to greater package version only #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 69 additions & 84 deletions README.md

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions src/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Access, Options } from "../options";

/**
* The main entry point of the GitHub Action
*
* @internal
*/
async function main(): Promise<void> {
Expand All @@ -17,24 +18,37 @@ async function main(): Promise<void> {
token: getInput("token", { required: true }),
registry: getInput("registry", { required: true }),
package: getInput("package", { required: true }),
checkVersion: getInput("check-version", { required: true }).toLowerCase() === "true",
checkVersion:
getInput("check-version", { required: true }).toLowerCase() === "true",
tag: getInput("tag"),
access: getInput("access") as Access,
dryRun: getInput("dry-run").toLowerCase() === "true",
greaterVersionOnly: getInput("greater-version-only").toLowerCase() === "true",
debug: debugHandler,
};

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

if (results.type === "none") {
console.log(`\n📦 ${results.package} v${results.version} is already published to ${options.registry}`);
console.log(
`\n📦 ${results.package} v${results.version} is already published to ${options.registry}`
);
}
if (results.type === "lower") {
console.log(
`\n📦 ${results.package} v${results.version} is lower than the version published to ${options.registry}`
);
}
else if (results.dryRun) {
console.log(`\n📦 ${results.package} v${results.version} was NOT actually published to ${options.registry} (dry run)`);
console.log(
`\n📦 ${results.package} v${results.version} was NOT actually published to ${options.registry} (dry run)`
);
}
else {
console.log(`\n📦 Successfully published ${results.package} v${results.version} to ${options.registry}`);
console.log(
`\n📦 Successfully published ${results.package} v${results.version} to ${options.registry}`
);
}

// Set the GitHub Actions output variables
Expand Down
13 changes: 11 additions & 2 deletions src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface NormalizedOptions {
access?: Access;
dryRun: boolean;
checkVersion: boolean;
greaterVersionOnly: boolean;
quiet: boolean;
debug: Debug;
}
Expand All @@ -22,7 +23,10 @@ export interface NormalizedOptions {
* @internal
*/
export function normalizeOptions(options: Options): NormalizedOptions {
let registryURL = typeof options.registry === "string" ? new URL(options.registry) : options.registry;
let registryURL =
typeof options.registry === "string"
? new URL(options.registry)
: options.registry;

return {
token: options.token || "",
Expand All @@ -31,7 +35,12 @@ export function normalizeOptions(options: Options): NormalizedOptions {
tag: options.tag || "latest",
access: options.access,
dryRun: options.dryRun || false,
checkVersion: options.checkVersion === undefined ? true : Boolean(options.checkVersion),
checkVersion:
options.checkVersion === undefined ? true : Boolean(options.checkVersion),
greaterVersionOnly:
options.greaterVersionOnly === undefined
? false
: Boolean(options.greaterVersionOnly),
quiet: options.quiet || false,
debug: options.debug || (() => undefined),
};
Expand Down
22 changes: 18 additions & 4 deletions src/npm-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ export async function npmPublish(opts: Options = {}): Promise<Results> {
// Determine if/how the version has changed
let diff = semver.diff(manifest.version, publishedVersion);

if (diff || !options.checkVersion) {
// Compare both versions to see if it's changed
let cmp = semver.compare(manifest.version, publishedVersion);

let shouldPublish =
!options.checkVersion ||
// compare returns 1 if manifest is higher than published
(options.greaterVersionOnly && cmp === 1) ||
// compare returns 0 if the manifest is the same as published
cmp !== 0;

if (shouldPublish) {
// Publish the new version to NPM
await npm.publish(manifest, options);
}

let results: Results = {
package: manifest.name,
type: diff || "none",
// The version should be marked as lower if we disallow decrementing the version
type:
(options.greaterVersionOnly && cmp === -1 && "lower") || diff || "none",
version: manifest.version.raw,
oldVersion: publishedVersion.raw,
tag: options.tag,
access: options.access || (manifest.name.startsWith("@") ? "restricted" : "public"),
dryRun: options.dryRun
access:
options.access ||
(manifest.name.startsWith("@") ? "restricted" : "public"),
dryRun: options.dryRun,
};

options.debug("OUTPUT:", results);
Expand Down
8 changes: 8 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export interface Options {
*/
quiet?: boolean;

/**
* Only publish the package if the version number in package.json
* is greater than the latest on NPM.
*
* Defaults to `false`
*/
greaterVersionOnly?: boolean;

/**
* A function to call to log debug messages.
*
Expand Down
2 changes: 1 addition & 1 deletion src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface Results {
/**
* The type of version change that occurred
*/
type: ReleaseType | "none";
type: ReleaseType | "lower" | "none";

/**
* The name of the NPM package that was published
Expand Down
Loading