@aws-amplify/amplify-util-uibuilder 1.5.0-studio-forms.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/CHANGELOG.md +1 -2
  2. package/amplify-plugin.json +3 -10
  3. package/lib/clients/amplify-studio-client.d.ts +2 -13
  4. package/lib/clients/amplify-studio-client.d.ts.map +1 -1
  5. package/lib/clients/amplify-studio-client.js +4 -72
  6. package/lib/clients/amplify-studio-client.js.map +1 -1
  7. package/lib/commands/generateComponents.d.ts.map +1 -1
  8. package/lib/commands/generateComponents.js +15 -36
  9. package/lib/commands/generateComponents.js.map +1 -1
  10. package/lib/commands/help.d.ts +1 -1
  11. package/lib/commands/help.d.ts.map +1 -1
  12. package/lib/commands/help.js +2 -2
  13. package/lib/commands/help.js.map +1 -1
  14. package/lib/commands/utils/codegenResources.d.ts +2 -12
  15. package/lib/commands/utils/codegenResources.d.ts.map +1 -1
  16. package/lib/commands/utils/codegenResources.js +3 -70
  17. package/lib/commands/utils/codegenResources.js.map +1 -1
  18. package/lib/commands/utils/getUiBuilderComponentsPath.d.ts.map +1 -1
  19. package/lib/commands/utils/getUiBuilderComponentsPath.js.map +1 -1
  20. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts +20 -12
  21. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts.map +1 -1
  22. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js +3 -36
  23. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js.map +1 -1
  24. package/lib/commands/version.d.ts +1 -1
  25. package/lib/commands/version.d.ts.map +1 -1
  26. package/lib/commands/version.js +2 -2
  27. package/lib/commands/version.js.map +1 -1
  28. package/lib/event-handlers/handle-PostEnvAdd.d.ts +1 -1
  29. package/lib/event-handlers/handle-PostEnvAdd.d.ts.map +1 -1
  30. package/lib/event-handlers/handle-PostEnvAdd.js +6 -4
  31. package/lib/event-handlers/handle-PostEnvAdd.js.map +1 -1
  32. package/lib/event-handlers/handle-PostPull.d.ts +1 -1
  33. package/lib/event-handlers/handle-PostPull.d.ts.map +1 -1
  34. package/lib/event-handlers/handle-PostPull.js +3 -4
  35. package/lib/event-handlers/handle-PostPull.js.map +1 -1
  36. package/package.json +7 -8
  37. package/src/__tests__/cloneComponentsFromEnv.test.ts +0 -11
  38. package/src/__tests__/createUiBuilderComponent.test.ts +1 -19
  39. package/src/__tests__/generateComponents.test.ts +1 -35
  40. package/src/__tests__/getAmplifyDataSchema.test.ts +3 -43
  41. package/src/__tests__/handleEvents.test.ts +21 -0
  42. package/src/__tests__/syncAmplifyUiComponents.test.ts +8 -111
  43. package/src/clients/amplify-studio-client.ts +63 -146
  44. package/src/commands/generateComponents.ts +16 -46
  45. package/src/commands/help.ts +2 -5
  46. package/src/commands/utils/codegenResources.ts +4 -108
  47. package/src/commands/utils/getUiBuilderComponentsPath.ts +2 -7
  48. package/src/commands/utils/syncAmplifyUiBuilderComponents.ts +29 -74
  49. package/src/commands/version.ts +2 -5
  50. package/src/event-handlers/handle-PostEnvAdd.ts +6 -8
  51. package/src/event-handlers/handle-PostPull.ts +3 -7
  52. package/tsconfig.tsbuildinfo +1 -1
  53. package/lib/commands/utils/featureFlags.d.ts +0 -3
  54. package/lib/commands/utils/featureFlags.d.ts.map +0 -1
  55. package/lib/commands/utils/featureFlags.js +0 -34
  56. package/lib/commands/utils/featureFlags.js.map +0 -1
  57. package/lib/event-handlers/handle-PostPush.d.ts +0 -3
  58. package/lib/event-handlers/handle-PostPush.d.ts.map +0 -1
  59. package/lib/event-handlers/handle-PostPush.js +0 -15
  60. package/lib/event-handlers/handle-PostPush.js.map +0 -1
  61. package/src/__tests__/utils.ts +0 -70
  62. package/src/commands/utils/featureFlags.ts +0 -61
  63. package/src/event-handlers/handle-PostPush.ts +0 -15
