Monday, 3 June 2019

Trying to figure a way to read all the files in a directory, re-create and write files programmatically

I'm trying to use node built in file structure module/package in trying to read and write files. I'm looking for a way on how I can read all the files in specific directory, re-create the files and write for any changes.

Basically if I have a file called templates/_template-1.html it would re-create it to a different directory called pages/template-1.html. Instead of having to declare each file manually within the gulpfile.js. The code below is currently a work in progress.

It basically prints tpl files written then re-writes them to basic html.

/*------------------ INDEX -------------------*/
/* Build index file for the final HTML form
 */
gulp.task("build:index", function() {
    let templateFiles = glob.sync("templates/**/*.tpl"),
        templates = {},

        RESPONSIVE_HTML = fs.readFileSync("_responsive.html", "utf8"),
        THE_HTML = fs.readFileSync("_design-system.html", "utf8"),
        THE_VISUAL_LIBRARY = fs.readFileSync("_visual-library.html", "utf8");

    // Discover all templates
    for (let file in templateFiles) {
        file = templateFiles[file];

        let template = /templates\/(.+?)\.tpl/gi.exec(file)[1],
            text = fs.readFileSync(file, "utf8");

        template = path.basename(file, '.tpl');
        templates[template] = text;
    }

    // --------------------------------------------------------
    // Visible templates:
    _.each(templates, (partial, name) => {
        interpolateTemplate(partial, name, templates);
    });

    // replace the main HTML file
    for (let template in templates) {
        RESPONSIVE_HTML = RESPONSIVE_HTML.replace(new RegExp(`{[@$]${template}}`, "g"), templates[template]);
        THE_HTML = THE_HTML.replace(new RegExp(`{[@$]${template}}`, "g"), templates[template]);
        THE_VISUAL_LIBRARY = THE_VISUAL_LIBRARY.replace(new RegExp(`{[@$]${template}}`, "g"), templates[template]);
    }

    fs.writeFileSync("design-system.html", beautify(THE_HTML), "utf8");
    fs.writeFileSync("responsive.html", beautify(RESPONSIVE_HTML), "utf8");
    fs.writeFileSync("visual-library.html", beautify(THE_VISUAL_LIBRARY), "utf8");
});


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>BDO Components</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify-css.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify-html.js"></script>
    <script src="assets/js/libs.js" type="text/javascript"></script>

    <link rel="stylesheet" href="assets/css/assets-page.css" />
    <link rel="stylesheet" href="assets/css/component-guide.css" />
</head>

<body>

    <div class="display-panels">
        {$control-bar}

        <div class="preview-pane -hide-code">
            {$globals}
            {$design-references}
            {$component-modifiers}

            <div class="section-block element-group --show-code --components -component" 
                data-name="Typesetting">
                {$typesetting}
            </div>

            <div class="section-block element-group --show-code --components -component" 
                data-name="Elements">
                {$elements}
            </div>

            <div class="section-block element-group --show-code --components -component" 
                data-name="Low Level Components">
                {$low-level-components}
            </div>

            <div class="section-block element-group --show-code --components -component" 
                data-name="High Level Components">
                {$high-level-components}
            </div>

        </div>
        <div class="index-group">
        </div>
    </div>

    <script src="assets/js/app.js" type="text/javascript"></script>
</body>

</html>



from Trying to figure a way to read all the files in a directory, re-create and write files programmatically

No comments:

Post a Comment