You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/packages/playground/fetch-repl.js

25 lines
650 B

// Fetch a REPL from svelte.dev and write the files to the src/ directory
// Usage: node fetch-repl.js <id> or node fetch-repl.js <url>
import { writeFileSync } from 'fs';
const id = process.argv[2];
if (!id) {
throw new Error('Missing id');
}
let json_url = `https://svelte.dev/repl/api/${id}.json`;
try {
const tmp = new URL(id);
tmp.pathname = tmp.pathname.replace(/\/repl/, '/repl/api');
tmp.pathname += '.json';
json_url = tmp.href;
} catch {}
const repl_data = await fetch(json_url).then((r) => r.json());
for (const component of repl_data.components) {
writeFileSync(`src/${component.name}.${component.type}`, component.source);
}