Skip to content

Commit

Permalink
Fix recursive bugs (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Idered and sindresorhus authored Feb 27, 2022
1 parent c08f103 commit 0934ab1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions glob-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export default class GlobPattern {
this.originalPath = pattern;
this.destination = destination;
this.options = options;
this.isDirectory = false;

if (
!isDynamicPattern(pattern)
&& fs.existsSync(pattern)
&& fs.lstatSync(pattern).isDirectory()
) {
this.path = [pattern, '**'].join('/');
this.isDirectory = true;
}
}

Expand Down
40 changes: 36 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import arrify from 'arrify';
import cpFile from 'cp-file';
import pFilter from 'p-filter';
import {isDynamicPattern} from 'globby';
import micromatch from 'micromatch';
import CpyError from './cpy-error.js';
import GlobPattern from './glob-pattern.js';

Expand Down Expand Up @@ -55,6 +56,24 @@ class Entry {
}
}

/**
Expand patterns like `'node_modules/{globby,micromatch}'` into `['node_modules/globby', 'node_modules/micromatch']`.
@param {string[]} patterns
@returns {string[]}
*/
const expandPatternsWithBraceExpansion = patterns => {
let collection = [];
for (const pattern of patterns) {
collection = [...collection, ...micromatch.braces(pattern, {
expand: true,
nodupes: true,
})];
}

return collection;
};

/**
@param {object} props
@param {Entry} props.entry
Expand Down Expand Up @@ -82,6 +101,15 @@ const preprocessDestinationPath = ({entry, destination, options}) => {
return path.join(destination, entry.name);
}

// TODO: This check will not work correctly if `options.cwd` and `entry.path` are on different partitions on Windows, see: https://github.com/sindresorhus/import-local/pull/12
if (entry.pattern.isDirectory && path.relative(options.cwd, entry.path).startsWith('..')) {
return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath), path.relative(entry.pattern.originalPath, entry.path));
}

if (!entry.pattern.isDirectory && entry.path === entry.relativePath) {
return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath), path.relative(entry.pattern.originalPath, entry.path));
}

return path.join(options.cwd, destination, path.relative(options.cwd, entry.path));
};

Expand Down Expand Up @@ -136,16 +164,20 @@ export default function cpy(
let entries = [];
let completedFiles = 0;
let completedSize = 0;

/**
@type {GlobPattern[]}
*/
let patterns = arrify(source).map(string => string.replace(/\\/g, '/'));
let patterns = expandPatternsWithBraceExpansion(arrify(source))
.map(string => string.replace(/\\/g, '/'));
const sources = patterns.filter(item => !item.startsWith('!'));
const ignore = patterns.filter(item => item.startsWith('!'));

if (patterns.length === 0 || !destination) {
if (sources.length === 0 || !destination) {
throw new CpyError('`source` and `destination` required');
}

patterns = patterns.map(pattern => new GlobPattern(pattern, destination, options));
patterns = patterns.map(pattern => new GlobPattern(pattern, destination, {...options, ignore}));

for (const pattern of patterns) {
/**
Expand All @@ -162,7 +194,7 @@ export default function cpy(
);
}

if (matches.length === 0 && !isDynamicPattern(pattern.originalPath)) {
if (matches.length === 0 && !isDynamicPattern(pattern.originalPath) && !isDynamicPattern(ignore)) {
throw new CpyError(
`Cannot copy \`${pattern.originalPath}\`: the file doesn't exist`,
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"cp-file": "^9.1.0",
"globby": "^12.0.2",
"junk": "^4.0.0",
"micromatch": "^4.0.4",
"nested-error-stacks": "^2.1.0",
"p-filter": "^3.0.0",
"p-map": "^5.3.0"
Expand Down

0 comments on commit 0934ab1

Please sign in to comment.