@@ -10,19 +10,14 @@ import {
10
10
  RenderTextComponentResponse,
11
11
  GenericDataSchema,
12
12
  getGenericFromDataStore,
13
- StudioForm,
14
- StudioSchema,
15
13
  } from '@aws-amplify/codegen-ui';
16
14
  import {
17
15
  AmplifyRenderer,
18
- AmplifyFormRenderer,
19
16
  ReactThemeStudioTemplateRenderer,
20
17
  ReactIndexStudioTemplateRenderer,
21
18
  ModuleKind,
22
19
  ScriptTarget,
23
20
  ScriptKind,
24
- UtilTemplateType,
25
- ReactUtilsStudioTemplateRenderer,
26
21
  } from '@aws-amplify/codegen-ui-react';
27
22
  import { printer } from 'amplify-prompts';
28
23
  import {
@@ -88,46 +83,15 @@ export const createUiBuilderTheme = (context: $TSContext, schema: StudioTheme):
88
83
  }
89
84
  };
90
85
 
91
- /**
92
- * Writes form file to the work space
93
- */
94
- export const createUiBuilderForm = (context: $TSContext, schema: StudioForm, dataSchema?: GenericDataSchema): StudioForm => {
95
- const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
96
- const rendererFactory = new StudioTemplateRendererFactory(
97
- (form: StudioForm) => (new AmplifyFormRenderer(form, dataSchema, config) as unknown) as StudioTemplateRenderer<
98
- unknown,
99
- StudioForm,
100
- FrameworkOutputManager<unknown>,
101
- RenderTextComponentResponse
102
- >,
103
- );
104
-
105
- const outputPathDir = uiBuilderComponentsPath;
106
-
107
- const rendererManager = new StudioTemplateRendererManager(rendererFactory, {
108
- outputPathDir,
109
- });
110
-
111
- try {
112
- rendererManager.renderSchemaToTemplate(schema);
113
- return schema;
114
- } catch (e) {
115
- printer.debug(e);
116
- printer.debug('Skipping invalid form with schema');
117
- printer.debug(JSON.stringify(schema, null, 2));
118
- throw e;
119
- }
120
- };
121
-
122
86
  /**
123
87
  * Writes index file to the work space
124
88
  */
