Skip to content

Commit

Permalink
feat: insert empty example in the .env.example
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 29, 2024
1 parent f0f522d commit b5d4746
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
8 changes: 5 additions & 3 deletions commands/env/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { CommandOptions } from '../../types/ace.js'
import { args, BaseCommand, flags } from '../../modules/ace/main.js'
import stringHelpers from '../../src/helpers/string.js'
import { args, BaseCommand, flags } from '../../modules/ace/main.js'

const ALLOWED_TYPES = ['string', 'boolean', 'number', 'enum'] as const
type AllowedTypes = (typeof ALLOWED_TYPES)[number]
Expand Down Expand Up @@ -98,8 +98,10 @@ export default class EnvAdd extends BaseCommand {
*/
const codemods = await this.createCodemods()
const transformedName = stringHelpers.snakeCase(this.name).toUpperCase()

await codemods.defineEnvVariables({ [transformedName]: this.value })
await codemods.defineEnvVariables(
{ [transformedName]: this.value },
{ withEmptyExampleValue: true }
)

/**
* Add the environment variable to the `start/env.ts` file
Expand Down
7 changes: 5 additions & 2 deletions modules/ace/codemods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ export class Codemods extends EventEmitter {
/**
* Define one or more environment variables
*/
async defineEnvVariables(environmentVariables: Record<string, number | string | boolean>) {
async defineEnvVariables(
environmentVariables: Record<string, number | string | boolean>,
options?: { withEmptyExampleValue?: boolean }
) {
const editor = new EnvEditor(this.#app.appRoot)
await editor.load()

Object.keys(environmentVariables).forEach((key) => {
const value = environmentVariables[key]
editor.add(key, value)
editor.add(key, value, options?.withEmptyExampleValue)
})

await editor.save()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"@adonisjs/bodyparser": "^10.0.1",
"@adonisjs/config": "^5.0.1",
"@adonisjs/encryption": "^6.0.1",
"@adonisjs/env": "^6.0.0",
"@adonisjs/env": "^6.1.0",
"@adonisjs/events": "^9.0.1",
"@adonisjs/fold": "^10.1.0",
"@adonisjs/hash": "^9.0.2",
Expand Down
17 changes: 17 additions & 0 deletions tests/ace/codemods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ test.group('Codemods | environment variables', (group) => {
await assert.fileContains('.env', 'CORS_ENABLED=true')
})

test('do not insert env value in .env.example if specified', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')

/**
* Creating .env file so that we can update it.
*/
await fs.create('.env', '')
await fs.create('.env.example', '')

const codemods = new Codemods(ace.app, ace.ui.logger)
await codemods.defineEnvVariables({ SECRET_VALUE: 'secret' }, { withEmptyExampleValue: true })
await assert.fileContains('.env', 'SECRET_VALUE=secret')
await assert.fileContains('.env.example', 'SECRET_VALUE=')
})

test('do not define env variables when file does not exists', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
Expand Down
8 changes: 4 additions & 4 deletions tests/commands/env_add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test.group('Env Add command', () => {
await command.exec()

await assert.fileContains('.env', 'VARIABLE=value')
await assert.fileContains('.env.example', 'VARIABLE=value')
await assert.fileContains('.env.example', 'VARIABLE=')
await assert.fileContains('./start/env.ts', 'VARIABLE: Env.schema.string()')
})

Expand All @@ -52,7 +52,7 @@ test.group('Env Add command', () => {
await command.exec()

await assert.fileContains('.env', 'STRIPE_API_KEY=value')
await assert.fileContains('.env.example', 'STRIPE_API_KEY=value')
await assert.fileContains('.env.example', 'STRIPE_API_KEY=')
await assert.fileContains('./start/env.ts', 'STRIPE_API_KEY: Env.schema.string()')
})

Expand Down Expand Up @@ -80,7 +80,7 @@ test.group('Env Add command', () => {
await command.exec()

await assert.fileContains('.env', 'VARIABLE=bar')
await assert.fileContains('.env.example', 'VARIABLE=bar')
await assert.fileContains('.env.example', 'VARIABLE=')
await assert.fileContains(
'./start/env.ts',
"VARIABLE: Env.schema.enum(['foo', 'bar'] as const)"
Expand Down Expand Up @@ -110,7 +110,7 @@ test.group('Env Add command', () => {
await command.exec()

await assert.fileContains('.env', 'MY_VARIABLE_NAME=my_value')
await assert.fileContains('.env.example', 'MY_VARIABLE_NAME=my_value')
await assert.fileContains('.env.example', 'MY_VARIABLE_NAME=')
await assert.fileContains('./start/env.ts', 'MY_VARIABLE_NAME: Env.schema.string()')
})
})

0 comments on commit b5d4746

Please sign in to comment.