Skip to content

Commit

Permalink
fix: windows fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Aug 26, 2020
1 parent f965c11 commit d52353e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
37 changes: 24 additions & 13 deletions lib/file_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ class FileSystem {
const onlySymlinks = {}
for (const fullPath in symlinks) {
if (symlinks[fullPath]) {
const relativeToRepoPath = path.relative(this.targetDir, fullPath).split(path.sep).join('/')
const relativeToRepoPath = this.normalizePath(path.relative(this.targetDir, fullPath))
onlySymlinks[relativeToRepoPath] = true
}
}

// Remove all symlinks
return filePaths.filter(filePath => !onlySymlinks[filePath])
return filePaths.filter(filePath => !onlySymlinks[this.normalizePath(filePath)])
}

glob (globs, options) {
return glob(globs, options)
.then(res => res.filter(relativePath => this.shouldInclude(relativePath)))
async glob (globs, options) {
const fixedGlobs = typeof globs === 'string' ? this.normalizePath(globs) : globs.map(g => this.normalizePath(g))
return (await glob(fixedGlobs, options)).map(p => this.normalizePath(p)).filter(p => this.shouldInclude(p))
}

/**
Expand All @@ -112,7 +112,8 @@ class FileSystem {
* @returns {Promise<Array<string>>} A list of paths relative to this.targetDir
*/
async findAll (globs, nocase = false) {
return this.glob(globs, { cwd: this.targetDir, nocase: !!nocase })
const fixedGlobs = typeof globs === 'string' ? this.normalizePath(globs) : globs.map(g => this.normalizePath(g))
return this.glob(fixedGlobs, { cwd: this.targetDir, nocase: !!nocase })
}

async isBinaryFile (relativeFile) {
Expand All @@ -130,8 +131,18 @@ class FileSystem {

shouldInclude (filePath) {
if (this.filterPaths.length === 0) { return true }
const resolvedPath = path.relative(this.targetDir, path.resolve(this.targetDir, filePath))
return this.filterPaths.some(p => resolvedPath.startsWith(p))
const resolvedPath = this.normalizePath(path.relative(this.targetDir, path.resolve(this.targetDir, filePath)))
return this.filterPaths
.map(p => this.normalizePath(p))
.some(p => resolvedPath.startsWith(p))
}

normalizePath (filepath) {
if (process.platform === 'win32') {
return filepath.split(path.sep).join('/')
} else {
return filepath
}
}

/**
Expand All @@ -142,11 +153,11 @@ class FileSystem {
*/
async getFileContents (relativeFile) {
const file = path.resolve(this.targetDir, relativeFile)
const exists = await FileSystem.fileExists(file)
if (exists && (await fs.promises.stat(file)).isFile()) {
return fs.promises.readFile(file, 'utf8')
try {
return await fs.promises.readFile(file, 'utf8')
} catch (e) {
return undefined
}
return undefined
}

/**
Expand Down Expand Up @@ -177,7 +188,7 @@ class FileSystem {
try {
fd = await fs.promises.open(path.resolve(this.targetDir, file), 'r')
} catch (e) {
fd.close()
if (fd) fd.close()
// File doesn't exist or is a directory
if (e.message.includes('ENOENT')) {
return undefined
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions rules/file-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ async function fileContents (fs, options, not = false) {
}

const results = await Promise.all(files.map(async file => {
let fileContents = await fs.getFileContents(file)
const fileContents = await fs.getFileContents(file)
if (fileContents === undefined) {
fileContents = ''
return new Result(
'Did not find file matching the specified patterns',
fileList.map(f => { return { passed: false, pattern: f } }),
!options['fail-on-non-existent'])
}
const regexp = new RegExp(options.content, options.flags)

Expand Down

0 comments on commit d52353e

Please sign in to comment.