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 1.9 KiB
Newer Older
peter_rabbit's avatar
peter_rabbit committed
#!/usr/bin/env node

"use strict";

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
function getPageHtml(pagename) {
    return `
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
peter_rabbit's avatar
peter_rabbit committed
        <title>Kuadrado Software - ${pagename}</title>
peter_rabbit's avatar
peter_rabbit committed
        <link rel="icon" type="image/svg+xml" href="/favicon.svg">
peter_rabbit's avatar
peter_rabbit committed
        <link href="../../style/style.css" rel="stylesheet" />
    </head>
    <body>
        <main></main>
    </body>
    <script type="text/javascript" src="./${pagename}.js"></script>
</html>
    `;
}

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
    page.write(getPageHtml(p));
}

// If pages have been deleted in source, remove them in output directory too.
for (const dir of fs.readdirSync(`${curDir}/public`).filter(f => {
    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
}