8
8
*/
9
9
10
10
import { join } from 'path'
11
+ import { tasks , files , logger } from '@adonisjs/sink'
11
12
import { BaseCommand , args } from '@adonisjs/core/build/standalone'
13
+
12
14
import { Manifest } from '../src/Manifest'
13
15
14
16
/**
15
17
* Configure a package
16
18
*/
17
19
export default class Configure extends BaseCommand {
18
20
public static commandName = 'configure'
19
- public static description = 'Configure a given AdonisJS package '
21
+ public static description = 'Configure one or more AdonisJS packages '
20
22
public static aliases = [ 'invoke' ]
21
23
22
24
/**
23
25
* Use yarn when building for production to install dependencies
24
26
*/
25
- @args . string ( {
26
- description : 'Name of the package you want to configure' ,
27
+ @args . spread ( {
28
+ description : 'Name of the package(s) you want to configure' ,
27
29
} )
28
- public name : string
30
+ public packages : string [ ]
29
31
30
32
/**
31
33
* Configure encore
32
34
*/
33
35
private async configureEncore ( ) {
34
- const { files, logger } = await import ( '@adonisjs/sink' )
35
-
36
36
/**
37
37
* Create the webpack config file
38
38
*/
@@ -55,6 +55,9 @@ export default class Configure extends BaseCommand {
55
55
logger . action ( 'create' ) . succeeded ( 'resources/js/app.js' )
56
56
}
57
57
58
+ /**
59
+ * Install Encore
60
+ */
58
61
const pkgFile = new files . PackageJsonFile ( this . application . appRoot )
59
62
pkgFile . install ( '@symfony/webpack-encore' )
60
63
@@ -70,23 +73,120 @@ export default class Configure extends BaseCommand {
70
73
}
71
74
72
75
/**
73
- * Invoked automatically by ace
76
+ * Configure tests
74
77
*/
75
- public async run ( ) {
76
- if ( this . name === 'encore' ) {
77
- await this . configureEncore ( )
78
- return
78
+ private async configureTests ( ) {
79
+ /**
80
+ * Create "test.ts" file
81
+ */
82
+ const testsEntryPointFile = new files . MustacheFile (
83
+ this . application . appRoot ,
84
+ 'test.ts' ,
85
+ join ( __dirname , '..' , 'templates/test-entrypoint.txt' )
86
+ )
87
+ if ( ! testsEntryPointFile . exists ( ) ) {
88
+ testsEntryPointFile . apply ( { } ) . commit ( )
89
+ logger . action ( 'create' ) . succeeded ( 'test.ts' )
90
+ }
91
+
92
+ /**
93
+ * Create "tests/bootstrap.ts" file
94
+ */
95
+ const testsBootstrapFile = new files . MustacheFile (
96
+ this . application . appRoot ,
97
+ 'tests/bootstrap.ts' ,
98
+ join ( __dirname , '..' , 'templates/tests/bootstrap.txt' )
99
+ )
100
+ if ( ! testsBootstrapFile . exists ( ) ) {
101
+ testsBootstrapFile . apply ( { } ) . commit ( )
102
+ logger . action ( 'create' ) . succeeded ( 'tests/bootstrap.ts' )
79
103
}
80
104
81
- const { tasks } = await import ( '@adonisjs/sink' )
105
+ /**
106
+ * Create "tests/functional/hello-world.spec.ts" file
107
+ */
108
+ const helloWorldTestFile = new files . MustacheFile (
109
+ this . application . appRoot ,
110
+ 'tests/functional/hello-world.spec.ts' ,
111
+ join ( __dirname , '..' , 'templates/tests/functional/hello-world.spec.txt' )
112
+ )
113
+ if ( ! helloWorldTestFile . exists ( ) ) {
114
+ helloWorldTestFile . apply ( { } ) . commit ( )
115
+ logger . action ( 'create' ) . succeeded ( 'tests/functional/hello-world.spec.ts' )
116
+ }
82
117
83
- await new tasks . Instructions (
84
- this . name ,
118
+ /**
119
+ * Create "contracts/tests.ts" file
120
+ */
121
+ const testsContractsFile = new files . MustacheFile (
85
122
this . application . appRoot ,
86
- this . application ,
87
- true
88
- ) . execute ( )
123
+ 'contracts/tests.ts' ,
124
+ join ( __dirname , '..' , 'templates/tests-contract.txt' )
125
+ )
126
+ if ( ! testsContractsFile . exists ( ) ) {
127
+ testsContractsFile . apply ( { } ) . commit ( )
128
+ logger . action ( 'create' ) . succeeded ( 'contracts/tests.ts' )
129
+ }
130
+
131
+ /**
132
+ * Update AdonisRc file with test suites
133
+ */
134
+ const rcFile = new files . AdonisRcFile ( this . application . appRoot )
135
+ rcFile . set ( 'tests' , {
136
+ suites : [
137
+ {
138
+ name : 'functional' ,
139
+ files : [ 'tests/functional/**/*.spec(.ts|.js)' ] ,
140
+ timeout : 60 * 1000 ,
141
+ } ,
142
+ ] ,
143
+ } )
89
144
145
+ rcFile . commit ( )
146
+ logger . action ( 'update' ) . succeeded ( '.adonisrc.json' )
147
+
148
+ /**
149
+ * Install required dependencies
150
+ */
151
+ const pkgFile = new files . PackageJsonFile ( this . application . appRoot )
152
+ pkgFile . install ( '@japa/runner' )
153
+ pkgFile . install ( '@japa/preset-adonis' )
154
+
155
+ const spinner = logger . await ( logger . colors . gray ( 'installing @japa/runner, @japa/preset-adonis' ) )
156
+
157
+ try {
158
+ await pkgFile . commitAsync ( )
159
+ spinner . update ( 'Installed' )
160
+ } catch ( error ) {
161
+ spinner . update ( 'Unable to install packages' )
162
+ logger . fatal ( error )
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Configure a give package
168
+ */
169
+ private async configurePackage ( name : string ) {
170
+ if ( name === 'encore' ) {
171
+ await this . configureEncore ( )
172
+ return
173
+ }
174
+
175
+ if ( name === 'tests' ) {
176
+ await this . configureTests ( )
177
+ return
178
+ }
179
+
180
+ await new tasks . Instructions ( name , this . application . appRoot , this . application , true ) . execute ( )
90
181
await new Manifest ( this . application . appRoot , this . logger ) . generate ( )
91
182
}
183
+
184
+ /**
185
+ * Invoked automatically by ace
186
+ */
187
+ public async run ( ) {
188
+ for ( let name of this . packages ) {
189
+ await this . configurePackage ( name )
190
+ }
191
+ }
92
192
}
0 commit comments