diff --git a/src/cli/commands/help.ts b/src/cli/commands/help.ts index 2cd48c781d..27b6697dac 100644 --- a/src/cli/commands/help.ts +++ b/src/cli/commands/help.ts @@ -1,8 +1,17 @@ import * as fs from 'fs'; import * as path from 'path'; +import stripAnsi from 'strip-ansi'; const DEFAULT_HELP = 'snyk'; +const readHelpFile = (filename: string): string => { + const file = fs.readFileSync(filename, 'utf8'); + if (typeof process.env.NO_COLOR !== 'undefined' || !process.stdout.isTTY) { + return stripAnsi(file); + } + return file; +}; + export = async function help(item: string | boolean) { if (!item || item === true || typeof item !== 'string' || item === 'help') { item = DEFAULT_HELP; @@ -18,13 +27,13 @@ export = async function help(item: string | boolean) { '../../../help/commands-txt', item === DEFAULT_HELP ? DEFAULT_HELP + '.txt' : `snyk-${item}.txt`, ); - return fs.readFileSync(filename, 'utf8'); + return readHelpFile(filename); } catch (error) { const filename = path.resolve( __dirname, '../../../help/commands-txt', DEFAULT_HELP + '.txt', ); - return fs.readFileSync(filename, 'utf8'); + return readHelpFile(filename); } }; diff --git a/test/smoke/spec/snyk_basic_spec.sh b/test/smoke/spec/snyk_basic_spec.sh index fbe918e717..39874d1cfb 100644 --- a/test/smoke/spec/snyk_basic_spec.sh +++ b/test/smoke/spec/snyk_basic_spec.sh @@ -87,6 +87,30 @@ Describe "Snyk CLI basics" # TODO: unusable with our current docker issues The stderr should equal "" End + + Describe "prints help info without ascii escape sequences" + It "has NO_COLOR set" + snyk_help_no_color() { + NO_COLOR='' snyk help + } + + When run snyk_help_no_color + The output should not include "[1mN" + The output should not include "[0m" + The output should not include "[4mC" + End + + It "is not tty" + snyk_help_no_tty() { + snyk help | cat + } + + When run snyk_help_no_tty + The output should not include "[1mN" + The output should not include "[0m" + The output should not include "[4mC" + End + End End Describe "snyk config"