@@ -20,6 +20,7 @@ var child_process = require('child_process');
2020var fs = require ( 'fs' ) ;
2121var path = require ( 'path' ) ;
2222var temp = require ( 'temp' ) ;
23+ var mkdirp = require ( 'mkdirp' ) ;
2324require ( 'es6-promise' ) . polyfill ( ) ;
2425
2526function run ( args , stdin , cwd ) {
@@ -44,11 +45,23 @@ function run(args, stdin, cwd) {
4445}
4546
4647describe ( 'jscodeshift CLI' , ( ) => {
47- function createTempFileWith ( content ) {
48+ function createTempFileWith ( content , filename ) {
4849 var info = temp . openSync ( ) ;
50+ var filePath = info . path ;
4951 fs . writeSync ( info . fd , content ) ;
5052 fs . closeSync ( info . fd ) ;
51- return info . path ;
53+ if ( filename ) {
54+ filePath = renameFileTo ( filePath , filename ) ;
55+ }
56+ return filePath ;
57+ }
58+
59+ function renameFileTo ( oldPath , newFilename ) {
60+ var projectPath = path . dirname ( oldPath ) ;
61+ var newPath = path . join ( projectPath , newFilename ) ;
62+ mkdirp . sync ( path . dirname ( newPath ) ) ;
63+ fs . renameSync ( oldPath , newPath ) ;
64+ return newPath ;
5265 }
5366
5467 function createTransformWith ( content ) {
@@ -120,13 +133,8 @@ describe('jscodeshift CLI', () => {
120133 var transform = createTransformWith (
121134 'return (function() { "use strict"; const a = 42; }).toString();'
122135 ) ;
123- var babelrc = createTempFileWith ( `{"ignore": ["${ transform } "]}` ) ;
124- //var babelrc = createTempFileWith('{}');
125- var projectPath = path . dirname ( babelrc ) ;
126- fs . renameSync ( babelrc , path . join ( projectPath , '.babelrc' ) ) ;
127- var source = createTempFileWith ( 'a' ) ;
128- fs . renameSync ( source , path . join ( projectPath , 'source.js' ) ) ;
129- source = path . join ( projectPath , 'source.js' ) ;
136+ var babelrc = createTempFileWith ( `{"ignore": ["${ transform } "]}` , '.babelrc' ) ;
137+ var source = createTempFileWith ( 'a' , 'source.js' ) ;
130138
131139 return run ( [ '-t' , transform , source ] ) . then (
132140 ( [ stdout , stderr ] ) => {
@@ -174,6 +182,79 @@ describe('jscodeshift CLI', () => {
174182 ) ;
175183 } ) ;
176184
185+ describe ( 'ignoring' , ( ) => {
186+ var transform = createTransformWith (
187+ 'return "transform" + fileInfo.source;'
188+ ) ;
189+ var sources = [ ] ;
190+
191+ beforeEach ( ( ) => {
192+ sources = [ ] ;
193+ sources . push ( createTempFileWith ( 'a' , 'a.js' ) ) ;
194+ sources . push ( createTempFileWith ( 'a' , 'a-test.js' ) ) ;
195+ // sources.push(createTempFileWith('b', 'src/lib/b.js'));
196+ } ) ;
197+
198+ pit ( 'supports basic glob' , ( ) => {
199+ var pattern = '*-test.js' ;
200+ return run ( [ '-t' , transform , '--ignore-pattern' , pattern , ...sources ] ) . then (
201+ ( [ stdout , stderr ] ) => {
202+ expect ( fs . readFileSync ( sources [ 0 ] ) . toString ( ) ) . toBe ( 'transforma' ) ;
203+ expect ( fs . readFileSync ( sources [ 1 ] ) . toString ( ) ) . toBe ( 'a' ) ;
204+ }
205+ ) ;
206+ } ) ;
207+
208+ pit ( 'supports filename match' , ( ) => {
209+ var pattern = 'a.js' ;
210+ return run ( [ '-t' , transform , '--ignore-pattern' , pattern , ...sources ] ) . then (
211+ ( [ stdout , stderr ] ) => {
212+ expect ( fs . readFileSync ( sources [ 0 ] ) . toString ( ) ) . toBe ( 'a' ) ;
213+ expect ( fs . readFileSync ( sources [ 1 ] ) . toString ( ) ) . toBe ( 'transforma' ) ;
214+ }
215+ ) ;
216+ } ) ;
217+
218+ pit ( 'accepts a list of patterns' , ( ) => {
219+ var patterns = [ '--ignore-pattern' , 'a.js' , '--ignore-pattern' , '*-test.js' ] ;
220+ return run ( [ '-t' , transform , ...patterns , ...sources ] ) . then (
221+ ( [ stdout , stderr ] ) => {
222+ expect ( fs . readFileSync ( sources [ 0 ] ) . toString ( ) ) . toBe ( 'a' ) ;
223+ expect ( fs . readFileSync ( sources [ 1 ] ) . toString ( ) ) . toBe ( 'a' ) ;
224+ }
225+ ) ;
226+ } ) ;
227+
228+ pit ( 'sources ignore patterns from configuration file' , ( ) => {
229+ var patterns = [ 'sub/dir/' , '*-test.js' ] ;
230+ var gitignore = createTempFileWith ( patterns . join ( '\n' ) , '.gitignore' ) ;
231+ sources . push ( createTempFileWith ( 'subfile' , 'sub/dir/file.js' ) ) ;
232+
233+ return run ( [ '-t' , transform , '--ignore-config' , gitignore , ...sources ] ) . then (
234+ ( [ stdout , stderr ] ) => {
235+ expect ( fs . readFileSync ( sources [ 0 ] ) . toString ( ) ) . toBe ( 'transforma' ) ;
236+ expect ( fs . readFileSync ( sources [ 1 ] ) . toString ( ) ) . toBe ( 'a' ) ;
237+ expect ( fs . readFileSync ( sources [ 2 ] ) . toString ( ) ) . toBe ( 'subfile' ) ;
238+ }
239+ ) ;
240+ } ) ;
241+
242+ pit ( 'accepts a list of configuration files' , ( ) => {
243+ var gitignore = createTempFileWith ( [ 'sub/dir/' ] . join ( '\n' ) , '.gitignore' ) ;
244+ var eslintignore = createTempFileWith ( [ '**/*test.js' , 'a.js' ] . join ( '\n' ) , '.eslintignore' ) ;
245+ var configs = [ '--ignore-config' , gitignore , '--ignore-config' , eslintignore ] ;
246+ sources . push ( createTempFileWith ( 'subfile' , 'sub/dir/file.js' ) ) ;
247+
248+ return run ( [ '-t' , transform , ...configs , ...sources ] ) . then (
249+ ( [ stdout , stderr ] ) => {
250+ expect ( fs . readFileSync ( sources [ 0 ] ) . toString ( ) ) . toBe ( 'a' ) ;
251+ expect ( fs . readFileSync ( sources [ 1 ] ) . toString ( ) ) . toBe ( 'a' ) ;
252+ expect ( fs . readFileSync ( sources [ 2 ] ) . toString ( ) ) . toBe ( 'subfile' ) ;
253+ }
254+ ) ;
255+ } ) ;
256+ } ) ;
257+
177258 describe ( 'output' , ( ) => {
178259 pit ( 'shows workers info and stats at the end by default' , ( ) => {
179260 var source = createTempFileWith ( 'a' ) ;
0 commit comments