/* eslint-disable no-console */
import express from 'express'
import fs from 'fs-extra'
import globby from 'globby'
import Markdown from 'markdown-it'
import path from 'path'
import { WIDTH } from './utils'
const ERRORS_DIR = path.join(__dirname, '..', '..')
const SNAPSHOT_HTML = path.join(ERRORS_DIR, '__snapshot-html__')
const SNAPSHOT_HTML_LOCAL = path.join(ERRORS_DIR, '__snapshot-html-local__')
const SNAPSHOT_MARKDOWN = path.join(ERRORS_DIR, '__snapshot-md__')
const app = express()
const LINKS = `
`
const SEARCH_HANDLER = `
`
const getFiles = async (baseDir: string, spec?: string) => {
const pattern = spec ? `${spec}*` : '**/*'
return (await globby(`${baseDir}/${pattern}`))
}
async function getRows (offset = 0, baseList: boolean = false, spec?: string) {
const baseDir = baseList ? SNAPSHOT_HTML : SNAPSHOT_HTML_LOCAL
const toCompare = await getFiles(baseDir, spec)
const rows = toCompare.filter((f) => f.endsWith('.html')).sort().slice(offset, offset + 10).map((f) => path.basename(f).split('.')[0]).map((name) => {
const width = baseList ? WIDTH : 550
// const height = baseList ? HEIGHT : 600
const height = 400
return `
| ${name} |
|
${baseList ? '' : `
|
|
`}
`
})
if (toCompare.length > offset + 10) {
rows.push(` |
`)
}
return rows.join('\n')
}
app.get('/', async (req, res) => {
try {
const rows = await getRows()
res.type('html').send(`
${LINKS}
`)
} catch (e) {
res.json({ errStack: e.stack })
}
})
app.get('/base-list', async (req, res) => {
const spec = req.query.spec as string
try {
const rows = await getRows(0, true, spec)
res.type('html').send(`
${LINKS}
${SEARCH_HANDLER}
`)
} catch (e) {
res.json({ errStack: e.stack })
}
})
app.get<{offset: number}>('/load-more/:offset', async (req, res) => {
const rows = await getRows(req.params.offset)
res.send(rows)
})
app.get<{offset: number}>('/load-more-base/:offset', async (req, res) => {
const rows = await getRows(req.params.offset, true)
res.send(rows)
})
app.get('/looks-good/:name', async (req, res) => {
try {
await fs.move(
path.join(SNAPSHOT_HTML_LOCAL, `${req.params.name}.html`),
path.join(SNAPSHOT_HTML, `${req.params.name}.html`),
{ overwrite: true },
)
res.json({ ok: true })
} catch (e) {
res.status(400).json({ stack: e.stack })
}
})
app.get<{name: string, type: string}>('/html/:name/:type', async (req, res) => {
const pathToFile = path.join(ERRORS_DIR, req.params.type, `${req.params.name}.html`)
try {
const contents = await fs.readFile(pathToFile, 'utf8')
res.type('html').send(contents.replace(/\/g, '').replace('overflow: hidden;', ''))
} catch (e) {
res.json({ errStack: e })
}
})
app.get('/md', async (req, res) => {
const spec = req.query.spec as string
try {
const toRender = (await getFiles(SNAPSHOT_MARKDOWN, spec)).filter((f) => f.endsWith('.md')).sort()
const markdownContents = await Promise.all(toRender.map((f) => fs.readFile(f, 'utf8')))
const md = new Markdown({
html: true,
linkify: true,
})
res.type('html').send(`
${LINKS}
${SEARCH_HANDLER}
${toRender.map((r, i) => {
return `
${path.basename(r).split('.')[0]}
${md.render(markdownContents[i] ?? '')}
${markdownContents[i] ?? ''}
`
}).join('\n')}
`)
} catch (e) {
res.json({ stack: e.stack })
}
})
app.listen(5555, () => {
console.log(`Comparison server listening on: http://localhost:5555`)
})