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 {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (options) {
|
2019-01-29 09:31:14 +00:00
|
|
|
this.rootDir = options.rootDir
|
|
|
|
this.addIgnoresRules()
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
static get IGNORE_FILENAME () {
|
2019-01-29 09:31:14 +00:00
|
|
|
return '.nuxtignore'
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.ignoreFile
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
readIgnoreFile () {
|
2019-01-29 09:31:14 +00:00
|
|
|
if (this.findIgnoreFile()) {
|
|
|
|
return fs.readFileSync(this.ignoreFile, 'utf8')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
addIgnoresRules () {
|
2019-01-29 09:31:14 +00:00
|
|
|
const content = this.readIgnoreFile()
|
|
|
|
if (content) {
|
|
|
|
this.ignore.add(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
filter (paths) {
|
2019-01-29 09:31:14 +00:00
|
|
|
if (this.ignore) {
|
|
|
|
return this.ignore.filter([].concat(paths || []))
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
reload () {
|
2019-01-29 09:31:14 +00:00
|
|
|
delete this.ignore
|
|
|
|
delete this.ignoreFile
|
|
|
|
this.addIgnoresRules()
|
|
|
|
}
|
|
|
|
}
|