Skip to content

Commit

Permalink
better error message for no NPM_token in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Mar 14, 2021
1 parent be5c068 commit 6f883b0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/utils/exec-promise.ts
Expand Up @@ -81,7 +81,13 @@ export default async function execPromise(
} else {
// Tools can occasionally print to stderr but not fail, so print that just in case.
if (allStderr.length) {
log.log.warn(allStderr);
if (allStderr.includes("Failed to replace env in config")) {
const error = new Error(allStderr);
error.stack = (error.stack || "") + callSite;
reject(error);
} else {
log.log.warn(allStderr);
}
}

// Resolve the string of the whole stdout
Expand Down
1 change: 1 addition & 0 deletions plugins/npm/package.json
Expand Up @@ -41,6 +41,7 @@
"@auto-it/core": "link:../../packages/core",
"@auto-it/package-json-utils": "link:../../packages/package-json-utils",
"await-to-js": "^2.1.1",
"endent": "^2.0.1",
"env-ci": "^5.0.1",
"fp-ts": "^2.5.3",
"get-monorepo-packages": "^1.1.0",
Expand Down
32 changes: 29 additions & 3 deletions plugins/npm/src/index.ts
@@ -1,6 +1,7 @@
import envCi from "env-ci";
import * as fs from "fs";
import path from "path";
import endent from "endent";
import { Memoize as memoize } from "typescript-memoize";
import { RestEndpointMethodTypes } from "@octokit/rest";
import * as t from "io-ts";
Expand Down Expand Up @@ -983,7 +984,7 @@ export default class NPMPlugin implements IPlugin {
this.name,
async ({ bump, canaryIdentifier, dryRun, quiet }) => {
if (this.setRcToken) {
await setTokenOnCI(auto.logger);
await this.setTokenOnCI(auto);
auto.logger.verbose.info("Set CI NPM_TOKEN");
}

Expand Down Expand Up @@ -1181,7 +1182,7 @@ export default class NPMPlugin implements IPlugin {
this.name,
async (preReleaseVersions, { bump, dryRun }) => {
if (this.setRcToken) {
await setTokenOnCI(auto.logger);
await this.setTokenOnCI(auto);
auto.logger.verbose.info("Set CI NPM_TOKEN");
}

Expand Down Expand Up @@ -1360,7 +1361,7 @@ export default class NPMPlugin implements IPlugin {
}

if (this.setRcToken) {
await setTokenOnCI(auto.logger);
await this.setTokenOnCI(auto);
auto.logger.verbose.info("Set CI NPM_TOKEN");
}

Expand Down Expand Up @@ -1495,4 +1496,29 @@ export default class NPMPlugin implements IPlugin {
}
});
}

/** The the NPM token */
private async setTokenOnCI(auto: Auto) {
try {
await setTokenOnCI(auto.logger);
} catch (error) {
if (
// eslint-disable-next-line no-template-curly-in-string
error.message?.includes("Failed to replace env in config: ${NPM_TOKEN}")
) {
auto.logger.log.error(endent`
Uh oh! It looks like you don\'t have a NPM_TOKEN available in your environment.
To fix:
- Ensure you've added a NPM_TOKEN environment variable
- Ensure that it's exposed to your CI step
`);
auto.logger.verbose.error(error);
process.exit(1);
} else {
throw error;
}
}
}
}

0 comments on commit 6f883b0

Please sign in to comment.