-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint camelcase: off */ | ||
const {promisify} = require('util') | ||
const {join} = require('path') | ||
const {createWriteStream} = require('fs') | ||
const {createGzip} = require('zlib') | ||
const finished = promisify(require('stream').finished) | ||
const {ensureDir} = require('fs-extra') | ||
const csvWriter = require('csv-write-stream') | ||
const pumpify = require('pumpify') | ||
|
||
const {prepareAdresses} = require('../../formatters/csv-bal.cjs') | ||
|
||
function createCsvWriteStream(file) { | ||
return pumpify.obj( | ||
csvWriter({separator: ';'}), | ||
createGzip(), | ||
file | ||
) | ||
} | ||
|
||
function waitForDrain(stream) { | ||
if (stream.writableLength > stream.writableHighWaterMark) { | ||
return new Promise(resolve => { | ||
stream.once('drain', resolve) | ||
}) | ||
} | ||
} | ||
|
||
async function createWriter(outputPath, departement) { | ||
await ensureDir(outputPath) | ||
const adressesFile = createWriteStream(join(outputPath, `adresses-${departement}.csv.gz`)) | ||
const adressesStream = createCsvWriteStream(adressesFile) | ||
|
||
return { | ||
async writeAdresses({voies, numeros}) { | ||
prepareAdresses({voies, numeros}, {includesAlt: true}) | ||
.forEach(a => adressesStream.write(a)) | ||
|
||
await waitForDrain(adressesStream) | ||
}, | ||
|
||
async finish() { | ||
adressesStream.end() | ||
await finished(adressesFile) | ||
} | ||
} | ||
} | ||
|
||
module.exports = {createWriter} |
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