125
- export const generateAmplifyUiBuilderIndexFile = (context: $TSContext, schemas: StudioSchema[]): void => {
89
+ export const generateAmplifyUiBuilderIndexFile = (context: $TSContext, schemas: StudioComponent[]): RenderTextComponentResponse => {
126
90
  const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
127
91
  const rendererFactory = new StudioTemplateRendererFactory(
128
- (schema: StudioSchema[]) => (new ReactIndexStudioTemplateRenderer(schema, config) as unknown) as StudioTemplateRenderer<
92
+ (component: StudioComponent[]) => (new ReactIndexStudioTemplateRenderer(component, config) as unknown) as StudioTemplateRenderer<
129
93
  unknown,
130
- StudioSchema[],
94
+ StudioComponent[],
131
95
  FrameworkOutputManager<unknown>,
132
96
  RenderTextComponentResponse
133
97
  >,
@@ -140,50 +104,7 @@ export const generateAmplifyUiBuilderIndexFile = (context: $TSContext, schemas:
140
104
  });
141
105
 
142
106
  try {
143
- rendererManager.renderSchemaToTemplate(schemas);
144
- } catch (e) {
145
- printer.debug(e);
146
- printer.debug('Failed to generate component index file');
147
- throw e;
148
- }
149
- };
150
-
151
- type UtilFileChecks = {
152
- hasForms: boolean;
153
- hasViews: boolean;
154
- };
155
-
156
- /**
157
- * Writes utils file to the work space
158
- */
159
- export const generateAmplifyUiBuilderUtilFile = (context: $TSContext, { hasForms, hasViews }: UtilFileChecks): void => {
160
- const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
161
- const rendererFactory = new StudioTemplateRendererFactory(
162
- (utils: UtilTemplateType[]) => (new ReactUtilsStudioTemplateRenderer(utils, config)),
163
- );
164
-
165
- const outputPathDir = uiBuilderComponentsPath;
166
-
167
- const rendererManager = new StudioTemplateRendererManager(rendererFactory, {
168
- outputPathDir,
169
- });
170
-
171
- const utils = new Set<UtilTemplateType>();
172
-
173
- if (hasForms) {
174
- utils.add('validation');
175
- utils.add('formatter');
176
- utils.add('fetchByPath');
177
- }
178
-
179
- if (hasViews) {
180
- utils.add('formatter');
181
- }
182
-
183
- try {
184
- if (utils.size) {
185
- rendererManager.renderSchemaToTemplate([...utils]);
186
- }
107
+ return rendererManager.renderSchemaToTemplate(schemas);
187
108
  } catch (e) {
188
109
  printer.debug(e);
189
110
  printer.debug('Failed to generate component index file');
@@ -198,9 +119,6 @@ export const generateAmplifyUiBuilderUtilFile = (context: $TSContext, { hasForms
198
119
  export const getAmplifyDataSchema = async (
199
120
  studioClient: AmplifyStudioClient,
200
121
  ): Promise<GenericDataSchema | undefined> => {
201
- if (!studioClient.isSupportedGraphQL) {
202
- return undefined;
203
- }
204
122
  try {
205
123
  const meta = stateManager.getMeta();
206
124
  const resourceName = Object.entries(meta[AmplifyCategories.API]).find(
@@ -220,25 +138,3 @@ export const getAmplifyDataSchema = async (
220
138
  return undefined;
221
139
  }
222
140
  };
223
-
224
- /**
225
- * generates base create/update froms from names
226
- */
227
- export const generateBaseForms = (modelMap: {[model: string]: Set<'create' | 'update'>}): StudioForm[] => {
228
- const getSchema = (name: string, type: 'create' | 'update') : StudioForm => ({
229
- name: `${name}${type === 'create' ? 'CreateForm' : 'UpdateForm'}`,
230
- formActionType: type,
231
- dataType: { dataSourceType: 'DataStore', dataTypeName: name },
232
- fields: {},
233
- sectionalElements: {},
234
- style: {},
235
- cta: {},
236
- });
237
-
238
- const schemas : StudioForm[] = [];
239
-
240
- Object.entries(modelMap).forEach(([name, set]) => {
241
- set.forEach(type => schemas.push(getSchema(name, type)));
242
- });
243
- return schemas;
244
- };
@@ -1,19 +1,14 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs-extra';
3
- import { $TSContext } from 'amplify-cli-core';
4
3
  import { extractArgs } from './extractArgs';
4
+ import { $TSContext } from 'amplify-cli-core';
5
5
 
6
- /**
7
- *
8
- * locates components path directory
9
- */
10
- export const getUiBuilderComponentsPath = (context: $TSContext): string => {
6
+ export const getUiBuilderComponentsPath = (context: $TSContext) => {
11
7
  const args = extractArgs(context);
12
8
  const srcDir = args.srcDir ? args.srcDir : context.exeInfo.projectConfig.javascript.config.SourceDir;
13
9
  const uiBuilderComponentsPath = path.resolve(path.join('.', srcDir, 'ui-components'));
14
10
 
15
11
  if (!fs.existsSync(uiBuilderComponentsPath)) {
16
- // eslint-disable-next-line spellcheck/spell-checker
17
12
  fs.mkdirpSync(uiBuilderComponentsPath);
18
13
  }
19
14
 
@@ -1,41 +1,30 @@
1
1
  import { printer } from 'amplify-prompts';
2
2
  import { $TSContext } from 'amplify-cli-core';
3
3
  import {
4
- StudioComponent, StudioTheme, GenericDataSchema, StudioForm, StudioSchema,checkIsSupportedAsForm
4
+ StudioComponent, StudioTheme, GenericDataSchema,
5
5
  } from '@aws-amplify/codegen-ui';
6
- import {
7
- createUiBuilderComponent,
8
- createUiBuilderForm,
9
- createUiBuilderTheme,
10
- generateBaseForms,
11
- } from './codegenResources';
6
+ import { createUiBuilderComponent, createUiBuilderTheme } from './codegenResources';
12
7
  import { getUiBuilderComponentsPath } from './getUiBuilderComponentsPath';
13
8
 
14
- type CodegenResponse<T extends StudioSchema> =
15
- | {
16
- resultType: 'SUCCESS';
17
- schema: T;
18
- schemaName?: string;
19
- }
20
- | {
21
- resultType: 'FAILURE';
22
- schemaName: string;
23
- error: Error;
24
- schema?: T;
25
- };
26
-
27
9
  /**
28
10
  * Returns instances of StudioComponent from the component schemas
29
11
  */
30
- export const generateUiBuilderComponents = (
31
- context: $TSContext,
32
- componentSchemas: any[], // eslint-disable-line @typescript-eslint/no-explicit-any
33
- dataSchema?: GenericDataSchema,
34
- ): CodegenResponse<StudioComponent>[] => {
35
- const componentResults = componentSchemas.map<CodegenResponse<StudioComponent>>(schema => {
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ export const generateUiBuilderComponents = (context: $TSContext, componentSchemas: any[], dataSchema?: GenericDataSchema): ({
14
+ resultType: string;
15
+ component: StudioComponent;
16
+ schemaName?: undefined;
17
+ error?: undefined;
18
+ } | {
19
+ resultType: string;
20
+ schemaName: string;
21
+ error: Error;
22
+ component?: undefined;
23
+ })[] => {
24
+ const componentResults = componentSchemas.map(schema => {
36
25
  try {
37
26
  const component = createUiBuilderComponent(context, schema, dataSchema);
38
- return { resultType: 'SUCCESS', schema: component };
27
+ return { resultType: 'SUCCESS', component };
39
28
  } catch (e) {
40
29
  printer.debug(`Failure caught processing ${schema.name}`);
41
30
  printer.debug(e);
@@ -55,11 +44,21 @@ export const generateUiBuilderComponents = (
55
44
  * Returns instances of StudioTheme from theme schemas
56
45
  */
57
46
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- export const generateUiBuilderThemes = (context: $TSContext, themeSchemas: any[]): CodegenResponse<StudioTheme>[] => {
59
- const themeResults = themeSchemas.map<CodegenResponse<StudioTheme>>(schema => {
47
+ export const generateUiBuilderThemes = (context: $TSContext, themeSchemas: any[]): ({
48
+ resultType: string;
49
+ theme: StudioTheme;
50
+ schemaName?: undefined;
51
+ error?: undefined;
52
+ } | {
53
+ resultType: string;
54
+ schemaName: string;
55
+ error: Error;
56
+ theme?: undefined;
57
+ })[] => {
58
+ const themeResults = themeSchemas.map(schema => {
60
59
  try {
61
60
  const theme = createUiBuilderTheme(context, schema);
62
- return { resultType: 'SUCCESS', schema: theme };
61
+ return { resultType: 'SUCCESS', theme };
63
62
  } catch (e) {
64
63
  printer.debug(`Failure caught processing ${schema.name}`);
65
64
  printer.debug(e);
@@ -72,47 +71,3 @@ export const generateUiBuilderThemes = (context: $TSContext, themeSchemas: any[]
72
71
  );
73
72
  return themeResults;
74
73
  };
75
-
76
- /**
77
- * Returns instances of StudioForm from form schemas
78
- */
79
- export const generateUiBuilderForms = (
80
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
- context: $TSContext,
82
- formSchemas: any[],
83
- dataSchema?: GenericDataSchema,
84
- autoGenerateForms?: boolean,
85
- ): CodegenResponse<StudioForm>[] => {
86
- const modelMap: { [model: string]: Set<'create' | 'update'> } = {};
87
- if (dataSchema?.dataSourceType === 'DataStore' && autoGenerateForms) {
88
- Object.entries(dataSchema.models).forEach(([name, model]) => {
89
- if(checkIsSupportedAsForm(model) && !model.isJoinTable) {
90
- modelMap[name] = new Set(['create', 'update']);
91
- }
92
- });
93
- }
94
- const codegenForm = (schema: StudioForm): CodegenResponse<StudioForm> => {
95
- try {
96
- const form = createUiBuilderForm(context, schema, dataSchema);
97
- return { resultType: 'SUCCESS', schema: form };
98
- } catch (e) {
99
- printer.debug(`Failure caught processing ${schema.name}`);
100
- printer.debug(e);
101
- return { resultType: 'FAILURE', schemaName: schema.name, error: e };
102
- }
103
- };
104
- const formResults = formSchemas.map((schema: StudioForm) => {
105
- if (schema?.dataType && schema.dataType?.dataSourceType === 'DataStore') {
106
- modelMap[schema.dataType.dataTypeName]?.delete(schema.formActionType);
107
- }
108
- return codegenForm(schema);
109
- });
110
-
111
- // append remaining models
112
- formResults.push(...generateBaseForms(modelMap).map(codegenForm));
113
-
114
- printer.debug(
115
- `Generated ${formResults.filter(result => result.resultType === 'SUCCESS').length} forms in ${getUiBuilderComponentsPath(context)}`,
116
- );
117
- return formResults;
118
- };
@@ -1,9 +1,6 @@
1
1
  import { printer } from 'amplify-prompts';
2
2
  import path from 'path';
3
3
 
4
- /**
5
- * runs version command
6
- */
7
- export const run = async (): Promise<void> => {
4
+ export async function run() {
8
5
  printer.info((await import(path.join(__dirname, '..', '..', 'package.json'))).version);
9
- };
6
+ }
@@ -1,16 +1,14 @@
1
1
  import { $TSContext } from 'amplify-cli-core';
2
2
  import { printer } from 'amplify-prompts';
3
- import { run as cloneComponentsFromEnv } from '../commands/cloneComponentsFromEnv';
4
3
 
5
- /**
6
- *
7
- * runs after adding an environment
8
- */
9
- export const run = async (context: $TSContext): Promise<void> => {
4
+ export async function run(context: $TSContext) {
10
5
  try {
11
- await cloneComponentsFromEnv(context);
6
+ await context.amplify.invokePluginMethod(context, 'ui-builder', undefined, 'executeAmplifyCommand', [
7
+ context,
8
+ 'cloneComponentsFromEnv',
9
+ ]);
12
10
  } catch (e) {
13
11
  // Swallow all errors from the uibuilder plugin
14
12
  printer.debug(e);
15
13
  }
16
- };
14
+ }
@@ -1,15 +1,11 @@
1
1
  import { $TSContext } from 'amplify-cli-core';
2
2
  import { printer } from 'amplify-prompts';
3
- import { run as generateComponents } from '../commands/generateComponents';
4
3
 
5
- /**
6
- * The code to run after a pull.
7
- */
8
- export const run = async (context: $TSContext): Promise<void> => {
4
+ export async function run(context: $TSContext) {
9
5
  try {
10
- await generateComponents(context);
6
+ await context.amplify.invokePluginMethod(context, 'ui-builder', undefined, 'executeAmplifyCommand', [context, 'generateComponents']);
11
7
  } catch (e) {
12
8
  // Swallow all errors from the uibuilder plugin
13
9
  printer.debug(e);
14
10
  }
15
- };
11
+ }