From b74131ccffee067c4556af198427dbd5a525a5c8 Mon Sep 17 00:00:00 2001 From: Juan Norris Date: Thu, 25 Feb 2021 10:11:31 -0300 Subject: [PATCH] Add quiet option to CLI, to suppress non-error logs --- package.json | 2 +- src/cli-launcher.ts | 8 +++++++- src/cli.ts | 19 ++++++++++--------- src/logger.ts | 10 ++++++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 19d9e62..99c834a 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphql-schema-utilities", - "version": "1.1.3", + "version": "1.1.4", "description": "Merge GraphQL Schema files and validate queries against the merged Schema", "repository": { "type": "git", diff --git a/src/cli-launcher.ts b/src/cli-launcher.ts index b2e6dae..4531ec3 100755 --- a/src/cli-launcher.ts +++ b/src/cli-launcher.ts @@ -26,6 +26,8 @@ program 'Use a glob that that contains your graphql operation files to test against the merged schema file.', '') .option('-d, --includeDirectives', 'By default will NOT merge the directives, unless you added this flag.', false) + .option('-q, --quiet', + 'Run in quite mode, suppressing all logs except errors.', false) .parse(process.argv); if (!program.schema) { @@ -49,7 +51,11 @@ if (!program.schema) { ? '\n subscription: Subscription' : ''} \n}\n\n`; data = typeDefs + data; } - process.stdout.write(data); + + if (!program.quiet) { + process.stdout.write(data); + } + if (program.output) { ensureDirectoryExistence(program.output); fs.writeFile(program.output, data, (err) => { diff --git a/src/cli.ts b/src/cli.ts index 133b18b..9ca77b1 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -13,6 +13,7 @@ import { makeExecutableSchema, } from 'graphql-tools'; import * as validator from './index'; +import { consoleLogger } from './logger'; import { ValidationRule } from './types'; import * as utils from './utilities'; import { validateSchemaWithSourceMap } from './validate-schema'; @@ -24,11 +25,11 @@ import { validateSchemaWithSourceMap } from './validate-schema'; */ export function mergeGQLSchemas(schemaPattern: string): Promise { return new Promise((resolve, reject) => { - console.log(`\nLoading schema from ${schemaPattern}`); + consoleLogger.log(`\nLoading schema from ${schemaPattern}`); utils.readGlob(schemaPattern).then((files) => { if (!files.length) { const noFilesMatching = `No matching files were found with Glob: ${schemaPattern}.`; - console.error(noFilesMatching); + consoleLogger.error(noFilesMatching); reject(new Error(noFilesMatching)); } Promise.all( @@ -37,7 +38,7 @@ export function mergeGQLSchemas(schemaPattern: string): Promise { .readFile(file) .then((subSchema: string) => subSchema) .catch((error) => { - console.error( + consoleLogger.error( `An error occurred while trying to read your graphql schemas in ${schemaPattern}. \n`, error, ); @@ -71,25 +72,25 @@ export function validateOperations( rules?: ReadonlyArray, ): Promise { return new Promise((resolve, reject) => { - console.log( + consoleLogger.log( `\nValidating queries for ${queriesPattern} using loaded schema`, ); function outputErrors(errs) { - console.log('\nErrors found:'); + consoleLogger.log('\nErrors found:'); errs.forEach((err) => { - console.log(`\nFile: ${err.file}`); + consoleLogger.log(`\nFile: ${err.file}`); err.errors.forEach((errStr) => { - console.log(`\t${errStr}`); + consoleLogger.log(`\t${errStr}`); }); }); - console.log('\n'); + consoleLogger.log('\n'); } validator .validateQueryFiles(queriesPattern, validSchema, rules) .then(() => { - console.log('All queries are valid\n'); + consoleLogger.log('All queries are valid\n'); resolve(); }) .catch((errs) => { diff --git a/src/logger.ts b/src/logger.ts index 060c688..dc478ef 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,5 +1,7 @@ import chalk from 'chalk'; +import * as program from 'commander'; + export interface ILogger { error(...args); warn(...args); @@ -12,11 +14,15 @@ export class ConsoleLogger implements ILogger { } public log(message: string, ...args) { - console.log(message, args.length ? args : ''); + if (!program.quiet) { + console.log(message, args.length ? args : ''); + } } public warn(warnMessage: string, ...args) { - console.warn(chalk.yellow(warnMessage), args.length ? args : ''); + if (!program.quiet) { + console.warn(chalk.yellow(warnMessage), args.length ? args : ''); + } } }