Skip to content

Commit

Permalink
fix: add workaround to expand directory when globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jul 19, 2018
1 parent bdf33a2 commit 8cd24ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
27 changes: 24 additions & 3 deletions lib/glob-assets.js
@@ -1,16 +1,27 @@
const path = require('path');
const {basename} = require('path');
const {isPlainObject, castArray, uniqWith} = require('lodash');
const {isPlainObject, castArray, uniqWith, uniq} = require('lodash');
const dirGlob = require('dir-glob');
const globby = require('globby');
const debug = require('debug')('semantic-release:github');

const filesTransform = (files, cwd, transform) =>
files.map(file => `${file.startsWith('!') ? '!' : ''}${transform(cwd, file.startsWith('!') ? file.slice(1) : file)}`);

module.exports = async ({cwd}, assets) =>
uniqWith(
[]
.concat(
...(await Promise.all(
assets.map(async asset => {
// Wrap single glob definition in Array
const glob = castArray(isPlainObject(asset) ? asset.path : asset);
let glob = castArray(isPlainObject(asset) ? asset.path : asset);
// TODO Temporary workaround for https://github.com/kevva/dir-glob/issues/7 and https://github.com/mrmlnc/fast-glob/issues/47
glob = uniq([
...filesTransform(await dirGlob(filesTransform(glob, cwd, path.resolve)), cwd, path.relative),
...glob,
]);

// Skip solo negated pattern (avoid to include every non js file with `!**/*.js`)
if (glob.length <= 1 && glob[0].startsWith('!')) {
debug(
Expand All @@ -19,7 +30,15 @@ module.exports = async ({cwd}, assets) =>
);
return [];
}
const globbed = await globby(glob, {cwd, expandDirectories: true, gitignore: false, dot: true});

const globbed = await globby(glob, {
cwd,
expandDirectories: true,
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,
Expand All @@ -34,10 +53,12 @@ module.exports = async ({cwd}, assets) =>
// - 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;
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
"aggregate-error": "^1.0.0",
"bottleneck": "^2.0.1",
"debug": "^3.1.0",
"dir-glob": "^2.0.0",
"fs-extra": "^7.0.0",
"globby": "^8.0.0",
"http-proxy-agent": "^2.1.0",
Expand Down
4 changes: 2 additions & 2 deletions test/glob-assets.test.js
Expand Up @@ -169,8 +169,8 @@ test('Accept negated globs', async t => {

test('Expand directories', async t => {
const cwd = tempy.directory();
await copy(fixtures, path.resolve(cwd, '.'));
const globbedAssets = await globAssets({cwd}, [['.']]);
await copy(fixtures, path.resolve(cwd, 'dir'));
const globbedAssets = await globAssets({cwd}, [['dir']]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['dir', 'dir/upload_other.txt', 'dir/upload.txt', 'dir/.dotfile']));
});
Expand Down

0 comments on commit 8cd24ca

Please sign in to comment.