@@ -11,27 +11,22 @@ const pagesPath = path.join(config.get().cache, 'cache/pages');
11
11
const shortIndexFile = path . join ( pagesPath , 'shortIndex.json' ) ;
12
12
13
13
function findPlatform ( page , preferredPlatform ) {
14
- return new Promise ( ( resolve , reject ) => {
15
- // Load the index
16
- getShortIndex ( )
17
- . then ( ( idx ) => {
18
- // First, check whether page is in the index
19
- if ( ! ( page in idx ) ) {
20
- return resolve ( null ) ;
21
- }
22
- // Get the platforms
23
- let platforms = idx [ page ] ;
24
- if ( platforms . indexOf ( preferredPlatform ) >= 0 ) {
25
- return resolve ( preferredPlatform ) ;
26
- } else if ( platforms . indexOf ( 'common' ) >= 0 ) {
27
- return resolve ( 'common' ) ;
28
- }
29
- return resolve ( null ) ;
30
- } )
31
- . catch ( ( err ) => {
32
- return reject ( err ) ;
33
- } ) ;
34
- } ) ;
14
+ // Load the index
15
+ return getShortIndex ( )
16
+ . then ( ( idx ) => {
17
+ // First, check whether page is in the index
18
+ if ( ! ( page in idx ) ) {
19
+ return null ;
20
+ }
21
+ // Get the platforms
22
+ let platforms = idx [ page ] ;
23
+ if ( platforms . indexOf ( preferredPlatform ) >= 0 ) {
24
+ return preferredPlatform ;
25
+ } else if ( platforms . indexOf ( 'common' ) >= 0 ) {
26
+ return 'common' ;
27
+ }
28
+ return null ;
29
+ } ) ;
35
30
}
36
31
37
32
// hasPage is always called after the index is created,
@@ -46,41 +41,30 @@ function hasPage(page) {
46
41
47
42
// Return all commands available in the local cache.
48
43
function commands ( ) {
49
- return new Promise ( ( resolve , reject ) => {
50
- getShortIndex ( )
51
- . then ( ( idx ) => {
52
- return resolve ( Object . keys ( idx ) . sort ( ) ) ;
53
- } )
54
- . catch ( ( err ) => {
55
- return reject ( err ) ;
56
- } ) ;
44
+ return getShortIndex ( ) . then ( ( idx ) => {
45
+ return Object . keys ( idx ) . sort ( ) ;
57
46
} ) ;
58
47
}
59
48
60
49
// Return all commands for a given platform.
61
50
// P.S. - The platform 'common' is always included.
62
51
function commandsFor ( platform ) {
63
- return new Promise ( ( resolve , reject ) => {
64
- getShortIndex ( )
65
- . then ( ( idx ) => {
66
- let commands = Object . keys ( idx )
67
- . filter ( ( cmd ) => {
68
- return idx [ cmd ] . indexOf ( platform ) !== - 1 || idx [ cmd ] . indexOf ( 'common' ) !== - 1 ;
69
- } )
70
- . sort ( ) ;
71
- resolve ( commands ) ;
72
- } )
73
- . catch ( ( err ) => {
74
- return reject ( err ) ;
75
- } ) ;
76
- } ) ;
52
+ return getShortIndex ( )
53
+ . then ( ( idx ) => {
54
+ let commands = Object . keys ( idx )
55
+ . filter ( ( cmd ) => {
56
+ return idx [ cmd ] . indexOf ( platform ) !== - 1 || idx [ cmd ] . indexOf ( 'common' ) !== - 1 ;
57
+ } )
58
+ . sort ( ) ;
59
+ return commands ;
60
+ } ) ;
77
61
}
78
62
79
63
// Delete the index file.
80
64
function clearPagesIndex ( ) {
81
65
return fs . unlink ( shortIndexFile )
82
66
. then ( ( ) => {
83
- clearRuntimeIndex ( ) ;
67
+ return clearRuntimeIndex ( ) ;
84
68
} )
85
69
. catch ( ( err ) => {
86
70
// If the file is not present, then it is already unlinked and our job is done.
@@ -97,69 +81,39 @@ function clearRuntimeIndex() {
97
81
}
98
82
99
83
function rebuildPagesIndex ( ) {
100
- return new Promise ( ( resolve , reject ) => {
101
- clearPagesIndex ( )
102
- . then ( ( ) => {
103
- return getShortIndex ( ) ;
104
- } )
105
- . then ( ( ) => {
106
- resolve ( ) ;
107
- } )
108
- . catch ( ( err ) => {
109
- reject ( err ) ;
110
- } ) ;
84
+ return clearPagesIndex ( ) . then ( ( ) => {
85
+ return getShortIndex ( ) ;
111
86
} ) ;
112
87
}
113
88
114
89
// If the variable is not set, read the file and set it.
115
90
// Else, just return the variable.
116
91
function getShortIndex ( ) {
117
- return new Promise ( ( resolve , reject ) => {
118
- if ( shortIndex ) {
119
- resolve ( shortIndex ) ;
120
- } else {
121
- readShortPagesIndex ( )
122
- . then ( ( idx ) => {
123
- resolve ( idx ) ;
124
- } )
125
- . catch ( ( err ) => {
126
- reject ( err ) ;
127
- } ) ;
128
- }
129
- } ) ;
92
+ if ( shortIndex ) {
93
+ return Promise . resolve ( shortIndex ) ;
94
+ }
95
+ return readShortPagesIndex ( ) ;
130
96
}
131
97
132
98
// Read the index file, and load it into memory.
133
99
// If the file does not exist, create the data structure, write the file,
134
100
// and load it into memory.
135
101
function readShortPagesIndex ( ) {
136
- return new Promise ( ( resolve , reject ) => {
137
- fs . readFile ( shortIndexFile , 'utf8' )
138
- . then ( ( idx ) => {
139
- // File is present, so just parse
140
- // and set the shortIndex variable,
141
- // then resolve the value.
142
- idx = JSON . parse ( idx ) ;
143
- shortIndex = idx ;
144
- return resolve ( idx ) ;
145
- } )
146
- . catch ( ( ) => {
147
- // File is not present; we need to create the index.
148
- let idx = buildShortPagesIndex ( ) ;
149
- if ( Object . keys ( idx ) . length > 0 ) {
150
- fs . writeFile ( shortIndexFile , JSON . stringify ( idx ) )
151
- . then ( ( ) => {
152
- shortIndex = idx ;
153
- return resolve ( idx ) ;
154
- } )
155
- . catch ( ( err ) => {
156
- reject ( err ) ;
157
- } ) ;
158
- } else {
159
- return resolve ( idx ) ;
160
- }
102
+ return fs . readJson ( shortIndexFile )
103
+ . catch ( ( ) => {
104
+ // File is not present; we need to create the index.
105
+ let idx = buildShortPagesIndex ( ) ;
106
+ if ( Object . keys ( idx ) . length <= 0 ) {
107
+ return idx ;
108
+ }
109
+ return fs . writeJson ( shortIndexFile , idx ) . then ( ( ) => {
110
+ return idx ;
161
111
} ) ;
162
- } ) ;
112
+ } )
113
+ . then ( ( idx ) => {
114
+ shortIndex = idx ;
115
+ return shortIndex ;
116
+ } ) ;
163
117
}
164
118
165
119
function buildShortPagesIndex ( ) {
0 commit comments