1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
- import { fileURLToPath } from 'url' ;
4
3
5
4
import matter from 'gray-matter' ;
6
5
import { s } from 'hastscript' ;
@@ -29,8 +28,7 @@ import remarkTocHeadings, {TocNode} from './remark-toc-headings';
29
28
import remarkVariables from './remark-variables' ;
30
29
import { FrontMatter , Platform , PlatformConfig } from './types' ;
31
30
32
- // @ts -ignore
33
- const directoryName = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
31
+ const root = process . cwd ( ) ;
34
32
35
33
function formatSlug ( slug : string ) {
36
34
return slug . replace ( / \. ( m d x | m d ) / , '' ) ;
@@ -98,8 +96,8 @@ async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
98
96
return frontMatter ;
99
97
}
100
98
101
- function getAllFilesFrontMatter ( ) {
102
- const docsPath = path . join ( directoryName , '..' , 'docs' ) ;
99
+ export function getAllFilesFrontMatter ( folder : string = 'docs' ) {
100
+ const docsPath = path . join ( root , folder ) ;
103
101
const files = getAllFilesRecursively ( docsPath ) ;
104
102
const allFrontMatter : FrontMatter [ ] = [ ] ;
105
103
files . forEach ( file => {
@@ -117,10 +115,15 @@ function getAllFilesFrontMatter() {
117
115
allFrontMatter . push ( {
118
116
...( frontmatter as FrontMatter ) ,
119
117
slug : formatSlug ( fileName ) ,
120
- sourcePath : path . join ( 'docs' , fileName ) ,
118
+ sourcePath : path . join ( folder , fileName ) ,
121
119
} ) ;
122
120
} ) ;
123
121
122
+ if ( folder !== 'docs' ) {
123
+ // We exit early if we're not in the docs folder. We use this for the changelog.
124
+ return allFrontMatter ;
125
+ }
126
+
124
127
// Add all `common` files in the right place.
125
128
const platformsPath = path . join ( docsPath , 'platforms' ) ;
126
129
const platformNames = fs
@@ -215,17 +218,17 @@ function getAllFilesFrontMatter() {
215
218
}
216
219
217
220
export async function getFileBySlug ( slug : string ) {
218
- const configPath = path . join ( directoryName , '..' , slug , 'config.yml' ) ;
221
+ const configPath = path . join ( root , slug , 'config.yml' ) ;
219
222
220
223
let configFrontmatter : PlatformConfig | undefined ;
221
224
if ( fs . existsSync ( configPath ) ) {
222
225
configFrontmatter = yaml . load ( fs . readFileSync ( configPath , 'utf8' ) ) as PlatformConfig ;
223
226
}
224
227
225
- let mdxPath = path . join ( directoryName , '..' , `${ slug } .mdx` ) ;
226
- let mdxIndexPath = path . join ( directoryName , '..' , slug , 'index.mdx' ) ;
227
- let mdPath = path . join ( directoryName , '..' , `${ slug } .md` ) ;
228
- let mdIndexPath = path . join ( directoryName , '..' , slug , 'index.md' ) ;
228
+ let mdxPath = path . join ( root , `${ slug } .mdx` ) ;
229
+ let mdxIndexPath = path . join ( root , slug , 'index.mdx' ) ;
230
+ let mdPath = path . join ( root , `${ slug } .md` ) ;
231
+ let mdIndexPath = path . join ( root , slug , 'index.md' ) ;
229
232
230
233
if (
231
234
slug . indexOf ( 'docs/platforms/' ) === 0 &&
@@ -246,20 +249,18 @@ export async function getFileBySlug(slug: string) {
246
249
commonFilePath = path . join ( commonPath , slugParts . slice ( 3 ) . join ( '/' ) ) ;
247
250
}
248
251
if ( commonFilePath && fs . existsSync ( commonPath ) ) {
249
- mdxPath = path . join ( directoryName , '..' , `${ commonFilePath } .mdx` ) ;
250
- mdxIndexPath = path . join ( directoryName , '..' , commonFilePath , 'index.mdx' ) ;
251
- mdPath = path . join ( directoryName , '..' , `${ commonFilePath } .md` ) ;
252
- mdIndexPath = path . join ( directoryName , '..' , commonFilePath , 'index.md' ) ;
252
+ mdxPath = path . join ( root , `${ commonFilePath } .mdx` ) ;
253
+ mdxIndexPath = path . join ( root , commonFilePath , 'index.mdx' ) ;
254
+ mdPath = path . join ( root , `${ commonFilePath } .md` ) ;
255
+ mdIndexPath = path . join ( root , commonFilePath , 'index.md' ) ;
253
256
}
254
257
}
255
258
256
- const sourcePath =
257
- [ mdxPath , mdxIndexPath , mdPath ] . find ( p => fs . existsSync ( p ) ) ?? mdIndexPath ;
259
+ const sourcePath = [ mdxPath , mdxIndexPath , mdPath ] . find ( fs . existsSync ) ?? mdIndexPath ;
258
260
const source = fs . readFileSync ( sourcePath , 'utf8' ) ;
259
261
260
262
process . env . ESBUILD_BINARY_PATH = path . join (
261
- directoryName ,
262
- '..' ,
263
+ root ,
263
264
'node_modules' ,
264
265
'esbuild' ,
265
266
'bin' ,
@@ -284,10 +285,7 @@ export async function getFileBySlug(slug: string) {
284
285
[ remarkTocHeadings , { exportRef : toc } ] ,
285
286
remarkGfm ,
286
287
remarkFormatCodeBlocks ,
287
- [
288
- remarkImageSize ,
289
- { sourceFolder : cwd , publicFolder : path . join ( directoryName , '..' , 'public' ) } ,
290
- ] ,
288
+ [ remarkImageSize , { sourceFolder : cwd , publicFolder : path . join ( root , 'public' ) } ] ,
291
289
remarkMdxImages ,
292
290
remarkCodeTitles ,
293
291
remarkCodeTabs ,
@@ -355,7 +353,7 @@ export async function getFileBySlug(slug: string) {
355
353
} ;
356
354
// Set the `outdir` to a public location for this bundle.
357
355
// this where this images will be copied
358
- options . outdir = path . join ( directoryName , '..' , 'public' , 'mdx-images' ) ;
356
+ options . outdir = path . join ( root , 'public' , 'mdx-images' ) ;
359
357
360
358
// Set write to true so that esbuild will output the files.
361
359
options . write = true ;
0 commit comments