refactor: avoid using `_path` variable (#5582)

This commit is contained in:
Pooya Parsa 2019-04-22 23:20:07 +04:30 committed by GitHub
parent 1ef50f58a1
commit 111132df23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 21 deletions

View File

@ -539,9 +539,10 @@ export default class Builder {
} catch (err) {
throw new Error(`Could not compile template ${src}: ${err.message}`)
}
const _path = r(this.options.buildDir, dst)
// Ensure parent dir exits and write file
await fsExtra.outputFile(_path, content, 'utf8')
const relativePath = r(this.options.buildDir, dst)
await fsExtra.outputFile(relativePath, content, 'utf8')
})
)
}
@ -662,12 +663,12 @@ export default class Builder {
this.createFileWatcher(
nuxtRestartWatch,
['all'],
(event, _path) => {
(event, fileName) => {
if (['add', 'change', 'unlink'].includes(event) === false) {
return
}
this.nuxt.callHook('watch:fileChanged', this, _path) // Legacy
this.nuxt.callHook('watch:restart', { event, path: _path })
this.nuxt.callHook('watch:fileChanged', this, fileName) // Legacy
this.nuxt.callHook('watch:restart', { event, path: fileName })
},
this.assignWatcher('restart')
)

View File

@ -242,17 +242,17 @@ export default class Generator {
}
}
let _path
let fileName
if (this.options.generate.subFolders) {
_path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html
_path = _path === '/404/index.html' ? '/404.html' : _path // /404 -> /404.html
fileName = path.join(route, path.sep, 'index.html') // /about -> /about/index.html
fileName = fileName === '/404/index.html' ? '/404.html' : fileName // /404 -> /404.html
} else {
_path = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html')
fileName = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html')
}
// Call hook to let user update the path & html
const page = { route, path: _path, html }
const page = { route, path: fileName, html }
await this.nuxt.callHook('generate:page', page)
page.path = path.join(this.distPath, page.path)

View File

@ -49,15 +49,15 @@ export const relativeTo = function relativeTo(...args) {
}
// Resolve path
const _path = r(...args)
const resolvedPath = r(...args)
// Check if path is an alias
if (startsWithSrcAlias(_path)) {
return _path
if (startsWithSrcAlias(resolvedPath)) {
return resolvedPath
}
// Make correct relative path
let rp = path.relative(dir, _path)
let rp = path.relative(dir, resolvedPath)
if (rp[0] !== '.') {
rp = '.' + path.sep + rp
}

View File

@ -4,22 +4,22 @@ import consola from 'consola'
import { r } from './resolve'
export const flatRoutes = function flatRoutes(router, _path = '', routes = []) {
export const flatRoutes = function flatRoutes(router, fileName = '', routes = []) {
router.forEach((r) => {
if ([':', '*'].some(c => r.path.includes(c))) {
return
}
if (r.children) {
if (_path === '' && r.path === '/') {
if (fileName === '' && r.path === '/') {
routes.push('/')
}
return flatRoutes(r.children, _path + r.path + '/', routes)
return flatRoutes(r.children, fileName + r.path + '/', routes)
}
_path = _path.replace(/^\/+$/, '/')
fileName = fileName.replace(/^\/+$/, '/')
routes.push(
(r.path === '' && _path[_path.length - 1] === '/'
? _path.slice(0, -1)
: _path) + r.path
(r.path === '' && fileName[fileName.length - 1] === '/'
? fileName.slice(0, -1)
: fileName) + r.path
)
})
return routes