1 | 'use strict';
|
2 |
|
3 | const fs = require('fs-extra');
|
4 | const path = require('path');
|
5 | const config = require('./config');
|
6 | const utils = require('./utils');
|
7 |
|
8 | let shortIndex = null;
|
9 |
|
10 | const pagesPath = path.join(config.get().cache, 'cache/pages');
|
11 | const shortIndexFile = path.join(pagesPath, 'shortIndex.json');
|
12 |
|
13 | function findPlatform(page, preferredPlatform, done) {
|
14 |
|
15 | getShortIndex((idx) => {
|
16 |
|
17 | if (! (page in idx)) {
|
18 | return done(null);
|
19 | }
|
20 |
|
21 | let platforms = idx[page];
|
22 | if (platforms.indexOf(preferredPlatform) >= 0) {
|
23 | return done(preferredPlatform);
|
24 | } else if (platforms.indexOf('common') >= 0) {
|
25 | return done('common');
|
26 | }
|
27 | return done(null);
|
28 | });
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 | function hasPage(page) {
|
34 | if (!shortIndex) {
|
35 | return false;
|
36 | }
|
37 | return page in shortIndex;
|
38 | }
|
39 |
|
40 |
|
41 | function commands(done) {
|
42 | getShortIndex((idx) => {
|
43 | return done(Object.keys(idx).sort());
|
44 | });
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | function commandsFor(platform, done) {
|
50 | getShortIndex((idx) => {
|
51 | let commands = Object.keys(idx)
|
52 | .filter((cmd) => {
|
53 |
|
54 | return idx[cmd].indexOf(platform) !== -1
|
55 | || idx[cmd].indexOf('common') !== -1 ;
|
56 |
|
57 | })
|
58 | .sort();
|
59 | done(commands);
|
60 | });
|
61 | }
|
62 |
|
63 |
|
64 | function clearPagesIndex(done) {
|
65 | fs.unlink(shortIndexFile, () => {
|
66 | clearRuntimeIndex();
|
67 | done();
|
68 | });
|
69 | }
|
70 |
|
71 |
|
72 | function clearRuntimeIndex() {
|
73 | shortIndex = null;
|
74 | }
|
75 |
|
76 | function rebuildPagesIndex(done) {
|
77 | clearPagesIndex(() => {
|
78 | getShortIndex(()=>{
|
79 | return done();
|
80 | });
|
81 | });
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 | function getShortIndex(done) {
|
87 | if (!shortIndex) {
|
88 | return readShortPagesIndex(done);
|
89 | }
|
90 | return done(shortIndex);
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function readShortPagesIndex(done) {
|
97 | fs.readFile(shortIndexFile, 'utf8', (err, idx) => {
|
98 |
|
99 | if (err) {
|
100 | idx = buildShortPagesIndex();
|
101 | if (Object.keys(idx).length > 0) {
|
102 | fs.writeFile(shortIndexFile, JSON.stringify(idx), (err) => {
|
103 | if (err) {
|
104 | console.error(err);
|
105 | }
|
106 | shortIndex = idx;
|
107 | return done(idx);
|
108 | });
|
109 | }
|
110 | } else {
|
111 | idx = JSON.parse(idx);
|
112 | shortIndex = idx;
|
113 | return done(idx);
|
114 | }
|
115 | });
|
116 | }
|
117 |
|
118 | function buildShortPagesIndex() {
|
119 | try {
|
120 | let files = utils.walkSync(pagesPath);
|
121 | files = files.filter(utils.isPage);
|
122 | let reducer = (index, file) => {
|
123 | let os = utils.parsePlatform(file);
|
124 | let page = utils.parsePagename(file);
|
125 | if (index[page]) {
|
126 | index[page].push(os);
|
127 | } else {
|
128 | index[page] = [os];
|
129 | }
|
130 | return index;
|
131 | };
|
132 | return files.reduce(reducer, {});
|
133 | } catch (e) {
|
134 | return {};
|
135 | }
|
136 | }
|
137 |
|
138 | module.exports = {
|
139 | getShortIndex,
|
140 | hasPage,
|
141 | findPlatform,
|
142 | commands,
|
143 | commandsFor,
|
144 | clearPagesIndex,
|
145 | clearRuntimeIndex,
|
146 | rebuildPagesIndex
|
147 | };
|