chore: robustify some scripts (#17491)

* chore: robustify some scripts

- make it possible to call them with any cwd
- add ability to create test while downloading playground

* use parseArgs

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/17517/head
Simon H 5 days ago committed by GitHub
parent 917ea2d4ae
commit 319ceae20e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,6 +1,16 @@
// Creates a test from the existing playground. cwd needs to be playground/sandbox
// Creates a test from the existing playground. Can be called from anywhere.
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the directory where this script is located
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Base paths relative to this script's location
const sandbox_dir = path.resolve(__dirname, '..');
const src_dir = path.join(sandbox_dir, 'src');
const tests_dir = path.resolve(__dirname, '../../../packages/svelte/tests');
// Get target folder from command line arguments
let target_folder = process.argv[2];
@ -20,7 +30,7 @@ if (!target_folder.startsWith('runtime-')) {
process.exit(1);
}
target_folder = path.join(
path.resolve('../../packages/svelte/tests', target_folder.split('/')[0]),
path.resolve(tests_dir, target_folder.split('/')[0]),
'samples',
target_folder.split('/')[1]
);
@ -56,8 +66,8 @@ if (exists) {
const files = fs.readdirSync(target_folder);
for (const file of files) {
if (file !== '_config.js') {
const filePath = path.join(target_folder, file);
fs.rmSync(filePath, { recursive: true, force: true });
const file_path = path.join(target_folder, file);
fs.rmSync(file_path, { recursive: true, force: true });
}
}
} else {
@ -65,7 +75,7 @@ if (exists) {
}
// Starting file
const app_svelte_path = path.resolve('./src/App.svelte');
const app_svelte_path = path.join(src_dir, 'App.svelte');
const collected_files = new Set();
const processed_files = new Set();
@ -117,7 +127,7 @@ 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);
const relative_path = path.relative(src_dir, file_path);
let target_path = path.join(target_folder, relative_path);
// Rename App.svelte to main.svelte

@ -1,19 +1,36 @@
import fs from 'node:fs';
import path from 'node:path';
import { parseArgs } from 'node:util';
const arg = process.argv[2];
const { values, positionals } = parseArgs({
options: {
'create-test': {
type: 'string'
}
},
allowPositionals: true
});
const create_test_name = values['create-test'] ?? null;
const url_arg = positionals[0];
if (!url_arg) {
console.error(`Missing URL argument`);
process.exit(1);
}
/** @type {URL} */
let url;
try {
url = new URL(arg);
url = new URL(url_arg);
} catch (e) {
console.error(`${arg} is not a URL`);
console.error(`${url_arg} is not a URL`);
process.exit(1);
}
if (url.origin !== 'https://svelte.dev' || !url.pathname.startsWith('/playground/')) {
console.error(`${arg} is not a Svelte playground URL`);
console.error(`${url_arg} is not a Svelte playground URL`);
process.exit(1);
}
@ -47,6 +64,55 @@ if (url.hash.length > 1) {
});
}
for (const file of files) {
fs.writeFileSync(`src/${file.name}`, file.contents);
const base_dir = import.meta.dirname;
if (create_test_name) {
const test_parts = create_test_name.split('/').filter(Boolean);
if (test_parts.length > 2) {
console.error(
`Invalid test name "${create_test_name}". Expected e.g. "hello-world" or "runtime-legacy/hello-world"`
);
process.exit(1);
}
const suite_name = test_parts.length === 2 ? test_parts[0] : 'runtime-runes';
const test_name = test_parts[test_parts.length - 1];
const output_dir = path.join(
base_dir,
'../../..',
'packages/svelte/tests',
suite_name,
'samples',
test_name
);
if (fs.existsSync(output_dir)) {
console.warn(`Test folder "${output_dir}" already exists, overriding its contents`);
fs.rmSync(output_dir, { recursive: true, force: true });
}
fs.mkdirSync(output_dir, { recursive: true });
for (const file of files) {
const output_name = file.name === 'App.svelte' ? 'main.svelte' : file.name;
const output_path = path.join(output_dir, output_name);
fs.mkdirSync(path.dirname(output_path), { recursive: true });
fs.writeFileSync(output_path, file.contents);
}
fs.writeFileSync(
path.join(output_dir, '_config.js'),
`import { test } from '../../test';
export default test({
async test({ assert, target }) {
}
});
`
);
} else {
for (const file of files) {
fs.writeFileSync(path.join(base_dir, '..', 'src', file.name), file.contents);
}
}

Loading…
Cancel
Save