-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore: add script to generate test from playground #16602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // Creates a test from the existing playground. cwd needs to be playground/sandbox | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
|
|
||
| // Get target folder from command line arguments | ||
| let target_folder = process.argv[2]; | ||
| if (!target_folder) { | ||
| console.error( | ||
| 'Please provide a target folder as an argument. Example: node create-test.js runtime-runes/my-test' | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| if (!target_folder.includes('/')) { | ||
| target_folder = 'runtime-runes/' + target_folder; | ||
| } | ||
| if (!target_folder.startsWith('runtime-')) { | ||
| console.error( | ||
| 'Target folder must start with "runtime-" (can only convert to these kinds of tests)' | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| target_folder = path.join( | ||
| path.resolve('../../packages/svelte/tests', target_folder.split('/')[0]), | ||
| 'samples', | ||
| target_folder.split('/')[1] | ||
| ); | ||
|
|
||
| // Create target directory if it doesn't exist | ||
| if (!fs.existsSync(target_folder)) { | ||
| fs.mkdirSync(target_folder, { recursive: true }); | ||
| } | ||
|
|
||
| // Starting file | ||
| const app_svelte_path = path.resolve('./src/App.svelte'); | ||
| const collected_files = new Set(); | ||
| const processed_files = new Set(); | ||
|
|
||
| function collect_imports(file_path) { | ||
| if (processed_files.has(file_path) || !fs.existsSync(file_path)) { | ||
| return; | ||
| } | ||
|
|
||
| processed_files.add(file_path); | ||
| collected_files.add(file_path); | ||
|
|
||
| const content = fs.readFileSync(file_path, 'utf8'); | ||
|
|
||
| // Regex to match import statements | ||
| const import_regex = /import\s+(?:[^'"]*\s+from\s+)?['"]([^'"]+)['"]/g; | ||
| let match; | ||
|
|
||
| while ((match = import_regex.exec(content)) !== null) { | ||
| const import_path = match[1]; | ||
|
|
||
| // Skip node_modules imports | ||
| if (!import_path.startsWith('.')) { | ||
| continue; | ||
| } | ||
|
|
||
| // Resolve relative import path | ||
| const resolved_path = path.resolve(path.dirname(file_path), import_path); | ||
|
|
||
| // Try different extensions if file doesn't exist | ||
| const extensions = ['', '.svelte', '.js', '.ts']; | ||
| let actual_path = null; | ||
|
|
||
| for (const ext of extensions) { | ||
| const test_path = resolved_path + ext; | ||
| if (fs.existsSync(test_path)) { | ||
| actual_path = test_path; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (actual_path) { | ||
| collect_imports(actual_path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Start collecting from App.svelte | ||
| collect_imports(app_svelte_path); | ||
|
|
||
| // Copy collected files to target folder | ||
| for (const file_path of collected_files) { | ||
| const relative_path = path.relative(path.resolve('./src'), file_path); | ||
| let target_path = path.join(target_folder, relative_path); | ||
|
|
||
| // Rename App.svelte to main.svelte | ||
| if (path.basename(file_path) === 'App.svelte') { | ||
| target_path = path.join(target_folder, path.dirname(relative_path), 'main.svelte'); | ||
| } | ||
|
|
||
| // Ensure target directory exists | ||
| const target_dir = path.dirname(target_path); | ||
| if (!fs.existsSync(target_dir)) { | ||
| fs.mkdirSync(target_dir, { recursive: true }); | ||
| } | ||
|
|
||
| // Copy file | ||
| fs.copyFileSync(file_path, target_path); | ||
| console.log(`Copied: ${file_path} -> ${target_path}`); | ||
| } | ||
|
|
||
| // Create empty _config.js | ||
| const config_path = path.join(target_folder, '_config.js'); | ||
| fs.writeFileSync( | ||
| config_path, | ||
| `import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| async test({ assert, target }) { | ||
| } | ||
| }); | ||
| ` | ||
| ); | ||
| console.log(`Created: ${config_path}`); | ||
|
|
||
| console.log(`\nTest files created in: ${target_folder}`); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.