Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const promisify = require('util').promisify;
- const path = require('path');
- const fs = require('fs');
- const readdirp = promisify(fs.readdir);
- const statp = promisify(fs.stat);
- async function countFileLines(file) {
- let count = 0;
- fs.createReadStream(file).on('data', function(chunk) {
- count += chunk.toString('utf8')
- .split(/\r\n|[\n\r\u0085\u2028\u2029]/g)
- .length-1;
- }).on('end', function() {
- lines += count;
- console.log(file, count);
- console.log(lines);
- }).on('error', function(err) {
- console.error(err);
- });
- }
- async function count(files) {
- for (let file of files) {
- await countFileLines(file, lines);
- }
- }
- async function scanDir(dir, results = []) {
- let files = await readdirp(dir);
- for (let f of files) {
- let fullPath = path.join(dir, f);
- let stat = await statp(fullPath);
- if (stat.isDirectory()) {
- await scanDir(fullPath, results);
- } else {
- results.push(fullPath);
- }
- }
- return results;
- }
- async function executeAsync(path) {
- await scanDir(path).then(files => count(files)).then(() => console.log(lines));
- }
- let lines = 0;
- const start = new Date();
- executeAsync('/Users/benroszko/Desktop/MyProjects/studia/SemestrV/TW/node/PAM08');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement