Skip to content

Commit

Permalink
Only apply filters on files from a directory, fixes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Aug 12, 2015
1 parent 462ea7f commit 7532a65
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions bin/__tests__/jscodeshift-test.js
Expand Up @@ -66,13 +66,13 @@ describe('jscodeshift CLI', () => {
);

return Promise.all([
run(['--no-extensions', '-t', transformA, sourceA, sourceB]).then(
run(['-t', transformA, sourceA, sourceB]).then(
([stdout, stderr]) => {
expect(fs.readFileSync(sourceA).toString()).toBe('transforma');
expect(fs.readFileSync(sourceB).toString()).toBe('transformb\n');
}
),
run(['--no-extensions', '-t', transformB, sourceC]).then(
run(['-t', transformB, sourceC]).then(
([stdout, stderr]) => {
expect(fs.readFileSync(sourceC).toString()).toBe(sourceC);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('jscodeshift CLI', () => {
' typeof api.stats === "function"',
' );',
].join('\n'));
return run(['--no-extensions', '-t', transform, source]).then(
return run(['-t', transform, source]).then(
([stdout, stderr]) => {
expect(fs.readFileSync(source).toString()).toBe('true');
}
Expand All @@ -110,7 +110,7 @@ describe('jscodeshift CLI', () => {
pit('passes options along to the transform', () => {
var source = createTempFileWith('a');
var transform = createTransformWith('return options.foo;');
return run(['--no-extensions', '-t', transform, '--foo=42', source]).then(
return run(['-t', transform, '--foo=42', source]).then(
([stdout, stderr]) => {
expect(fs.readFileSync(source).toString()).toBe('42');
}
Expand Down
16 changes: 9 additions & 7 deletions src/Runner.js
Expand Up @@ -53,7 +53,7 @@ function showStats(stats) {
names.forEach(name => console.log(name + ':', stats[name]));
}

function getAllFiles(paths) {
function getAllFiles(paths, filter) {
return Promise.all(
paths.map(file => new Promise((resolve, reject) => {
fs.lstat(file, (err, stat) => {
Expand All @@ -64,7 +64,10 @@ function getAllFiles(paths) {
}

if (stat.isDirectory()) {
dir.files(file, (err, list) => resolve(list));
dir.files(
file,
(err, list) => resolve(list ? list.filter(filter) : [])
);
} else {
resolve([file]);
}
Expand All @@ -90,11 +93,10 @@ function run(transformFile, paths, options) {
return;
}

getAllFiles(paths)
.then(files => files.filter(
name => !extensions || extensions.indexOf(path.extname(name)) != -1
))
.then(files => {
getAllFiles(
paths,
name => !extensions || extensions.indexOf(path.extname(name)) != -1
).then(files => {
if (files.length === 0) {
console.log('No files selected, nothing to do.');
return;
Expand Down

0 comments on commit 7532a65

Please sign in to comment.