Skip to content

Commit

Permalink
feat($core): return current app instance in node api
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Mar 10, 2019
1 parent 547e4f9 commit 1c2a6b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/@vuepress/core/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ function createApp (options) {
async function dev (options) {
const app = createApp(options)
await app.process()
await app.dev()
return app.dev()
}

async function build (options) {
const app = createApp(options)
await app.process()
await app.build()
return app.build()
}

exports.createApp = createApp
Expand Down
39 changes: 25 additions & 14 deletions packages/@vuepress/core/lib/node/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ module.exports = class App {
}

/**
* Load pages, load plugins, apply plugins / plugin options, etc.
* A asynchronous method used to prepare the context of the current app. which
* contains loading pages and plugins, apply plugins, etc.
*
* @returns {Promise<void>}
* @api private
Expand Down Expand Up @@ -445,30 +446,39 @@ module.exports = class App {
}

/**
* Start a dev process with correct app context
* Launch a dev process with current app context.
*
* @returns {Promise<void>}
* @returns {Promise<App>}
* @api public
*/

async dev (callback) {
async dev () {
this.isProd = false
this.devProcess = new DevProcess(this)
await this.devProcess.process()

this.devProcess
.on('fileChanged', ({ type, target }) => {
console.log(`Reload due to ${chalk.red(type)} ${chalk.cyan(path.relative(this.sourceDir, target))}`)
this.process()
})
.createServer()
.listen(callback)
const error = await new Promise(resolve => {
try {
this.devProcess
.on('fileChanged', ({ type, target }) => {
console.log(`Reload due to ${chalk.red(type)} ${chalk.cyan(path.relative(this.sourceDir, target))}`)
this.process()
})
.createServer()
.listen(resolve)
} catch (err) {
resolve(err)
}
})
if (error) {
throw error
}
return this
}

/**
* Start a build process with correct app context
* Launch a build process with current app context
*
* @returns {Promise<void>}
* @returns {Promise<App>}
* @api public
*/

Expand All @@ -477,6 +487,7 @@ module.exports = class App {
this.buildProcess = new BuildProcess(this)
await this.buildProcess.process()
await this.buildProcess.render()
return this
}
}

0 comments on commit 1c2a6b2

Please sign in to comment.