@@ -12,6 +12,9 @@ import { join, extname } from 'path'
12
12
import { args , flags } from '@adonisjs/core/build/standalone'
13
13
14
14
import { BaseGenerator } from './Base'
15
+ import type { AppEnvironments } from '@ioc:Adonis/Core/Application'
16
+
17
+ const ALLOWED_ENVIRONMENTS : AppEnvironments [ ] = [ 'console' , 'web' , 'repl' , 'test' ]
15
18
16
19
/**
17
20
* Command to make a new preloaded file
@@ -23,11 +26,6 @@ export default class MakePreloadFile extends BaseGenerator {
23
26
protected resourceName : string
24
27
protected createExact = true
25
28
26
- /**
27
- * List of allowed environments
28
- */
29
- private allowedEnvironments = [ 'console' , 'web' , 'repl' ]
30
-
31
29
/**
32
30
* Command name
33
31
*/
@@ -41,19 +39,16 @@ export default class MakePreloadFile extends BaseGenerator {
41
39
@args . string ( { description : 'Name of the file' } )
42
40
public name : string
43
41
44
- @flags . string ( {
45
- description : ' Define the environment in which you want to load this file' ,
42
+ @flags . array ( {
43
+ description : ` Define the preload file environment. Accepted values " ${ ALLOWED_ENVIRONMENTS } "` ,
46
44
} )
47
- public environment : ( 'console' | 'web' | 'repl' ) [ ]
45
+ public environment : AppEnvironments [ ]
48
46
49
47
/**
50
- * Validates environments to ensure they are allowed. Especially when
51
- * defined as a flag.
48
+ * Check if the mentioned environments are valid
52
49
*/
53
- private validateEnvironments (
54
- environments : string [ ]
55
- ) : environments is ( 'console' | 'web' | 'repl' ) [ ] {
56
- return ! environments . find ( ( environment ) => ! this . allowedEnvironments . includes ( environment ) )
50
+ private isValidEnviroment ( environment : string [ ] ) : environment is AppEnvironments [ ] {
51
+ return ! environment . find ( ( one ) => ! ALLOWED_ENVIRONMENTS . includes ( one as any ) )
57
52
}
58
53
59
54
/**
@@ -70,50 +65,53 @@ export default class MakePreloadFile extends BaseGenerator {
70
65
return this . application . rcFile . directories . start || 'start'
71
66
}
72
67
73
- public async prepare ( ) {
74
- this . environment = await this . prompt . multiple (
75
- 'Select the environment(s) in which you want to load this file' ,
76
- [
77
- {
78
- name : 'console' ,
79
- message : 'During ace commands' ,
80
- } ,
81
- {
82
- name : 'repl' ,
83
- message : 'During repl session' ,
84
- } ,
85
- {
86
- name : 'web' ,
87
- message : 'During HTTP server' ,
88
- } ,
89
- ] ,
90
- {
91
- validate ( choices ) {
92
- return choices && choices . length ? true : 'Use space to select the environment'
93
- } ,
94
- }
95
- )
96
- }
97
-
98
68
/**
99
69
* Run command
100
70
*/
101
71
public async run ( ) {
102
- const environments =
103
- typeof this . environment === 'string'
104
- ? ( this . environment as string ) . split ( ',' )
105
- : this . environment
106
-
107
72
/**
108
- * Show error when defined environments are invalid
73
+ * Ensure the environments are valid when provided via flag
109
74
*/
110
- if ( ! this . validateEnvironments ( environments ) ) {
75
+ if ( this . environment && this . environment . length && ! this . isValidEnviroment ( this . environment ) ) {
111
76
this . logger . error (
112
- `Invalid environments "${ environments } ". Only "${ this . allowedEnvironments } " are allowed`
77
+ `Invalid environment(s) "${ this . environment } ". Only "${ ALLOWED_ENVIRONMENTS } " are allowed`
113
78
)
114
79
return
115
80
}
116
81
82
+ let environments : string [ ] = this . environment
83
+
84
+ /**
85
+ * Prompt user to select one or more environments
86
+ */
87
+ if ( ! environments ) {
88
+ environments = await this . prompt . multiple (
89
+ 'Select the environment(s) in which you want to load this file' ,
90
+ [
91
+ {
92
+ name : 'all' ,
93
+ message : 'Load file in all environments' ,
94
+ } ,
95
+ {
96
+ name : 'console' ,
97
+ message : 'Environment for ace commands' ,
98
+ } ,
99
+ {
100
+ name : 'repl' ,
101
+ message : 'Environment for the REPL session' ,
102
+ } ,
103
+ {
104
+ name : 'web' ,
105
+ message : 'Environment for HTTP requests' ,
106
+ } ,
107
+ {
108
+ name : 'test' ,
109
+ message : 'Environment for the test process' ,
110
+ } ,
111
+ ]
112
+ )
113
+ }
114
+
117
115
/**
118
116
* Generate resource file
119
117
*/
@@ -131,10 +129,13 @@ export default class MakePreloadFile extends BaseGenerator {
131
129
const relativePath = file . toJSON ( ) . relativepath
132
130
const rcFile = new files . AdonisRcFile ( this . application . appRoot )
133
131
134
- if ( environments && environments . length ) {
135
- rcFile . setPreload ( `./${ slash ( relativePath ) . replace ( extname ( relativePath ) , '' ) } ` , environments )
136
- } else {
132
+ if ( ! environments || ! environments . length || environments . includes ( 'all' ) ) {
137
133
rcFile . setPreload ( `./${ slash ( relativePath ) . replace ( extname ( relativePath ) , '' ) } ` )
134
+ } else {
135
+ rcFile . setPreload (
136
+ `./${ slash ( relativePath ) . replace ( extname ( relativePath ) , '' ) } ` ,
137
+ environments as AppEnvironments [ ]
138
+ )
138
139
}
139
140
140
141
rcFile . commit ( )
0 commit comments