-
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.
csv legacy without ids and new csv legacy with ids
- Loading branch information
Showing
3 changed files
with
139 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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 {prepareAdressesWithIds, prepareLieuxDitsWithIds} = require('../../formatters/csv-legacy.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-with-ids-${departement}.csv.gz`)) | ||
const lieuxDitsFile = createWriteStream(join(outputPath, `lieux-dits-with-ids-${departement}-beta.csv.gz`)) | ||
|
||
const adressesStream = createCsvWriteStream(adressesFile) | ||
const lieuxDitsStream = createCsvWriteStream(lieuxDitsFile) | ||
|
||
return { | ||
async writeAdresses({voies, numeros}) { | ||
prepareLieuxDitsWithIds({voies}) | ||
.forEach(ld => lieuxDitsStream.write(ld)) | ||
|
||
prepareAdressesWithIds({voies, numeros}) | ||
.forEach(a => adressesStream.write(a)) | ||
|
||
await Promise.all([ | ||
waitForDrain(adressesStream), | ||
waitForDrain(lieuxDitsStream) | ||
]) | ||
}, | ||
|
||
async finish() { | ||
adressesStream.end() | ||
lieuxDitsStream.end() | ||
|
||
await Promise.all([ | ||
finished(adressesFile), | ||
finished(lieuxDitsFile) | ||
]) | ||
} | ||
} | ||
} | ||
|
||
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