Nuxt/packages/builder/src/ignore.js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-01-29 09:31:14 +00:00
import path from 'path'
import fs from 'fs-extra'
import ignore from 'ignore'
export default class Ignore {
constructor (options) {
2019-01-29 09:31:14 +00:00
this.rootDir = options.rootDir
this.ignoreOptions = options.ignoreOptions
this.ignoreArray = options.ignoreArray
2019-01-29 09:31:14 +00:00
this.addIgnoresRules()
}
static get IGNORE_FILENAME () {
2019-01-29 09:31:14 +00:00
return '.nuxtignore'
}
findIgnoreFile () {
2019-01-29 09:31:14 +00:00
if (!this.ignoreFile) {
const ignoreFile = path.resolve(this.rootDir, Ignore.IGNORE_FILENAME)
if (fs.existsSync(ignoreFile) && fs.statSync(ignoreFile).isFile()) {
this.ignoreFile = ignoreFile
this.ignore = ignore(this.ignoreOptions)
2019-01-29 09:31:14 +00:00
}
}
return this.ignoreFile
}
readIgnoreFile () {
2019-01-29 09:31:14 +00:00
if (this.findIgnoreFile()) {
return fs.readFileSync(this.ignoreFile, 'utf8')
}
}
addIgnoresRules () {
2019-01-29 09:31:14 +00:00
const content = this.readIgnoreFile()
if (content) {
this.ignore.add(content)
}
if (this.ignoreArray && this.ignoreArray.length > 0) {
if (!this.ignore) {
this.ignore = ignore(this.ignoreOptions)
}
this.ignore.add(this.ignoreArray)
}
2019-01-29 09:31:14 +00:00
}
filter (paths) {
2019-01-29 09:31:14 +00:00
if (this.ignore) {
return this.ignore.filter([].concat(paths || []))
}
return paths
}
reload () {
2019-01-29 09:31:14 +00:00
delete this.ignore
delete this.ignoreFile
this.addIgnoresRules()
}
}