-
Notifications
You must be signed in to change notification settings - Fork 0
/
createJsonFile.js
65 lines (45 loc) · 1.5 KB
/
createJsonFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var fs = require( 'fs' ),
CHUNK_SIZE = 100000; //number of lines to write to disk at a time
function createJsonFile( lines, file, callback ){
console.log( 'Generating %s with %d lines', file, lines );
var writeStream = fs.createWriteStream( file, { flags: 'w+' } ),
i = 1,
output = '',
moreLinesThanChunks = ( lines > CHUNK_SIZE ),
totalChunks = moreLinesThanChunks ? Math.floor( lines / CHUNK_SIZE ) : 1,
chunk = ( moreLinesThanChunks ? lines / totalChunks : lines ),
percent = ( 100 / totalChunks ),
percentWritten = percent;
// console.log( 'total chunks: ' + totalChunks );
// console.log( 'chunk size: ' + chunk );
// console.log( 'percent: ' + percent );
// console.log( 'percentWritten: ' + percentWritten );
writeStream.on( 'close', function(){
console.log( 'File created!\n' );
callback();
} );
writeStream.write( "{\n" );
for( ; i <= lines; i++ ){
output += '"item' + i + '": "value' + i + '"' + ( i === lines ? '' : ',' ) + '\n';
if( i % chunk === 0 ){
writeStream.write( output );
output = '';
console.log( percentWritten.toFixed( 2 ) + '% complete' );
percentWritten += percent;
}
}
if( output.length > 0 ){
writeStream.write( output );
output = '';
}
writeStream.write( "}\n" );
writeStream.end();
}
process.on( 'message', function( params ){
if( params.name === 'createFile' ){
//console.log( 'createFile message received' );
createJsonFile( params.lines, params.file, function(){
process.send( { name: 'fileCreated' } );
} );
}
} );