'use strict';

const fs = require('fs').promises;

fs.path = require('path');
fs.constants = require('fs').constants;

/**
 * @description Remove a file or directory.
 * @example
 * await app.util.fs.remove('./path')
 * @param {string} path - Path to directory.
 * @returns {boolean} - True if done.
 */
fs.remove = async (path) => {

    if (await fs.isFile(path)) {

        await fs.unlink(path);

    } else if (await fs.isSymbolicLink(path)) {

        await fs.unlink(path);

    } else if (await fs.isDirectory(path)) {

        for (let file of await fs.readdir(path)) {
            await fs.remove(fs.path.join(path, file));
        }

        await fs.rmdir(path);
    }

};

fs.isFile = async (path) => {

    let stats = null;

    try {
        stats = await fs.lstat(path);
    } catch (err) {
        return false;
    }

    return stats.isFile();
};

fs.isSymbolicLink = async (path) => {
    let stats = null;

    try {
        stats = await fs.lstat(path);
    } catch (err) {
        return false;
    }

    return stats.isSymbolicLink();
};

fs.isDirectory = async (path) => {

    let stats = null;

    try {
        stats = await fs.lstat(path);
    } catch (err) {
        return false;
    }

    return stats.isDirectory();
};

fs.ensureDirExists = async (path) => {

    await fs.mkdir(path, {recursive: true});

    return true;
};

fs.ensureFileExists = async (path) => {

    try {
        await fs.access(path, fs.constants.R_OK);
        return true;
    } catch (err) {
        await fs.ensureDirExists(fs.path.dirname(path))
    }

    await fs.appendFile(path, '');

    return true;
};

fs.isWritable = async (path) => {
    try {
        await fs.access(path, fs.constants.R_OK | fs.constants.W_OK);
        return true;
    } catch (err) {
        return false;
    }
};

module.exports = fs;

@@ -1806,16 +1806,332 @@
   %7D%0A%7D;%0A%0A
+fs.pathToObject = async (path) =%3E %7B%0A%0A    let obj = %7B%7D;%0A%0A    if (await fs.isDirectory(path)) %7B%0A        for (let file of await fs.readdir(path)) %7B%0A            obj%5Bfile%5D = await fs.pathToObject(fs.path.join(path, file));%0A        %7D%0A    %7D else if (await fs.isFile(path)) %7B%0A        return path;%0A    %7D%0A%0A    return obj;%0A%7D;%0A%0A
 module.e

add path to object function to fs util