1
1
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' ;
3
4
import {
4
5
cpSync ,
5
6
existsSync ,
6
7
mkdirSync ,
7
8
readFileSync ,
8
9
readdirSync ,
9
10
rmSync ,
10
- rmdirSync ,
11
11
writeFileSync
12
12
} from 'node:fs' ;
13
13
import { format } from 'prettier' ;
14
14
import ts from 'typescript' ;
15
15
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' ) ;
16
21
17
22
// Adjust the following variables as needed for your local setup
18
23
19
24
/** 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
+
21
27
/** 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' ) ;
23
29
/** 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' ) ;
25
31
26
32
/**
27
33
* Depending on your setup, this will either clone the Svelte and SvelteKit repositories
@@ -32,37 +38,30 @@ let sveltekit_repo_path = '../../../svelte-kit';
32
38
export async function sync_docs ( ) {
33
39
if ( use_git ) {
34
40
try {
35
- mkdirSync ( 'repos' ) ;
41
+ mkdirSync ( REPOS ) ;
36
42
} catch {
37
43
// ignore if it already exists
38
44
}
39
45
40
- const cwd = process . cwd ( ) ;
41
- process . chdir ( 'repos' ) ;
42
46
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' )
45
49
] ) ;
46
- process . chdir ( cwd ) ;
47
50
48
- svelte_repo_path = 'repos /svelte' ;
49
- sveltekit_repo_path = 'repos /kit' ;
51
+ svelte_repo_path = ` ${ REPOS } /svelte` ;
52
+ sveltekit_repo_path = ` ${ REPOS } /kit` ;
50
53
}
51
54
52
55
await sync_svelte_docs ( ) ;
53
56
await sync_kit_docs ( ) ;
54
57
}
55
58
56
59
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` ) ;
63
62
64
63
const svelte_modules = await read_svelte_types ( ) ;
65
- const files = glob ( 'content/docs/ svelte/**/*.md' ) ;
64
+ const files = glob ( ` ${ DOCS } / svelte/**/*.md` ) ;
66
65
67
66
for ( const file of files ) {
68
67
const content = await replace_export_type_placeholders (
@@ -75,12 +74,8 @@ async function sync_svelte_docs() {
75
74
}
76
75
77
76
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` ) ;
84
79
85
80
const sveltekit_modules = await read_kit_types ( ) ;
86
81
@@ -117,7 +112,7 @@ async function sync_kit_docs() {
117
112
config . comment = kit_config . comment =
118
113
'See the [configuration reference](/docs/kit/configuration) for details.' ;
119
114
120
- const kit_files = glob ( 'content/docs/ kit/**/*.md' ) ;
115
+ const kit_files = glob ( ` ${ DOCS } / kit/**/*.md` ) ;
121
116
122
117
for ( const file of kit_files ) {
123
118
const content = await replace_export_type_placeholders (
@@ -143,25 +138,26 @@ function replace_strings(obj: any, replace: (str: string) => string) {
143
138
}
144
139
}
145
140
146
- async function cloneRepo ( repo : string ) {
141
+ async function clone_repo ( repo : string ) {
147
142
const regex_result = / h t t p s : \/ \/ g i t h u b .c o m \/ \w + \/ ( \w + ) .g i t / . exec ( repo ) ;
148
143
if ( ! regex_result || regex_result . length < 2 ) {
149
144
throw new Error ( `Expected https://github.com/xxx/xxx.git, but got ${ repo } ` ) ;
150
145
}
151
146
152
- const dirname = regex_result [ 1 ] ;
147
+ const dirname = ` ${ REPOS } / ${ regex_result [ 1 ] } ` ;
153
148
if ( existsSync ( dirname ) ) {
154
149
// TODO skip if we detect that same branch is already cloned
155
- rmdirSync ( dirname , { recursive : true } ) ;
150
+ rmSync ( dirname , { recursive : true } ) ;
156
151
}
157
152
158
- await invoke ( 'git' , [ 'clone' , '--depth' , '1' , repo ] ) ;
153
+ await invoke ( 'git' , [ 'clone' , '--depth' , '1' , repo ] , {
154
+ cwd : REPOS
155
+ } ) ;
159
156
}
160
157
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
+
165
161
return new Promise < void > ( ( resolve ) => {
166
162
child . on ( 'close' , ( code ) => {
167
163
if ( ! code ) {
0 commit comments