// scripts/prepare-tests.ts import fs from 'fs' import path from 'path' const DIST_TYPES = ['bundled', 'standalone', 'auto', 'esm', 'demo'] as const function createHTML(type: (typeof DIST_TYPES)[number], minified: boolean = false) { if (type === 'demo' && minified) return let template = fs.readFileSync('index.html', 'utf8') // Remove the existing script tags template = template .replace('', '') .replace('', '') .replace('', '') .replace('', '') const css_file_name = `example${minified ? '.min' : ''}.css` const css_url = type !== 'demo' ? `/${css_file_name}` : `${css_file_name}` const css_link = `` template = template.replace('', `${css_link}\n`) // Generate script tags based on format and minified state let scripts = '' const jsExt = minified ? '.min.js' : '.js' switch (type) { case 'bundled': scripts = ` ` break case 'demo': scripts = ` ` break case 'standalone': scripts = ` ` break case 'auto': scripts = ` ` break case 'esm': scripts = ` ` break } template = template.replace('', `${scripts}\n`) // Save the file const outputPath = type === 'demo' ? 'docs/index.html' : `dist/${type}${minified ? '.min' : ''}.html` // if no such directory, create it if (!fs.existsSync(path.dirname(outputPath))) { fs.mkdirSync(path.dirname(outputPath), { recursive: true }) } fs.writeFileSync(outputPath, template) console.log(`Created ${outputPath}`) } function cleanup() { DIST_TYPES.forEach((type) => { if (type === 'demo') return ;['', '.min'].forEach((suffix) => { const filePath = `dist/${type}${suffix}.html` if (fs.existsSync(filePath)) { fs.unlinkSync(filePath) console.log(`Cleaned up ${filePath}`) } }) }) } // copy css files and bundled js to /docs, so they can be used in the demo fs.copyFileSync('dist/example.css', 'docs/example.css') // fs.copyFileSync('dist/sortable.js', 'docs/sortable.js') // fs.copyFileSync('dist/sortable.a11y.js', 'docs/sortable.a11y.js') fs.copyFileSync('dist/sortable.auto.js', 'docs/sortable.auto.js') if (process.argv.includes('cleanup')) { cleanup() } else { DIST_TYPES.forEach((type) => { createHTML(type, false) // Unminified version createHTML(type, true) // Minified version }) }