From 1c2a6b2563cd553efe7851fefcc9d9f9b69cb52a Mon Sep 17 00:00:00 2001
From: ULIVZ <472590061@qq.com>
Date: Mon, 11 Mar 2019 02:47:53 +0800
Subject: [PATCH] feat($core): return current app instance in node api

---
 packages/@vuepress/core/lib/index.js    |  4 +--
 packages/@vuepress/core/lib/node/App.js | 39 ++++++++++++++++---------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/packages/@vuepress/core/lib/index.js b/packages/@vuepress/core/lib/index.js
index 9051524575..add8f2fd85 100644
--- a/packages/@vuepress/core/lib/index.js
+++ b/packages/@vuepress/core/lib/index.js
@@ -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
diff --git a/packages/@vuepress/core/lib/node/App.js b/packages/@vuepress/core/lib/node/App.js
index bae5b61557..e853e22e37 100755
--- a/packages/@vuepress/core/lib/node/App.js
+++ b/packages/@vuepress/core/lib/node/App.js
@@ -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
@@ -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
    */
 
@@ -477,6 +487,7 @@ module.exports = class App {
     this.buildProcess = new BuildProcess(this)
     await this.buildProcess.process()
     await this.buildProcess.render()
+    return this
   }
 }