Skip to content

Commit 1cbec5f

Browse files
authored
Update sync script (#114)
* move sync script to scripts * make it work on non-toy operating systems * control use_git via env var * tweak stuff * use absolute paths * more absolute paths * snake_case
1 parent 671baf0 commit 1cbec5f

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

apps/svelte.dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"check": "node scripts/update.js && svelte-kit sync && svelte-check",
1414
"format": "prettier --write .",
1515
"lint": "prettier --check .",
16-
"sync-docs": "tsx src/sync-docs.ts"
16+
"sync-docs": "tsx scripts/sync-docs.ts"
1717
},
1818
"dependencies": {
1919
"@codemirror/autocomplete": "^6.9.0",

apps/svelte.dev/src/sync-docs.ts renamed to apps/svelte.dev/scripts/sync-docs.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { replace_export_type_placeholders, type Modules } from '@sveltejs/site-kit/markdown';
2-
import { spawn } from 'node:child_process';
2+
import { spawn, type SpawnOptions } from 'node:child_process';
3+
import path from 'node:path';
34
import {
45
cpSync,
56
existsSync,
67
mkdirSync,
78
readFileSync,
89
readdirSync,
910
rmSync,
10-
rmdirSync,
1111
writeFileSync
1212
} from 'node:fs';
1313
import { format } from 'prettier';
1414
import ts from 'typescript';
1515
import glob from 'tiny-glob/sync';
16+
import { fileURLToPath } from 'node:url';
17+
18+
const dirname = fileURLToPath(new URL('.', import.meta.url));
19+
const REPOS = path.join(dirname, '../repos');
20+
const DOCS = path.join(dirname, '../content/docs');
1621

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

1924
/** If `true`, will checkout the docs from Git. If `false`, will use the `..._repo_path` vars to get content from your local file system */
20-
const use_git = false;
25+
const use_git = process.env.USE_GIT === 'true';
26+
2127
/** The path to your local Svelte repository (only relevant if `use_git` is `false`) */
22-
let svelte_repo_path = '../../../svelte';
28+
let svelte_repo_path = path.join(dirname, '../../../../svelte');
2329
/** The path to your local SvelteKit repository (only relevant if `use_git` is `false`) */
24-
let sveltekit_repo_path = '../../../svelte-kit';
30+
let sveltekit_repo_path = path.join(dirname, '../../../../svelte-kit');
2531

2632
/**
2733
* Depending on your setup, this will either clone the Svelte and SvelteKit repositories
@@ -32,37 +38,30 @@ let sveltekit_repo_path = '../../../svelte-kit';
3238
export async function sync_docs() {
3339
if (use_git) {
3440
try {
35-
mkdirSync('repos');
41+
mkdirSync(REPOS);
3642
} catch {
3743
// ignore if it already exists
3844
}
3945

40-
const cwd = process.cwd();
41-
process.chdir('repos');
4246
await Promise.all([
43-
cloneRepo('https://github.com/sveltejs/svelte.git'),
44-
cloneRepo('https://github.com/sveltejs/kit.git')
47+
clone_repo('https://github.com/sveltejs/svelte.git'),
48+
clone_repo('https://github.com/sveltejs/kit.git')
4549
]);
46-
process.chdir(cwd);
4750

48-
svelte_repo_path = 'repos/svelte';
49-
sveltekit_repo_path = 'repos/kit';
51+
svelte_repo_path = `${REPOS}/svelte`;
52+
sveltekit_repo_path = `${REPOS}/kit`;
5053
}
5154

5255
await sync_svelte_docs();
5356
await sync_kit_docs();
5457
}
5558

5659
async function sync_svelte_docs() {
57-
cpSync(
58-
new URL(`../${svelte_repo_path}/documentation/docs`, import.meta.url).pathname.slice(1),
59-
'content/docs/svelte',
60-
{ recursive: true }
61-
);
62-
migrate_meta_json('content/docs/svelte');
60+
cpSync(`${svelte_repo_path}/documentation/docs`, `${DOCS}/svelte`, { recursive: true });
61+
migrate_meta_json(`${DOCS}/svelte`);
6362

6463
const svelte_modules = await read_svelte_types();
65-
const files = glob('content/docs/svelte/**/*.md');
64+
const files = glob(`${DOCS}/svelte/**/*.md`);
6665

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

7776
async function sync_kit_docs() {
78-
cpSync(
79-
new URL(`../${sveltekit_repo_path}/documentation/docs`, import.meta.url).pathname.slice(1),
80-
'content/docs/kit',
81-
{ recursive: true }
82-
);
83-
migrate_meta_json('content/docs/kit');
77+
cpSync(`${sveltekit_repo_path}/documentation/docs`, `${DOCS}/kit`, { recursive: true });
78+
migrate_meta_json(`${DOCS}/kit`);
8479

8580
const sveltekit_modules = await read_kit_types();
8681

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

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

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

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

152-
const dirname = regex_result[1];
147+
const dirname = `${REPOS}/${regex_result[1]}`;
153148
if (existsSync(dirname)) {
154149
// TODO skip if we detect that same branch is already cloned
155-
rmdirSync(dirname, { recursive: true });
150+
rmSync(dirname, { recursive: true });
156151
}
157152

158-
await invoke('git', ['clone', '--depth', '1', repo]);
153+
await invoke('git', ['clone', '--depth', '1', repo], {
154+
cwd: REPOS
155+
});
159156
}
160157

161-
function invoke(cmd: string, args: string[]) {
162-
const child = spawn(cmd, args);
163-
child.stdout.on('data', (data) => console.log(data.toString()));
164-
child.stderr.on('data', (data) => console.error(data.toString()));
158+
function invoke(cmd: string, args: string[], opts: SpawnOptions) {
159+
const child = spawn(cmd, args, { ...opts, stdio: 'inherit' });
160+
165161
return new Promise<void>((resolve) => {
166162
child.on('close', (code) => {
167163
if (!code) {

0 commit comments

Comments
 (0)