Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/svelte.dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check": "node scripts/update.js && svelte-kit sync && svelte-check",
"format": "prettier --write .",
"lint": "prettier --check .",
"sync-docs": "tsx src/sync-docs.ts"
"sync-docs": "tsx scripts/sync-docs.ts"
},
"dependencies": {
"@codemirror/autocomplete": "^6.9.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { replace_export_type_placeholders, type Modules } from '@sveltejs/site-kit/markdown';
import { spawn } from 'node:child_process';
import { spawn, type SpawnOptions } from 'node:child_process';
import path from 'node:path';
import {
cpSync,
existsSync,
mkdirSync,
readFileSync,
readdirSync,
rmSync,
rmdirSync,
writeFileSync
} from 'node:fs';
import { format } from 'prettier';
import ts from 'typescript';
import glob from 'tiny-glob/sync';
import { fileURLToPath } from 'node:url';

const dirname = fileURLToPath(new URL('.', import.meta.url));
const REPOS = path.join(dirname, '../repos');
const DOCS = path.join(dirname, '../content/docs');

// Adjust the following variables as needed for your local setup

/** If `true`, will checkout the docs from Git. If `false`, will use the `..._repo_path` vars to get content from your local file system */
const use_git = false;
const use_git = process.env.USE_GIT === 'true';

/** The path to your local Svelte repository (only relevant if `use_git` is `false`) */
let svelte_repo_path = '../../../svelte';
let svelte_repo_path = path.join(dirname, '../../../../svelte');
/** The path to your local SvelteKit repository (only relevant if `use_git` is `false`) */
let sveltekit_repo_path = '../../../svelte-kit';
let sveltekit_repo_path = path.join(dirname, '../../../../svelte-kit');

/**
* Depending on your setup, this will either clone the Svelte and SvelteKit repositories
Expand All @@ -32,37 +38,30 @@ let sveltekit_repo_path = '../../../svelte-kit';
export async function sync_docs() {
if (use_git) {
try {
mkdirSync('repos');
mkdirSync(REPOS);
} catch {
// ignore if it already exists
}

const cwd = process.cwd();
process.chdir('repos');
await Promise.all([
cloneRepo('https://github.com/sveltejs/svelte.git'),
cloneRepo('https://github.com/sveltejs/kit.git')
clone_repo('https://github.com/sveltejs/svelte.git'),
clone_repo('https://github.com/sveltejs/kit.git')
]);
process.chdir(cwd);

svelte_repo_path = 'repos/svelte';
sveltekit_repo_path = 'repos/kit';
svelte_repo_path = `${REPOS}/svelte`;
sveltekit_repo_path = `${REPOS}/kit`;
}

await sync_svelte_docs();
await sync_kit_docs();
}

async function sync_svelte_docs() {
cpSync(
new URL(`../${svelte_repo_path}/documentation/docs`, import.meta.url).pathname.slice(1),
'content/docs/svelte',
{ recursive: true }
);
migrate_meta_json('content/docs/svelte');
cpSync(`${svelte_repo_path}/documentation/docs`, `${DOCS}/svelte`, { recursive: true });
migrate_meta_json(`${DOCS}/svelte`);

const svelte_modules = await read_svelte_types();
const files = glob('content/docs/svelte/**/*.md');
const files = glob(`${DOCS}/svelte/**/*.md`);

for (const file of files) {
const content = await replace_export_type_placeholders(
Expand All @@ -75,12 +74,8 @@ async function sync_svelte_docs() {
}

async function sync_kit_docs() {
cpSync(
new URL(`../${sveltekit_repo_path}/documentation/docs`, import.meta.url).pathname.slice(1),
'content/docs/kit',
{ recursive: true }
);
migrate_meta_json('content/docs/kit');
cpSync(`${sveltekit_repo_path}/documentation/docs`, `${DOCS}/kit`, { recursive: true });
migrate_meta_json(`${DOCS}/kit`);

const sveltekit_modules = await read_kit_types();

Expand Down Expand Up @@ -117,7 +112,7 @@ async function sync_kit_docs() {
config.comment = kit_config.comment =
'See the [configuration reference](/docs/kit/configuration) for details.';

const kit_files = glob('content/docs/kit/**/*.md');
const kit_files = glob(`${DOCS}/kit/**/*.md`);

for (const file of kit_files) {
const content = await replace_export_type_placeholders(
Expand All @@ -143,25 +138,26 @@ function replace_strings(obj: any, replace: (str: string) => string) {
}
}

async function cloneRepo(repo: string) {
async function clone_repo(repo: string) {
const regex_result = /https:\/\/github.com\/\w+\/(\w+).git/.exec(repo);
if (!regex_result || regex_result.length < 2) {
throw new Error(`Expected https://github.com/xxx/xxx.git, but got ${repo}`);
}

const dirname = regex_result[1];
const dirname = `${REPOS}/${regex_result[1]}`;
if (existsSync(dirname)) {
// TODO skip if we detect that same branch is already cloned
rmdirSync(dirname, { recursive: true });
rmSync(dirname, { recursive: true });
}

await invoke('git', ['clone', '--depth', '1', repo]);
await invoke('git', ['clone', '--depth', '1', repo], {
cwd: REPOS
});
}

function invoke(cmd: string, args: string[]) {
const child = spawn(cmd, args);
child.stdout.on('data', (data) => console.log(data.toString()));
child.stderr.on('data', (data) => console.error(data.toString()));
function invoke(cmd: string, args: string[], opts: SpawnOptions) {
const child = spawn(cmd, args, { ...opts, stdio: 'inherit' });

return new Promise<void>((resolve) => {
child.on('close', (code) => {
if (!code) {
Expand Down
Loading