看起来似乎可以用了

This commit is contained in:
枫谷剑仙 2022-06-22 19:45:02 +08:00
parent 0d46e88430
commit c8beb6bc9e
4 changed files with 6259 additions and 95 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,4 @@
@echo off
echo 开始更新 service-worker.js
node.exe update-service-worker.js
pause

View File

@ -36,31 +36,37 @@ const cachesArr = [];
for (let server of servers) {
const cardsHash = filesHashArr(`./images/cards_${server.code}`);
const voiceHash = filesHashArr(`./sound/voice/${server.code}`);
cachesArr.push([`card-${server.code}`, cardsHash]);
cachesArr.push([`voice-${server.code}`, voiceHash]);
cachesArr.push(...cardsHash);
cachesArr.push(...voiceHash);
}
//字体
const fontsHash = filesHashArr("./fonts", /\.woff2?$/i);
cachesArr.push([`fonts`, fontsHash]);
cachesArr.push(...fontsHash);
//程序
const programHash = filesHashArr("./", /\.(js|html|css)$/i);
programHash.splice(programHash.findIndex(item=>item[0] === "service-worker.js"), 1); //不要把service-worker自身加入
cachesArr.push(...programHash);
//语言
const lanuageHash = filesHashArr("./languages", /\.(js|css)$/i);
cachesArr.push(...lanuageHash);
//UI图片
const UiImageHash = filesHashArr("./images", /\.(png|webp|svg)$/i);
cachesArr.push([`program`, programHash.concat(UiImageHash)]);
cachesArr.push(...UiImageHash);
//第三方库
const libraryHash = filesHashArr("./library", /\.js/i); //第三方库
cachesArr.push(...libraryHash);
const library_aaaHash = filesHashArr("./library/jy4340132-aaa", /\.(js|wasm)$/i); //播放语音的库
cachesArr.push([`library`, libraryHash.concat(library_aaaHash)]);
cachesArr.push(...library_aaaHash);
//数据
const dataHash = filesHashArr("./monsters-info", /\.json$/i);
cachesArr.push([`data`, dataHash]);
cachesArr.push(...dataHash);
//文档
const docHash = filesHashArr("./doc", /\.html$/i);
const docImageHash = filesHashArr("./doc/images", /\.(png|webp|svg)$/i);
cachesArr.push([`document`, docHash.concat(docImageHash)]);
cachesArr.push(...docHash.concat(docImageHash));
const swJs = fs.readFileSync('./service-worker.js', 'utf-8');
let formatterHashes = false;
const newSwJs = swJs.replace(/(const\s+cachesArray\s*=\s*)[\s\S]+?;/i, `$1${JSON.stringify(cachesArr, undefined, formatterHashes ? "\t" : undefined)};`);
let formatterHashes = true;
const newSwJs = swJs.replace(/(const\s+cachesMap\s*=\s*)[\s\S]+?;/i, `$1new Map(${JSON.stringify(cachesArr, undefined, formatterHashes ? "\t" : undefined)});`);
fs.writeFileSync('./service-worker.js', newSwJs, 'utf-8');
console.log("更新 service-worker.js 完毕");

View File

@ -1,14 +0,0 @@
//import { basename } from "https://deno.land/std/path/mod.ts";
import { createHash } from "https://deno.land/std/hash/mod.ts";
let dirName = "images/cards_ja";
for await (const dirEntry of Deno.readDir(dirName)) {
if (dirEntry.isFile) {
const fileName = `${dirName}/${dirEntry.name}`;
const hash = createHash("md5");
const file = await Deno.readFile(fileName);
hash.update(file);
const hashInHex = hash.toString(); // returns 5fe084ee423ff7e0c7709e9437cee89d
console.log(hashInHex);
}
}