Skip to content

Commit ec8407f

Browse files
vladimyragnivade
authored andcommitted
Asynchronously build shortIndex
- refactoring cache crawler to use concurrent async approach
1 parent d2826b1 commit ec8407f

File tree

5 files changed

+59
-39
lines changed

5 files changed

+59
-39
lines changed

lib/index.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ function readShortPagesIndex() {
106106
})
107107
.catch(() => {
108108
// File is not present; we need to create the index.
109-
let idx = buildShortPagesIndex();
109+
return buildShortPagesIndex();
110+
})
111+
.then((idx) => {
110112
if (Object.keys(idx).length <= 0) {
111113
return idx;
112114
}
@@ -118,23 +120,24 @@ function readShortPagesIndex() {
118120
}
119121

120122
function buildShortPagesIndex() {
121-
try {
122-
let files = utils.walkSync(pagesPath);
123-
files = files.filter(utils.isPage);
124-
let reducer = (index, file) => {
125-
let os = utils.parsePlatform(file);
126-
let page = utils.parsePagename(file);
127-
if (index[page]) {
128-
index[page].push(os);
129-
} else {
130-
index[page] = [os];
131-
}
132-
return index;
133-
};
134-
return files.reduce(reducer, {});
135-
} catch (e) {
136-
return {};
137-
}
123+
return utils.walk(pagesPath)
124+
.then((files) => {
125+
files = files.filter(utils.isPage);
126+
let reducer = (index, file) => {
127+
let os = utils.parsePlatform(file);
128+
let page = utils.parsePagename(file);
129+
if (index[page]) {
130+
index[page].push(os);
131+
} else {
132+
index[page] = [os];
133+
}
134+
return index;
135+
};
136+
return files.reduce(reducer, {});
137+
})
138+
.catch(() => {
139+
return {};
140+
});
138141
}
139142

140143
module.exports = {

lib/utils.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
var fs = require('fs-extra');
2-
var path = require('path');
3-
var glob = require('glob');
1+
const fs = require('fs-extra');
2+
const path = require('path');
3+
const glob = require('glob');
4+
const promiseMap = require('p-map');
5+
6+
const flatten = (arr) => {
7+
return arr.reduce((acc, it) => {
8+
return acc.concat(it);
9+
}, []);
10+
};
411

512
module.exports = {
613
parsePlatform(pagefile) {
@@ -23,20 +30,24 @@ module.exports = {
2330
};
2431
},
2532

26-
walkSync(dir, filelist) {
27-
var files = fs.readdirSync(dir);
28-
filelist = filelist || [];
29-
files.forEach((file) => {
30-
if (fs.statSync(path.join(dir, file)).isDirectory()) {
31-
filelist = this.walkSync(path.join(dir, file), filelist);
32-
}
33-
else {
34-
filelist.push(path.join(path.basename(dir), file));
35-
}
36-
});
37-
return filelist;
33+
walk(dir) {
34+
return fs.readdir(dir)
35+
.then((items) => {
36+
return promiseMap(items, (item) => {
37+
const itemPath = path.join(dir, item);
38+
return fs.stat(itemPath).then((stat) => {
39+
if (stat.isDirectory()) {
40+
return this.walk(itemPath);
41+
}
42+
return path.join(path.basename(dir), item);
43+
});
44+
});
45+
})
46+
.then((paths) => {
47+
return flatten(paths);
48+
});
3849
},
39-
50+
4051
glob(string,options) {
4152
return new Promise((resolve, reject) => {
4253
glob(string, options, (err, data) => {

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"natural": "^0.5.4",
5858
"ora": "^2.0.0",
5959
"os-homedir": "^1.0.1",
60+
"p-map": "^1.2.0",
6061
"request": "^2.75.0",
6162
"unzip2": "^0.2.5"
6263
},

test/index.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Index building', () => {
3232

3333
describe('failure', () => {
3434
before(() => {
35-
sinon.stub(utils, 'walkSync').throws('dummy error');
35+
sinon.stub(utils, 'walk').rejects('dummy error');
3636
});
3737

3838
it('shortIndex should not be created', () => {
@@ -43,7 +43,7 @@ describe('Index building', () => {
4343

4444
describe('success', () => {
4545
before(() => {
46-
sinon.stub(utils, 'walkSync').returns(pages);
46+
sinon.stub(utils, 'walk').resolves(pages);
4747
});
4848

4949
it('correct shortIndex should be created', () => {
@@ -53,7 +53,7 @@ describe('Index building', () => {
5353
});
5454

5555
afterEach(() => {
56-
utils.walkSync.restore();
56+
utils.walk.restore();
5757
fs.readJson.restore();
5858
fs.writeJson.restore();
5959
});
@@ -62,13 +62,13 @@ describe('Index building', () => {
6262
describe('Index', () => {
6363
beforeEach(() => {
6464
index.clearRuntimeIndex();
65-
sinon.stub(utils, 'walkSync').returns(pages);
65+
sinon.stub(utils, 'walk').resolves(pages);
6666
sinon.stub(fs, 'readJson').rejects('dummy error');
6767
sinon.stub(fs, 'writeJson').resolves('');
6868
});
6969

7070
afterEach(() => {
71-
utils.walkSync.restore();
71+
utils.walk.restore();
7272
fs.readJson.restore();
7373
fs.writeJson.restore();
7474
});

0 commit comments

Comments
 (0)