Skip to content

Commit fc13b41

Browse files
committed
feat: add test and all options to make preload file command
1 parent 5a9af95 commit fc13b41

File tree

2 files changed

+78
-55
lines changed

2 files changed

+78
-55
lines changed

commands/Make/PreloadFile.ts

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { join, extname } from 'path'
1212
import { args, flags } from '@adonisjs/core/build/standalone'
1313

1414
import { BaseGenerator } from './Base'
15+
import type { AppEnvironments } from '@ioc:Adonis/Core/Application'
16+
17+
const ALLOWED_ENVIRONMENTS: AppEnvironments[] = ['console', 'web', 'repl', 'test']
1518

1619
/**
1720
* Command to make a new preloaded file
@@ -23,11 +26,6 @@ export default class MakePreloadFile extends BaseGenerator {
2326
protected resourceName: string
2427
protected createExact = true
2528

26-
/**
27-
* List of allowed environments
28-
*/
29-
private allowedEnvironments = ['console', 'web', 'repl']
30-
3129
/**
3230
* Command name
3331
*/
@@ -41,19 +39,16 @@ export default class MakePreloadFile extends BaseGenerator {
4139
@args.string({ description: 'Name of the file' })
4240
public name: string
4341

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}"`,
4644
})
47-
public environment: ('console' | 'web' | 'repl')[]
45+
public environment: AppEnvironments[]
4846

4947
/**
50-
* Validates environments to ensure they are allowed. Especially when
51-
* defined as a flag.
48+
* Check if the mentioned environments are valid
5249
*/
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))
5752
}
5853

5954
/**
@@ -70,50 +65,53 @@ export default class MakePreloadFile extends BaseGenerator {
7065
return this.application.rcFile.directories.start || 'start'
7166
}
7267

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-
9868
/**
9969
* Run command
10070
*/
10171
public async run() {
102-
const environments =
103-
typeof this.environment === 'string'
104-
? (this.environment as string).split(',')
105-
: this.environment
106-
10772
/**
108-
* Show error when defined environments are invalid
73+
* Ensure the environments are valid when provided via flag
10974
*/
110-
if (!this.validateEnvironments(environments)) {
75+
if (this.environment && this.environment.length && !this.isValidEnviroment(this.environment)) {
11176
this.logger.error(
112-
`Invalid environments "${environments}". Only "${this.allowedEnvironments}" are allowed`
77+
`Invalid environment(s) "${this.environment}". Only "${ALLOWED_ENVIRONMENTS}" are allowed`
11378
)
11479
return
11580
}
11681

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+
117115
/**
118116
* Generate resource file
119117
*/
@@ -131,10 +129,13 @@ export default class MakePreloadFile extends BaseGenerator {
131129
const relativePath = file.toJSON().relativepath
132130
const rcFile = new files.AdonisRcFile(this.application.appRoot)
133131

134-
if (environments && environments.length) {
135-
rcFile.setPreload(`./${slash(relativePath).replace(extname(relativePath), '')}`, environments)
136-
} else {
132+
if (!environments || !environments.length || environments.includes('all')) {
137133
rcFile.setPreload(`./${slash(relativePath).replace(extname(relativePath), '')}`)
134+
} else {
135+
rcFile.setPreload(
136+
`./${slash(relativePath).replace(extname(relativePath), '')}`,
137+
environments as AppEnvironments[]
138+
)
138139
}
139140

140141
rcFile.commit()

test/make-preloaded-file.spec.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test.group('Make Preloaded File', (group) => {
4141

4242
const preloadFile = new PreloadFile(app, new Kernel(app).mockConsoleOutput())
4343
preloadFile.name = 'viewGlobals'
44-
preloadFile.environment = 'console,web' as any
44+
preloadFile.environment = ['console', 'web']
4545
await preloadFile.run()
4646

4747
const viewGlobals = await fs.get('start/viewGlobals.ts')
@@ -93,7 +93,7 @@ test.group('Make Preloaded File', (group) => {
9393
})
9494
})
9595

96-
test('select environment as repl', async ({ assert }) => {
96+
test('set preload file environment as repl', async ({ assert }) => {
9797
await fs.add('.adonisrc.json', JSON.stringify({}))
9898

9999
const rcContents = readJSONSync(join(fs.basePath, '.adonisrc.json'))
@@ -127,12 +127,10 @@ test.group('Make Preloaded File', (group) => {
127127

128128
const preloadFile = new PreloadFile(app, new Kernel(app).mockConsoleOutput())
129129
preloadFile.prompt.on('prompt', (question) => {
130-
question.select(1)
130+
question.select(2)
131131
})
132132

133133
preloadFile.name = 'repl'
134-
preloadFile.environment = ['repl']
135-
136134
await preloadFile.exec()
137135

138136
const replFile = await fs.get('start/repl.ts')
@@ -149,4 +147,28 @@ test.group('Make Preloaded File', (group) => {
149147
],
150148
})
151149
})
150+
151+
test('do not set environment when all is selected', async ({ assert }) => {
152+
await fs.add('.adonisrc.json', JSON.stringify({}))
153+
154+
const rcContents = readJSONSync(join(fs.basePath, '.adonisrc.json'))
155+
const app = new Application(fs.basePath, 'test', rcContents)
156+
157+
const preloadFile = new PreloadFile(app, new Kernel(app).mockConsoleOutput())
158+
preloadFile.prompt.on('prompt', (question) => {
159+
question.select(0)
160+
})
161+
162+
preloadFile.name = 'events'
163+
await preloadFile.exec()
164+
165+
const replFile = await fs.get('start/events.ts')
166+
const preloadTemplate = await templates.get('preload-file.txt')
167+
assert.deepEqual(toNewlineArray(replFile), toNewlineArray(preloadTemplate))
168+
169+
const rcRawContents = await fs.get('.adonisrc.json')
170+
assert.deepEqual(JSON.parse(rcRawContents), {
171+
preloads: ['./start/events'],
172+
})
173+
})
152174
})

0 commit comments

Comments
 (0)