diff --git a/src/index.ts b/src/index.ts index 8766ede..d32ed74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,13 +13,15 @@ const log = console.log; log(chalk.yellowBright("swagger-to-mock")); commander .arguments("") - .action(async file => { + .option("-d, --dir ", "output directory") + .action(async (file, cmd) => { try { const content = parse(file); const responses = extractResponses(content); const schemas = extractSchemas(content); const composed = composeMockData(responses, schemas); - writeFiles(composed, log); + const outputPath = cmd.dir ? cmd.dir : "."; + writeFiles(composed, outputPath, log); log(chalk.yellowBright("Completed")); } catch (e) { log(chalk.redBright(e)); diff --git a/src/util.ts b/src/util.ts index 56c2e96..11f8e66 100644 --- a/src/util.ts +++ b/src/util.ts @@ -21,12 +21,13 @@ export const normalizePath = (path: string): string => { // Write each response to JSON files. export const writeFiles = ( data: { [file: string]: any }, + outputPath: string, log: (message: any) => void ): void => { Object.keys(data).forEach(key => { const val = data[key]; - const fileName = `${key}.json` - const path = join(".", fileName); + const fileName = `${key}.json`; + const path = join(outputPath, fileName); log(fileName); const formatted = JSON.stringify(val, null, 2); fs.writeFileSync(path, formatted);