From c7bf80d82c28c190e3f0136d51af5b7de1bc4868 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Thu, 15 Sep 2022 18:30:33 +0200 Subject: [PATCH] feat(cli): Add custom environment variable support to generated application (#2751) --- packages/cli/src/app/templates/config.tpl.ts | 9 +++ .../authentication/templates/config.tpl.ts | 79 +++++++++++-------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/packages/cli/src/app/templates/config.tpl.ts b/packages/cli/src/app/templates/config.tpl.ts index 3683e3877b..05971de523 100644 --- a/packages/cli/src/app/templates/config.tpl.ts +++ b/packages/cli/src/app/templates/config.tpl.ts @@ -12,6 +12,14 @@ const defaultConfig = ({}: AppGeneratorContext) => ({ } }) +const customEnvironment = { + port: { + __name: 'PORT', + __format: 'number' + }, + host: 'HOSTNAME' +} + const testConfig = { port: 8998 } @@ -20,3 +28,4 @@ export const generate = (ctx: AppGeneratorContext) => generator(ctx) .then(writeJSON(defaultConfig, toFile('config', 'default.json'))) .then(writeJSON(testConfig, toFile('config', 'test.json'))) + .then(writeJSON(customEnvironment, toFile('config', 'custom-environment-variables.json'))) diff --git a/packages/cli/src/authentication/templates/config.tpl.ts b/packages/cli/src/authentication/templates/config.tpl.ts index dd566ce220..14d72b0026 100644 --- a/packages/cli/src/authentication/templates/config.tpl.ts +++ b/packages/cli/src/authentication/templates/config.tpl.ts @@ -3,44 +3,55 @@ import { generator, toFile, mergeJSON } from '@feathershq/pinion' import { AuthenticationGeneratorContext } from '../index' export const generate = (ctx: AuthenticationGeneratorContext) => - generator(ctx).then( - mergeJSON(({ authStrategies }) => { - const authentication: any = { - entity: ctx.entity, - service: ctx.service, - secret: crypto.randomBytes(24).toString('base64'), - authStrategies: ['jwt'], - jwtOptions: { - header: { - typ: 'access' - }, - audience: 'https://yourdomain.com', - algorithm: 'HS256', - expiresIn: '1d' + generator(ctx) + .then( + mergeJSON(({ authStrategies }) => { + const authentication: any = { + entity: ctx.entity, + service: ctx.service, + secret: crypto.randomBytes(24).toString('base64'), + authStrategies: ['jwt'], + jwtOptions: { + header: { + typ: 'access' + }, + audience: 'https://yourdomain.com', + algorithm: 'HS256', + expiresIn: '1d' + } } - } - if (authStrategies.includes('local')) { - authentication.authStrategies.push('local') - authentication.local = { - usernameField: 'email', - passwordField: 'password' + if (authStrategies.includes('local')) { + authentication.authStrategies.push('local') + authentication.local = { + usernameField: 'email', + passwordField: 'password' + } } - } - const oauthStrategies = authStrategies.filter((name) => name !== 'local') + const oauthStrategies = authStrategies.filter((name) => name !== 'local') - if (oauthStrategies.length) { - authentication.oauth = oauthStrategies.reduce((oauth, name) => { - oauth[name] = { - key: '', - secret: '' - } + if (oauthStrategies.length) { + authentication.oauth = oauthStrategies.reduce((oauth, name) => { + oauth[name] = { + key: '', + secret: '' + } - return oauth - }, {} as any) - } + return oauth + }, {} as any) + } - return { authentication } - }, toFile('config', 'default.json')) - ) + return { authentication } + }, toFile('config', 'default.json')) + ) + .then( + mergeJSON( + { + authentication: { + secret: 'FEATHERS_SECRET' + } + }, + toFile('config', 'custom-environment-variables.json') + ) + )