Script Generate Cache Manifest

Date: 2018-09-26
const recursive = require("recursive-readdir");
const path = require("path");
const fs = require("fs");

const regexes = [
    /assets\//,
    /\.(html|css|js|ico|json|mf)$/i
];

const dirToProcess = "dist/";
const targetFile = "dist/index.mf"

recursive(dirToProcess, function (err, files) {
    // `files` is an array of file paths

    let filesToCache = [];
    files.forEach((file) => {
        const found = regexes.some((r) => r.test(file));
        if (found) {
            filesToCache.push(file.slice(dirToProcess.length));
        }
    });

    filesToCache.sort((a, b) => {
        return a.length - b.length;
    });

    filesToCache.unshift("CACHE MANIFEST");
    const randomStr = "#" + Math.random().toString().substring(2);
    filesToCache.push(randomStr);
    filesToCache.push("NETWORK:");
    filesToCache.push("*");

    const cacheManifest = filesToCache.join('\r\n');
    console.log(cacheManifest);
    fs.writeFileSync(targetFile, cacheManifest);
});
npm i --save recursive-readdir

 

13050cookie-checkScript Generate Cache Manifest