-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.js
228 lines (206 loc) Β· 6.7 KB
/
index.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
const fs = require('fs-extra');
const path = require('path');
const config = require('./config');
const utils = require('./utils');
let shortIndex = null;
const pagesPath = path.join(config.get().cache, 'cache');
const shortIndexFile = path.join(pagesPath, 'shortIndex.json');
function findPage(page, preferredPlatform, preferredLanguage) {
// Load the index
return getShortIndex()
.then((idx) => {
// First, check whether page is in the index
if (! (page in idx)) {
return null;
}
const targets = idx[page].targets;
// Remove unwanted stuff from lang code.
if (preferredLanguage.includes('.')) {
preferredLanguage = preferredLanguage.substring(0, preferredLanguage.indexOf('.'));
}
if (preferredLanguage.includes('@')) {
preferredLanguage = preferredLanguage.substring(0, preferredLanguage.indexOf('@'));
}
let ll;
if (preferredLanguage.includes('_')) {
ll = preferredLanguage.substring(0, preferredLanguage.indexOf('_'));
}
if (!hasLang(targets, preferredLanguage)) {
preferredLanguage = ll;
}
// Page resolution logic:
// 1. Look into the target platform, target lang
// 2. If not found, look into target platform, en lang.
// 3. If not found, look into common, target lang.
// 4. If not found, look into common, en lang.
// 5. If not found, look into any platform, target lang.
// 6. If not found, look into any platform, en lang.
let targetPlatform;
let targetLanguage;
if (hasPlatformLang(targets, preferredPlatform, preferredLanguage)) {
targetLanguage = preferredLanguage;
targetPlatform = preferredPlatform;
} else if (hasPlatformLang(targets, preferredPlatform, 'en')) {
targetLanguage = 'en';
targetPlatform = preferredPlatform;
} else if (hasPlatformLang(targets, 'common', preferredLanguage)) {
targetLanguage = preferredLanguage;
targetPlatform = 'common';
} else if (hasPlatformLang(targets, 'common', 'en')) {
targetLanguage = 'en';
targetPlatform = 'common';
} else if (targets.length > 0 && hasLang(targets, preferredLanguage)) {
targetLanguage = preferredLanguage;
targetPlatform = targets[0].platform;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targetPlatform} platform`);
} else if (targets.length > 0 && hasLang(targets, 'en')) {
targetLanguage = 'en';
targetPlatform = targets[0].platform;
console.log(`Command ${page} does not exist for the host platform. Displaying the page from ${targetPlatform} platform`);
}
if (!targetLanguage && !targetPlatform) {
return null;
}
let targetPath = 'pages';
if (targetLanguage !== 'en') {
targetPath += '.' + targetLanguage;
}
return path.join(targetPath, targetPlatform);
});
}
function hasPlatformLang(targets, preferredPlatform, preferredLanguage) {
return targets.some((t) => {
return t.platform === preferredPlatform && t.language === preferredLanguage;
});
}
function hasLang(targets, preferredLanguage) {
return targets.some((t) => {
return t.language === preferredLanguage;
});
}
// hasPage is always called after the index is created,
// hence just return the variable in memory.
// There is no need to re-read the index file again.
function hasPage(page) {
if (!shortIndex) {
return false;
}
return page in shortIndex;
}
// Return all commands available in the local cache.
function commands() {
return getShortIndex().then((idx) => {
return Object.keys(idx).sort();
});
}
// Return all commands for a given platform.
// P.S. - The platform 'common' is always included.
function commandsFor(platform) {
return getShortIndex()
.then((idx) => {
let commands = Object.keys(idx)
.filter((cmd) => {
let targets = idx[cmd].targets;
let platforms = targets.map((t) => {return t.platform;});
return platforms.indexOf(platform) !== -1 || platforms.indexOf('common') !== -1;
})
.sort();
return commands;
});
}
// Delete the index file.
function clearPagesIndex() {
return fs.unlink(shortIndexFile)
.then(() => {
return clearRuntimeIndex();
})
.catch((err) => {
// If the file is not present, then it is already unlinked and our job is done.
// So raise an error only if it is some other scenario.
if (err.code !== 'ENOENT') {
console.error(err);
}
});
}
// Set the shortIndex variable to null.
function clearRuntimeIndex() {
shortIndex = null;
}
function rebuildPagesIndex() {
return clearPagesIndex().then(() => {
return getShortIndex();
});
}
// If the variable is not set, read the file and set it.
// Else, just return the variable.
function getShortIndex() {
if (shortIndex) {
return Promise.resolve(shortIndex);
}
return readShortPagesIndex();
}
// Read the index file, and load it into memory.
// If the file does not exist, create the data structure, write the file,
// and load it into memory.
function readShortPagesIndex() {
return fs.readJson(shortIndexFile)
.then((idx) => {
shortIndex = idx;
return shortIndex;
})
.catch(() => {
// File is not present; we need to create the index.
return buildShortPagesIndex().then((idx) => {
if (Object.keys(idx).length <= 0) {
return idx;
}
shortIndex = idx;
return fs.writeJson(shortIndexFile, shortIndex).then(() => {
return shortIndex;
});
});
});
}
function buildShortPagesIndex() {
return utils.walk(pagesPath)
.then((files) => {
files = files.filter(utils.isPage);
let reducer = (index, file) => {
let platform = utils.parsePlatform(file);
let page = utils.parsePagename(file);
let language = utils.parseLanguage(file);
if (index[page]) {
let targets = index[page].targets;
let needsPush = true;
for (const target of targets) {
if (target.platform === platform && target.language === language) {
needsPush = false;
continue;
}
}
if (needsPush) {
targets.push({ platform, language });
index[page].targets = targets;
}
} else {
index[page] = {targets: [{ platform, language }]};
}
return index;
};
return files.reduce(reducer, {});
})
.catch(() => {
return {};
});
}
module.exports = {
getShortIndex,
hasPage,
findPage,
commands,
commandsFor,
clearPagesIndex,
clearRuntimeIndex,
rebuildPagesIndex
};