mirror of
https://gitee.com/huanghongxun/HMCL-docs.git
synced 2026-11-28 10:28:27 +08:00
refactor: refactor to 11ty to support Tencent Cloud EdgeOne Maker
continuous-integration/drone/push Build is failing
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import Eleventy from '@11ty/eleventy';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const arg=process.argv.indexOf('--output');
|
||||
const out=arg<0?'_site':process.argv[arg+1];
|
||||
const absolute=path.resolve(out);
|
||||
if(absolute!==path.resolve('_site')&&!absolute.startsWith(path.resolve('.cache')+path.sep))throw Error('Output must be _site or inside .cache');
|
||||
fs.rmSync(absolute,{recursive:true,force:true});
|
||||
const engine=new Eleventy('_eleventy',out,{configPath:'eleventy.config.js'});
|
||||
await engine.write();
|
||||
@@ -0,0 +1,22 @@
|
||||
import fs from 'node:fs';
|
||||
import {spawn} from 'node:child_process';
|
||||
import {server} from './serve.mjs';
|
||||
|
||||
let running=false,queued=false;
|
||||
async function build(){
|
||||
if(running){queued=true;return;}
|
||||
running=true;
|
||||
const ok=await new Promise(resolve=>{const p=spawn(process.execPath,['tools/build.mjs'],{stdio:'inherit'});p.on('exit',code=>resolve(code===0));});
|
||||
running=false;
|
||||
if(queued){queued=false;return build();}
|
||||
return ok;
|
||||
}
|
||||
if(!await build())process.exit(1);
|
||||
await server('_site',Number(process.env.PORT||4173));
|
||||
console.log(`Preview: http://127.0.0.1:${process.env.PORT||4173}/ ; rebuilds on source changes, refresh browser to view.`);
|
||||
let timer;
|
||||
const changed=()=>{clearTimeout(timer);timer=setTimeout(build,200);};
|
||||
for(const dir of ['_docs','_launcher','_modpack','_multiplayer','_downloads','_eula','_changelog','_changelogs','_pages','_data','_includes','_layouts','assets','theme','build','_eleventy','redirects']){
|
||||
if(fs.existsSync(dir))fs.watch(dir,{recursive:true},changed);
|
||||
}
|
||||
fs.watch('.',(_,name)=>{if(['index.md','index.json','settings.yaml','404.md','_config.yml','eleventy.config.js'].includes(String(name)))changed();});
|
||||
@@ -0,0 +1,28 @@
|
||||
import http from 'node:http';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const mime={'.html':'text/html; charset=utf-8','.js':'text/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.json':'application/json; charset=utf-8','.xml':'application/xml; charset=utf-8','.txt':'text/plain; charset=utf-8','.png':'image/png','.jpg':'image/jpeg','.gif':'image/gif','.webp':'image/webp','.woff2':'font/woff2','.ico':'image/x-icon'};
|
||||
export function server(directory='_site',port=4173){
|
||||
const root=path.resolve(directory);
|
||||
return new Promise(resolve=>{
|
||||
const s=http.createServer((req,res)=>{
|
||||
let pathname;try{pathname=decodeURIComponent(new URL(req.url,'http://localhost').pathname);}catch{res.writeHead(400);return res.end();}
|
||||
let file=path.resolve(root,'.'+pathname);if(file!==root&&!file.startsWith(root+path.sep)){res.writeHead(403);return res.end();}
|
||||
if(!fs.existsSync(file)&&/^\/assets\/.+\.(?:png|jpe?g|gif|tiff?)\.webp$/.test(pathname)&&fs.existsSync(file.slice(0,-5))){
|
||||
res.writeHead(301,{Location:encodeURI(pathname.slice(0,-5))+new URL(req.url,'http://localhost').search});return res.end();
|
||||
}
|
||||
if(fs.existsSync(file)&&fs.statSync(file).isDirectory()){
|
||||
if(!pathname.endsWith('/')){res.writeHead(301,{Location:pathname+'/'+new URL(req.url,'http://localhost').search});return res.end();}
|
||||
file=path.join(file,'index.html');
|
||||
}
|
||||
const found=fs.existsSync(file)&&fs.statSync(file).isFile();if(!found)file=path.join(root,'404.html');
|
||||
res.writeHead(found?200:404,{'Content-Type':mime[path.extname(file)]||'application/octet-stream','Cache-Control':'no-store'});
|
||||
if(req.method==='HEAD')return res.end();
|
||||
if(fs.existsSync(file))fs.createReadStream(file).pipe(res);else res.end('Not found');
|
||||
});s.listen(port,'127.0.0.1',()=>resolve(s));
|
||||
});
|
||||
}
|
||||
if(process.argv[1]&&path.resolve(process.argv[1])===path.resolve(new URL(import.meta.url).pathname)){
|
||||
await server(process.argv[2]||'_site',Number(process.env.PORT||4173));console.log(`Preview: http://127.0.0.1:${process.env.PORT||4173}/`);
|
||||
}
|
||||
Reference in New Issue
Block a user