-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathglob-assets.js
68 lines (61 loc) · 3.11 KB
/
glob-assets.js
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
const path = require('path');
const {isPlainObject, castArray, uniqWith, uniq} = require('lodash');
const dirGlob = require('dir-glob');
const globby = require('globby');
const debug = require('debug')('semantic-release:gitlab');
module.exports = async ({pkgRoot, cwd}, assets) => {
return uniqWith(
[]
.concat(
...(await Promise.all(
assets.map(async asset => {
// Wrap single glob definition in Array
let glob = castArray(isPlainObject(asset) ? asset.path : asset);
// TODO Temporary workaround for https://github.com/mrmlnc/fast-glob/issues/47
glob = uniq([...(await dirGlob(glob, {cwd})), ...glob]);
// Skip solo negated pattern (avoid to include every non js file with `!**/*.js`)
if (glob.length <= 1 && glob[0].startsWith('!')) {
debug(
'skipping the negated glob %o as its alone in its group and would retrieve a large amount of files',
glob[0]
);
return [];
}
const globbed = await globby(glob, {
cwd,
expandDirectories: false, // TODO Temporary workaround for https://github.com/mrmlnc/fast-glob/issues/47
gitignore: false,
dot: true,
onlyFiles: false,
});
if (isPlainObject(asset)) {
if (globbed.length > 1) {
// If asset is an Object with a glob the `path` property that resolve to multiple files,
// Output an Object definition for each file matched and set each one with:
// - `path` of the matched file
// - `label` based on the actual file name (to avoid assets with duplicate `label`s)
// - `filepath` ignored (also to avoid duplicates)
// - other properties of the original asset definition
const {filepath, ...others} = asset;
return globbed.map(file => ({...others, path: file, label: path.relative(pkgRoot || '.', file)}));
}
// If asset is an Object, output an Object definition with:
// - `path` of the matched file if there is one, or the original `path` definition (will be considered as a missing file)
// - other properties of the original asset definition
return {...asset, path: globbed[0] || asset.path};
}
if (globbed.length > 0) {
// If asset is a String definition, output each files matched
return globbed;
}
// If asset is a String definition but no match is found, output the elements of the original glob (each one will be considered as a missing file)
return glob;
})
// Sort with Object first, to prioritize Object definition over Strings in dedup
))
)
.sort(asset => (isPlainObject(asset) ? -1 : 1)),
// Compare `path` property if Object definition, value itself if String
(a, b) => path.resolve(cwd, isPlainObject(a) ? a.path : a) === path.resolve(cwd, isPlainObject(b) ? b.path : b)
);
};