@@ -2,18 +2,21 @@ const fs = require('fs')
22const path = require ( 'path' )
33const globby = require ( 'globby' )
44const mkdirp = require ( 'mkdirp' )
5+ const { promisify } = require ( 'util' )
6+ const readFile = promisify ( fs . readFile )
7+ const writeFile = promisify ( fs . writeFile )
58const yaml = require ( 'yaml-front-matter' )
69const tempPath = path . resolve ( __dirname , 'app/.temp' )
710const { inferTitle, extractHeaders } = require ( './util' )
811
912mkdirp ( tempPath )
1013
1114const tempCache = new Map ( )
12- function writeTemp ( file , content ) {
15+ async function writeTemp ( file , content ) {
1316 // cache write to avoid hitting the dist if it didn't change
1417 const cached = tempCache . get ( file )
1518 if ( cached !== content ) {
16- fs . writeFileSync ( path . join ( tempPath , file ) , content )
19+ await writeFile ( path . join ( tempPath , file ) , content )
1720 tempCache . set ( file , content )
1821 }
1922}
@@ -26,14 +29,14 @@ module.exports = async function prepare (sourceDir) {
2629 const routesCode = await genRoutesFile ( options )
2730 const componentCode = await genComponentRegistrationFile ( options )
2831
29- writeTemp ( 'routes.js' , [
32+ await writeTemp ( 'routes.js' , [
3033 componentCode ,
3134 routesCode
3235 ] . join ( '\n\n' ) )
3336
3437 // 3. generate siteData
3538 const dataCode = `export const siteData = ${ JSON . stringify ( options . siteData , null , 2 ) } `
36- writeTemp ( 'siteData.js' , dataCode )
39+ await writeTemp ( 'siteData.js' , dataCode )
3740
3841 // 4. generate basic polyfill if need to support older browsers
3942 let polyfillCode = ``
@@ -42,13 +45,13 @@ module.exports = async function prepare (sourceDir) {
4245`import 'es6-promise/auto'
4346if (!Object.assign) Object.assign = require('object-assign')`
4447 }
45- writeTemp ( 'polyfill.js' , polyfillCode )
48+ await writeTemp ( 'polyfill.js' , polyfillCode )
4649
4750 // 5. handle user override
4851 if ( options . useDefaultTheme ) {
4952 const overridePath = path . resolve ( sourceDir , '.vuepress/override.styl' )
5053 const hasUserOverride = fs . existsSync ( overridePath )
51- writeTemp ( `override.styl` , hasUserOverride ? `@import(${ JSON . stringify ( overridePath ) } )` : `` )
54+ await writeTemp ( `override.styl` , hasUserOverride ? `@import(${ JSON . stringify ( overridePath ) } )` : `` )
5255 }
5356
5457 return options
@@ -141,13 +144,13 @@ async function resolveOptions (sourceDir) {
141144 }
142145
143146 // resolve pages
144- const pagesData = options . pageFiles . map ( file => {
147+ const pagesData = await Promise . all ( options . pageFiles . map ( async ( file ) => {
145148 const data = {
146149 path : fileToPath ( file )
147150 }
148151
149152 // extract yaml frontmatter
150- const content = fs . readFileSync ( path . resolve ( sourceDir , file ) , 'utf-8' )
153+ const content = await readFile ( path . resolve ( sourceDir , file ) , 'utf-8' )
151154 const frontmatter = yaml . loadFront ( content )
152155 // infer title
153156 const title = inferTitle ( frontmatter )
@@ -163,7 +166,7 @@ async function resolveOptions (sourceDir) {
163166 data . frontmatter = frontmatter
164167 }
165168 return data
166- } )
169+ } ) )
167170
168171 // resolve site data
169172 options . siteData = {
0 commit comments