Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const promisify = require('util').promisify;
  2. const path = require('path');
  3. const fs = require('fs');
  4. const readdirp = promisify(fs.readdir);
  5. const statp = promisify(fs.stat);
  6.  
  7.  
  8. async function countFileLines(file) {
  9.     let count = 0;
  10.  
  11.     fs.createReadStream(file).on('data', function(chunk) {
  12.         count += chunk.toString('utf8')
  13.         .split(/\r\n|[\n\r\u0085\u2028\u2029]/g)
  14.         .length-1;
  15.     }).on('end', function() {
  16.         lines += count;
  17.         console.log(file, count);
  18.         console.log(lines);
  19.     }).on('error', function(err) {
  20.         console.error(err);
  21.     });
  22. }
  23.  
  24. async function count(files) {
  25.     for (let file of files) {
  26.         await countFileLines(file, lines);
  27.     }
  28. }
  29.  
  30. async function scanDir(dir, results = []) {
  31.     let files = await readdirp(dir);
  32.     for (let f of files) {
  33.         let fullPath = path.join(dir, f);
  34.         let stat = await statp(fullPath);
  35.         if (stat.isDirectory()) {
  36.             await scanDir(fullPath, results);
  37.         } else {
  38.             results.push(fullPath);
  39.         }
  40.     }
  41.     return results;
  42. }
  43.  
  44. async function executeAsync(path) {
  45.     await scanDir(path).then(files => count(files)).then(() => console.log(lines));
  46. }
  47.  
  48. let lines = 0;
  49. const start = new Date();
  50.  
  51. executeAsync('/Users/benroszko/Desktop/MyProjects/studia/SemestrV/TW/node/PAM08');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement