diff --git a/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx b/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx index 0a1508a7..bd5efc7f 100644 --- a/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx +++ b/packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx @@ -39,6 +39,7 @@ import { Option } from "../utils/json-schema" import { createActiveStoreMiddleware, createAuthenticationCheckerMiddleware, + createTTYCheckMiddleware, } from "../generate-command" import { detect } from "../../../lib/detect-package-manager" import { getCredentials } from "../../../lib/authentication/get-token" @@ -70,6 +71,7 @@ export function createD2CCommand( describe: "generate Elastic Path storefront", builder: async (yargs) => { const result = yargs + .middleware(createTTYCheckMiddleware(ctx)) .middleware(createAuthenticationCheckerMiddleware(ctx)) .middleware(createActiveStoreMiddleware(ctx)) .positional("location", { diff --git a/packages/composable-cli/src/commands/generate/generate-command.tsx b/packages/composable-cli/src/commands/generate/generate-command.tsx index 95de535a..a620578f 100644 --- a/packages/composable-cli/src/commands/generate/generate-command.tsx +++ b/packages/composable-cli/src/commands/generate/generate-command.tsx @@ -16,8 +16,8 @@ import { isAuthenticated } from "../../util/check-authenticated" import { trackCommandHandler } from "../../util/track-command-handler" import { isTTY } from "../../util/is-tty" import { SetStoreCommandArguments } from "../store/store.types" -import { renderInfo } from "../ui" -import { outputContent } from "../output" +import { renderInfo, renderWarning } from "../ui" +import { outputContent, outputToken } from "../output" import chalk from "chalk" export function createGenerateCommand( @@ -110,6 +110,22 @@ export function createAuthenticationCheckerMiddleware( } } +export function createTTYCheckMiddleware( + _ctx: CommandContext, +): MiddlewareFunction { + return async function ttyCheckMiddleware(_args: yargs.ArgumentsCamelCase) { + if (!isTTY()) { + renderWarning({ + headline: "Non-interactive environment detected", + body: outputContent`This command requires a ${outputToken.cyan( + "TTY", + )} and may not work in non-interactive environments.`.value, + }) + } + return + } +} + export function createActiveStoreMiddleware( ctx: CommandContext, ): MiddlewareFunction { diff --git a/packages/composable-cli/src/util/is-tty.ts b/packages/composable-cli/src/util/is-tty.ts index be00e8d6..77681464 100644 --- a/packages/composable-cli/src/util/is-tty.ts +++ b/packages/composable-cli/src/util/is-tty.ts @@ -1,16 +1,14 @@ export function isTTY(): boolean { - const isTruthy = (value: undefined | string) => { - // Returns true if value is a string that is anything but 0 or false. - return ( - value !== undefined && value !== "0" && value.toUpperCase() !== "FALSE" - ) - } - // If we force TTY, we always return true. - const force = process.env["NG_FORCE_TTY"] + const force = process.env["COMPOSABLE_FORCE_TTY"] if (force !== undefined) { return isTruthy(force) } return !!process.stdout.isTTY && !isTruthy(process.env["CI"]) } + +function isTruthy(value: undefined | string) { + // Returns true if value is a string that is anything but 0 or false. + return value !== undefined && value !== "0" && value.toUpperCase() !== "FALSE" +}