mirror of
https://github.com/dorny/paths-filter.git
synced 2025-06-08 00:59:04 +00:00
Extend filter syntax with optional specification of file status: add, modified, deleted (#22)
* Add support for specification of change type (add,modified,delete) * Use NULL as separator in git-diff command output * Improve PR test workflow * Fix the workflow file
This commit is contained in:
parent
caef9bef1f
commit
1ff702da35
11 changed files with 397 additions and 90 deletions
110
src/filter.ts
110
src/filter.ts
|
@ -1,49 +1,101 @@
|
|||
import * as jsyaml from 'js-yaml'
|
||||
import * as minimatch from 'minimatch'
|
||||
import {File, ChangeStatus} from './file'
|
||||
|
||||
// Type definition of object we expect to load from YAML
|
||||
interface FilterYaml {
|
||||
[name: string]: FilterItemYaml
|
||||
}
|
||||
type FilterItemYaml =
|
||||
| string // Filename pattern, e.g. "path/to/*.js"
|
||||
| {[changeTypes: string]: string} // Change status and filename, e.g. added|modified: "path/to/*.js"
|
||||
| FilterItemYaml[] // Supports referencing another rule via YAML anchor
|
||||
|
||||
// Minimatch options used in all matchers
|
||||
const MinimatchOptions: minimatch.IOptions = {
|
||||
dot: true
|
||||
}
|
||||
|
||||
// Internal representation of one item in named filter rule
|
||||
// Created as simplified form of data in FilterItemYaml
|
||||
interface FilterRuleItem {
|
||||
status?: ChangeStatus[] // Required change status of the matched files
|
||||
matcher: minimatch.IMinimatch // Matches the filename
|
||||
}
|
||||
|
||||
export default class Filter {
|
||||
rules: {[key: string]: minimatch.IMinimatch[]} = {}
|
||||
rules: {[key: string]: FilterRuleItem[]} = {}
|
||||
|
||||
constructor(yaml: string) {
|
||||
const doc = jsyaml.safeLoad(yaml)
|
||||
if (typeof doc !== 'object') {
|
||||
this.throwInvalidFormatError()
|
||||
}
|
||||
|
||||
const opts: minimatch.IOptions = {
|
||||
dot: true
|
||||
}
|
||||
|
||||
for (const name of Object.keys(doc)) {
|
||||
const patternsNode = doc[name]
|
||||
if (!Array.isArray(patternsNode)) {
|
||||
this.throwInvalidFormatError()
|
||||
}
|
||||
const patterns = flat(patternsNode) as string[]
|
||||
if (!patterns.every(x => typeof x === 'string')) {
|
||||
this.throwInvalidFormatError()
|
||||
}
|
||||
this.rules[name] = patterns.map(x => new minimatch.Minimatch(x, opts))
|
||||
// Creates instance of Filter and load rules from YAML if it's provided
|
||||
constructor(yaml?: string) {
|
||||
if (yaml) {
|
||||
this.load(yaml)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns dictionary with match result per rules group
|
||||
match(paths: string[]): {[key: string]: boolean} {
|
||||
// Load rules from YAML string
|
||||
load(yaml: string): void {
|
||||
const doc = jsyaml.safeLoad(yaml) as FilterYaml
|
||||
if (typeof doc !== 'object') {
|
||||
this.throwInvalidFormatError('Root element is not an object')
|
||||
}
|
||||
|
||||
for (const [key, item] of Object.entries(doc)) {
|
||||
this.rules[key] = this.parseFilterItemYaml(item)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns dictionary with match result per rule
|
||||
match(files: File[]): {[key: string]: boolean} {
|
||||
const result: {[key: string]: boolean} = {}
|
||||
for (const [key, patterns] of Object.entries(this.rules)) {
|
||||
const match = paths.some(fileName => patterns.some(rule => rule.match(fileName)))
|
||||
const match = files.some(file =>
|
||||
patterns.some(
|
||||
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.matcher.match(file.filename)
|
||||
)
|
||||
)
|
||||
result[key] = match
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private throwInvalidFormatError(): never {
|
||||
throw new Error('Invalid filter YAML format: Expected dictionary of string arrays')
|
||||
private parseFilterItemYaml(item: FilterItemYaml): FilterRuleItem[] {
|
||||
if (Array.isArray(item)) {
|
||||
return flat(item.map(i => this.parseFilterItemYaml(i)))
|
||||
}
|
||||
|
||||
if (typeof item === 'string') {
|
||||
return [{status: undefined, matcher: new minimatch.Minimatch(item, MinimatchOptions)}]
|
||||
}
|
||||
|
||||
if (typeof item === 'object') {
|
||||
return Object.entries(item).map(([key, pattern]) => {
|
||||
if (typeof key !== 'string' || typeof pattern !== 'string') {
|
||||
this.throwInvalidFormatError(
|
||||
`Expected [key:string]= pattern:string, but [${key}:${typeof key}]= ${pattern}:${typeof pattern} found`
|
||||
)
|
||||
}
|
||||
return {
|
||||
status: key
|
||||
.split('|')
|
||||
.map(x => x.trim())
|
||||
.filter(x => x.length > 0)
|
||||
.map(x => x.toLowerCase()) as ChangeStatus[],
|
||||
matcher: new minimatch.Minimatch(pattern, MinimatchOptions)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
this.throwInvalidFormatError(`Unexpected element type '${typeof item}'`)
|
||||
}
|
||||
|
||||
private throwInvalidFormatError(message: string): never {
|
||||
throw new Error(`Invalid filter YAML format: ${message}.`)
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new array with all sub-array elements recursively concatenated
|
||||
// Creates a new array with all sub-array elements concatenated
|
||||
// In future could be replaced by Array.prototype.flat (supported on Node.js 11+)
|
||||
function flat(arr: any[]): any[] {
|
||||
return arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flat(val) : val), [])
|
||||
function flat<T>(arr: T[][]): T[] {
|
||||
return arr.reduce((acc, val) => acc.concat(val), [])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue