Skip to content

Commit

Permalink
Merge pull request #11 from yayoc/7-add-dir-option
Browse files Browse the repository at this point in the history
Add an -d option to specify output directory
  • Loading branch information
yayoc authored Feb 25, 2019
2 parents b34ba3d + d34d5d2 commit e16044b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const log = console.log;
log(chalk.yellowBright("swagger-to-mock"));
commander
.arguments("<file>")
.action(async file => {
.option("-d, --dir <name>", "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));
Expand Down
5 changes: 3 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e16044b

Please sign in to comment.