UNPKG

3.72 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs-extra');
4const path = require('path');
5const config = require('./config');
6const utils = require('./utils');
7
8let shortIndex = null;
9
10const pagesPath = path.join(config.get().cache, 'cache/pages');
11const shortIndexFile = path.join(pagesPath, 'shortIndex.json');
12
13function findPlatform(page, preferredPlatform, done) {
14 // Load the index
15 getShortIndex((idx) => {
16 // First checking whether page is there or not
17 if (! (page in idx)) {
18 return done(null);
19 }
20 // Getting the platforms
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// hasPage is always called after index is created, hence just return the variable
32// in memory. There is no need to re-read the index file again.
33function hasPage(page) {
34 if (!shortIndex) {
35 return false;
36 }
37 return page in shortIndex;
38}
39
40// This returns all commands available in the local cache
41function commands(done) {
42 getShortIndex((idx) => {
43 return done(Object.keys(idx).sort());
44 });
45}
46
47// This returns all commands for a given platform.
48// P.S. - The platform 'common' is always included.
49function commandsFor(platform, done) {
50 getShortIndex((idx) => {
51 let commands = Object.keys(idx)
52 .filter((cmd) => {
53 /* eslint-disable */ // To allow using -1 to check if it contains in the array
54 return idx[cmd].indexOf(platform) !== -1
55 || idx[cmd].indexOf('common') !== -1 ;
56 /* eslint-enable */
57 })
58 .sort();
59 done(commands);
60 });
61}
62
63// Delete the index file
64function clearPagesIndex(done) {
65 fs.unlink(shortIndexFile, () => {
66 clearRuntimeIndex();
67 done();
68 });
69}
70
71// Set the variable to null
72function clearRuntimeIndex() {
73 shortIndex = null;
74}
75
76function rebuildPagesIndex(done) {
77 clearPagesIndex(() => {
78 getShortIndex(()=>{
79 return done();
80 });
81 });
82}
83
84// If the variable is not set, read the file and set it.
85// Else, just return the variable
86function getShortIndex(done) {
87 if (!shortIndex) {
88 return readShortPagesIndex(done);
89 }
90 return done(shortIndex);
91}
92
93// Reads the index file, and loads it into memory.
94// If the file is not created, create the data structure, write the file, and load
95// it into memory
96function readShortPagesIndex(done) {
97 fs.readFile(shortIndexFile, 'utf8', (err, idx) => {
98 // file is not present, need to create the index
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 { // Just parse and set shortIndex variable, then return the value
111 idx = JSON.parse(idx);
112 shortIndex = idx;
113 return done(idx);
114 }
115 });
116}
117
118function 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
138module.exports = {
139 getShortIndex,
140 hasPage,
141 findPlatform,
142 commands,
143 commandsFor,
144 clearPagesIndex,
145 clearRuntimeIndex,
146 rebuildPagesIndex
147};