diff --git a/apps/svelte.dev/package.json b/apps/svelte.dev/package.json index 8a581ce22c..6a5f60fd94 100644 --- a/apps/svelte.dev/package.json +++ b/apps/svelte.dev/package.json @@ -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", diff --git a/apps/svelte.dev/src/sync-docs.ts b/apps/svelte.dev/scripts/sync-docs.ts similarity index 90% rename from apps/svelte.dev/src/sync-docs.ts rename to apps/svelte.dev/scripts/sync-docs.ts index db0c787182..5f220f00c8 100644 --- a/apps/svelte.dev/src/sync-docs.ts +++ b/apps/svelte.dev/scripts/sync-docs.ts @@ -1,5 +1,6 @@ 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, @@ -7,21 +8,26 @@ import { 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 @@ -32,21 +38,18 @@ 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(); @@ -54,15 +57,11 @@ export async function sync_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( @@ -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(); @@ -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( @@ -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((resolve) => { child.on('close', (code) => { if (!code) {