aboutsummaryrefslogtreecommitdiff
path: root/CI/utils/walk.js
blob: 899128daa33766481b1587f41c8b89bc3c8b2daa (plain)
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
var fs = require('fs');
var path = require('path');
var walk = function (dir, done) {
	var results = [];
	fs.readdir(dir, function (err, list) {
		if (err) return done(err);
		var pending = list.length;
		if (!pending) return done(null, results);
		list.forEach(function (file) {
			file = path.resolve(dir, file);
			fs.stat(file, function (err, stat) {
				if (stat && stat.isDirectory()) {
					walk(file, function (err, res) {
						results = results.concat(res);
						if (!--pending) done(null, results);
					});
				} else {
					results.push(file);
					if (!--pending) done(null, results);
				}
			});
		});
	});
};

async function walkP(dir) { //walk function but promisified
	return new Promise((res, rej) => {
		walk(dir, (err, resu) => {
			if (err) {
				rej(err)
				return
			}

			res(resu)
		});
	})
}

module.exports = { walk, walkP } //https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
//i was just too lazy to code this myself OMEGALUL