refactor: encapsulate pages validation into a separate function (#4826)

* refactor: encapsulate pages validation into a separate function

* refactor: remove else
This commit is contained in:
Xin Du (Clark) 2019-01-21 18:40:37 +00:00 committed by Pooya Parsa
parent 3966b265f9
commit e3cc28c31e

View File

@ -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