@@ -12,11 +12,11 @@ import { program } from 'commander'
12
12
import { execFileSync } from 'child_process'
13
13
import frontmatter from '#src/frame/lib/read-frontmatter.js'
14
14
import patterns from '#src/frame/lib/patterns.js'
15
- import addRedirectToFrontmatter from '#src /redirects/scripts/helpers/add-redirect-to-frontmatter.js '
15
+ import addRedirectToFrontmatter from '@ /redirects/scripts/helpers/add-redirect-to-frontmatter'
16
16
import walkFiles from '#src/workflows/walk-files.ts'
17
17
18
- const contentFiles = walkFiles ( 'content' , '.md' , { includeEarlyAccess : true } )
19
- const contentDir = path . posix . join ( process . cwd ( ) , 'content' )
18
+ const contentFiles : string [ ] = walkFiles ( 'content' , '.md' , { includeEarlyAccess : true } )
19
+ const contentDir : string = path . posix . join ( process . cwd ( ) , 'content' )
20
20
21
21
program
22
22
. description ( 'Move a product-level early access docs set to a category level.' )
@@ -34,29 +34,30 @@ program
34
34
)
35
35
. parse ( process . argv )
36
36
37
- const oldPathId = program . opts ( ) . oldPath . replace ( 'content/' , '' )
38
- const newPathId = program . opts ( ) . newPath . replace ( 'content/' , '' )
37
+ const oldPathId : string = program . opts ( ) . oldPath . replace ( 'content/' , '' )
38
+ const newPathId : string = program . opts ( ) . newPath . replace ( 'content/' , '' )
39
39
40
- const oldPath = path . posix . join ( contentDir , oldPathId )
41
- const newPath = path . posix . join ( contentDir , newPathId )
40
+ const oldPath : string = path . posix . join ( contentDir , oldPathId )
41
+ const newPath : string = path . posix . join ( contentDir , newPathId )
42
42
43
43
if ( ! fs . existsSync ( oldPath ) ) {
44
44
console . error ( `Error! Can't find ${ oldPath } ` )
45
45
process . exit ( 1 )
46
46
}
47
47
48
- const filesToMigrate = contentFiles . filter ( ( file ) => file . includes ( `/${ oldPathId } /` ) )
48
+ const filesToMigrate : string [ ] = contentFiles . filter ( ( file ) => file . includes ( `/${ oldPathId } /` ) )
49
49
50
50
if ( ! filesToMigrate . length ) {
51
51
console . error ( `Error! Can't find any files in ${ oldPath } ` )
52
52
process . exit ( 1 )
53
53
}
54
54
55
- const migratePath = path . posix . join ( contentDir , newPathId )
55
+ const migratePath : string = path . posix . join ( contentDir , newPathId )
56
56
57
57
// 1. Update the image and data refs in the to-be-migrated early access files BEFORE moving them.
58
58
try {
59
- execFileSync ( 'src/early-access/scripts/update-data-and-image-paths.js' , [
59
+ execFileSync ( 'tsx' , [
60
+ 'src/early-access/scripts/update-data-and-image-paths.ts' ,
60
61
'-p' ,
61
62
`content/${ oldPathId } ` ,
62
63
'--remove' ,
@@ -66,28 +67,30 @@ try {
66
67
process . exit ( 1 )
67
68
}
68
69
69
- const variablesToMove = [ ]
70
- const reusablesToMove = [ ]
71
- const imagesToMove = [ ]
70
+ const variablesToMove : string [ ] = [ ]
71
+ const reusablesToMove : string [ ] = [ ]
72
+ const imagesToMove : string [ ] = [ ]
72
73
73
74
// 2. Add redirects to and update frontmatter in the to-be-migrated early access files BEFORE moving them.
74
75
filesToMigrate . forEach ( ( filepath ) => {
75
76
const { content, data } = frontmatter ( fs . readFileSync ( filepath , 'utf8' ) )
76
- const redirectString = filepath
77
+ const redirectString : string = filepath
77
78
. replace ( 'content/' , '/' )
78
79
. replace ( '/index.md' , '' )
79
80
. replace ( '.md' , '' )
80
- data . redirect_from = addRedirectToFrontmatter ( data . redirect_from , redirectString )
81
- delete data . hidden
82
- delete data . noEarlyAccessBanner
83
- delete data . earlyAccessToc
84
- fs . writeFileSync ( filepath , frontmatter . stringify ( content , data , { lineWidth : 10000 } ) )
81
+ if ( data ) {
82
+ data . redirect_from = addRedirectToFrontmatter ( data . redirect_from , redirectString )
83
+ delete data . hidden
84
+ delete data . noEarlyAccessBanner
85
+ delete data . earlyAccessToc
86
+ fs . writeFileSync ( filepath , frontmatter . stringify ( content || '' , data ) )
87
+ }
85
88
86
89
// 4. Find the data files and images referenced in the early access files so we can move them over.
87
- const dataRefs = content . match ( patterns . dataReference ) || [ ]
88
- const variables = dataRefs . filter ( ( ref ) => ref . includes ( 'variables' ) )
89
- const reusables = dataRefs . filter ( ( ref ) => ref . includes ( 'reusables' ) )
90
- const images = content . match ( patterns . imagePath ) || [ ]
90
+ const dataRefs : string [ ] = content ? content . match ( patterns . dataReference ) || [ ] : [ ]
91
+ const variables : string [ ] = dataRefs . filter ( ( ref ) => ref . includes ( 'variables' ) )
92
+ const reusables : string [ ] = dataRefs . filter ( ( ref ) => ref . includes ( 'reusables' ) )
93
+ const images : string [ ] = content ? content . match ( patterns . imagePath ) || [ ] : [ ]
91
94
92
95
variablesToMove . push ( ...variables )
93
96
reusablesToMove . push ( ...reusables )
@@ -103,73 +106,78 @@ Array.from(new Set(imagesToMove)).forEach((imageRef) => moveImage(imageRef))
103
106
execFileSync ( 'mv' , [ oldPath , migratePath ] )
104
107
105
108
// 5. Update the parent product TOC with the new child path.
106
- const parentProductTocPath = path . posix . join ( path . dirname ( newPath ) , 'index.md' )
107
- const parentProducToc = frontmatter ( fs . readFileSync ( parentProductTocPath , 'utf-8' ) )
108
- parentProducToc . data . children . push ( `/${ path . basename ( newPathId ) } ` )
109
+ const parentProductTocPath : string = path . posix . join ( path . dirname ( newPath ) , 'index.md' )
110
+ const parentProductToc = frontmatter ( fs . readFileSync ( parentProductTocPath , 'utf-8' ) )
111
+ if ( parentProductToc . data && Array . isArray ( parentProductToc . data . children ) ) {
112
+ parentProductToc . data . children . push ( `/${ path . basename ( newPathId ) } ` )
113
+ }
109
114
110
115
fs . writeFileSync (
111
116
parentProductTocPath ,
112
- frontmatter . stringify ( parentProducToc . content , parentProducToc . data , { lineWidth : 10000 } ) ,
117
+ frontmatter . stringify ( parentProductToc . content || '' , parentProductToc . data || { } ) ,
113
118
)
114
119
115
120
// 6. Optionally, update the new product TOC with the new title.
116
121
if ( program . opts ( ) . newTitle ) {
117
- const productTocPath = path . posix . join ( newPath , 'index.md' )
122
+ const productTocPath : string = path . posix . join ( newPath , 'index.md' )
118
123
const productToc = frontmatter ( fs . readFileSync ( productTocPath , 'utf-8' ) )
119
- productToc . data . title = program . opts ( ) . newTitle
124
+ if ( productToc . data ) {
125
+ productToc . data . title = program . opts ( ) . newTitle
126
+ }
120
127
121
128
fs . writeFileSync (
122
129
productTocPath ,
123
- frontmatter . stringify ( productToc . content , productToc . data , { lineWidth : 10000 } ) ,
130
+ frontmatter . stringify ( productToc . content || '' , productToc . data || { } ) ,
124
131
)
125
132
}
126
133
127
134
// 7. Update internal links now that the files have been moved.
128
135
console . log ( '\nRunning script to update internal links...' )
129
- execFileSync ( 'src/links/scripts/update-internal-links.js' )
136
+ execFileSync ( 'tsx' , [ ' src/links/scripts/update-internal-links.ts' ] )
130
137
131
138
console . log ( `
132
139
Done! Did the following:
133
140
- Moved content/${ oldPathId } files to content/${ newPathId }
134
- - Ran ./src/early-access/scripts/update-data-and-images -paths.js
141
+ - Ran ./src/early-access/scripts/update-data-and-image -paths.ts
135
142
- Added redirects to the moved files
136
143
- Updated children frontmatter entries in index.md files
137
- - Ran ./src/links/scripts/update-internal-links.js
144
+ - Ran ./src/links/scripts/update-internal-links.ts
138
145
139
146
Please review all the changes in docs-internal and docs-early-access, especially to index.md files. You may need to do some manual cleanup.
140
147
` )
141
148
142
- function moveVariable ( dataRef ) {
149
+ function moveVariable ( dataRef : string ) : void {
143
150
// Get the data filepath from the data reference,
144
151
// where the data reference looks like: {% data variables.foo.bar %}
145
152
// and the data filepath looks like: data/variables/foo.yml with key of 'bar'.
146
- const variablePathArray = dataRef
147
- . match ( / { % (?: d a t a | i n d e n t e d _ d a t a _ r e f e r e n c e ) ( .* ?) % } / ) [ 1 ]
148
- . split ( '.' )
149
- // If early access is part of the path, remove it (since the path below already includes it)
150
- . filter ( ( n ) => n !== 'early-access' )
153
+ const variablePathArray : string [ ] =
154
+ dataRef
155
+ . match ( / { % (?: d a t a | i n d e n t e d _ d a t a _ r e f e r e n c e ) ( .* ?) % } / ) ?. [ 1 ]
156
+ . split ( '.' )
157
+ // If early access is part of the path, remove it (since the path below already includes it)
158
+ . filter ( ( n ) => n !== 'early-access' ) || [ ]
151
159
152
160
// Given a string `variables.foo.bar` split into an array, we want the last segment 'bar', which is the variable key.
153
161
// Then pop 'bar' off the array because it's not really part of the filepath.
154
162
// The filepath we want is `variables/foo.yml`.
155
- const variableKey = last ( variablePathArray )
163
+ const variableKey : string = last ( variablePathArray ) as string
156
164
157
165
variablePathArray . pop ( )
158
166
159
- const oldVariablePath = path . posix . join (
167
+ const oldVariablePath : string = path . posix . join (
160
168
process . cwd ( ) ,
161
169
'data/early-access' ,
162
170
`${ variablePathArray . join ( '/' ) } .yml` ,
163
171
)
164
- const newVariablePath = path . posix . join (
172
+ const newVariablePath : string = path . posix . join (
165
173
process . cwd ( ) ,
166
174
'data' ,
167
175
`${ variablePathArray . join ( '/' ) } .yml` ,
168
176
)
169
- const nonAltPath = newVariablePath . replace ( '-alt.yml' , '.yml' )
170
- const oldAltPath = oldVariablePath . replace ( '.yml' , '-alt.yml' )
177
+ const nonAltPath : string = newVariablePath . replace ( '-alt.yml' , '.yml' )
178
+ const oldAltPath : string = oldVariablePath . replace ( '.yml' , '-alt.yml' )
171
179
172
- let oldPath = oldVariablePath
180
+ let oldPath : string = oldVariablePath
173
181
174
182
// If the old variable path doesn't exist, assume no migration needed.
175
183
if ( ! fs . existsSync ( oldVariablePath ) ) {
@@ -184,12 +192,17 @@ function moveVariable(dataRef) {
184
192
}
185
193
}
186
194
187
- const variableFileContent = yaml . load ( fs . readFileSync ( oldPath , 'utf8' ) )
188
- const value = variableFileContent [ variableKey ]
195
+ const variableFileContent : Record < string , any > = yaml . load (
196
+ fs . readFileSync ( oldPath , 'utf8' ) ,
197
+ ) as Record < string , any >
198
+ const value : any = variableFileContent [ variableKey ]
189
199
190
200
// If the variable file already exists, add the key/value pair.
191
201
if ( fs . existsSync ( nonAltPath ) ) {
192
- const content = yaml . load ( fs . readFileSync ( nonAltPath , 'utf8' ) )
202
+ const content : Record < string , any > = yaml . load ( fs . readFileSync ( nonAltPath , 'utf8' ) ) as Record <
203
+ string ,
204
+ any
205
+ >
193
206
if ( ! content [ variableKey ] ) {
194
207
const newString = `\n\n${ variableKey } : ${ value } `
195
208
fs . appendFileSync ( nonAltPath , newString )
@@ -199,19 +212,24 @@ function moveVariable(dataRef) {
199
212
}
200
213
}
201
214
202
- function moveReusable ( dataRef ) {
215
+ function moveReusable ( dataRef : string ) : void {
203
216
// Get the data filepath from the data reference,
204
217
// where the data reference looks like: {% data reusables.foo.bar %}
205
218
// and the data filepath looks like: data/reusables/foo/bar.md.
206
- const reusablePath = dataRef
207
- . match ( / { % (?: d a t a | i n d e n t e d _ d a t a _ r e f e r e n c e ) ( \S * ?) .* % } / ) [ 1 ]
208
- . split ( '.' )
209
- // If early access is part of the path, remove it (since the path below already includes it)
210
- . filter ( ( n ) => n !== 'early-access' )
211
- . join ( '/' )
212
-
213
- const oldReusablePath = path . posix . join ( process . cwd ( ) , 'data/early-access' , `${ reusablePath } .md` )
214
- const newReusablePath = path . posix . join ( process . cwd ( ) , 'data' , `${ reusablePath } .md` )
219
+ const reusablePath : string =
220
+ dataRef
221
+ . match ( / { % (?: d a t a | i n d e n t e d _ d a t a _ r e f e r e n c e ) ( \S * ?) .* % } / ) ?. [ 1 ]
222
+ . split ( '.' )
223
+ // If early access is part of the path, remove it (since the path below already includes it)
224
+ . filter ( ( n ) => n !== 'early-access' )
225
+ . join ( '/' ) || ''
226
+
227
+ const oldReusablePath : string = path . posix . join (
228
+ process . cwd ( ) ,
229
+ 'data/early-access' ,
230
+ `${ reusablePath } .md` ,
231
+ )
232
+ const newReusablePath : string = path . posix . join ( process . cwd ( ) , 'data' , `${ reusablePath } .md` )
215
233
216
234
// If the old reusable path doesn't exist, assume no migration needed.
217
235
if ( ! fs . existsSync ( oldReusablePath ) ) {
@@ -229,14 +247,18 @@ function moveReusable(dataRef) {
229
247
}
230
248
}
231
249
232
- function moveImage ( imageRef ) {
233
- const imagePath = imageRef
250
+ function moveImage ( imageRef : string ) : void {
251
+ const imagePath : string = imageRef
234
252
. replace ( '/assets/images/' , '' )
235
253
// If early access is part of the path, remove it (since the path below already includes it)
236
254
. replace ( 'early-access' , '' )
237
255
238
- const oldImagePath = path . posix . join ( process . cwd ( ) , 'assets/images/early-access' , imagePath )
239
- const newImagePath = path . posix . join ( process . cwd ( ) , 'assets/images' , imagePath )
256
+ const oldImagePath : string = path . posix . join (
257
+ process . cwd ( ) ,
258
+ 'assets/images/early-access' ,
259
+ imagePath ,
260
+ )
261
+ const newImagePath : string = path . posix . join ( process . cwd ( ) , 'assets/images' , imagePath )
240
262
241
263
// If the old image path doesn't exist, assume no migration needed.
242
264
if ( ! fs . existsSync ( oldImagePath ) ) {
0 commit comments