Skip to content

Commit

Permalink
fix(cli): commas in import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
etnoy committed Oct 8, 2024
1 parent 063969c commit b8976f1
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 39 deletions.
17 changes: 16 additions & 1 deletion cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"node": ">=20.0.0"
},
"dependencies": {
"fast-glob": "^3.3.2",
"fastq": "^1.17.1",
"lodash-es": "^4.17.21"
},
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const scan = async (pathsToCrawl: string[], options: UploadOptionsDto) => {
const files = await crawl({
pathsToCrawl,
recursive: options.recursive,
exclusionPattern: options.ignore,
exclusionPatterns: options.ignore,
includeHidden: options.includeHidden,
extensions: [...image, ...video],
});
Expand Down
26 changes: 8 additions & 18 deletions cli/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const tests: Test[] = [
test: 'should exclude by file extension',
options: {
pathsToCrawl: ['/photos/'],
exclusionPattern: '**/*.tif',
exclusionPatterns: '**/*.tif',
},
files: {
'/photos/image.jpg': true,
Expand All @@ -82,7 +82,7 @@ const tests: Test[] = [
test: 'should exclude by file extension without case sensitivity',
options: {
pathsToCrawl: ['/photos/'],
exclusionPattern: '**/*.TIF',
exclusionPatterns: '**/*.TIF',
},
files: {
'/photos/image.jpg': true,
Expand All @@ -93,7 +93,7 @@ const tests: Test[] = [
test: 'should exclude by folder',
options: {
pathsToCrawl: ['/photos/'],
exclusionPattern: '**/raw/**',
exclusionPatterns: '**/raw/**',
recursive: true,
},
files: {
Expand All @@ -115,17 +115,7 @@ const tests: Test[] = [
'/albums/image3.jpg': true,
},
},
{
test: 'should support globbing paths',
options: {
pathsToCrawl: ['/photos*'],
},
files: {
'/photos1/image1.jpg': true,
'/photos2/image2.jpg': true,
'/images/image3.jpg': false,
},
},

{
test: 'should crawl a single path without trailing slash',
options: {
Expand Down Expand Up @@ -223,7 +213,7 @@ const tests: Test[] = [
test: 'should support ignoring full filename',
options: {
pathsToCrawl: ['/photos'],
exclusionPattern: '**/image2.jpg',
exclusionPatterns: '**/image2.jpg',
},
files: {
'/photos/image1.jpg': true,
Expand All @@ -235,7 +225,7 @@ const tests: Test[] = [
test: 'should support ignoring file extensions',
options: {
pathsToCrawl: ['/photos'],
exclusionPattern: '**/*.png',
exclusionPatterns: '**/*.png',
},
files: {
'/photos/image1.jpg': true,
Expand All @@ -248,7 +238,7 @@ const tests: Test[] = [
options: {
pathsToCrawl: ['/photos'],
recursive: true,
exclusionPattern: '**/raw/**',
exclusionPatterns: '**/raw/**',
},
files: {
'/photos/image1.jpg': true,
Expand All @@ -264,7 +254,7 @@ const tests: Test[] = [
// Currently, fast-glob has some caveat when dealing with `/`.
pathsToCrawl: ['/*s'],
recursive: true,
exclusionPattern: '/images/**',
exclusionPatterns: '/images/**',
},
files: {
'/photos/image1.jpg': true,
Expand Down
28 changes: 12 additions & 16 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface CrawlOptions {
pathsToCrawl: string[];
recursive?: boolean;
includeHidden?: boolean;
exclusionPattern?: string;
exclusionPatterns?: string;
extensions: string[];
}

Expand All @@ -113,7 +113,7 @@ const convertPathToPatternOnWin = (path: string) => {
};

export const crawl = async (options: CrawlOptions): Promise<string[]> => {
const { extensions: extensionsWithPeriod, recursive, pathsToCrawl, exclusionPattern, includeHidden } = options;
const { extensions: extensionsWithPeriod, recursive, pathsToCrawl, exclusionPatterns, includeHidden } = options;
const extensions = extensionsWithPeriod.map((extension) => extension.replace('.', ''));

if (pathsToCrawl.length === 0) {
Expand Down Expand Up @@ -141,27 +141,23 @@ export const crawl = async (options: CrawlOptions): Promise<string[]> => {
}
}

let searchPattern: string;
if (patterns.length === 1) {
searchPattern = patterns[0];
} else if (patterns.length === 0) {
if (patterns.length === 0) {
return crawledFiles;
} else {
searchPattern = '{' + patterns.join(',') + '}';
}

if (recursive) {
searchPattern = searchPattern + '/**/';
}

searchPattern = `${searchPattern}/*.{${extensions.join(',')}}`;
const searchPatterns = patterns.map((pattern) => {
let escapedPattern = pattern;
if (recursive) {
escapedPattern = escapedPattern + '/**';
}
return `${escapedPattern}/*.{${extensions.join(',')}}`;
});

const globbedFiles = await glob(searchPattern, {
const globbedFiles = await glob(searchPatterns, {
absolute: true,
caseSensitiveMatch: false,
onlyFiles: true,
dot: includeHidden,
ignore: [`**/${exclusionPattern}`],
ignore: [`**/${exclusionPatterns}`],
});
globbedFiles.push(...crawledFiles);
return globbedFiles.sort();
Expand Down
Loading

0 comments on commit b8976f1

Please sign in to comment.