-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
executable file
·106 lines (97 loc) · 2.66 KB
/
main.mjs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
import { dirname } from 'path';
import * as core from '@actions/core';
import { globby } from 'globby';
import micromatch from 'micromatch';
function isEmpty(xs) {
return xs.length === 0;
}
function getInputs() {
const patterns = core.getMultilineInput('patterns');
const rootPatterns = core.getMultilineInput('root-patterns');
if (isEmpty(patterns) && isEmpty(rootPatterns)) {
throw new Error('Either "patterns" or "root-patterns" must be provided');
}
if (!isEmpty(patterns) && !isEmpty(rootPatterns)) {
throw new Error('Only one of "patterns" or "root-patterns" can be provided');
}
const filterPatterns = core.getMultilineInput('filter-patterns', {
required: true,
});
let source = core.getInput('source') || null;
try {
source = JSON.parse(source);
} catch (err) {
throw new Error(
'If provided, "source" must be a JSON-formatted array of filepaths',
{ cause: err },
);
}
return {
patterns,
rootPatterns,
filterPatterns,
source,
};
}
async function fsGlob(patterns) {
return globby(patterns, {
expandDirectories: false,
gitignore: true,
markDirectories: true,
onlyFiles: false,
});
}
function memGlob(patterns, source) {
return micromatch(source, patterns);
}
function glob(patterns, source = null) {
return source ? memGlob(patterns, source) : fsGlob(patterns);
}
async function directMatch(patterns, source) {
const matches = await glob(patterns, source);
core.debug(JSON.stringify({ patterns, matches, source }, null, 2));
core.setOutput('matches', matches);
}
function hoist(rootPatterns, paths) {
const roots = [];
let remaining = [...paths];
do {
roots.push(...micromatch(remaining, rootPatterns));
remaining = micromatch
.not(remaining, rootPatterns)
.map((x) => `${dirname(x)}/`);
} while (remaining.filter((x) => x !== './').length > 0);
roots.push(...micromatch(remaining, rootPatterns));
return Array.from(new Set(roots));
}
async function hoistMatch(rootPatterns, filterPatterns, source) {
const fromFilters = await glob(filterPatterns, source);
const matches = hoist(rootPatterns, fromFilters);
core.debug(JSON.stringify({
rootPatterns,
filterPatterns,
fromFilters,
matches,
source,
}, null, 2));
core.setOutput('matches', matches);
}
(async () => {
const {
filterPatterns,
patterns,
rootPatterns,
source,
} = getInputs();
switch (true) {
case !isEmpty(patterns):
directMatch(patterns, source);
break;
case !isEmpty(rootPatterns):
hoistMatch(rootPatterns, filterPatterns, source);
break;
default:
throw new Error('Unreachable');
}
})();