Skip to content

Commit

Permalink
Support purge target (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 15, 2024
1 parent 0ea2e22 commit eeb2629
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ CLI to convert a postman collection to httpyac file or files.
## Usage
`httpyac-import --sourcePath=sample.postman_collection.json --targetPath=output`

* sourcePath - path to the postman collection json file
* sourcePath - path to the Postman collection json file
* 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
* ignoreHeaders - optional list of headers to ignore, useful when using default headers. Supports regex patterns (eg SomeHeader.*)
* splitRequests - determines whether to split requests into separate files. Optional, defaults to true
* target - either file or console, defaults to file
* purgeTargetPath - determines whether to delete target path. Optional, defaults to false

## Request Lines

Expand Down Expand Up @@ -112,8 +113,5 @@ There is no current support for:
* Non-javascript scripting languages
* Non-JSON body in POST/PUT requests

## Known Issues
* Mandatory parameters - the sourcePath and targetPath should be mandatory but [ts-command-line-args ](https://www.npmjs.com/package/ts-command-line-args) seems to demand they be set as optional

## Running from the repository
`npm run import -- -- --sourcePath=sample.postman_collection.json --targetPath=output`
`npm run import -- -- --sourcePath=sample.postman_collection.json --targetPath=output`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "httpyac-import",
"version": "0.6.0",
"version": "1.0.0",
"description": "CLI to convert Postman collection to httpyac files",
"homepage": "https://github.com/matthewturner/httpyac-import",
"repository": {
Expand Down
10 changes: 9 additions & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IOptions {
target: string;
ignoreHeaders?: string[];
splitRequests?: boolean,
purgeTargetPath?: boolean,
help?: boolean;
}

Expand All @@ -34,6 +35,9 @@ export function parseOptions(): IOptions {
splitRequests: {
type: Boolean, alias: 'f', optional: true as const, description: 'Determines whether to split requests into separate files [default: true]'
},
purgeTargetPath: {
type: Boolean, alias: 'p', optional: true as const, description: 'Deletes target path and all contents [default: false]'
},
help: {
type: Boolean, optional: true, alias: 'h', description: 'Prints this usage guide'
},
Expand All @@ -60,9 +64,13 @@ export function parseOptions(): IOptions {
}

if (options.splitRequests === undefined) {
logger.warn('One file will be created per request');
logger.warn('Requests will be split into separate files. Control with --splitRequests=[true|false]');
options.splitRequests = true;
}

if (options.purgeTargetPath === undefined) {
options.purgeTargetPath = false;
}

return options;
}
13 changes: 10 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node

import { writeFileSync, readFileSync, mkdirSync, existsSync } from 'fs';
import { writeFileSync, readFileSync, mkdirSync, existsSync, rmSync } from 'fs';
import { Collection, ItemGroup, Item, PropertyList } from 'postman-collection';
import { sanitize, outputDirectory, outputPathFor } from './helpers';
import { RequestDefinitionBuilder } from './RequestDefinitionBuilder';
Expand All @@ -11,17 +11,24 @@ const packageInfo = JSON.parse(readFileSync('./package.json').toString());

const logger = rootLogger.getSubLogger();

logger.silly(`HttpYac Import v${packageInfo.version}`);

const options = parseOptions();

if (options.purgeTargetPath) {
if (existsSync(options.targetPath)) {
logger.warn(`Purging target path ${options.targetPath}...`)
rmSync(options.targetPath, { recursive: true });
}
}

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 Down
5 changes: 5 additions & 0 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ export const rootLogger = new Logger<ILogObj>({ type: 'hidden' });
rootLogger.attachTransport((logObj: ILogObj) => {
const meta = <IMeta>logObj._meta;
switch (meta.logLevelId) {
case 0:
console.log(chalk.whiteBright(logObj[0]));
break;
case 3:
console.log('');
console.log(chalk.blueBright(logObj[0]));
break;
case 4:
console.log('');
console.log(chalk.yellowBright(logObj[0]));
break;
default:
Expand Down

0 comments on commit eeb2629

Please sign in to comment.