UNPKG

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