1- import http , { get } from "http" ;
2- import path from "path" ;
1+ import http from "http" ;
32import semver from "semver" ;
4- import fs from "fs/promises" ;
53import Router from "find-my-way" ;
6- import Handlebars from "handlebars" ;
74import { formatDistanceToNow } from "date-fns" ;
85
96import { getLatest } from "./cache.js" ;
@@ -23,44 +20,103 @@ export async function carrots(config: Configuration) {
2320 return ;
2421 }
2522
26- // Render html from handlebars template
27- const filepath = path . join ( process . cwd ( ) , "lib" , "index.hbs" ) ;
28- const filecontent = await fs . readFile ( filepath , "utf8" ) ;
29- const template = Handlebars . compile ( filecontent ) ;
30- const files : Record < string , TemplateFile > = { } ;
31- for ( const platform of platforms ) {
23+ const data = platforms . map ( ( platform ) => {
3224 const asset = latest . get ( platform ) ;
33- if ( asset ) {
34- files [ platform ] = {
35- key : platform ,
36- url : asset . url ,
37- size : asset . size ,
38- filename : asset . url . split ( "/" ) . pop ( ) || "" ,
39- platform : PLATFORMS [ platform ] . name ,
40- } ;
41- }
42- }
43- const sortedFiles : Record < string , TemplateFile > = { } ;
44- Object . keys ( files )
45- . sort ( ( a , b ) => {
46- return files [ a ] . platform . localeCompare ( files [ b ] . platform ) ;
47- } )
48- . forEach ( ( key ) => {
49- sortedFiles [ key ] = files [ key ] ;
50- } ) ;
51- const variables : TemplateVariables = {
52- account : config . account || "" ,
53- repository : config . repository || "" ,
54- date : date
55- ? formatDistanceToNow ( new Date ( date ) , { addSuffix : true } )
56- : "" ,
57- files : sortedFiles ,
58- version : version || "" ,
59- releaseNotes : `https://github.com/${ config . account } /${ config . repository } /releases/tag/${ version } ` ,
60- allReleases : `https://github.com/${ config . account } /${ config . repository } /releases` ,
61- github : `https://github.com/${ config . account } /${ config . repository } ` ,
62- } ;
63- const html = template ( variables ) ;
25+ if ( ! asset ) return null ;
26+ return {
27+ id : platform ,
28+ platform : PLATFORMS [ platform ] . platform ,
29+ arch : PLATFORMS [ platform ] . arch ,
30+ version : asset . version ,
31+ date : asset . date ,
32+ fileName : asset . name ,
33+ } ;
34+ } ) ;
35+
36+ const html = `
37+ <!DOCTYPE html>
38+ <html lang="en">
39+ <head>
40+ <meta charset="utf-8" />
41+ <title>${ config . account } /${ config . repository } </title>
42+ <meta name="viewport" content="width=device-width, initial-scale=1" />
43+ <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Inter">
44+ <style>
45+ body {
46+ font-family: 'Inter', sans-serif;
47+ margin: 0;
48+ background: #000;
49+ color: #fff;
50+ }
51+ h1 {
52+ font-size: 2rem;
53+ }
54+ a {
55+ color: #fa0;
56+ }
57+ main {
58+ padding: 2rem;
59+ margin: 0 auto;
60+ max-width: 768px;
61+ }
62+ table {
63+ width: 100%;
64+ border-collapse: collapse;
65+ margin-bottom: 2rem;
66+ table-layout: fixed;
67+ }
68+ th {
69+ font-weight: bold;
70+ background: #111;
71+ }
72+ th, td {
73+ border: 1px solid #444;
74+ padding: 0.5rem;
75+ text-align: left;
76+ vertical-align: top;
77+ position: relative;
78+ word-break: break-word;
79+ }
80+ </style>
81+ </head>
82+ <body>
83+ <main>
84+ <h1>${ config . account } /${ config . repository } </h1>
85+ <table>
86+ <tr>
87+ <th>OS</th>
88+ <th>Chip</th>
89+ <th>Version</th>
90+ <th colspan="2">Date</th>
91+ <th>Download</th>
92+ </tr>
93+ ${ data
94+ . map ( ( asset ) =>
95+ asset
96+ ? `
97+ <tr id="${ asset . id } ">
98+ <td>${ asset . platform } </td>
99+ <td>${ asset . arch } </td>
100+ <td>${ asset . version } </td>
101+ <td colspan="2">${ formatDistanceToNow (
102+ new Date ( asset . date ) ,
103+ {
104+ addSuffix : true ,
105+ } ,
106+ ) } </td>
107+ <td><a href="/download/${ asset . id } ">${
108+ asset . fileName
109+ } </a></td>
110+ </tr>
111+ `
112+ : `` ,
113+ )
114+ . join ( "" ) }
115+ </table>
116+ </main>
117+ </body>
118+ </html>
119+ ` ;
64120
65121 // Send response
66122 res . statusCode = 200 ;
0 commit comments