Skip to content

Commit

Permalink
Merge pull request #1460 from intuit/npm-enterprise-available
Browse files Browse the repository at this point in the history
Fix finding available canary version and add logging
  • Loading branch information
hipstersmoothie authored Aug 11, 2020
2 parents b22417b + 2709ea9 commit 7254217
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@ import {
SEMVER,
validatePluginConfiguration,
ShipitRelease,
DEFAULT_PRERELEASE_BRANCHES,
} from "@auto-it/core";
import getPackages from "get-monorepo-packages";
import { gt, gte, inc, ReleaseType } from "semver";

import getConfigFromPackageJson from "./package-config";
import setTokenOnCI, { getRegistry, DEFAULT_REGISTRY } from "./set-npm-token";
import { loadPackageJson, writeFile, isMonorepo, readFile } from "./utils";
import { DEFAULT_PRERELEASE_BRANCHES } from '@auto-it/core/dist/config';

const { isCi } = envCi();
const VERSION_COMMIT_MESSAGE = '"Bump version to: %s [skip ci]"';

/** Get the last published version for a npm package */
async function getPublishedVersion(name: string) {
try {
return await execPromise("npm", ["view", name, "version"]);
return await execPromise("npm", [
"view",
name,
"version",
"--registry",
await getRegistry(),
]);
} catch (error) {}
}

Expand Down Expand Up @@ -453,6 +459,26 @@ const tagIndependentNextReleases = async (
);
};

/** Find an available canary version to publish */
const findAvailableCanaryVersion = async (
auto: Auto,
packageName: string,
startVersion: string
) => {
let canaryVersion = startVersion;

// eslint-disable-next-line no-await-in-loop
while (await getPublishedVersion(`${packageName}@${canaryVersion}`)) {
auto.logger.verbose.info(
`Version "${canaryVersion}" is taken! Trying another...`
);
canaryVersion = inc(canaryVersion, "prerelease")!;
}

auto.logger.verbose.info(`Version "${canaryVersion}" is available!`);
return canaryVersion;
};

/** Publish to NPM. Works in both a monorepo setting and for a single package. */
export default class NPMPlugin implements IPlugin {
/** The name of the plugin */
Expand Down Expand Up @@ -857,10 +883,11 @@ export default class NPMPlugin implements IPlugin {

if (!isIndependent) {
const { name } = getMonorepoPackage();
// eslint-disable-next-line no-await-in-loop
while (await getPublishedVersion(`${name}@${canaryVersion}`)) {
canaryVersion = inc(canaryVersion, "prerelease")!;
}
canaryVersion = await findAvailableCanaryVersion(
auto,
name,
canaryVersion
);
}

await execPromise("npx", [
Expand Down Expand Up @@ -927,18 +954,12 @@ export default class NPMPlugin implements IPlugin {
};
}

let canaryVersion = determineNextVersion(
lastRelease,
current,
bump,
preid
const canaryVersion = await findAvailableCanaryVersion(
auto,
name,
determineNextVersion(lastRelease, current, bump, preid)
);

// eslint-disable-next-line no-await-in-loop
while (await getPublishedVersion(`${name}@${canaryVersion}`)) {
canaryVersion = inc(canaryVersion, "prerelease")!;
}

if (this.canaryScope) {
await setCanaryScope(this.canaryScope, ["./"]);
}
Expand Down

0 comments on commit 7254217

Please sign in to comment.