import { mkdir, readFile, writeFile, unlink } from 'node:fs/promises' import { createI18n } from 'vue-i18n' import { directoryMessages } from '../src/directory/messages.ts' const i18n = createI18n({ legacy: false, locale: 'zh-CN', messages: directoryMessages }) const t = (key: string) => i18n.global.t(key) const escape = (value: string) => value.replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]!) const text = (key: string) => `${escape(t(key))}` const navigation = [ ['mirrors', '/'], ['news', 'https://www.itouc.cn/announcements/'], ['status', '/status'], ['feedback', 'https://git.itouc.cn/ITStudio_OUC/OUCMirror-WebUI/issues'], ].map(([key, href]) => `${text(`common.nav.${key}`)}`).join('') const languages = Object.entries(directoryMessages).map(([locale, messages]) => ``).join('') const themes = ['light', 'dark', 'system'].map(theme => ``).join('') const specials: Record = { navigation, languages, themes } const html = (await readFile('dist/directory.html', 'utf8')).replace(/\{\{([\w.]+)\}\}/g, (_, key) => { if (key in specials) return specials[key]! if (!i18n.global.te(key)) throw new Error(`Missing directory template message: ${key}`) return escape(t(key)) }) const parts = html.split('') if (parts.length !== 2) throw new Error('Expected exactly one Nginx directory insertion point') await mkdir('dist/fancy-index', { recursive: true }) await writeFile('dist/fancy-index/header.html', parts[0]!) await writeFile('dist/fancy-index/footer.html', parts[1]!) // Translate the four native Fancyindex labels on the server as well, so the // default Chinese listing is complete and accessible even without JavaScript. const replacements = [ ['File Name', `${text('directory.name')}`], ['File Size', `${text('directory.size')}`], ['Date', `${text('directory.modified')}`], ['Parent directory/', `${text('directory.parent')}`], ] const quote = (value: string) => '"' + value.replace(/\\/g, '\\\\').replace(/\$/g, '\\$').replace(/"/g, '\\"') + '"' await writeFile('dist/fancy-index/labels.conf', 'sub_filter_once on;\n' + replacements.map(([before, after]) => `sub_filter ${quote(before!)} ${quote(after!)};`).join('\n') + '\n') // Only the two internal fragments are served; Nginx inserts the table between them. await unlink('dist/directory.html') console.log('Built Nginx directory header, footer and locale labels.')