@@ -8,67 +8,86 @@ require('please-upgrade-node')(pkg);
88const fs = require ( 'fs' ) ;
99const { createRequire } = require ( 'module' ) ;
1010const path = require ( 'path' ) ;
11- const chalk = require ( 'chalk' ) ;
1211const spawn = require ( 'cross-spawn' ) ;
1312
1413const workingDirectoryRequire = createRequire ( path . resolve ( process . cwd ( ) , 'index.js' ) ) ;
1514
15+ const chalkImport = import ( 'chalk' ) ;
16+
17+ async function logWarning ( ...args ) {
18+ const { default : chalk } = await chalkImport ;
19+ console . warn ( chalk . yellow ( ...args ) ) ;
20+ }
21+
22+ async function logError ( ...args ) {
23+ const { default : chalk } = await chalkImport ;
24+ console . error ( chalk . red ( ...args ) ) ;
25+ }
26+
1627try {
1728 workingDirectoryRequire ( 'eslint' ) ;
1829} catch ( x ) {
19- console . error ( chalk . red ( 'eslint was not found.' ) ) ;
20- console . error (
21- chalk . red ( 'suppress-eslint-errors requires eslint to be installed in the working directory.' )
22- ) ;
23- process . exit ( 1 ) ;
30+ Promise . all ( [
31+ logError ( 'eslint was not found.' ) ,
32+ logError ( 'suppress-eslint-errors requires eslint to be installed in the working directory.' ) ,
33+ ] ) . finally ( ( ) => process . exit ( 1 ) ) ;
2434}
2535
2636const jscodeshiftPath = require . resolve ( 'jscodeshift/bin/jscodeshift' ) ;
2737const transformPath = require . resolve ( '../transforms/suppress-eslint-errors' ) ;
2838
29- const gitignoreArguments = [ ] ;
30- const gitignorePath = path . resolve ( process . cwd ( ) , '.gitignore' ) ;
31- if ( fs . existsSync ( gitignorePath ) ) {
32- if (
33- fs
34- . readFileSync ( gitignorePath , { encoding : 'utf8' } )
35- . split ( '\n' )
36- . findIndex ( ( line ) => line . startsWith ( '!' ) ) !== - 1
37- ) {
38- console . warn (
39- chalk . yellow (
40- 'your .gitignore contains exclusions, which jscodeshift does not properly support.'
41- )
42- ) ;
43- console . warn ( chalk . yellow ( 'skipping the ignore-config option.' ) ) ;
44- } else {
45- gitignoreArguments . push ( `--ignore-config=.gitignore` ) ;
46- }
47- }
39+ async function findGitignoreArguments ( ) {
40+ const gitignorePath = path . resolve ( process . cwd ( ) , '.gitignore' ) ;
4841
49- const result = spawn . sync (
50- 'node' ,
51- [ jscodeshiftPath , '--no-babel' , '-t' , transformPath ]
52- . concat ( gitignoreArguments )
53- . concat ( process . argv . slice ( 2 ) ) ,
54- {
55- stdio : 'inherit' ,
42+ if ( ! fs . existsSync ( gitignorePath ) ) {
43+ return [ ] ;
5644 }
57- ) ;
58-
59- if ( result . signal ) {
60- if ( result . signal === 'SIGKILL' ) {
61- console . error (
62- 'The script failed because the process exited too early. ' +
63- 'This probably means the system ran out of memory or someone called ' +
64- '`kill -9` on the process.'
65- ) ;
66- } else if ( result . signal === 'SIGTERM' ) {
67- console . error (
68- 'The script failed because the process exited too early. ' +
69- 'Someone might have called `kill` or `killall`, or the system could ' +
70- 'be shutting down.'
45+
46+ const allLines = fs . readFileSync ( gitignorePath , { encoding : 'utf8' } ) . split ( '\n' ) ;
47+ if ( allLines . findIndex ( ( line ) => line . startsWith ( '!' ) ) !== - 1 ) {
48+ await logWarning (
49+ 'your .gitignore contains exclusions, which jscodeshift does not properly support.'
7150 ) ;
51+ await logWarning ( 'skipping the ignore-config option.' ) ;
52+
53+ return [ ] ;
7254 }
73- process . exit ( 1 ) ;
55+
56+ return [ `--ignore-config=.gitignore` ] ;
7457}
58+
59+ ( async function runJsCodeShift ( ) {
60+ const result = spawn . sync (
61+ 'node' ,
62+ [
63+ jscodeshiftPath ,
64+ '--no-babel' ,
65+ '-t' ,
66+ transformPath ,
67+ ...( await findGitignoreArguments ( ) ) ,
68+ ...process . argv . slice ( 2 ) ,
69+ ] ,
70+ {
71+ stdio : 'inherit' ,
72+ }
73+ ) ;
74+
75+ if ( result . signal ) {
76+ if ( result . signal === 'SIGKILL' ) {
77+ console . error (
78+ 'The script failed because the process exited too early. ' +
79+ 'This probably means the system ran out of memory or someone called ' +
80+ '`kill -9` on the process.'
81+ ) ;
82+ } else if ( result . signal === 'SIGTERM' ) {
83+ console . error (
84+ 'The script failed because the process exited too early. ' +
85+ 'Someone might have called `kill` or `killall`, or the system could ' +
86+ 'be shutting down.'
87+ ) ;
88+ }
89+ process . exit ( 1 ) ;
90+ }
91+
92+ process . exit ( result . status ) ;
93+ } ) ( ) ;
0 commit comments