@@ -2,17 +2,34 @@ require('dotenv').config();
2
2
import fs from 'fs' ;
3
3
import Bluebird from 'bluebird' ;
4
4
5
- const { API_DOCS_PATH } = process . env ;
5
+ const { GUIDES_DOCS_PATH , API_DOCS_PATH } = process . env ;
6
+
7
+ /**
8
+ * Returns the correct root path to the content of the project requested
9
+ *
10
+ * @param projectType - Type of project we're indexing. "api" or "guides"
11
+ * @returns {string } - Path to the project
12
+ */
13
+ export function getProjectFolderPath ( projectType ) {
14
+ const isCorrectType = [ 'guides' , 'api' ] . includes ( projectType ) ;
15
+ const folderPaths = { guides : GUIDES_DOCS_PATH , api : API_DOCS_PATH } ;
16
+
17
+ if ( ! isCorrectType ) throw new Error ( 'Incorrect projectType' ) ;
18
+
19
+ return folderPaths [ projectType ] ;
20
+ }
6
21
7
22
/**
8
23
* Opens a file and JSON.parses it synchronously.
9
24
* The result will be the contents of the file.
10
25
*
11
- * @param {String } fileName - Path to the file
26
+ * @param {String } fileName - Path to the file
27
+ * @param {String } projectType - Type of project we're indexing. "api" or "guides"
12
28
* @returns {Promise }
13
29
*/
14
- export function readTmpFile ( fileName ) {
15
- const fileContents = fs . readFileSync ( `${ API_DOCS_PATH } /${ fileName } ` ) ;
30
+ export function readTmpFile ( projectType , fileName ) {
31
+ const folderPath = getProjectFolderPath ( projectType ) ;
32
+ const fileContents = fs . readFileSync ( `${ folderPath } /${ fileName } ` ) ;
16
33
17
34
return JSON . parse ( fileContents ) ;
18
35
}
@@ -21,17 +38,34 @@ export function readTmpFile(fileName) {
21
38
* Opens a file and JSON.parses it asynchronously.
22
39
* The result will be a promise with the contents as the resolved value.
23
40
*
24
- * @param {String } fileName - Path to the file
41
+ * @param {String } fileName - Path to the file
42
+ * @param {String } projectType - Type of project we're indexing. "api" or "guides"
25
43
* @returns {Promise }
26
44
*/
27
- export function readTmpFileAsync ( fileName ) {
45
+ export function readTmpFileAsync ( projectType , fileName ) {
28
46
const readFileAsync = Bluebird . promisify ( fs . readFile ) ;
29
- const filePath = `${ API_DOCS_PATH } /${ fileName } ` ;
47
+ const folderPath = getProjectFolderPath ( projectType ) ;
48
+ const filePath = `${ folderPath } /${ fileName } ` ;
30
49
31
50
return readFileAsync ( filePath ) . then ( JSON . parse ) ;
32
51
}
33
52
53
+ export function readTmpFileFactory ( projectType ) {
54
+ return function ( fileName ) {
55
+ return readTmpFile ( projectType , fileName ) ;
56
+ }
57
+ }
58
+
59
+ export function readTmpFileAsyncFactory ( projectType ) {
60
+ return function ( fileName ) {
61
+ return readTmpFileAsync ( projectType , fileName ) ;
62
+ }
63
+ }
64
+
34
65
export default {
35
66
readTmpFile,
36
- readTmpFileAsync
67
+ readTmpFileAsync,
68
+
69
+ readTmpFileFactory,
70
+ readTmpFileAsyncFactory
37
71
} ;
0 commit comments