Created
October 29, 2018 16:23
-
-
Save danieldiekmeier/e45542bf8d47942e91b202573ad8deee to your computer and use it in GitHub Desktop.
JSONLog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fsExtra from 'fs-extra' | |
export default function jsonLog (filepath) { | |
return async function (data) { | |
await fsExtra.ensureFile(filepath) | |
await fsExtra.appendFile(filepath, JSON.stringify(data) + '\n') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import test from 'ava' | |
import path from 'path' | |
import os from 'os' | |
import fs from 'fs' | |
import jsonLog from '../json-log.js' | |
test('writes json to a file', async t => { | |
const filepath = path.join(os.tmpdir(), 'test-' + Math.random() + '.txt') | |
const logger = jsonLog(filepath) | |
await logger({ fields: 'example', problems: [1, 2, 3] }) | |
await logger({ fields: 'example2', problems: [1, 2, 3] }) | |
const file = fs.readFileSync(filepath, 'utf-8') | |
t.is(file, `{"fields":"example","problems":[1,2,3]}\n{"fields":"example2","problems":[1,2,3]}\n`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment