1+ #!/usr/bin/env node
12const chalk = require ( 'chalk' ) ;
23const commander = require ( 'commander' ) ;
3- const dns = require ( 'dns ' ) ;
4+ const fs = require ( 'fs-extra ' ) ;
45const { existsSync } = require ( 'fs' ) ;
5- const envinfo = require ( 'envinfo' ) ;
6- const execSync = require ( 'child_process' ) . execSync ;
7- const fsExtra = require ( 'fs-extra' ) ;
8- const hyperquest = require ( 'hyperquest' ) ;
9- const inquirer = require ( 'inquirer' ) ;
10- const os = require ( 'os' ) ;
116const path = require ( 'path' ) ;
127const semver = require ( 'semver' ) ;
138const spawn = require ( 'cross-spawn' ) ;
14- const tmp = require ( 'tmp' ) ;
15- const unpack = require ( 'tar-pack' ) . unpack ;
16- const url = require ( 'url' ) ;
17- const validateProjectName = require ( 'validate-npm-package-name' ) ;
189
1910const enginePackages = {
20- puppeteer : [ 'puppeteer@4 ' ] ,
11+ puppeteer : [ 'puppeteer@5 ' ] ,
2112 playwright : [ 'playwright@1' ] ,
2213 testcafe : [ 'testcafe@1' ] ,
2314 webdriverio : [ 'webdriverio@6' ] ,
2415} ;
2516
2617const codeceptPackages = [
18+ 'codeceptjs' ,
19+ '@codeceptjs/configure' ,
2720 '@codeceptjs/ui' ,
2821 '@codeceptjs/examples' ,
2922 '@codeceptjs/configure'
3023] ;
3124
25+ const CFonts = require ( 'cfonts' ) ;
26+
27+ CFonts . say ( 'Create|CodeceptJS' , {
28+ font : 'chrome' , // define the font face
29+ align : 'left' , // define text alignment
30+ colors : [ 'system' ] , // define all colors
31+ background : 'transparent' , // define the background color, you can also use `backgroundColor` here as key
32+ space : true , // define if the output text should have empty lines on top and on the bottom
33+ maxLength : '0' , // define how many character can be on one line
34+ gradient : [ 'yellow' , "#805ad5" ] , // define your two gradient colors
35+ independentGradient : true , // define if you want to recalculate the gradient for each new line
36+ transitionGradient : false , // define if this is a transition between colors directly
37+ env : 'node' // define the environment CFonts is being executed in
38+ } ) ;
39+ console . log ( ' 🔌 Supercharged End 2 End Testing 🌟' ) ;
40+
3241let projectName ;
42+ let useYarn ;
43+ let packageJson = fs . readJsonSync ( 'package.json' ) ;
3344
3445const program = new commander . Command ( 'Create CodeceptJS' )
3546 . version ( packageJson . version )
36- . arguments ( '< project-directory> ' )
37- . usage ( `${ chalk . green ( '< project-directory> ' ) } [options]` )
47+ . arguments ( '[ project] ' )
48+ . usage ( `${ chalk . green ( '[ project] ' ) } [options]` )
3849 . action ( name => {
3950 projectName = name ;
4051 } )
52+ . option ( '--use-yarn' )
4153 . option ( '--verbose' , 'print additional logs' )
4254 . option ( '--info' , 'print environment debug info' )
4355
@@ -52,20 +64,21 @@ const program = new commander.Command('Create CodeceptJS')
5264
5365 . allowUnknownOption ( )
5466 . on ( '--help' , ( ) => {
55- console . log ( ` Only ${ chalk . green ( '<project-directory>' ) } is required.` ) ;
5667 console . log ( ) ;
5768 } )
5869 . parse ( process . argv ) ;
5970
60- if ( typeof projectName === 'undefined' ) {
61- console . error ( 'Please specify the project directory:' ) ;
71+ if ( typeof projectName === 'undefined' && ! existsSync ( 'package.json' ) ) {
72+ console . error ( 'There is no package.json in current directory' ) ;
73+ console . log ( 'To create codeceptjs in a new project pass in a directory name:' ) ;
6274 console . log (
6375 ` ${ chalk . cyan ( program . name ( ) ) } ${ chalk . green ( '<project-directory>' ) } `
6476 ) ;
6577 console . log ( ) ;
6678 console . log ( 'For example:' ) ;
6779 console . log ( ` ${ chalk . cyan ( program . name ( ) ) } ${ chalk . green ( 'codeceptjs-tests' ) } ` ) ;
6880 console . log ( ) ;
81+ console . log ( 'To update current project to include codeceptjs packages, run this script in a directory with package.json' ) ;
6982 console . log (
7083 `Run ${ chalk . cyan ( `${ program . name ( ) } --help` ) } to see all options.`
7184 ) ;
@@ -74,7 +87,9 @@ if (typeof projectName === 'undefined') {
7487
7588// npx create-codeceptjs --template typescript --playwright codecept-tests
7689
77- function createCodecept ( opts ) {
90+ createCodecept ( program . opts ( ) ) ;
91+
92+ async function createCodecept ( opts ) {
7893 const unsupportedNodeVersion = ! semver . satisfies ( process . version , '>=12' ) ;
7994 if ( unsupportedNodeVersion ) {
8095 console . log (
@@ -85,17 +100,33 @@ function createCodecept(opts) {
85100 ) ;
86101 }
87102
88- const root = path . resolve ( name ) ;
89- const appName = path . basename ( root ) ;
90- fs . ensureDirSync ( name ) ;
103+ useYarn = opts . useYarn ;
104+
105+ const root = path . join ( process . cwd ( ) , projectName || '' ) ;
106+ fs . ensureDirSync ( root ) ;
107+
108+ process . chdir ( root ) ;
91109
92- if ( ! isSafeToCreateProjectIn ( root , name ) ) {
93- process . exit ( 1 ) ;
94- }
95- console . log ( ) ;
96110
97- console . log ( `Creating a new CodeceptJS project` ) ;
98111 console . log ( ) ;
112+ console . log ( `Creating CodeceptJS project in ${ chalk . bold ( root ) } ` ) ;
113+ console . log ( ) ;
114+
115+
116+ let deps = codeceptPackages ;
117+ if ( opts . puppeteer ) {
118+ console . log ( `Powered by ${ chalk . yellow ( 'Puppeteer' ) } engine` ) ;
119+ deps . push ( enginePackages . puppeteer ) ;
120+ } else if ( opts . webdriverio ) {
121+ console . log ( `Powered by ${ chalk . yellow ( 'WebDriver' ) } engine` ) ;
122+ deps . push ( enginePackages . webdriverio ) ;
123+ } else if ( opts . testcafe ) {
124+ console . log ( `Powered by ${ chalk . yellow ( 'TestCafe' ) } engine` ) ;
125+ deps . push ( enginePackages . testcafe ) ;
126+ } else {
127+ console . log ( `Powered by ${ chalk . yellow ( 'Playwright' ) } engine` ) ;
128+ deps . push ( enginePackages . playwright ) ;
129+ }
99130
100131 if ( ! existsSync ( 'package.json' ) ) {
101132 console . log ( 'package.json file does not exist in current dir, creating it...' ) ;
@@ -106,25 +137,46 @@ function createCodecept(opts) {
106137 private : true ,
107138 } ;
108139 fs . writeJsonSync ( 'package.json' , packageJson ) ;
140+ } else {
141+ console . log ( 'package.json found, adding codeceptjs dependencies & scripts into it' ) ;
109142 }
110143
111- let packageJson = fs . readJsonSync ( 'package.json' ) ;
112144
113145 if ( ! packageJson . scripts ) packageJson . scripts = { } ;
114146
115- packageJson . scripts [ 'e2e' ] = 'codeceptjs run --steps' ;
116- packageJson . scripts [ 'e2e:app' ] = 'codecept-ui --app' ;
117- packageJson . scripts [ 'e2e:server' ] = 'codecept-ui' ;
147+ packageJson . scripts [ 'codecept' ] = 'codeceptjs run --steps' ;
148+ packageJson . scripts [ 'codecept:headless' ] = 'HEADLESS=true codeceptjs run --steps' ;
149+ packageJson . scripts [ 'codecept:app' ] = 'codecept-ui --app' ;
150+ packageJson . scripts [ 'codecept:server' ] = 'codecept-ui' ;
118151
119- packageJson . scripts [ 'e2e:example' ] = 'codeceptjs run --steps -c node_modules/@codeceptjs/examples' ;
120- packageJson . scripts [ 'e2e:example:app' ] = 'codecept-ui --app -c node_modules/@codeceptjs/examples' ;
121- packageJson . scripts [ 'e2e:example:server' ] = 'codecept-ui -c node_modules/@codeceptjs/examples' ;
152+ packageJson . scripts [ 'codecept:demo' ] = 'codeceptjs run --steps -c node_modules/@codeceptjs/examples' ;
153+ packageJson . scripts [ 'codecept:demo:headless' ] = 'HEADLESS=true codeceptjs run --steps -c node_modules/@codeceptjs/examples' ;
154+ packageJson . scripts [ 'codecept:demo:app' ] = 'codecept-ui --app -c node_modules/@codeceptjs/examples' ;
155+ packageJson . scripts [ 'codecept:demo:server' ] = 'codecept-ui -c node_modules/@codeceptjs/examples' ;
122156
123157 fs . writeJsonSync ( 'package.json' , packageJson ) ;
124158
125- let initDeps = false ;
126159
127- if ( opts )
160+ // await install(deps.flat());
161+
162+ console . log ( 'Finished installing packages.' ) ;
163+
164+ console . log ( ) ;
165+ console . log ( chalk . bold ( 'What\'s next?' ) ) ;
166+ console . log ( )
167+ console . log ( 'Try CodeceptJS now with a demo project:' ) ;
168+ console . log ( '➕' , chalk . bold . cyan ( 'npm run codecept:demo' ) , '- executes codeceptjs tests for a demo project' ) ;
169+ console . log ( '➕' , chalk . cyan ( 'npm run codecept:demo:headless' ) , '- executes codeceptjs tests headlessly (no window shown)' ) ;
170+ console . log ( '➕' , chalk . bold . cyan ( 'npm run codecept:demo:app' ) , '- starts codeceptjs UI application for a demo project' ) ;
171+ console . log ( '➕' , chalk . cyan ( 'npm run codecept:demo:server' ) , '- starts codeceptjs UI as a webserver for a demo project' ) ;
172+ console . log ( ) ;
173+ console . log ( 'Initialize CodeceptJS for your project:' ) ;
174+ console . log ( '🔨' , chalk . yellow ( 'npx codeceptjs init' ) , '- initialize codeceptjs for current project' , chalk . bold ( 'required' ) ) ;
175+ console . log ( '➕' , chalk . cyan ( 'npm run codecept' ) , '- runs codeceptjs tests for current project' ) ;
176+ console . log ( '➕' , chalk . cyan ( 'npm run codecept:headless' ) , '- executes codeceptjs tests headlessly (no window shown)' ) ;
177+ console . log ( '➕' , chalk . cyan ( 'npm run codecept:app' ) , '- starts codeceptjs UI application for current project' ) ;
178+ console . log ( '➕' , chalk . cyan ( 'npm run codecept:server' ) , '- starts codeceptJS UI as webserver' ) ;
179+ // if (opts)
128180 // npx create-codeceptjs <> --playwright
129181 // npx create-codeceptjs <> --puppeteer
130182 // npx create-codeceptjs <> --testcafe
@@ -138,16 +190,31 @@ function install(dependencies, verbose) {
138190 return new Promise ( ( resolve , reject ) => {
139191 let command ;
140192 let args ;
141- command = 'npm' ;
142- args = [
143- 'install' ,
144- '--save-dev' ,
145- '--loglevel' ,
146- 'error' ,
147- ] . concat ( dependencies ) ;
148-
149- if ( verbose ) {
150- args . push ( '--verbose' ) ;
193+
194+ console . log ( 'Installing packages: ' , chalk . green ( dependencies . join ( ', ' ) ) ) ;
195+
196+ if ( useYarn ) {
197+ command = 'yarnpkg' ;
198+ args = [ 'add' , '--exact' ] ;
199+ [ ] . push . apply ( args , dependencies ) ;
200+
201+ // Explicitly set cwd() to work around issues like
202+ // https://github.com/facebook/create-react-app/issues/3326.
203+ // Unfortunately we can only do this for Yarn because npm support for
204+ // equivalent --prefix flag doesn't help with this issue.
205+ // This is why for npm, we run checkThatNpmCanReadCwd() early instead.
206+ args . push ( '--cwd' ) ;
207+ args . push ( root ) ;
208+
209+ } else {
210+ command = 'npm' ;
211+ args = [
212+ 'install' ,
213+ '--save-dev' ,
214+ '--loglevel' ,
215+ 'error' ,
216+ ] . concat ( dependencies ) ;
217+
151218 }
152219
153220 const child = spawn ( command , args , { stdio : 'inherit' } ) ;
@@ -161,4 +228,4 @@ function install(dependencies, verbose) {
161228 resolve ( ) ;
162229 } ) ;
163230 } ) ;
164- }
231+ }
0 commit comments