Skip to content

Commit 7592cb4

Browse files
authored
feat: add secret type to env:add command (#5157)
1 parent e3534b9 commit 7592cb4

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎commands/env/add.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { type CommandOptions } from '../../types/ace.ts'
1111
import stringHelpers from '../../src/helpers/string.ts'
1212
import { args, BaseCommand, flags } from '../../modules/ace/main.ts'
1313

14-
const ALLOWED_TYPES = ['string', 'boolean', 'number', 'enum'] as const
14+
const ALLOWED_TYPES = ['string', 'secret', 'boolean', 'number', 'enum'] as const
1515
type AllowedTypes = (typeof ALLOWED_TYPES)[number]
1616

1717
/**
@@ -23,7 +23,7 @@ type AllowedTypes = (typeof ALLOWED_TYPES)[number]
2323
* ```
2424
* ace env:add
2525
* ace env:add DATABASE_URL postgres://localhost:5432/mydb
26-
* ace env:add API_KEY secret --type=string
26+
* ace env:add API_KEY secret --type=secret
2727
* ace env:add PORT 3333 --type=number
2828
* ace env:add DEBUG true --type=boolean
2929
* ace env:add LOG_LEVEL info --type=enum --enum-values=debug,info,warn,error
@@ -65,7 +65,7 @@ export default class EnvAdd extends BaseCommand {
6565
declare value: string
6666

6767
/**
68-
* Data type of the environment variable (string, boolean, number, enum)
68+
* Data type of the environment variable (string, secret, boolean, number, enum)
6969
*/
7070
@flags.string({ description: 'Type of the variable' })
7171
declare type: AllowedTypes
@@ -150,6 +150,7 @@ export default class EnvAdd extends BaseCommand {
150150
*/
151151
const validation = {
152152
string: 'Env.schema.string()',
153+
secret: 'Env.schema.secret()',
153154
number: 'Env.schema.number()',
154155
boolean: 'Env.schema.boolean()',
155156
enum: `Env.schema.enum(['${this.enumValues.join("','")}'] as const)`,

‎tests/commands/env_add.spec.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,53 @@ test.group('Env Add command', () => {
3434
await assert.fileContains('./start/env.ts', 'VARIABLE: Env.schema.string()')
3535
})
3636

37+
test('add secret env variable using the type flag', async ({ assert, fs }) => {
38+
await fs.createJson('tsconfig.json', {})
39+
await fs.create('.env', '')
40+
await fs.create('.env.example', '')
41+
await fs.create(
42+
'./start/env.ts',
43+
`import { Env } from '@adonisjs/core/env'
44+
export default await Env.create(new URL('../', import.meta.url), {})`
45+
)
46+
47+
const ace = await new AceFactory().make(fs.baseUrl)
48+
await ace.app.init()
49+
ace.ui.switchMode('raw')
50+
51+
const command = await ace.create(EnvAdd, ['api_key', 'my_secret', '--type=secret'])
52+
await command.exec()
53+
54+
await assert.fileContains('.env', 'API_KEY=my_secret')
55+
await assert.fileContains('.env.example', 'API_KEY=')
56+
await assert.fileNotContains('.env.example', 'my_secret')
57+
await assert.fileContains('./start/env.ts', 'API_KEY: Env.schema.secret()')
58+
})
59+
60+
test('select secret type from the prompt', async ({ assert, fs }) => {
61+
await fs.createJson('tsconfig.json', {})
62+
await fs.create('.env', '')
63+
await fs.create('.env.example', '')
64+
await fs.create(
65+
'./start/env.ts',
66+
`import { Env } from '@adonisjs/core/env'
67+
export default await Env.create(new URL('../', import.meta.url), {})`
68+
)
69+
70+
const ace = await new AceFactory().make(fs.baseUrl)
71+
await ace.app.init()
72+
ace.ui.switchMode('raw')
73+
74+
const command = await ace.create(EnvAdd, ['api_key', 'my_secret'])
75+
command.prompt.trap('Select the variable type').replyWith('secret')
76+
await command.exec()
77+
78+
await assert.fileContains('.env', 'API_KEY=my_secret')
79+
await assert.fileContains('.env.example', 'API_KEY=')
80+
await assert.fileNotContains('.env.example', 'my_secret')
81+
await assert.fileContains('./start/env.ts', 'API_KEY: Env.schema.secret()')
82+
})
83+
3784
test('convert variable to screaming snake case', async ({ assert, fs }) => {
3885
await fs.createJson('tsconfig.json', {})
3986
await fs.create('.env', '')

0 commit comments

Comments
 (0)