puppy-api-docs
Version:
Genernate material api docs from your comments.
92 lines (76 loc) • 2.7 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const fileManager = {};
/**
* Check if folder exists, if not then create it.
* @param {String} folderPath Path of the folder.
*/
fileManager.checkAndCreateFolder = function(folderPath) {
const exists = fs.existsSync(folderPath);
if (!exists) {
fs.mkdirSync(folderPath);
}
};
/**
* Save the html data with given name and at given path.
* @param {String} name Name of the file.
* @param {String} filePath Path where the file should be created.
* @param {String} data Html data to be saved.
*/
fileManager.saveFile = function(name, filePath, data) {
const result = fs.writeFileSync(path.join(filePath, name), data);
};
/**
* Copy a folder and its contents to a given path.
* @param {String} sourcePath Path of the folder which needs to be copied.
* @param {String} destinationPath Path where the source folder needs to be copied.
*/
fileManager.copyFolder = function(sourcePath, destinationPath) {
const sourceExists = fs.existsSync(sourcePath);
const destinationExists = fs.existsSync(sourcePath);
if (sourceExists) {
// If the destination folder doesn't exist, create it.
if (!destinationExists) {
fs.mkdirSync(destinationPath);
}
fs.copySync(sourcePath, destinationPath);
}
}
/**
* Retrieve the list of all .js files wihtin a folder and children folders.
* @param {String} folderPath Path of folder to retrieve all .js files paths from.
* @returns {[Sting]} Paths of all the .js Files found.
*/
fileManager.readJsFiles = (folderPath) => {
// List of all folders yet to be parsed
const folderPaths = [folderPath];
// List of all js files tracked
const jsFiles = [];
while(folderPaths.length !== 0) {
// Get a folder path
const folder = folderPaths.pop();
if (folder.indexOf("node_modules") !== -1) {
continue;
}
// Get all the file in folder
const filesInFolder = fs.readdirSync(folder);
// Loop of all files, look for js files and sub folders
filesInFolder.forEach((file) => {
const fileStats = fs.lstatSync(path.join(folder, file));
if (fileStats.isDirectory()) {
folderPaths.push(path.join(folder, file));
} else if (fileStats.isFile() && file.endsWith('.js')) {
jsFiles.push(path.join(folder, file));
}
});
}
return jsFiles;
}
/**
* Get the contents of a file as string.
* @param {String} path of file to be read.
*/
fileManager.readAsString = (path) => {
return fs.readFileSync(path, 'utf8');
}
module.exports = fileManager;