11const debug = require ( 'debug' ) ( 'add-typescript-to-cypress' )
22const chalk = require ( 'chalk' )
3+ const terminalBanner = require ( 'terminal-banner' ) . terminalBanner
34const amDependency = require ( 'am-i-a-dependency' ) ( )
5+
46if ( amDependency ) {
5- console . log ( 'adding TypeScript plugin to Cypress' )
6- console . log ( 'current folder' , process . cwd ( ) )
7+ debug ( 'current folder' , process . cwd ( ) )
78
89 const fs = require ( 'fs' )
910 const path = require ( 'path' )
@@ -17,6 +18,18 @@ if (amDependency) {
1718 // node_modules/@bahmutov /add-typescript-to-cypress
1819 const root = path . join ( process . cwd ( ) , '..' , '..' , '..' )
1920 const cypressFolder = path . join ( root , 'cypress' )
21+ const pluginsFolder = path . join ( cypressFolder , 'plugins' )
22+ const ourPreprocessorFilename = path . join (
23+ pluginsFolder ,
24+ 'cy-ts-preprocessor.js'
25+ )
26+
27+ if ( fs . existsSync ( ourPreprocessorFilename ) ) {
28+ debug ( 'found existing file' , ourPreprocessorFilename )
29+ debug ( 'no need to overwrite again' )
30+ process . exit ( 0 )
31+ }
32+
2033 if ( ! fs . existsSync ( cypressFolder ) ) {
2134 console . error ( '⚠️ Cannot find "cypress" folder in %s' , chalk . yellow ( root ) )
2235 console . error ( 'Please scaffold Cypress folder by opening Cypress once' )
@@ -32,10 +45,14 @@ if (amDependency) {
3245 }
3346
3447 const addPluginFile = ( ) => {
35- const plugins = path . join ( cypressFolder , 'plugins ')
36- const pluginsIndex = path . join ( plugins , 'index.js' )
48+ console . log ( 'copying plugin file ')
49+ const pluginsIndex = path . join ( pluginsFolder , 'index.js' )
3750 const sourcePlugin = path . join ( __dirname , 'plugin.js' )
3851 shell . cp ( sourcePlugin , pluginsIndex )
52+
53+ console . log ( 'copying TS preprocessor file' )
54+ const sourcePreprocessor = path . join ( __dirname , 'cy-ts-preprocessor.js' )
55+ shell . cp ( sourcePreprocessor , ourPreprocessorFilename )
3956 }
4057
4158 const addTSConfigFile = ( ) => {
@@ -52,6 +69,7 @@ if (amDependency) {
5269 }
5370 }
5471
72+ terminalBanner ( 'adding TypeScript plugin for Cypress' )
5573 addPluginFile ( )
5674 addTSConfigFile ( )
5775} else {
0 commit comments