Skip to content
This repository was archived by the owner on Sep 21, 2019. It is now read-only.

Safety/QOL fixes #39

Merged
merged 7 commits into from
May 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix multiple file/glob parameters
Refs #31

Refs #37
  • Loading branch information
akx committed May 19, 2018
commit 62e3e42275dc4522ca505c73eaf6facaf06e18ac
38 changes: 29 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ import * as prettier from 'prettier';
import { run } from '.';
import { CompilationOptions } from './compiler';

function resolveGlobs(globPatterns: string[]): string[] {
const files: string[] = [];
function addFile(file: string) {
file = path.resolve(file);
if (files.indexOf(file) === -1) {
files.push(file);
}
}
globPatterns.forEach(pattern => {
if (/[{}*?+\[\]]/.test(pattern)) {
// Smells like globs
glob.sync(pattern, {}).forEach(file => {
addFile(file);
});
} else {
addFile(pattern);
}
});
return files;
}

program
.version('1.0.0')
.option('--arrow-parens <avoid|always>', 'Include parentheses around a sole arrow function parameter.', 'avoid')
Expand All @@ -25,11 +46,8 @@ program
.option('--keep-original-files', 'Keep original files', false)
.option('--keep-temporary-files', 'Keep temporary files', false)
.usage('[options] <filename or glob>')
.command('* <glob>')
.action(globPattern => {
if (!globPattern) {
throw new Error('You must provide a file name or glob pattern to transform');
}
.command('* [glob/filename...]')
.action((globPatterns: string[]) => {
const prettierOptions: prettier.Options = {
arrowParens: program.arrowParens,
bracketSpacing: !program.noBracketSpacing,
Expand All @@ -45,10 +63,12 @@ program
const compilationOptions: CompilationOptions = {
ignorePrettierErrors: !!program.ignorePrettierErrors,
};
const files = glob.sync(globPattern, {});
const files = resolveGlobs(globPatterns);
if (!files.length) {
throw new Error('Nothing to do. You must provide file names or glob patterns to transform.');
}
let errors = false;
for (const file of files) {
const filePath = path.resolve(file);
for (const filePath of files) {
const newPath = filePath.replace(/\.jsx?$/, '.tsx');
const temporaryPath = filePath.replace(/\.jsx?$/, `_js2ts_${+new Date()}.tsx`);
try {
Expand All @@ -59,7 +79,7 @@ program
fs.unlinkSync(filePath);
}
} catch (error) {
console.warn(`Failed to convert ${file}`);
console.warn(`Failed to convert ${filePath}`);
console.warn(error);
errors = true;
}
Expand Down