From 0ea2e22029fcb543b7239db168476bb1941b83be Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sun, 15 Sep 2024 10:18:03 +0100 Subject: [PATCH] Improve output (#30) --- README.md | 1 + package-lock.json | 17 +++++++++-------- package.json | 1 + src/Options.ts | 18 ++++++++++++++---- src/RequestDefinitionBuilder.ts | 2 +- src/app.ts | 32 ++++++++++++++++++++++++-------- src/logging.ts | 30 ++++++++++++++++-------------- 7 files changed, 66 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index fa66b32..51e8247 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ CLI to convert a postman collection to httpyac file or files. * targetPath - path to the root of the .http files, will be created if it doesn't exist * ignoreHeaders - optional list of headers to ignore, useful when using default headers. Supports regex patterns * splitRequests - determines whether to split requests into separate files. Optional, defaults to true +* target - either file or console, defaults to file ## Request Lines diff --git a/package-lock.json b/package-lock.json index 460d5f2..ae3f872 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "httpyac-import", - "version": "0.5.1", + "version": "0.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "httpyac-import", - "version": "0.5.1", + "version": "0.6.0", "license": "ISC", "dependencies": { + "chalk": "^4.1.0", "postman-collection": "^4.5.0", "ts-command-line-args": "^2.5.1", "tslog": "^4.9.3" @@ -1561,9 +1562,9 @@ ] }, "node_modules/chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", + "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -5454,9 +5455,9 @@ "dev": true }, "chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", + "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", "requires": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" diff --git a/package.json b/package.json index 515a3f8..4f594ec 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "author": "Matthew Turner", "license": "ISC", "dependencies": { + "chalk": "^4.1.0", "postman-collection": "^4.5.0", "ts-command-line-args": "^2.5.1", "tslog": "^4.9.3" diff --git a/src/Options.ts b/src/Options.ts index 005d828..0f1ad17 100644 --- a/src/Options.ts +++ b/src/Options.ts @@ -6,6 +6,7 @@ const logger = rootLogger.getSubLogger(); export interface IOptions { sourcePath: string; targetPath: string; + target: string; ignoreHeaders?: string[]; splitRequests?: boolean, help?: boolean; @@ -17,7 +18,10 @@ export function parseOptions(): IOptions { type: String, alias: 's', optional: true as const, description: 'Path to the exported postman_collection.json' }, targetPath: { - type: String, alias: 'd', optional: true as const, description: 'Path to the root directory to output the .http files' + type: String, alias: 't', optional: true as const, description: 'Path to the root directory to output the .http files' + }, + target: { + type: String, alias: 'o', optional: true as const, description: 'Either console or file [default: file]' }, ignoreHeaders: { type: String, @@ -44,9 +48,15 @@ export function parseOptions(): IOptions { process.exit(1); } - if (options.targetPath === undefined) { - logger.error('Target path must be supplied with --targetPath=path'); - process.exit(2); + if (options.target === undefined) { + options.target = 'file'; + } + + if (options.target === 'file') { + if (options.targetPath === undefined) { + logger.error('Target path must be supplied with --targetPath=path'); + process.exit(2); + } } if (options.splitRequests === undefined) { diff --git a/src/RequestDefinitionBuilder.ts b/src/RequestDefinitionBuilder.ts index 4dae9b4..01354d6 100644 --- a/src/RequestDefinitionBuilder.ts +++ b/src/RequestDefinitionBuilder.ts @@ -160,7 +160,7 @@ export class RequestDefinitionBuilder { shouldInclude(header: string): boolean { for (const ignoreHeader of this._ignoreHeaders) { if (header.match(ignoreHeader)) { - this._logger.info(`Ignoring header ${header}...`); + this._logger.debug(`Ignoring header ${header}...`); return false; } } diff --git a/src/app.ts b/src/app.ts index c7b0f65..e2e2e35 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,8 @@ import { RequestDefinitionBuilder } from './RequestDefinitionBuilder'; import { parseOptions } from './Options'; import { rootLogger } from './logging'; +const packageInfo = JSON.parse(readFileSync('./package.json').toString()); + const logger = rootLogger.getSubLogger(); const options = parseOptions(); @@ -16,6 +18,10 @@ const targetPaths = [options.targetPath]; const sourcePostmanCollection = JSON.parse(readFileSync(options.sourcePath).toString()); const sourceCollection = new Collection(sourcePostmanCollection); +let lastTargetFilePath = ''; + +logger.info(`HttpYac Import v${packageInfo.version}\n`); + function processItems(items: PropertyList>) { for (const item of items.all()) { if (item instanceof Item) { @@ -33,15 +39,23 @@ function processItems(items: PropertyList>) { function processItem(item: Item) { const directory = outputDirectory(options, targetPaths); - if (!existsSync(directory)) { - logger.info(`Creating directory ${directory}...`); - mkdirSync(directory, { recursive: true }); + if (options.target != 'console') { + if (!existsSync(directory)) { + logger.info(`Creating directory ${directory}...`); + mkdirSync(directory, { recursive: true }); + } } const filePath = outputPathFor(item, options, targetPaths); - logger.info(`Outputting to file ${filePath}...`); - logger.info('Writing request definition...'); + if (lastTargetFilePath == filePath) { + logger.debug(`Appending request ${item.name}...`); + } else { + lastTargetFilePath = filePath; + logger.info(`Outputting to file ${filePath}...`); + logger.debug(`Writing request ${item.name}...`); + } + const requestDefinition = new RequestDefinitionBuilder() .ignoreHeaders(options.ignoreHeaders) .includeSeparatorIf(existsSync(filePath)) @@ -49,9 +63,11 @@ function processItem(item: Item) { .build() .toString(); - logger.info(requestDefinition); - - writeFileSync(filePath, requestDefinition, { flag: 'a' }); + if (options.target == 'console') { + logger.debug(requestDefinition); + } else { + writeFileSync(filePath, requestDefinition, { flag: 'a' }); + } } processItems(sourceCollection.items); \ No newline at end of file diff --git a/src/logging.ts b/src/logging.ts index a26cd34..798bd09 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -1,18 +1,20 @@ -import { Logger, ILogObj } from 'tslog'; +import { Logger, ILogObj, IMeta } from 'tslog'; +import chalk = require('chalk'); -export const rootLogger = new Logger({ - prettyLogTemplate: "{{logLevelName}}\t", - prettyLogStyles: { - logLevelName: { - "*": ["bold", "black", "bgWhiteBright", "dim"], - SILLY: ["bold", "white"], - TRACE: ["bold", "whiteBright"], - DEBUG: ["bold", "green"], - INFO: ["bold", "blue"], - WARN: ["bold", "yellow"], - ERROR: ["bold", "red"], - FATAL: ["bold", "redBright"], - } +export const rootLogger = new Logger({ type: 'hidden' }); + +rootLogger.attachTransport((logObj: ILogObj) => { + const meta = logObj._meta; + switch (meta.logLevelId) { + case 3: + console.log(chalk.blueBright(logObj[0])); + break; + case 4: + console.log(chalk.yellowBright(logObj[0])); + break; + default: + console.log(logObj[0]); + break; } });