|
| 1 | +// Creates a test from the existing playground. cwd needs to be playground/sandbox |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +// Get target folder from command line arguments |
| 6 | +let target_folder = process.argv[2]; |
| 7 | +if (!target_folder) { |
| 8 | + console.error( |
| 9 | + 'Please provide a target folder as an argument. Example: node create-test.js runtime-runes/my-test' |
| 10 | + ); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | +if (!target_folder.includes('/')) { |
| 14 | + target_folder = 'runtime-runes/' + target_folder; |
| 15 | +} |
| 16 | +if (!target_folder.startsWith('runtime-')) { |
| 17 | + console.error( |
| 18 | + 'Target folder must start with "runtime-" (can only convert to these kinds of tests)' |
| 19 | + ); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | +target_folder = path.join( |
| 23 | + path.resolve('../../packages/svelte/tests', target_folder.split('/')[0]), |
| 24 | + 'samples', |
| 25 | + target_folder.split('/')[1] |
| 26 | +); |
| 27 | + |
| 28 | +// Create target directory if it doesn't exist |
| 29 | +if (!fs.existsSync(target_folder)) { |
| 30 | + fs.mkdirSync(target_folder, { recursive: true }); |
| 31 | +} |
| 32 | + |
| 33 | +// Starting file |
| 34 | +const app_svelte_path = path.resolve('./src/App.svelte'); |
| 35 | +const collected_files = new Set(); |
| 36 | +const processed_files = new Set(); |
| 37 | + |
| 38 | +function collect_imports(file_path) { |
| 39 | + if (processed_files.has(file_path) || !fs.existsSync(file_path)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + processed_files.add(file_path); |
| 44 | + collected_files.add(file_path); |
| 45 | + |
| 46 | + const content = fs.readFileSync(file_path, 'utf8'); |
| 47 | + |
| 48 | + // Regex to match import statements |
| 49 | + const import_regex = /import\s+(?:[^'"]*\s+from\s+)?['"]([^'"]+)['"]/g; |
| 50 | + let match; |
| 51 | + |
| 52 | + while ((match = import_regex.exec(content)) !== null) { |
| 53 | + const import_path = match[1]; |
| 54 | + |
| 55 | + // Skip node_modules imports |
| 56 | + if (!import_path.startsWith('.')) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Resolve relative import path |
| 61 | + const resolved_path = path.resolve(path.dirname(file_path), import_path); |
| 62 | + |
| 63 | + // Try different extensions if file doesn't exist |
| 64 | + const extensions = ['', '.svelte', '.js', '.ts']; |
| 65 | + let actual_path = null; |
| 66 | + |
| 67 | + for (const ext of extensions) { |
| 68 | + const test_path = resolved_path + ext; |
| 69 | + if (fs.existsSync(test_path)) { |
| 70 | + actual_path = test_path; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (actual_path) { |
| 76 | + collect_imports(actual_path); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Start collecting from App.svelte |
| 82 | +collect_imports(app_svelte_path); |
| 83 | + |
| 84 | +// Copy collected files to target folder |
| 85 | +for (const file_path of collected_files) { |
| 86 | + const relative_path = path.relative(path.resolve('./src'), file_path); |
| 87 | + let target_path = path.join(target_folder, relative_path); |
| 88 | + |
| 89 | + // Rename App.svelte to main.svelte |
| 90 | + if (path.basename(file_path) === 'App.svelte') { |
| 91 | + target_path = path.join(target_folder, path.dirname(relative_path), 'main.svelte'); |
| 92 | + } |
| 93 | + |
| 94 | + // Ensure target directory exists |
| 95 | + const target_dir = path.dirname(target_path); |
| 96 | + if (!fs.existsSync(target_dir)) { |
| 97 | + fs.mkdirSync(target_dir, { recursive: true }); |
| 98 | + } |
| 99 | + |
| 100 | + // Copy file |
| 101 | + fs.copyFileSync(file_path, target_path); |
| 102 | + console.log(`Copied: ${file_path} -> ${target_path}`); |
| 103 | +} |
| 104 | + |
| 105 | +// Create empty _config.js |
| 106 | +const config_path = path.join(target_folder, '_config.js'); |
| 107 | +fs.writeFileSync( |
| 108 | + config_path, |
| 109 | + `import { test } from '../../test'; |
| 110 | +
|
| 111 | +export default test({ |
| 112 | + async test({ assert, target }) { |
| 113 | + } |
| 114 | +}); |
| 115 | +` |
| 116 | +); |
| 117 | +console.log(`Created: ${config_path}`); |
| 118 | + |
| 119 | +console.log(`\nTest files created in: ${target_folder}`); |
0 commit comments