{"size": 1723, "ext": "js", "max_stars_repo_path": "commons/virtual-file.js", "max_stars_repo_name": "siimple/siimple", "max_stars_repo_stars_event_min_datetime": "2015-10-26T02:13:28.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-05T19:17:54.000Z", "max_issues_repo_path": "commons/virtual-file.js", "max_issues_repo_name": "siimple/siimple", "max_issues_repo_issues_event_min_datetime": "2016-01-15T19:47:56.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-15T08:30:43.000Z", "max_forks_repo_path": "commons/virtual-file.js", "max_forks_repo_name": "siimple/siimple", "max_forks_repo_forks_event_min_datetime": "2015-12-21T18:21:31.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-21T12:53:29.000Z"}
{"max_stars_count": 585, "max_issues_count": 73, "max_forks_count": 84, "avg_line_length": 28.2459016393, "max_line_length": 85, "alphanum_fraction": 0.604759141}
let fs = require("fs");
let path = require("path");
let yaml = require("js-yaml");

//Front matter regex
let frontMatter = /^-{3}(?:[^\r\n]*?)(?:\r?\n)([\s\S]*?)^-{3}(?:[\s\S]*?)(?:\r?\n)/m;

//Generate vfile path
let getVFilePath = function (vfile) {
    return path.format({
        "root": "/",
        "dir": vfile.dirname,
        "name": vfile.filename,
        "ext": vfile.extname
    });
};

//Read the content of the viftual-file
let readVFile = function (vfile) {
    vfile.content = fs.readFileSync(getVFilePath(vfile), "utf8");
    //Check if file starts with three '-'
    if (vfile.content.indexOf("---") === 0) {
        //Overwrite the file content and parse the front matter
        vfile.content = vfile.content.replace(frontMatter, function (match, block) {
            vfile.data = Object.assign({}, yaml.safeLoad(block));
            return ""; //Remove the front-matter section
        });
    }
    return vfile;
};

//Write a vfile
let writeVFile = function (vfile) {
    if (fs.existsSync(vfile.dirname) === false) {
        fs.mkdirSync(vfile.dirname, {
            "recursive": true
        });
    }
    //Write the file content
    return fs.writeFileSync(getVFilePath(vfile), vfile.content, "utf8");
};

//Create a new virtual file object
module.exports = function (filePath) {
    //Initialize the virtual file object
    return Object.assign({}, {
        "dirname": path.dirname(filePath),
        "filename": path.basename(filePath, path.extname(filePath)),
        "extname": path.extname(filePath),
        "data": {},
        "content": null
    });
};

//Export utility methods
module.exports.path = getVFilePath;
module.exports.read = readVFile;
module.exports.write = writeVFile;


