Skip to content

Commit 1c2a6b2

Browse files
committed
feat($core): return current app instance in node api
1 parent 547e4f9 commit 1c2a6b2

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

packages/@vuepress/core/lib/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ function createApp (options) {
1111
async function dev (options) {
1212
const app = createApp(options)
1313
await app.process()
14-
await app.dev()
14+
return app.dev()
1515
}
1616

1717
async function build (options) {
1818
const app = createApp(options)
1919
await app.process()
20-
await app.build()
20+
return app.build()
2121
}
2222

2323
exports.createApp = createApp

packages/@vuepress/core/lib/node/App.js

+25-14
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ module.exports = class App {
9191
}
9292

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

447448
/**
448-
* Start a dev process with correct app context
449+
* Launch a dev process with current app context.
449450
*
450-
* @returns {Promise<void>}
451+
* @returns {Promise<App>}
451452
* @api public
452453
*/
453454

454-
async dev (callback) {
455+
async dev () {
455456
this.isProd = false
456457
this.devProcess = new DevProcess(this)
457458
await this.devProcess.process()
458-
459-
this.devProcess
460-
.on('fileChanged', ({ type, target }) => {
461-
console.log(`Reload due to ${chalk.red(type)} ${chalk.cyan(path.relative(this.sourceDir, target))}`)
462-
this.process()
463-
})
464-
.createServer()
465-
.listen(callback)
459+
const error = await new Promise(resolve => {
460+
try {
461+
this.devProcess
462+
.on('fileChanged', ({ type, target }) => {
463+
console.log(`Reload due to ${chalk.red(type)} ${chalk.cyan(path.relative(this.sourceDir, target))}`)
464+
this.process()
465+
})
466+
.createServer()
467+
.listen(resolve)
468+
} catch (err) {
469+
resolve(err)
470+
}
471+
})
472+
if (error) {
473+
throw error
474+
}
475+
return this
466476
}
467477

468478
/**
469-
* Start a build process with correct app context
479+
* Launch a build process with current app context
470480
*
471-
* @returns {Promise<void>}
481+
* @returns {Promise<App>}
472482
* @api public
473483
*/
474484

@@ -477,6 +487,7 @@ module.exports = class App {
477487
this.buildProcess = new BuildProcess(this)
478488
await this.buildProcess.process()
479489
await this.buildProcess.render()
490+
return this
480491
}
481492
}
482493

0 commit comments

Comments
 (0)