Skip to content

Commit

Permalink
feat: check if terminal supports TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Feb 13, 2024
1 parent 72284ea commit 07bcea0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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", {
Expand Down
20 changes: 18 additions & 2 deletions packages/composable-cli/src/commands/generate/generate-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<SetStoreCommandArguments> {
Expand Down
14 changes: 6 additions & 8 deletions packages/composable-cli/src/util/is-tty.ts
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 07bcea0

Please sign in to comment.