-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JSONStream and createWriteStream() to output large json stats obj…
…ects with Better tests
- Loading branch information
Showing
6 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const mkdir = require('mkdirp'); | ||
const JSONStream = require('JSONStream'); | ||
|
||
module.exports = { | ||
streamObjectToJson | ||
}; | ||
|
||
async function streamObjectToJson(filepath, data) { | ||
await new Promise((resolve, reject) => { | ||
mkdir.sync(path.dirname(filepath)); | ||
|
||
const transformStream = JSONStream.stringifyObject(); | ||
const writableStream = fs.createWriteStream(filepath); | ||
transformStream.pipe(writableStream); | ||
transformStream.on('error', (err) => { | ||
reject(err); | ||
}); | ||
transformStream.on('end', () => { | ||
writableStream.end(); | ||
resolve(); | ||
}); | ||
Object.keys(data).forEach((key) => { | ||
transformStream.write([key, data[key]]); | ||
}); | ||
transformStream.end(); | ||
}); | ||
} |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const fs = require('fs'); | ||
const del = require('del'); | ||
const path = require('path'); | ||
|
||
const { streamObjectToJson } = require('../lib/fileUtils'); | ||
|
||
const OUTPUT_DIR = `${__dirname}/output`; | ||
|
||
describe('fileUtils', function () { | ||
before(function () { | ||
del.sync(`${__dirname}/output`); | ||
}); | ||
|
||
afterEach(function () { | ||
del.sync(`${__dirname}/output`); | ||
}); | ||
|
||
it('should print objects to files', async function () { | ||
const statsFilename = path.resolve(OUTPUT_DIR, 'stats.json'); | ||
await streamObjectToJson(statsFilename, { | ||
string: 'webpack-bundle-analyzer', | ||
list: ['foo', 'bar', 'baz'], | ||
obj: { | ||
name: 'webpack-bundle-analyzer', | ||
foo: 'bar', | ||
baz: null | ||
} | ||
}); | ||
|
||
await setImmediate(() => { | ||
expect(fs.existsSync(statsFilename)).to.be.true; | ||
|
||
const fileContent = fs.readFileSync(statsFilename, 'utf8'); | ||
expect(JSON.parse(fileContent)).to.deep.equal({ | ||
string: 'webpack-bundle-analyzer', | ||
list: ['foo', 'bar', 'baz'], | ||
obj: { | ||
name: 'webpack-bundle-analyzer', | ||
foo: 'bar', | ||
baz: null | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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