SHARE
TWEET

Untitled

a guest Mar 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3. const workingDir = path.join(__dirname, 'dumps');
  4. if (!fs.existsSync(workingDir)) {
  5.   fs.mkdirSync(workingDir);
  6. }
  7.  
  8. const workingFile = path.join(workingDir, 'sample.txt');
  9.  
  10. function getFilesizeInBytes(fPath) {
  11.   try {
  12.     const stats = fs.statSync(fPath);
  13.     return stats.isFile() ? stats.size : 0;
  14.   } catch (error) {
  15.     return 0;
  16.   }
  17. }
  18.  
  19. function writeToFile(stream, fPath, index = 1, step = 1000000, maxFileSizeMB = 100) {
  20.   const length = index + step
  21.   stream.once('finish', function(fd) {
  22.     const fileSizeinMB = getFilesizeInBytes(fPath) / 1000000.0
  23.     if (fileSizeinMB < maxFileSizeMB) {
  24.       execute(length, true)
  25.     } else {
  26.       console.log(`File finish size: ${getFilesizeInBytes(fPath) / 1000000.0} MB ~ Length: ${length -1} rows`)
  27.     }
  28.   })
  29.   stream.once('open', function(fd) {
  30.     for (var i = index; i < length; i++) {
  31.       stream.write(`Row${i}\n`)
  32.     }
  33.     stream.end()
  34.   })
  35. }
  36.  
  37. function execute(index = 1, append = false) {
  38.   const stream = fs.createWriteStream(workingFile, { flags: append ? 'a' : 'w' });
  39.   writeToFile(stream, workingFile, index);
  40. }
  41. execute();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top