Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
build.js 3.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • peter_rabbit's avatar
    peter_rabbit committed
    #!/usr/bin/env node
    
    "use strict";
    
    
    const build_conf = {
    
    peter_rabbit's avatar
    peter_rabbit committed
        protected_dirs: ["assets", "style", "articles"],
    
    peter_rabbit's avatar
    peter_rabbit committed
    const fs = require("fs");
    const browserify = require("browserify");
    
    peter_rabbit's avatar
    peter_rabbit committed
    const curDir = process.cwd();
    
    peter_rabbit's avatar
    peter_rabbit committed
    
    // Handle home page
    const b = browserify();
    
    peter_rabbit's avatar
    peter_rabbit committed
    b.add(`${curDir}/src/main.js`)
    
    peter_rabbit's avatar
    peter_rabbit committed
        .bundle()
    
    peter_rabbit's avatar
    peter_rabbit committed
        .pipe(fs.createWriteStream(`${curDir}/public/main.js`));
    
    peter_rabbit's avatar
    peter_rabbit committed
    
    // Handle subpages
    
    peter_rabbit's avatar
    peter_rabbit committed
    function getPageHtml(pageName, pageMeta) {
        let html = fs.readFileSync(`${curDir}/public/index.html`, "utf-8");
        const setMeta = function (metaName, value) {
            return html.replace(
                html.match(new RegExp(`<meta.+name="${metaName}".+>`, "g")),
    
    peter_rabbit's avatar
    peter_rabbit committed
                `<meta name="${metaName}" content="${value}"/>`
    
    peter_rabbit's avatar
    peter_rabbit committed
            );
        };
        const setTitle = function (value) {
            return html.replace(
                html.match(new RegExp(`<title.+</title>`, "g")),
                `<title>${value}</title>`
            );
        };
        const setStyleSheet = function () {
            return html.replace(
                html.match(new RegExp(`<link.+/style.css>`, "g")),
                `<link href="/style/style.css" rel="stylesheet" />`
            );
        };
        const setJs = function () {
            return html.replace(
                html.match(new RegExp(`<script.+main.js.+</script>`, "g")),
                `<script type="text/javascript" src="./${pageName}.js"></script>`
            );
        };
    
    peter_rabbit's avatar
    peter_rabbit committed
        const setAdditionalMeta = function (metas) {
            return html.replace(
                "</head>",
                `${metas
                    .map(kv => {
                        const [name, content] = kv;
                        return `<meta name="${name}" content="${content}"/>`;
                    })
                    .join("\n")}</head>`
            );
        };
    
    peter_rabbit's avatar
    peter_rabbit committed
        html = setMeta("description", pageMeta.description);
        html = setTitle(pageMeta.title);
        html = setStyleSheet();
        html = setJs();
    
    peter_rabbit's avatar
    peter_rabbit committed
        html = setAdditionalMeta(
            Object.entries(pageMeta).filter(kv => kv[0] !== "description" && kv[0] !== "title")
        );
    
    
    peter_rabbit's avatar
    peter_rabbit committed
        return html;
    
    peter_rabbit's avatar
    peter_rabbit committed
    }
    
    
    peter_rabbit's avatar
    peter_rabbit committed
    const pages = fs.readdirSync(`${curDir}/src/pages`);
    
    peter_rabbit's avatar
    peter_rabbit committed
    
    
    peter_rabbit's avatar
    peter_rabbit committed
    for (const p of pages) {
        const fPath = `${curDir}/src/pages/${p}`;
        const targetDirPath = `${curDir}/public/${p}`;
    
    peter_rabbit's avatar
    peter_rabbit committed
    
        if (!fs.existsSync(targetDirPath)) {
            fs.mkdirSync(targetDirPath);
        }
    
        const b = browserify();
    
        b.add(fPath)
            .bundle()
    
    peter_rabbit's avatar
    peter_rabbit committed
            .pipe(fs.createWriteStream(`${targetDirPath}/${p}.js`));
    
    peter_rabbit's avatar
    peter_rabbit committed
    
        const page = fs.createWriteStream(`${targetDirPath}/index.html`);
    
    peter_rabbit's avatar
    peter_rabbit committed
        const pageMeta = JSON.parse(fs.readFileSync(`${fPath}/meta.json`, "utf-8"));
        page.write(getPageHtml(p, pageMeta));
    
    peter_rabbit's avatar
    peter_rabbit committed
    }
    
    // If pages have been deleted in source, remove them in output directory too.
    for (const dir of fs.readdirSync(`${curDir}/public`).filter(f => {
    
        if (build_conf.protected_dirs.includes(f)) return false;
    
    peter_rabbit's avatar
    peter_rabbit committed
        const stats = fs.statSync(`${curDir}/public/${f}`);
        return stats.isDirectory();
    })) {
        if (!pages.includes(dir)) {
            const dPath = `${curDir}/public/${dir}`;
            try {
                const nestedFiles = fs.readdirSync(dPath);
                for (const nf of nestedFiles) {
                    fs.unlinkSync(`${dPath}/${nf}`);
                }
                fs.rmdirSync(dPath);
            } catch (error) {
                console.error(error);
            }
        }
    
    peter_rabbit's avatar
    peter_rabbit committed
    }