From 6f883b0d86022e86853f56f2863c4e1ff4867351 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Sun, 14 Mar 2021 12:41:44 -0700 Subject: [PATCH] better error message for no NPM_token in CI --- packages/core/src/utils/exec-promise.ts | 8 ++++++- plugins/npm/package.json | 1 + plugins/npm/src/index.ts | 32 ++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/core/src/utils/exec-promise.ts b/packages/core/src/utils/exec-promise.ts index 2408eb6dc..5d9f701a1 100644 --- a/packages/core/src/utils/exec-promise.ts +++ b/packages/core/src/utils/exec-promise.ts @@ -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 diff --git a/plugins/npm/package.json b/plugins/npm/package.json index 005883313..b05460c96 100644 --- a/plugins/npm/package.json +++ b/plugins/npm/package.json @@ -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", diff --git a/plugins/npm/src/index.ts b/plugins/npm/src/index.ts index 0c5e38c3e..dd127e899 100644 --- a/plugins/npm/src/index.ts +++ b/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"; @@ -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"); } @@ -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"); } @@ -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"); } @@ -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; + } + } + } }