generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-changed-files.ts
67 lines (61 loc) · 1.46 KB
/
get-changed-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as core from '@actions/core'
import * as github from '@actions/github'
import {createInterface} from 'readline'
import {existsSync} from 'fs'
import picomatch from 'picomatch'
import {spawn} from 'child_process'
interface ChangedFiles {
files: string[]
}
export async function getChangedFiles(): Promise<ChangedFiles> {
const pattern = core.getInput('files', {
required: false
})
const globs = pattern.length ? pattern.split(',') : ['**.php']
const isMatch = picomatch(globs)
console.log('Filter patterns:', globs)
const payload = github.context
try {
console.log(payload.sha)
const git = spawn(
'git',
[
'--no-pager',
'diff-tree',
'--no-commit-id',
'--name-status',
'--diff-filter=d',
'-r',
payload.sha
],
{
windowsHide: true,
timeout: 5000
}
)
const readline = createInterface({
input: git.stdout
})
const result: ChangedFiles = {
files: []
}
for await (const line of readline) {
const parsed = /^(?<status>[ACMR])[\s\t]+(?<file>\S+)$/.exec(line)
if (parsed?.groups) {
const file = parsed.groups['file']
if (isMatch(file) && existsSync(file)) {
result.files.push(file)
} else {
console.log('Skip:', file)
}
}
}
return result
} catch (err) {
console.log('Error')
console.error(err)
return {
files: []
}
}
}