Skip to content

Commit 62e3e42

Browse files
committed
Fix multiple file/glob parameters
Refs lyft#31 Refs lyft#37
1 parent 85a4bf0 commit 62e3e42

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/cli.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ import * as prettier from 'prettier';
99
import { run } from '.';
1010
import { CompilationOptions } from './compiler';
1111

12+
function resolveGlobs(globPatterns: string[]): string[] {
13+
const files: string[] = [];
14+
function addFile(file: string) {
15+
file = path.resolve(file);
16+
if (files.indexOf(file) === -1) {
17+
files.push(file);
18+
}
19+
}
20+
globPatterns.forEach(pattern => {
21+
if (/[{}*?+\[\]]/.test(pattern)) {
22+
// Smells like globs
23+
glob.sync(pattern, {}).forEach(file => {
24+
addFile(file);
25+
});
26+
} else {
27+
addFile(pattern);
28+
}
29+
});
30+
return files;
31+
}
32+
1233
program
1334
.version('1.0.0')
1435
.option('--arrow-parens <avoid|always>', 'Include parentheses around a sole arrow function parameter.', 'avoid')
@@ -25,11 +46,8 @@ program
2546
.option('--keep-original-files', 'Keep original files', false)
2647
.option('--keep-temporary-files', 'Keep temporary files', false)
2748
.usage('[options] <filename or glob>')
28-
.command('* <glob>')
29-
.action(globPattern => {
30-
if (!globPattern) {
31-
throw new Error('You must provide a file name or glob pattern to transform');
32-
}
49+
.command('* [glob/filename...]')
50+
.action((globPatterns: string[]) => {
3351
const prettierOptions: prettier.Options = {
3452
arrowParens: program.arrowParens,
3553
bracketSpacing: !program.noBracketSpacing,
@@ -45,10 +63,12 @@ program
4563
const compilationOptions: CompilationOptions = {
4664
ignorePrettierErrors: !!program.ignorePrettierErrors,
4765
};
48-
const files = glob.sync(globPattern, {});
66+
const files = resolveGlobs(globPatterns);
67+
if (!files.length) {
68+
throw new Error('Nothing to do. You must provide file names or glob patterns to transform.');
69+
}
4970
let errors = false;
50-
for (const file of files) {
51-
const filePath = path.resolve(file);
71+
for (const filePath of files) {
5272
const newPath = filePath.replace(/\.jsx?$/, '.tsx');
5373
const temporaryPath = filePath.replace(/\.jsx?$/, `_js2ts_${+new Date()}.tsx`);
5474
try {
@@ -59,7 +79,7 @@ program
5979
fs.unlinkSync(filePath);
6080
}
6181
} catch (error) {
62-
console.warn(`Failed to convert ${file}`);
82+
console.warn(`Failed to convert ${filePath}`);
6383
console.warn(error);
6484
errors = true;
6585
}

0 commit comments

Comments
 (0)