Skip to content

Commit 029b5ac

Browse files
committed
improvement: add repl environment to make:prldfile command
1 parent 37b7bf2 commit 029b5ac

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

commands/Make/PreloadFile.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class MakePreloadFile extends BaseGenerator {
2525
/**
2626
* List of allowed environments
2727
*/
28-
private allowedEnvironments = ['console', 'web', 'test']
28+
private allowedEnvironments = ['console', 'web', 'repl']
2929

3030
/**
3131
* Command name
@@ -42,7 +42,7 @@ export default class MakePreloadFile extends BaseGenerator {
4242
public name: string
4343

4444
@flags.string({
45-
description: 'Explicitly define the environment in which you want to load this file',
45+
description: 'Define the environment in which you want to load this file',
4646
async defaultValue(command: MakePreloadFile) {
4747
return command.prompt.multiple(
4848
'Select the environment(s) in which you want to load this file',
@@ -51,6 +51,10 @@ export default class MakePreloadFile extends BaseGenerator {
5151
name: 'console',
5252
message: 'During ace commands',
5353
},
54+
{
55+
name: 'repl',
56+
message: 'During repl session',
57+
},
5458
{
5559
name: 'web',
5660
message: 'During HTTP server',
@@ -64,15 +68,15 @@ export default class MakePreloadFile extends BaseGenerator {
6468
)
6569
},
6670
})
67-
public environment: ('console' | 'web')[]
71+
public environment: ('console' | 'web' | 'repl')[]
6872

6973
/**
7074
* Validates environments to ensure they are allowed. Especially when
7175
* defined as a flag.
7276
*/
7377
private validateEnvironments(
7478
environments: string[]
75-
): environments is ('console' | 'web' | 'test')[] {
79+
): environments is ('console' | 'web' | 'repl')[] {
7680
return !environments.find((environment) => !this.allowedEnvironments.includes(environment))
7781
}
7882

npm-audit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h5 class="card-title">
5555
<div class="card">
5656
<div class="card-body">
5757
<h5 class="card-title">
58-
November 9th 2020, 4:56:16 pm
58+
November 9th 2020, 5:07:24 pm
5959
</h5>
6060
<p class="card-text">Last updated</p>
6161
</div>

test/make-preloaded-file.spec.ts

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ test.group('Make Preloaded File', (group) => {
4444
preloadFile.environment = 'console,web' as any
4545
await preloadFile.run()
4646

47-
const AppProvider = await fs.get('start/viewGlobals.ts')
48-
const ProviderTemplate = await templates.get('preload-file.txt')
49-
assert.deepEqual(
50-
toNewlineArray(AppProvider),
51-
toNewlineArray(ProviderTemplate.replace('{{ filename }}', 'AppProvider'))
52-
)
47+
const viewGlobals = await fs.get('start/viewGlobals.ts')
48+
const preloadTemplate = await templates.get('preload-file.txt')
49+
assert.deepEqual(toNewlineArray(viewGlobals), toNewlineArray(preloadTemplate))
5350

5451
const rcRawContents = await fs.get('.adonisrc.json')
5552
assert.deepEqual(JSON.parse(rcRawContents), {
@@ -80,12 +77,9 @@ test.group('Make Preloaded File', (group) => {
8077
preloadFile.environment = ['console', 'web']
8178
await preloadFile.run()
8279

83-
const AppProvider = await fs.get('foo/viewGlobals.ts')
84-
const ProviderTemplate = await templates.get('preload-file.txt')
85-
assert.deepEqual(
86-
toNewlineArray(AppProvider),
87-
toNewlineArray(ProviderTemplate.replace('{{ filename }}', 'AppProvider'))
88-
)
80+
const viewGlobals = await fs.get('foo/viewGlobals.ts')
81+
const preloadTemplate = await templates.get('preload-file.txt')
82+
assert.deepEqual(toNewlineArray(viewGlobals), toNewlineArray(preloadTemplate))
8983

9084
const rcRawContents = await fs.get('.adonisrc.json')
9185
assert.deepEqual(JSON.parse(rcRawContents), {
@@ -98,4 +92,58 @@ test.group('Make Preloaded File', (group) => {
9892
],
9993
})
10094
})
95+
96+
test('select environment as repl', async (assert) => {
97+
await fs.add('.adonisrc.json', JSON.stringify({}))
98+
99+
const rcContents = importFresh(join(fs.basePath, '.adonisrc.json')) as any
100+
const app = new Application(fs.basePath, 'test', rcContents)
101+
102+
const preloadFile = new PreloadFile(app, new Kernel(app))
103+
preloadFile.name = 'repl'
104+
preloadFile.environment = ['repl']
105+
await preloadFile.run()
106+
107+
const replFile = await fs.get('start/repl.ts')
108+
const preloadTemplate = await templates.get('preload-file.txt')
109+
assert.deepEqual(toNewlineArray(replFile), toNewlineArray(preloadTemplate))
110+
111+
const rcRawContents = await fs.get('.adonisrc.json')
112+
assert.deepEqual(JSON.parse(rcRawContents), {
113+
preloads: [
114+
{
115+
file: './start/repl',
116+
environment: ['repl'],
117+
},
118+
],
119+
})
120+
})
121+
122+
test('prompt for environment when not explicitly defined', async (assert) => {
123+
await fs.add('.adonisrc.json', JSON.stringify({}))
124+
125+
const rcContents = importFresh(join(fs.basePath, '.adonisrc.json')) as any
126+
const app = new Application(fs.basePath, 'test', rcContents)
127+
128+
const preloadFile = new PreloadFile(app, new Kernel(app))
129+
preloadFile.prompt.on('prompt', (question) => {
130+
question.select(1)
131+
})
132+
133+
await preloadFile.kernel.runCommand(preloadFile, ['make:prldfile', 'repl'])
134+
135+
const replFile = await fs.get('start/repl.ts')
136+
const preloadTemplate = await templates.get('preload-file.txt')
137+
assert.deepEqual(toNewlineArray(replFile), toNewlineArray(preloadTemplate))
138+
139+
const rcRawContents = await fs.get('.adonisrc.json')
140+
assert.deepEqual(JSON.parse(rcRawContents), {
141+
preloads: [
142+
{
143+
file: './start/repl',
144+
environment: ['repl'],
145+
},
146+
],
147+
})
148+
})
101149
})

0 commit comments

Comments
 (0)