Skip to content
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

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adding comparison check for decrementing version
  • Loading branch information
Tohaker committed Sep 13, 2021
commit 63f31bcb99b8a253fe2502f385496323dd24084a
5 changes: 5 additions & 0 deletions src/action/index.ts
Original file line number Diff line number Diff line change
@@ -35,6 +35,11 @@ async function main(): Promise<void> {
`\n📦 ${results.package} v${results.version} is already published to NPM`
);
}
else if (options.greaterVersion && results.type === "lower") {
console.log(
`\n📦 ${results.package} v${results.version} is lower than the version published to NPM`
);
}
else if (results.dryRun) {
console.log(
`\n📦 ${results.package} v${results.version} was NOT actually published to NPM (dry run)`
20 changes: 16 additions & 4 deletions src/npm-publish.ts
Original file line number Diff line number Diff line change
@@ -18,19 +18,31 @@ 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.greaterVersion && 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",
type: (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);
2 changes: 1 addition & 1 deletion src/results.ts
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions test/specs/action/success.spec.js
Original file line number Diff line number Diff line change
@@ -123,6 +123,64 @@ describe("GitHub Action - success tests", () => {
npm.assert.ran(4);
});

it("should not publish a new version to NPM if the version is lower and greaterVersion flag is set", () => {
files.create([
{
path: "workspace/package.json",
contents: { name: "my-lib", version: "0.1.0" },
},
]);

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["view", "my-lib", "version"],
stdout: `1.0.0${EOL}`,
});

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["publish"],
env: { INPUT_TOKEN: "my-secret-token" },
stdout: `my-lib 0.1.0${EOL}`,
});

let cli = exec.action({
env: {
INPUT_TOKEN: "my-secret-token",
"INPUT_GREATER-VERSION": "true",
},
});

expect(cli).to.have.stderr("");
expect(cli).stdout.to.include("my-lib 0.1.0");
expect(cli).stdout.to.include(
"my-lib v0.1.0 is lower than the version published to NPM"
);
expect(cli).stdout.to.include("::set-output name=type::lower");
expect(cli).stdout.to.include("::set-output name=version::0.1.0");
expect(cli).stdout.to.include("::set-output name=old-version::1.0.0");
expect(cli).stdout.to.include("::set-output name=tag::latest");
expect(cli).stdout.to.include("::set-output name=access::public");
expect(cli).stdout.to.include("::set-output name=dry-run::false");
expect(cli).to.have.exitCode(0);

files.assert.contents(
"home/.npmrc",
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
`registry=https://registry.npmjs.org/${EOL}`
);

npm.assert.ran(4);
});

it("should publish a new version to NPM if no package exists", async () => {
files.create([
{