Skip to content

Commit ca442e4

Browse files
authored
chore: add script to generate test from playground (#16602)
* chore: add script to generate test from playground I often play around with stuff locally and then turn it into a test, this automates the last mile of that * ask if should override
1 parent 0480f04 commit ca442e4

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

playgrounds/sandbox/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"prod": "npm run build && node dist/server/ssr-prod",
1212
"preview": "vite preview",
1313
"download": "node scripts/download.js",
14-
"hash": "node scripts/hash.js"
14+
"hash": "node scripts/hash.js",
15+
"create-test": "node scripts/create-test.js"
1516
},
1617
"devDependencies": {
1718
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
const exists = fs.existsSync(target_folder);
29+
30+
// Check if target folder already exists and ask for confirmation
31+
if (exists) {
32+
console.log(`Target folder "${target_folder}" already exists.`);
33+
process.stdout.write('Do you want to override the existing test? (Y/n): ');
34+
35+
// Read user input synchronously
36+
const stdin = process.stdin;
37+
stdin.setRawMode(true);
38+
stdin.resume();
39+
stdin.setEncoding('utf8');
40+
41+
const response = await new Promise((resolve) => {
42+
stdin.on('data', (key) => {
43+
stdin.setRawMode(false);
44+
stdin.pause();
45+
process.stdout.write('\n');
46+
resolve(key);
47+
});
48+
});
49+
50+
if (response.toLowerCase() === 'n') {
51+
console.log('Operation cancelled.');
52+
process.exit(0);
53+
}
54+
55+
// Clear the existing target folder except for _config.js
56+
const files = fs.readdirSync(target_folder);
57+
for (const file of files) {
58+
if (file !== '_config.js') {
59+
const filePath = path.join(target_folder, file);
60+
fs.rmSync(filePath, { recursive: true, force: true });
61+
}
62+
}
63+
} else {
64+
fs.mkdirSync(target_folder, { recursive: true });
65+
}
66+
67+
// Starting file
68+
const app_svelte_path = path.resolve('./src/App.svelte');
69+
const collected_files = new Set();
70+
const processed_files = new Set();
71+
72+
function collect_imports(file_path) {
73+
if (processed_files.has(file_path) || !fs.existsSync(file_path)) {
74+
return;
75+
}
76+
77+
processed_files.add(file_path);
78+
collected_files.add(file_path);
79+
80+
const content = fs.readFileSync(file_path, 'utf8');
81+
82+
// Regex to match import statements
83+
const import_regex = /import\s+(?:[^'"]*\s+from\s+)?['"]([^'"]+)['"]/g;
84+
let match;
85+
86+
while ((match = import_regex.exec(content)) !== null) {
87+
const import_path = match[1];
88+
89+
// Skip node_modules imports
90+
if (!import_path.startsWith('.')) {
91+
continue;
92+
}
93+
94+
// Resolve relative import path
95+
const resolved_path = path.resolve(path.dirname(file_path), import_path);
96+
97+
// Try different extensions if file doesn't exist
98+
const extensions = ['', '.svelte', '.js', '.ts'];
99+
let actual_path = null;
100+
101+
for (const ext of extensions) {
102+
const test_path = resolved_path + ext;
103+
if (fs.existsSync(test_path)) {
104+
actual_path = test_path;
105+
break;
106+
}
107+
}
108+
109+
if (actual_path) {
110+
collect_imports(actual_path);
111+
}
112+
}
113+
}
114+
115+
// Start collecting from App.svelte
116+
collect_imports(app_svelte_path);
117+
118+
// Copy collected files to target folder
119+
for (const file_path of collected_files) {
120+
const relative_path = path.relative(path.resolve('./src'), file_path);
121+
let target_path = path.join(target_folder, relative_path);
122+
123+
// Rename App.svelte to main.svelte
124+
if (path.basename(file_path) === 'App.svelte') {
125+
target_path = path.join(target_folder, path.dirname(relative_path), 'main.svelte');
126+
}
127+
128+
// Ensure target directory exists
129+
const target_dir = path.dirname(target_path);
130+
if (!fs.existsSync(target_dir)) {
131+
fs.mkdirSync(target_dir, { recursive: true });
132+
}
133+
134+
// Copy file
135+
fs.copyFileSync(file_path, target_path);
136+
console.log(`Copied: ${file_path} -> ${target_path}`);
137+
}
138+
139+
// Create empty _config.js
140+
if (!exists) {
141+
const config_path = path.join(target_folder, '_config.js');
142+
fs.writeFileSync(
143+
config_path,
144+
`import { test } from '../../test';
145+
146+
export default test({
147+
async test({ assert, target }) {
148+
}
149+
});
150+
`
151+
);
152+
console.log(`Created: ${config_path}`);
153+
}
154+
155+
console.log(`\nTest files created in: ${target_folder}`);

0 commit comments

Comments
 (0)