Skip to content

Commit

Permalink
refactor(fileList): refactor with async/await (#3247)
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz authored Jan 4, 2019
1 parent acdd2dc commit 376142e
Showing 1 changed file with 48 additions and 66 deletions.
114 changes: 48 additions & 66 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,16 @@ class FileList {

let lastCompletedRefresh = this._refreshing
lastCompletedRefresh = Promise
.map(this._patterns, ({ pattern, type, nocache }) => {
.map(this._patterns, async ({ pattern, type, nocache }) => {
if (helper.isUrlAbsolute(pattern)) {
this.buckets.set(pattern, [new Url(pattern, type)])
return
}

const mg = new Glob(pathLib.normalize(pattern), { cwd: '/', follow: true, nodir: true, sync: true })
if (_.isEmpty(mg.found)) {
this.buckets.set(pattern, [])
log.warn(`Pattern "${pattern}" does not match any file.`)
return
}

return Promise
.filter(mg.found, (path) => {
const files = mg.found
.filter((path) => {
if (this._findExcluded(path)) {
log.debug(`Excluded file "${path}"`)
return false
Expand All @@ -86,22 +81,21 @@ class FileList {
return true
}
})
.map((path) => {
const file = new File(path, mg.statCache[path].mtime, nocache, type)
if (nocache) {
log.debug(`Not preprocessing "${pattern}" due to nocache`)
return file
} else {
return this._preprocess(file).then(() => file)
}
})
.then((files) => {
this.buckets.set(pattern, files)
.map((path) => new File(path, mg.statCache[path].mtime, nocache, type))

if (_.isEmpty(files)) {
log.warn(`All files matched by "${pattern}" were excluded or matched by prior matchers.`)
}
})
if (nocache) {
log.debug(`Not preprocessing "${pattern}" due to nocache`)
} else {
await Promise.map(files, (file) => this._preprocess(file))
}

this.buckets.set(pattern, files)

if (_.isEmpty(mg.found)) {
log.warn(`Pattern "${pattern}" does not match any file.`)
} else if (_.isEmpty(files)) {
log.warn(`All files matched by "${pattern}" were excluded or matched by prior matchers.`)
}
})
.then(() => {
// When we return from this function the file processing chain will be
Expand Down Expand Up @@ -172,41 +166,37 @@ class FileList {
return this.refresh()
}

addFile (path) {
async addFile (path) {
const excluded = this._findExcluded(path)
if (excluded) {
log.debug(`Add file "${path}" ignored. Excluded by "${excluded}".`)
return Promise.resolve(this.files)
return this.files
}

const pattern = this._findIncluded(path)
if (!pattern) {
log.debug(`Add file "${path}" ignored. Does not match any pattern.`)
return Promise.resolve(this.files)
return this.files
}

if (this._exists(path)) {
log.debug(`Add file "${path}" ignored. Already in the list.`)
return Promise.resolve(this.files)
return this.files
}

const file = new File(path)
this._getFilesByPattern(pattern.pattern).push(file)

return Promise
.all([fs.statAsync(path), this._refreshing])
.then(([stat]) => {
file.mtime = stat.mtime
return this._preprocess(file)
})
.then(() => {
log.info(`Added file "${path}".`)
this._emitModified()
return this.files
})
const [stat] = await Promise.all([fs.statAsync(path), this._refreshing])
file.mtime = stat.mtime
await this._preprocess(file)

log.info(`Added file "${path}".`)
this._emitModified()
return this.files
}

changeFile (path, force) {
async changeFile (path, force) {
const pattern = this._findIncluded(path)
const file = this._findFile(path, pattern)

Expand All @@ -215,37 +205,29 @@ class FileList {
return Promise.resolve(this.files)
}

return Promise
.all([fs.statAsync(path), this._refreshing])
.then(([stat]) => {
if (!force && stat.mtime <= file.mtime) throw new Promise.CancellationError()

file.mtime = stat.mtime
return this._preprocess(file)
})
.then(() => {
log.info(`Changed file "${path}".`)
this._emitModified(force)
return this.files
})
.catch(Promise.CancellationError, () => this.files)
const [stat] = await Promise.all([fs.statAsync(path), this._refreshing])
if (force || stat.mtime > file.mtime) {
file.mtime = stat.mtime
await this._preprocess(file)
log.info(`Changed file "${path}".`)
this._emitModified(force)
}
return this.files
}

removeFile (path) {
return Promise.try(() => {
const pattern = this._findIncluded(path)
const file = this._findFile(path, pattern)
async removeFile (path) {
const pattern = this._findIncluded(path)
const file = this._findFile(path, pattern)

if (file) {
helper.arrayRemove(this._getFilesByPattern(pattern.pattern), file)
log.info(`Removed file "${path}".`)
if (file) {
helper.arrayRemove(this._getFilesByPattern(pattern.pattern), file)
log.info(`Removed file "${path}".`)

this._emitModified()
} else {
log.debug(`Removed file "${path}" ignored. Does not match any file in the list.`)
}
return this.files
})
this._emitModified()
} else {
log.debug(`Removed file "${path}" ignored. Does not match any file in the list.`)
}
return this.files
}
}

Expand Down

0 comments on commit 376142e

Please sign in to comment.