@sujalchoudhari/solaris-ui
Version:
A UI framework to create HTML pages with just JavaScript.
199 lines • 7.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const logger_1 = __importDefault(require("./logger"));
/**
* FileManager
* -----
* A class dedicated for handling files and directories
* This class is responsible for managing project folders, copying files from source to build folders.
* @author Sujal Choudhari <sujalchoudhari@gmail.com>
*/
class FileManager {
/**
* Absolute path to current working directory
*/
basePath;
/**
* Creare a new instance of FileManager
* While creating a new instance of FileManager, base path will be automatically
* set to the current working directory.
*/
constructor(basepath = "") {
if (basepath == "")
this.basePath = path_1.default.resolve(process.cwd());
else
this.basePath = path_1.default.resolve(basepath);
}
/**
* Convert relative path to absolute path
* @param relativePath The relative path of any file or directory
* @returns The abosolute path of the file or directory.
* @deprecated No not use this method. Will be removed in future versions
*/
getAbsolutePath(relativePath) {
return path_1.default.resolve(this.basePath, relativePath);
}
/**
* Read a file synchronously
* @param path relative path of the file or directory
* @returns the contents of the file or directory. `null`if file is not found
*/
readFile(path) {
if (!fs_1.default.existsSync(path)) {
logger_1.default.warn(__filename, `Given file: ${path} does not exist`);
return null;
}
try {
return fs_1.default.readFileSync(path, 'utf8');
}
catch (err) {
logger_1.default.warn(__filename, `Error reading file: ${err}`);
return null;
}
}
/**
* Create a new directory
* @param path Path at which the directory should be created. The name of the directory should be
* included in the path
*/
createDirectory(path) {
fs_1.default.mkdirSync(path, { recursive: true });
}
/**
* Copy the directory from source to destination
* @param srcPath THe source path of the directory
* @param destPath the final destination path
*/
copyDirectory(srcPath, destPath) {
if (!fs_1.default.existsSync(srcPath)) {
logger_1.default.warn(__filename, `Source directory ${srcPath} does not exist.`);
return;
}
fs_1.default.mkdirSync(destPath, { recursive: true });
const entries = fs_1.default.readdirSync(srcPath, { withFileTypes: true });
for (const entry of entries) {
const srcEntryPath = path_1.default.join(srcPath, entry.name);
const destEntryPath = path_1.default.join(destPath, entry.name);
if (entry.isDirectory()) {
this.copyDirectory(srcEntryPath, destEntryPath);
}
else {
fs_1.default.copyFileSync(srcEntryPath, destEntryPath);
}
}
}
/**
* Get a list of names of files and in the directory and its subdirectories.
* @param directoryPath The path to the directory to get the names from
* @returns A List of the names (absolute paths)
*
* @author Ansh Sharma
*/
getAllFilesInDirectory(directoryPath) {
if (!fs_1.default.existsSync(directoryPath)) {
logger_1.default.warn(__filename, `Directory ${directoryPath} does not exist.`);
return [];
}
const entries = fs_1.default.readdirSync(directoryPath, { withFileTypes: true });
const files = [];
for (const entry of entries) {
if (entry.isDirectory()) {
const entryPath = path_1.default.join(directoryPath, entry.name);
files.push(...this.getAllFilesInDirectory(entryPath));
}
else {
const entryPath = path_1.default.join(directoryPath, entry.name);
files.push(entryPath);
}
}
return files;
}
/**
* Move a file from one directory to another
* @param srcPath Source path
* @param destPath Destination path
*/
moveFile(srcPath, destPath) {
if (!fs_1.default.existsSync(srcPath)) {
logger_1.default.warn(__filename, `Source file ${srcPath} does not exist.`);
return;
}
fs_1.default.renameSync(srcPath, destPath);
}
/**
* Copy a file to a destination
* @param srcPath Source file path
* @param destPath Destination file path
* @param srcPathType The type of the source path, relative or absolute. Default is relative
*/
copyFile(srcPath, destPath, srcPathType = "relative") {
const contents = this.readFile(srcPathType === "relative" ? path_1.default.join(process.cwd(), srcPath) : srcPath);
if (contents === null) {
logger_1.default.warn(__filename, "Failed to read file");
return;
}
let destFileName = path_1.default.basename(srcPath);
if (destPath.includes(".") && !destPath.endsWith("/")) {
destFileName = path_1.default.basename(destPath);
destPath = path_1.default.dirname(destPath);
}
this.createFile(path_1.default.join(destPath, destFileName), contents);
}
/**
* Create a new file at the specified path
* @param filePath New File Path, the name of file should be included in the path.
* @param contents The contents of the file to create with.
*/
createFile(filePath, contents) {
try {
fs_1.default.writeFileSync(filePath, contents);
}
catch (err) {
fs_1.default.writeFileSync(filePath, contents);
}
}
/**
* Copy the contents of the folder into another folder including subdirectories.
* @param srcPath Source file path
* @param destPath Destination path
*/
copyTree(srcPath, destPath) {
if (!fs_1.default.existsSync(srcPath)) {
logger_1.default.warn(__filename, srcPath, " does not exist");
return;
}
if (!fs_1.default.existsSync(destPath)) {
fs_1.default.mkdirSync(destPath);
}
const files = fs_1.default.readdirSync(srcPath);
for (const file of files) {
const srcFile = path_1.default.join(srcPath, file);
const destFile = path_1.default.join(destPath, file);
const stat = fs_1.default.statSync(srcFile);
if (stat.isDirectory()) {
this.copyTree(srcFile, destFile);
}
else {
fs_1.default.copyFileSync(srcFile, destFile);
}
}
}
/**
* Delete the specified folder
* @param path Path of the directory to remove
* @param force Remove the directory forefully. Even if the directory is not empty.
*/
removeDirectory(path, force = false) {
fs_1.default.rmSync(path, {
force: force,
recursive: true
});
}
}
exports.default = FileManager;
//# sourceMappingURL=filemanager.js.map