Skip to content

Commit

Permalink
feat(): Add destination directory (#22)
Browse files Browse the repository at this point in the history
* Added destination directory

* Added output directory

* fix(): fix output dir option
  • Loading branch information
vinay631 authored and Ben Ross committed Jun 29, 2018
1 parent 882f3b0 commit 8c318df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ parser.addArgument(['-f', '--file'], {
defaultValue: 'env.yml'
});

parser.addArgument(['-o', '--output-dir'], {
help: 'Output directory of .env',
dest: 'outputdir',
defaultValue: process.cwd()
});

const args = parser.parseArgs();
const config = loadConfig();

Expand All @@ -38,8 +44,8 @@ rewriter.rewrite(document).then(result => {
if (errors.length) {
throw new Error(`Validation errors found in result:\n${errors.join("\n")}`);
}
console.info(`Writing .env file to ${process.cwd()}/.env`);
writeFile(result);
console.info(`Writing .env file to ${args.outputdir}/.env`);
writeFile(result, args.outputdir);
}).catch((error: Error) => {
console.error(`Could not write .env file: ${error.stack}`);
process.exit(1);
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import * as fs from 'fs';
import * as path from 'path';

import { Document, InputDocument, Config } from './types';
import { resolvers } from './resolvers';

export function writeFile(document: Document) {
export function writeFile(document: Document, outputdir?: string) {
let output = '';
const keys = Object.keys(document);
for (const key of keys) {
if (document[key] !== undefined) {
output += `${key}=${document[key]}\n`;
}
}
fs.writeFileSync('.env', output);
let filename = '.env';
if (outputdir) {
filename = path.join(outputdir, filename);
}

fs.writeFileSync(filename, output);
}


Expand Down

0 comments on commit 8c318df

Please sign in to comment.