From e3cc28c31eff23598776ff22d8ac19f74e998230 Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Mon, 21 Jan 2019 18:40:37 +0000 Subject: [PATCH] refactor: encapsulate pages validation into a separate function (#4826) * refactor: encapsulate pages validation into a separate function * refactor: remove else --- packages/builder/src/builder.js | 38 ++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/builder/src/builder.js b/packages/builder/src/builder.js index 901fd0ea77..ad4fabf8f8 100644 --- a/packages/builder/src/builder.js +++ b/packages/builder/src/builder.js @@ -193,21 +193,7 @@ export default class Builder { // Call before hook await this.nuxt.callHook('build:before', this, this.options.build) - // Check if pages dir exists and warn if not - this._nuxtPages = typeof this.options.build.createRoutes !== 'function' - if (this._nuxtPages) { - if (!fsExtra.existsSync(path.join(this.options.srcDir, this.options.dir.pages))) { - const dir = this.options.srcDir - if (fsExtra.existsSync(path.join(this.options.srcDir, '..', this.options.dir.pages))) { - throw new Error( - `No \`${this.options.dir.pages}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?` - ) - } else { - this._defaultPage = true - consola.warn(`No \`${this.options.dir.pages}\` directory found in ${dir}. Using the default built-in page.`) - } - } - } + await this.validatePages() // Validate template try { @@ -248,6 +234,28 @@ export default class Builder { return this } + // Check if pages dir exists and warn if not + async validatePages() { + this._nuxtPages = typeof this.options.build.createRoutes !== 'function' + + if ( + !this._nuxtPages || + await fsExtra.exists(path.join(this.options.srcDir, this.options.dir.pages)) + ) { + return + } + + const dir = this.options.srcDir + if (await fsExtra.exists(path.join(this.options.srcDir, '..', this.options.dir.pages))) { + throw new Error( + `No \`${this.options.dir.pages}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?` + ) + } + + this._defaultPage = true + consola.warn(`No \`${this.options.dir.pages}\` directory found in ${dir}. Using the default built-in page.`) + } + validateTemplate() { // Validate template dependencies const templateDependencies = this.template.dependencies