Skip to content

Commit

Permalink
fix: minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Jan 27, 2024
1 parent 037ec16 commit 7d36ebe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
33 changes: 14 additions & 19 deletions src/app-definition-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import _s from 'underscore.string'
import { dump as yamlDump } from 'js-yaml'
import { getEntityNameStrings, kebabCase } from './string-utils.js'

export enum PermissionModel {
Global = 'Global',
User = 'User',
Organization = 'Organization',
}

export interface App {
entities: AppEntity[]
name: string
permissionModel: string
permissionModel: PermissionModel
}

export interface AppEntityProperty {
Expand Down Expand Up @@ -117,8 +123,8 @@ function writeAppYamlToFileSystem({ app, appName, appDescription }: { app: App;
title: app.name,
description: appDescription, // `${app.name} App`,
'x-codeGenie': {
// region: 'us-west-2',
permissionModel: app.permissionModel,
region: 'us-west-2',
permissionModel: ['Global', 'User', 'Organization'].includes(app.permissionModel) ? app.permissionModel : 'Global',
defaultAuthRoute,
entities,
},
Expand Down Expand Up @@ -241,12 +247,13 @@ function getDynamoDbSettings({ app, entity }: { app: App; entity: AppEntity }) {
const dynamoDbSettings: any = {}
const isRootEntity = getIsRootEntity({ entity })

// Don't mess with the User entity
if (entity.name === 'User') return dynamoDbSettings

if (isRootEntity) {
dynamoDbSettings.partitionKey = 'userId'
dynamoDbSettings.sortKey = getIdProperty({ entity })
if (['User', 'Organization'].includes(app.permissionModel)) {
dynamoDbSettings.partitionKey = app.permissionModel === 'User' ? 'userId' : 'orgId'
dynamoDbSettings.sortKey = getIdProperty({ entity })
}
} else {
const belongsToEntity = getBelongsToEntity({ app, entity })
if (!belongsToEntity) {
Expand Down Expand Up @@ -327,21 +334,9 @@ function getPropertyType({ entityProperty }: { entityProperty: AppEntityProperty
}
}

function hasUserIdProperty({ entity }: { entity: AppEntity }) {
return Boolean(entity.properties.userId)
}

function getJsonSchemaProperties({ app, entity }: { app: App; entity: AppEntity }) {
const jsonSchemaProperties: any = {}

if (getIsRootEntity({ entity }) && !hasUserIdProperty({ entity })) {
jsonSchemaProperties.userId = {
type: 'string',
// description: 'User',
readOnly: true,
}
}

for (const [propertyName, property] of Object.entries(entity.properties)) {
const schemaProperties: any = {
...getPropertyType({ entityProperty: property }),
Expand All @@ -356,7 +351,7 @@ function getJsonSchemaProperties({ app, entity }: { app: App; entity: AppEntity
}

if (property.title) schemaProperties.title = property.title
// if (property.description) schemaProperties.description = property.description
if (property.description) schemaProperties.description = property.description
if (property.isReadOnly) schemaProperties.readOnly = property.isReadOnly
if (property.defaultValue) schemaProperties.default = property.defaultValue
if (property.enumOptions) schemaProperties.enum = property.enumOptions
Expand Down
28 changes: 21 additions & 7 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ generating app...
awsProfileToCopy: Flags.string({
char: 'p',
description:
"The AWS Profile to copy in the ~/.aws/credentials file and used to deploy the application. Defaults to the 'default' profile. Specify --no-copy-aws-profile to skip this step",
"The AWS Profile to copy in the ~/.aws/credentials file and used to deploy the application. Defaults to the 'default' profile. Specify --noCopyAwsProfile to skip this step",
default: 'default',
}),
noCopyAwsProfile: Flags.boolean({
Expand All @@ -84,11 +84,16 @@ generating app...
required: false,
default: false,
}),
generateAppDefinitionOnly: Flags.boolean({
description: 'Generates app definition only (run `@codegenie/cli generate` without `--description` to generate source code).',
required: false,
default: false,
}),
}

async run(): Promise<{ description?: string; deploy: boolean; awsProfileToCopy: string; appDir: string }> {
const { flags } = await this.parse(Generate)
const { description, deploy, awsProfileToCopy, noCopyAwsProfile } = flags
const { description, deploy, awsProfileToCopy, noCopyAwsProfile, generateAppDefinitionOnly } = flags

if (description && description.length > 500) {
this.error('description must be less than 500 characters.', {
Expand All @@ -109,12 +114,21 @@ generating app...

// Usually we expect that `generate --description` is run NOT within an existing Code Genie project directory; therefore
// hasExistingAppDefinition will usually be false. If `generate --description` is run within an existing Code Genie project directory,
// handleExistingAppDefinition will throw unless --replaceAppDefinitionf is included, in which case we want `appDir` to remain as cwd().
// handleExistingAppDefinition will throw unless --replaceAppDefinition is included, in which case we want `appDir` to remain as cwd().
if (!hasExistingAppDefinition) {
appDir = getAppOutputDir({ appName: generateAppDefinitionResult.appName })
}
}

if (generateAppDefinitionOnly) {
return {
description,
deploy,
awsProfileToCopy,
appDir,
}
}

const { headObjectPresignedUrl, getObjectPresignedUrl } = await this.uploadAppDefinition({ appDir })

await this.downloadProject({
Expand Down Expand Up @@ -173,8 +187,8 @@ For now you can open \`./${appDirRelative}\` in your favorite IDE like VS Code.
/**
* Checks if a .codegenie directory already exists so that it doesn't accidentally get overwritten.
*
* Users can specify --replace-app-definition if they would prefer to ignore this and replace the directory.
* @throws When a .codegenie directory exists and --replace-app-definition wasn't specified
* Users can specify --replaceAppDefinition if they would prefer to ignore this and replace the directory.
* @throws When a .codegenie directory exists and --replaceAppDefinition wasn't specified
*/
async handleExistingAppDefinition() {
const { flags } = await this.parse(Generate)
Expand All @@ -193,7 +207,7 @@ For now you can open \`./${appDirRelative}\` in your favorite IDE like VS Code.
code: 'CODEGENIE_DIR_EXISTS',
suggestions: [
'If you want to regenerate based on the existing App Definition defined in .codegenie: run the same command again without the `--description` flag.',
"If you'd rather replace the existing .codegenie directory with a new AI-generated App Definition, re-run the command again with the `--replace-app-definition` flag.",
"If you'd rather replace the existing .codegenie directory with a new AI-generated App Definition, re-run the command again with the `--replaceAppDefinition` flag.",
],
})
}
Expand Down Expand Up @@ -351,11 +365,11 @@ For now you can open \`./${appDirRelative}\` in your favorite IDE like VS Code.
}

async runInitDev({ appDir, appName }: { appDir: string; appName: string }): Promise<undefined> {
ux.action.start('🌩️ Deploying to AWS')
const appDirRelative = getAppOutputDir({ appName, absolute: false })
this.log(
`The first deploy may take up to 10 minutes, but you don't have to wait that long to get started! Open \`./${appDirRelative}\` in your favorite IDE like VS Code to explore your project source code. Tip: You may even be able to simply run \`code ./${appDirRelative}\` in a separate terminal to open it.`
)
ux.action.start('🌩️ Deploying to AWS')
execSync('npm run init:dev', {
stdio: 'inherit',
cwd: appDir,
Expand Down

0 comments on commit 7d36ebe

Please sign in to comment.