Skip to content

Commit

Permalink
Improve output (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 15, 2024
1 parent 5fb8036 commit 0ea2e22
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 14 additions & 4 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = rootLogger.getSubLogger();
export interface IOptions {
sourcePath: string;
targetPath: string;
target: string;
ignoreHeaders?: string[];
splitRequests?: boolean,
help?: boolean;
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/RequestDefinitionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
32 changes: 24 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Item | ItemGroup<Item>>) {
for (const item of items.all()) {
if (item instanceof Item) {
Expand All @@ -33,25 +39,35 @@ function processItems(items: PropertyList<Item | ItemGroup<Item>>) {
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))
.from(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);
30 changes: 16 additions & 14 deletions src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Logger, ILogObj } from 'tslog';
import { Logger, ILogObj, IMeta } from 'tslog';
import chalk = require('chalk');

export const rootLogger = new Logger<ILogObj>({
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<ILogObj>({ type: 'hidden' });

rootLogger.attachTransport((logObj: ILogObj) => {
const meta = <IMeta>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;
}
});

0 comments on commit 0ea2e22

Please sign in to comment.