diff --git a/lib/file_system.js b/lib/file_system.js index 2824be82..f09a3a07 100644 --- a/lib/file_system.js +++ b/lib/file_system.js @@ -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)) } /** @@ -112,7 +112,8 @@ class FileSystem { * @returns {Promise>} 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) { @@ -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 + } } /** @@ -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 } /** @@ -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 diff --git a/package-lock.json b/package-lock.json index 7cf4384e..ad91b36e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "repolinter", - "version": "0.8.2", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/rules/file-contents.js b/rules/file-contents.js index 25474e38..ea60316f 100644 --- a/rules/file-contents.js +++ b/rules/file-contents.js @@ -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)