2020-05-20 22:31:16 +00:00
|
|
|
import * as jsyaml from 'js-yaml'
|
2020-10-23 10:33:44 +00:00
|
|
|
import * as micromatch from 'micromatch'
|
2020-07-11 15:17:56 +00:00
|
|
|
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
|
2020-10-23 10:33:44 +00:00
|
|
|
const MatchOptions: micromatch.Options = {
|
2020-07-11 15:17:56 +00:00
|
|
|
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
|
2020-10-23 10:33:44 +00:00
|
|
|
isMatch: (str: string) => boolean // Matches the filename
|
2020-07-11 15:17:56 +00:00
|
|
|
}
|
2020-05-20 22:31:16 +00:00
|
|
|
|
2020-08-30 19:18:14 +00:00
|
|
|
export interface FilterResults {
|
|
|
|
[key: string]: File[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Filter {
|
2020-07-11 15:17:56 +00:00
|
|
|
rules: {[key: string]: FilterRuleItem[]} = {}
|
2020-05-20 22:31:16 +00:00
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
// Creates instance of Filter and load rules from YAML if it's provided
|
|
|
|
constructor(yaml?: string) {
|
|
|
|
if (yaml) {
|
|
|
|
this.load(yaml)
|
2020-05-20 22:31:16 +00:00
|
|
|
}
|
2020-07-11 15:17:56 +00:00
|
|
|
}
|
2020-05-20 22:31:16 +00:00
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
// Load rules from YAML string
|
|
|
|
load(yaml: string): void {
|
2020-07-11 21:33:11 +00:00
|
|
|
if (!yaml) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
const doc = jsyaml.safeLoad(yaml) as FilterYaml
|
|
|
|
if (typeof doc !== 'object') {
|
|
|
|
this.throwInvalidFormatError('Root element is not an object')
|
2020-05-21 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
for (const [key, item] of Object.entries(doc)) {
|
|
|
|
this.rules[key] = this.parseFilterItemYaml(item)
|
2020-05-20 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 19:18:14 +00:00
|
|
|
match(files: File[]): FilterResults {
|
|
|
|
const result: FilterResults = {}
|
2020-05-20 22:31:16 +00:00
|
|
|
for (const [key, patterns] of Object.entries(this.rules)) {
|
2020-08-30 19:18:14 +00:00
|
|
|
result[key] = files.filter(file => this.isMatch(file, patterns))
|
2020-05-20 22:31:16 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-30 19:18:14 +00:00
|
|
|
private isMatch(file: File, patterns: FilterRuleItem[]): boolean {
|
|
|
|
return patterns.some(
|
2020-10-23 10:33:44 +00:00
|
|
|
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.isMatch(file.filename)
|
2020-08-30 19:18:14 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
private parseFilterItemYaml(item: FilterItemYaml): FilterRuleItem[] {
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
return flat(item.map(i => this.parseFilterItemYaml(i)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === 'string') {
|
2020-10-23 10:33:44 +00:00
|
|
|
return [{status: undefined, isMatch: micromatch.matcher(item, MatchOptions)}]
|
2020-07-11 15:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === 'object') {
|
|
|
|
return Object.entries(item).map(([key, pattern]) => {
|
2020-11-07 01:39:25 +00:00
|
|
|
if (typeof key !== 'string' || (typeof pattern !== 'string' && !Array.isArray(pattern))) {
|
2020-07-11 15:17:56 +00:00
|
|
|
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[],
|
2020-10-23 10:33:44 +00:00
|
|
|
isMatch: micromatch.matcher(pattern, MatchOptions)
|
2020-07-11 15:17:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.throwInvalidFormatError(`Unexpected element type '${typeof item}'`)
|
|
|
|
}
|
|
|
|
|
|
|
|
private throwInvalidFormatError(message: string): never {
|
|
|
|
throw new Error(`Invalid filter YAML format: ${message}.`)
|
2020-05-20 22:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 21:39:06 +00:00
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
// Creates a new array with all sub-array elements concatenated
|
2020-06-19 21:39:06 +00:00
|
|
|
// In future could be replaced by Array.prototype.flat (supported on Node.js 11+)
|
2020-07-11 15:17:56 +00:00
|
|
|
function flat<T>(arr: T[][]): T[] {
|
|
|
|
return arr.reduce((acc, val) => acc.concat(val), [])
|
2020-06-19 21:39:06 +00:00
|
|
|
}
|