Skip to content

Commit 87a97d4

Browse files
mschinissivakumar-kailasam
authored andcommitted
feat(fs): Adds support for reading from various project directories
1 parent db7971a commit 87a97d4

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/utils/fs.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@ require('dotenv').config();
22
import fs from 'fs';
33
import Bluebird from 'bluebird';
44

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+
}
621

722
/**
823
* Opens a file and JSON.parses it synchronously.
924
* The result will be the contents of the file.
1025
*
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"
1228
* @returns {Promise}
1329
*/
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}`);
1633

1734
return JSON.parse(fileContents);
1835
}
@@ -21,17 +38,34 @@ export function readTmpFile(fileName) {
2138
* Opens a file and JSON.parses it asynchronously.
2239
* The result will be a promise with the contents as the resolved value.
2340
*
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"
2543
* @returns {Promise}
2644
*/
27-
export function readTmpFileAsync(fileName) {
45+
export function readTmpFileAsync(projectType, fileName) {
2846
const readFileAsync = Bluebird.promisify(fs.readFile);
29-
const filePath = `${API_DOCS_PATH}/${fileName}`;
47+
const folderPath = getProjectFolderPath(projectType);
48+
const filePath = `${folderPath}/${fileName}`;
3049

3150
return readFileAsync(filePath).then(JSON.parse);
3251
}
3352

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+
3465
export default {
3566
readTmpFile,
36-
readTmpFileAsync
67+
readTmpFileAsync,
68+
69+
readTmpFileFactory,
70+
readTmpFileAsyncFactory
3771
};

0 commit comments

Comments
 (0)