{"size": 1820, "ext": "js", "max_stars_repo_path": "src/prettier/index.js", "max_stars_repo_name": "Criticalcarpet/eaf-linter", "max_stars_repo_stars_event_min_datetime": "2021-12-30T04:07:53.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-14T07:25:27.000Z", "max_issues_repo_path": "src/prettier/index.js", "max_issues_repo_name": "Criticalcarpet/eaf-linter", "max_issues_repo_issues_event_min_datetime": "2022-01-01T16:23:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-20T17:13:25.000Z", "max_forks_repo_path": "src/prettier/index.js", "max_forks_repo_name": "Criticalcarpet/eaf-linter", "max_forks_repo_forks_event_min_datetime": "2022-01-03T15:12:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-08T12:49:52.000Z"}
{"max_stars_count": 14, "max_issues_count": 10, "max_forks_count": 4, "avg_line_length": 20.2222222222, "max_line_length": 91, "alphanum_fraction": 0.6120879121}
const linter = require("../linter/index");
const path = require('path');
const fs = require('fs');
const Folder = require("../general/folder");

var components = fs.readdirSync(__dirname + "/components/").map(function (element, index) {

    return { 
        key: path.basename(element, path.extname(element)),
        value: require(path.join(__dirname, "/components/", element)) 
    };

}).reduce(function(map, obj) {

    map[obj.key] = obj.value;
    return map;

}, {})

function parse(item) {

    if (item == null) throw new Error(`Item is null`);

    if (item.type == undefined) throw new Error(`${item} does not have type.`);

    var component = components[item.type];

    if (component == null) throw new Error(`Type ${item.type}, does not exist.`);

    return component.write(item, {
        parse
    });

}

function stringifyAST(finalSyntaxTree) {

    // Create Output Data
    var output = [];

    // Go Through Syntax Tree
    finalSyntaxTree.forEach(function (element) {

        output.push(parse(element));

    });

    // Return Output
    return output.join("\n");

}

function cleanFile(input) {

    // Read AST and Pipe It 
    var syntaxTree = linter.fileToAST(input);
    var finalSyntaxTree = linter.pipe(syntaxTree);

    // Return the Stringified AST
    return stringifyAST(finalSyntaxTree);

}

function run() {

    var folder = new Folder(process.cwd());

    folder.forEachFile(function fileMethod(file, state) {

        if (path.extname(file.filePath) == ".js") {

            console.log(file.filePath);

            var fileData = fs.readFileSync(file.filePath, 'ascii');
            var cleanedFile = cleanFile(fileData);

            console.log(cleanedFile);
        
        }

    }, { });

}

module.exports = {
    cleanFile,
    stringifyAST,
    parse,
    run,
};