UNPKG

3.05 kBJavaScriptView Raw
1const path = require('path');
2const {basename} = require('path');
3const {isPlainObject, castArray, uniqWith, uniq} = require('lodash');
4const dirGlob = require('dir-glob');
5const globby = require('globby');
6const debug = require('debug')('semantic-release:github');
7
8module.exports = async ({cwd}, assets) =>
9 uniqWith(
10 []
11 .concat(
12 ...(await Promise.all(
13 assets.map(async asset => {
14 // Wrap single glob definition in Array
15 let glob = castArray(isPlainObject(asset) ? asset.path : asset);
16 // TODO Temporary workaround for https://github.com/mrmlnc/fast-glob/issues/47
17 glob = uniq([...(await dirGlob(glob, {cwd})), ...glob]);
18
19 // Skip solo negated pattern (avoid to include every non js file with `!**/*.js`)
20 if (glob.length <= 1 && glob[0].startsWith('!')) {
21 debug(
22 'skipping the negated glob %o as its alone in its group and would retrieve a large amount of files',
23 glob[0]
24 );
25 return [];
26 }
27
28 const globbed = await globby(glob, {
29 cwd,
30 expandDirectories: false, // TODO Temporary workaround for https://github.com/mrmlnc/fast-glob/issues/47
31 gitignore: false,
32 dot: true,
33 onlyFiles: false,
34 });
35
36 if (isPlainObject(asset)) {
37 if (globbed.length > 1) {
38 // If asset is an Object with a glob the `path` property that resolve to multiple files,
39 // Output an Object definition for each file matched and set each one with:
40 // - `path` of the matched file
41 // - `name` based on the actual file name (to avoid assets with duplicate `name`)
42 // - other properties of the original asset definition
43 return globbed.map(file => ({...asset, path: file, name: basename(file)}));
44 }
45
46 // If asset is an Object, output an Object definition with:
47 // - `path` of the matched file if there is one, or the original `path` definition (will be considered as a missing file)
48 // - other properties of the original asset definition
49 return {...asset, path: globbed[0] || asset.path};
50 }
51
52 if (globbed.length > 0) {
53 // If asset is a String definition, output each files matched
54 return globbed;
55 }
56
57 // 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)
58 return glob;
59 })
60 // Sort with Object first, to prioritize Object definition over Strings in dedup
61 ))
62 )
63 .sort(asset => (isPlainObject(asset) ? -1 : 1)),
64 // Compare `path` property if Object definition, value itself if String
65 (a, b) => path.resolve(cwd, isPlainObject(a) ? a.path : a) === path.resolve(cwd, isPlainObject(b) ? b.path : b)
66 );