|
| 1 | +// @ts-check |
| 2 | +import { readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { create } from 'sv'; |
| 6 | + |
| 7 | +// This download the currente Vite template from Github, adjusts it to our needs, and saves it to static/svelte-template.json |
| 8 | +// This is used by the Svelte REPL as part of the "download project" feature |
| 9 | + |
| 10 | +const force = process.env.FORCE_UPDATE === 'true'; |
| 11 | +const output_file = fileURLToPath(new URL('../static/svelte-template.json', import.meta.url)); |
| 12 | +const output_dir = fileURLToPath(new URL('./svelte-template', import.meta.url)); |
| 13 | + |
| 14 | +try { |
| 15 | + if (!force && statSync(output_file)) { |
| 16 | + console.info(`[update/template] ${output_file} exists. Skipping`); |
| 17 | + process.exit(0); |
| 18 | + } |
| 19 | +} catch { |
| 20 | + // create Svelte-Kit skelton app |
| 21 | + create(output_dir, { template: 'minimal', types: 'typescript', name: 'your-app' }); |
| 22 | + |
| 23 | + function get_all_files(dir) { |
| 24 | + const files = []; |
| 25 | + const items = readdirSync(dir, { withFileTypes: true }); |
| 26 | + |
| 27 | + for (const item of items) { |
| 28 | + const full_path = join(dir, item.name); |
| 29 | + if (item.isDirectory()) { |
| 30 | + files.push(...get_all_files(full_path)); |
| 31 | + } else { |
| 32 | + files.push(full_path.replaceAll('\\', '/')); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return files; |
| 37 | + } |
| 38 | + |
| 39 | + const all_files = get_all_files(output_dir); |
| 40 | + const files = []; |
| 41 | + |
| 42 | + for (let path of all_files) { |
| 43 | + const bytes = readFileSync(path); |
| 44 | + const string = bytes.toString(); |
| 45 | + let data = bytes.compare(Buffer.from(string)) === 0 ? string : [...bytes]; |
| 46 | + |
| 47 | + if (path.endsWith('routes/+page.svelte')) { |
| 48 | + data = `<script>\n\timport '../app.css';\n\timport App from './App.svelte';\n</script>\n\n<App />\n`; |
| 49 | + } |
| 50 | + |
| 51 | + files.push({ path: path.slice(output_dir.length + 1), data }); |
| 52 | + } |
| 53 | + |
| 54 | + files.push({ |
| 55 | + path: 'src/routes/+page.js', |
| 56 | + data: |
| 57 | + "// Because we don't know whether or not your playground app can run in a server environment, we disable server-side rendering.\n" + |
| 58 | + '// Make sure to test whether or not you can re-enable it, as SSR improves perceived performance and site accessibility.\n' + |
| 59 | + '// Read more about this option here: https://svelte.dev/docs/kit/page-options#ssr\n' + |
| 60 | + 'export const ssr = false;\n' |
| 61 | + }); |
| 62 | + |
| 63 | + // add CSS styles from playground to the project |
| 64 | + const html = readFileSync( |
| 65 | + join(output_dir, '../../../../packages/repl/src/lib/Output/srcdoc/index.html'), |
| 66 | + { encoding: 'utf-8' } |
| 67 | + ); |
| 68 | + const css = html |
| 69 | + .slice(html.indexOf('<style>') + 7, html.indexOf('</style>')) |
| 70 | + .split('\n') |
| 71 | + .map((line) => |
| 72 | + // remove leading \t |
| 73 | + line.slice(3) |
| 74 | + ) |
| 75 | + .join('\n') |
| 76 | + .trimStart(); |
| 77 | + files.push({ |
| 78 | + path: 'src/app.css', |
| 79 | + data: css |
| 80 | + }); |
| 81 | + |
| 82 | + writeFileSync(output_file, JSON.stringify(files)); |
| 83 | + |
| 84 | + // remove output dir afterwards to prevent it messing with Vite watcher |
| 85 | + rmSync(output_dir, { force: true, recursive: true }); |
| 86 | +} |
0 commit comments