Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 4 additions & 37 deletions lib/helper/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class FileSystem extends Helper {
*/
async waitForFile(name, sec = 1) {
if (sec === 0) assert.fail('Use `seeFile` instead of waiting 0 seconds!')
const waitTimeout = sec * 1000
this.file = path.join(this.dir, name)
this.debugSection('File', this.file)
return isFileExists(this.file, waitTimeout).catch(() => {
throw new Error(`file (${name}) still not present in directory ${this.dir} after ${waitTimeout / 1000} sec`)
})
for (const t = Date.now() + sec * 1000; !fileExists(this.file); ) {
if (t < Date.now()) throw new Error(`file (${name}) still not present in directory ${this.dir} after ${sec} sec`)
await new Promise(resolve => setTimeout(resolve, 500))
}
}

/**
Expand Down Expand Up @@ -192,36 +192,3 @@ function getFileContents(file, encoding = 'utf8') {
if (encoding === '') assert.fail('Encoding is an empty string, please set a valid encoding')
return fs.readFileSync(file, encoding)
}

/**
* @param {string} file
* @param {number} timeout
* @private
* @returns {Promise<any>}
*/
function isFileExists(file, timeout) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
watcher.close()
reject(new Error('File did not exists and was not created during the timeout.'))
}, timeout)

const dir = path.dirname(file)
const basename = path.basename(file)
const watcher = fs.watch(dir, (eventType, filename) => {
if (eventType === 'rename' && filename === basename) {
clearTimeout(timer)
watcher.close()
resolve()
}
})

fs.access(file, fs.constants.R_OK, err => {
if (!err) {
clearTimeout(timer)
watcher.close()
resolve()
}
})
})
}