@aws-amplify/amplify-util-uibuilder 1.2.0-uibuilder-ts.0 → 1.2.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 (85) hide show
  1. package/CHANGELOG.md +18 -2
  2. package/lib/commands/cloneComponentsFromEnv.d.ts +3 -0
  3. package/lib/commands/cloneComponentsFromEnv.d.ts.map +1 -0
  4. package/lib/commands/cloneComponentsFromEnv.js +68 -0
  5. package/lib/commands/cloneComponentsFromEnv.js.map +1 -0
  6. package/lib/commands/generateComponents.d.ts +3 -0
  7. package/lib/commands/generateComponents.d.ts.map +1 -0
  8. package/lib/commands/generateComponents.js +50 -0
  9. package/lib/commands/generateComponents.js.map +1 -0
  10. package/lib/commands/help.d.ts +2 -0
  11. package/lib/commands/help.d.ts.map +1 -0
  12. package/lib/commands/help.js +9 -0
  13. package/lib/commands/help.js.map +1 -0
  14. package/lib/commands/utils/createUiBuilderComponent.d.ts +5 -0
  15. package/lib/commands/utils/createUiBuilderComponent.d.ts.map +1 -0
  16. package/lib/commands/utils/createUiBuilderComponent.js +64 -0
  17. package/lib/commands/utils/createUiBuilderComponent.js.map +1 -0
  18. package/lib/commands/utils/extractArgs.d.ts +11 -0
  19. package/lib/commands/utils/extractArgs.d.ts.map +1 -0
  20. package/lib/commands/utils/extractArgs.js +26 -0
  21. package/lib/commands/utils/extractArgs.js.map +1 -0
  22. package/lib/commands/utils/getUiBuilderComponentsPath.d.ts +3 -0
  23. package/lib/commands/utils/getUiBuilderComponentsPath.d.ts.map +1 -0
  24. package/lib/commands/utils/getUiBuilderComponentsPath.js +20 -0
  25. package/lib/commands/utils/getUiBuilderComponentsPath.js.map +1 -0
  26. package/lib/commands/utils/notifyMissingPackages.d.ts +3 -0
  27. package/lib/commands/utils/notifyMissingPackages.d.ts.map +1 -0
  28. package/lib/commands/utils/notifyMissingPackages.js +45 -0
  29. package/lib/commands/utils/notifyMissingPackages.js.map +1 -0
  30. package/lib/commands/utils/shouldRenderComponents.d.ts +3 -0
  31. package/lib/commands/utils/shouldRenderComponents.d.ts.map +1 -0
  32. package/lib/commands/utils/shouldRenderComponents.js +34 -0
  33. package/lib/commands/utils/shouldRenderComponents.js.map +1 -0
  34. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts +31 -0
  35. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts.map +1 -0
  36. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js +116 -0
  37. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js.map +1 -0
  38. package/lib/commands/version.d.ts +2 -0
  39. package/lib/commands/version.d.ts.map +1 -0
  40. package/lib/commands/version.js +32 -0
  41. package/lib/commands/version.js.map +1 -0
  42. package/lib/event-handlers/handle-PostEnvAdd.d.ts +3 -0
  43. package/lib/event-handlers/handle-PostEnvAdd.d.ts.map +1 -0
  44. package/lib/event-handlers/handle-PostEnvAdd.js +17 -0
  45. package/lib/event-handlers/handle-PostEnvAdd.js.map +1 -0
  46. package/lib/event-handlers/handle-PostPull.d.ts +3 -0
  47. package/lib/event-handlers/handle-PostPull.d.ts.map +1 -0
  48. package/lib/event-handlers/handle-PostPull.js +14 -0
  49. package/lib/event-handlers/handle-PostPull.js.map +1 -0
  50. package/lib/index.d.ts +6 -0
  51. package/lib/index.d.ts.map +1 -0
  52. package/lib/index.js +22 -0
  53. package/lib/index.js.map +1 -0
  54. package/package.json +35 -5
  55. package/src/__tests__/cloneComponentsFromEnv.test.ts +49 -0
  56. package/src/__tests__/createUiBuilderComponent.test.ts +60 -0
  57. package/src/__tests__/generateComponents.test.ts +50 -0
  58. package/src/__tests__/handleEvents.test.ts +21 -0
  59. package/src/__tests__/help.test.ts +10 -0
  60. package/src/__tests__/mock.json +1 -0
  61. package/src/__tests__/notifyMissingPackages.test.ts +73 -0
  62. package/src/__tests__/package.json +3 -0
  63. package/{commands/utils/__tests__/shouldRenderComponents.test.js → src/__tests__/shouldRenderComponents.test.ts} +3 -2
  64. package/src/__tests__/syncAmplifyUiComponents.test.ts +150 -0
  65. package/src/__tests__/version.test.ts +10 -0
  66. package/{commands/cloneComponentsFromEnv.js → src/commands/cloneComponentsFromEnv.ts} +15 -19
  67. package/{commands/generateComponents.js → src/commands/generateComponents.ts} +10 -12
  68. package/src/commands/help.ts +5 -0
  69. package/{commands/utils/createUiBuilderComponent.js → src/commands/utils/createUiBuilderComponent.ts} +16 -20
  70. package/{commands/utils/extractArgs.js → src/commands/utils/extractArgs.ts} +3 -5
  71. package/{commands/utils/getUiBuilderComponentsPath.js → src/commands/utils/getUiBuilderComponentsPath.ts} +5 -8
  72. package/src/commands/utils/notifyMissingPackages.ts +40 -0
  73. package/{commands/utils/shouldRenderComponents.js → src/commands/utils/shouldRenderComponents.ts} +5 -8
  74. package/{commands/utils/syncAmplifyUiBuilderComponents.js → src/commands/utils/syncAmplifyUiBuilderComponents.ts} +23 -26
  75. package/src/commands/version.ts +6 -0
  76. package/{event-handlers/handle-PostEnvAdd.js → src/event-handlers/handle-PostEnvAdd.ts} +3 -6
  77. package/{event-handlers/handle-PostPull.js → src/event-handlers/handle-PostPull.ts} +3 -6
  78. package/{index.js → src/index.ts} +6 -10
  79. package/tsconfig.json +9 -0
  80. package/tsconfig.tsbuildinfo +1 -0
  81. package/commands/help.js +0 -7
  82. package/commands/utils/__tests__/createUiComponents.test.js +0 -46
  83. package/commands/utils/__tests__/syncAmplifyUiComponents.test.js +0 -67
  84. package/commands/utils/notifyMissingPackages.js +0 -28
  85. package/commands/version.js +0 -10
@@ -0,0 +1,150 @@
1
+ import { $TSContext } from 'amplify-cli-core';
2
+ import {
3
+ generateUiBuilderComponents,
4
+ getEnvName,
5
+ getAppId,
6
+ listUiBuilderComponents,
7
+ listUiBuilderThemes,
8
+ getAmplifyUIBuilderService,
9
+ resolveAppId,
10
+ generateUiBuilderThemes,
11
+ } from '../commands/utils/syncAmplifyUiBuilderComponents';
12
+ import aws from 'aws-sdk';
13
+ import * as CLICore from 'amplify-cli-core';
14
+ import * as createUiBuilderComponentDependency from '../commands/utils/createUiBuilderComponent';
15
+ const aws_mock = aws as any;
16
+ const createUiBuilderComponentDependency_mock = createUiBuilderComponentDependency as any;
17
+ describe('should sync amplify ui builder components', () => {
18
+ let context: $TSContext | any;
19
+ beforeEach(() => {
20
+ CLICore.FeatureFlags.getBoolean = () => false;
21
+ CLICore.FeatureFlags.getNumber = () => 0;
22
+ context = {
23
+ exeInfo: {
24
+ projectConfig: {
25
+ javascript: {
26
+ config: {
27
+ SourceDir: 'src',
28
+ },
29
+ },
30
+ },
31
+ },
32
+ amplify: {
33
+ invokePluginMethod: () => ({}),
34
+ },
35
+ parameters: {
36
+ argv: [],
37
+ },
38
+ input: {
39
+ options: {
40
+ appId: 'testAppId',
41
+ envName: 'testEnvName',
42
+ },
43
+ },
44
+ };
45
+
46
+ aws_mock.AmplifyUIBuilder = jest.fn(() => ({
47
+ exportComponents: jest.fn(() => ({
48
+ promise: jest.fn(() => ({
49
+ entities: [
50
+ {
51
+ appId: 'd37nrm8rzt3oek',
52
+ bindingProperties: {},
53
+ componentType: 'Box',
54
+ environmentName: 'staging',
55
+ id: 's-s4mU579Ycf6JGHwhqT',
56
+ name: 'aawwdd',
57
+ overrides: {},
58
+ properties: {},
59
+ variants: [],
60
+ },
61
+ ],
62
+ })),
63
+ })),
64
+ exportThemes: jest.fn(() => ({
65
+ promise: jest.fn(() => ({
66
+ entities: [{}],
67
+ })),
68
+ })),
69
+ }));
70
+
71
+ createUiBuilderComponentDependency_mock.createUiBuilderComponent = jest.fn();
72
+ createUiBuilderComponentDependency_mock.createUiBuilderTheme = jest.fn();
73
+ });
74
+
75
+ it('pulls components from aws-sdk and passes them to createUiBuilderComponent', () => {
76
+ generateUiBuilderComponents(context, []);
77
+ });
78
+
79
+ it('does not throw an error when createUiBuilderComponent fails', () => {
80
+ createUiBuilderComponentDependency_mock.createUiBuilderComponent = jest.fn(() => {
81
+ throw new Error('ahhh!');
82
+ });
83
+ expect(async () => generateUiBuilderComponents(context, [])).not.toThrow();
84
+ });
85
+
86
+ it('does not throw an error when createUiBuilderThemes fails', () => {
87
+ createUiBuilderComponentDependency_mock.createUiBuilderComponent = jest.fn(() => {
88
+ throw new Error('ahhh!');
89
+ });
90
+ expect(async () => generateUiBuilderThemes(context, [])).not.toThrow();
91
+ });
92
+
93
+ it('can getAmplifyUIBuilderService', async () => {
94
+ process.env.UI_BUILDER_ENDPOINT = 'https://mock-endpoint.com';
95
+ process.env.UI_BUILDER_REGION = 'mock-region';
96
+ const service = await getAmplifyUIBuilderService(context, 'testEnv', 'testAppId');
97
+ expect(Object.keys(service)).toContain('exportComponents');
98
+ expect(Object.keys(service)).toContain('exportThemes');
99
+ });
100
+ it('can listUiBuilderThemes', async () => {
101
+ const themes = await listUiBuilderThemes(context, 'testEnv');
102
+ expect(themes.entities).toHaveLength(1);
103
+ });
104
+ it('can listUiBuilderComponents', async () => {
105
+ const components = await listUiBuilderComponents(context, 'testEnv');
106
+ expect(components.entities).toHaveLength(1);
107
+ });
108
+ it('can getAppId', async () => {
109
+ const appId = await getAppId(context);
110
+ expect(appId).toBe('testAppId');
111
+ });
112
+ it('can getEnvName', () => {
113
+ const envName = getEnvName(context);
114
+ expect(envName).toBe('testEnvName');
115
+ });
116
+ it('can resolveAppId', async () => {
117
+ context.amplify.invokePluginMethod = () => 'testAppId';
118
+ const appId = await resolveAppId(context);
119
+ expect(appId).toBe('testAppId');
120
+ });
121
+ it('can throw on getAppId', async () => {
122
+ context.input.options.appId = null;
123
+ context.amplify.invokePluginMethod = () => null;
124
+ expect(async () => await getAppId(context)).rejects.toThrowError();
125
+ });
126
+ it('can generate ui builder components', async () => {
127
+ createUiBuilderComponentDependency_mock.createUiBuilderComponent = jest.fn().mockImplementation(() => ({}));
128
+ const components = generateUiBuilderComponents(context, [{}, {}]);
129
+ expect(components.every(component => component.resultType === 'SUCCESS')).toBeTruthy();
130
+ });
131
+ it('can handle failed generation generate ui builder components', async () => {
132
+ createUiBuilderComponentDependency_mock.createUiBuilderComponent = jest.fn().mockImplementation(() => {
133
+ throw new Error('ahh!');
134
+ });
135
+ const components = generateUiBuilderComponents(context, [{}, {}]);
136
+ expect(components.every(component => component.resultType === 'FAILURE')).toBeTruthy();
137
+ });
138
+ it('can generate ui builder themes', async () => {
139
+ createUiBuilderComponentDependency_mock.createUiBuilderTheme = jest.fn().mockImplementation(() => ({}));
140
+ const themes = generateUiBuilderThemes(context, [{}, {}]);
141
+ expect(themes.every(theme => theme.resultType === 'SUCCESS')).toBeTruthy();
142
+ });
143
+ it('can handle failed generation generate ui builder themes', async () => {
144
+ createUiBuilderComponentDependency_mock.createUiBuilderTheme = jest.fn().mockImplementation(() => {
145
+ throw new Error('ahh!');
146
+ });
147
+ const themes = generateUiBuilderThemes(context, [{}, {}]);
148
+ expect(themes.every(theme => theme.resultType === 'FAILURE')).toBeTruthy();
149
+ });
150
+ });
@@ -0,0 +1,10 @@
1
+ import { printer } from 'amplify-prompts';
2
+ import { run } from '../commands/version';
3
+ jest.mock('amplify-prompts');
4
+ printer.info = jest.fn();
5
+ describe('can run version script', () => {
6
+ it('runs version script', async () => {
7
+ await run();
8
+ expect(printer.info).toBeCalledTimes(1);
9
+ });
10
+ });
@@ -1,9 +1,10 @@
1
- const aws = require('aws-sdk');
2
- const { extractArgs } = require('./utils/extractArgs');
3
- const { listUiBuilderComponents } = require('./utils/syncAmplifyUiBuilderComponents');
4
- const { printer } = require('amplify-prompts');
1
+ import aws from 'aws-sdk';
2
+ import { extractArgs } from './utils/extractArgs';
3
+ import { listUiBuilderComponents } from './utils/syncAmplifyUiBuilderComponents';
4
+ import { printer } from 'amplify-prompts';
5
+ import { $TSAny, $TSContext } from 'amplify-cli-core';
5
6
 
6
- async function run(context) {
7
+ export async function run(context: $TSContext) {
7
8
  printer.debug('Running create components command in amplify-util-uibuilder');
8
9
  const args = extractArgs(context);
9
10
  const sourceEnvName = args.sourceEnvName ? args.sourceEnvName : context.exeInfo.sourceEnvName;
@@ -11,9 +12,11 @@ async function run(context) {
11
12
 
12
13
  const existingComponents = await listUiBuilderComponents(context, sourceEnvName);
13
14
  if (existingComponents.entities.length === 0) {
14
- `${existingComponents.entities.length} components exist in source env. Skipping creation of local components.`;
15
+ printer.debug(`${existingComponents.entities.length} components exist in source env. Skipping creation of local components.`);
16
+ return;
15
17
  }
16
18
  const existingComponentsNewEnv = await listUiBuilderComponents(context, newEnvName);
19
+
17
20
  if (existingComponentsNewEnv.entities.length > 0) {
18
21
  printer.debug(
19
22
  `${existingComponentsNewEnv.entities.length} components already exist in new env. Skipping creation of local components.`,
@@ -21,15 +24,13 @@ async function run(context) {
21
24
  return;
22
25
  }
23
26
 
24
- const environmentName = args.environmentName ? args.environmentName : context.exeInfo.localEnvInfo.envName;
25
-
26
- const appId = args.appId ? args.appId : context.exeInfo.teamProviderInfo[environmentName].awscloudformation.AmplifyAppId;
27
-
28
- const awsConfigInfo = await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'loadConfigurationForEnv', [
27
+ const environmentName = args.environmentName ?? context.exeInfo.localEnvInfo.envName;
28
+ const appId = args.appId ?? context.exeInfo.teamProviderInfo[environmentName].awscloudformation.AmplifyAppId;
29
+ const awsConfigInfo = (await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'loadConfigurationForEnv', [
29
30
  context,
30
31
  newEnvName,
31
32
  appId,
32
- ]);
33
+ ])) as $TSAny;
33
34
 
34
35
  if (process.env.UI_BUILDER_ENDPOINT) {
35
36
  awsConfigInfo.endpoint = process.env.UI_BUILDER_ENDPOINT;
@@ -40,13 +41,12 @@ async function run(context) {
40
41
  }
41
42
 
42
43
  const amplifyUIBuilder = new aws.AmplifyUIBuilder(awsConfigInfo);
43
-
44
44
  const components = existingComponents.entities;
45
45
  if (!components.length) {
46
46
  printer.debug(`No UIBuilder components found in app ${appId} from env ${sourceEnvName}. Skipping component clone process.`);
47
47
  return;
48
48
  }
49
- const promises = components.map(async component => {
49
+ const promises = components.map(async (component: any) => {
50
50
  return await amplifyUIBuilder
51
51
  .createComponent({
52
52
  appId,
@@ -66,11 +66,7 @@ async function run(context) {
66
66
  });
67
67
  await Promise.all(promises);
68
68
 
69
- console.log(
69
+ printer.info(
70
70
  `Successfully cloned ${components.length} UIBuilder components in app ${appId} from env ${sourceEnvName} to env ${newEnvName}.`,
71
71
  );
72
72
  }
73
-
74
- module.exports = {
75
- run,
76
- };
@@ -1,15 +1,17 @@
1
- const { notifyMissingPackages } = require('./utils/notifyMissingPackages');
2
- const { shouldRenderComponents } = require('./utils/shouldRenderComponents');
3
- const {
1
+ import { notifyMissingPackages } from './utils/notifyMissingPackages';
2
+ import { shouldRenderComponents } from './utils/shouldRenderComponents';
3
+ import {
4
4
  listUiBuilderComponents,
5
5
  listUiBuilderThemes,
6
6
  generateUiBuilderComponents,
7
7
  generateUiBuilderThemes,
8
- } = require('./utils/syncAmplifyUiBuilderComponents');
9
- const { generateAmplifyUiBuilderIndexFile } = require('./utils/createUiBuilderComponent');
10
- const { printer } = require('amplify-prompts');
11
- const ora = require('ora');
12
- async function run(context) {
8
+ } from './utils/syncAmplifyUiBuilderComponents';
9
+ import { generateAmplifyUiBuilderIndexFile } from './utils/createUiBuilderComponent';
10
+ import { printer } from 'amplify-prompts';
11
+ import ora from 'ora';
12
+ import { $TSContext } from 'amplify-cli-core';
13
+
14
+ export async function run(context: $TSContext) {
13
15
  printer.debug('Running generate components command in amplify-util-uibuilder');
14
16
  if (!(await shouldRenderComponents(context))) {
15
17
  return;
@@ -48,7 +50,3 @@ async function run(context) {
48
50
 
49
51
  notifyMissingPackages(context);
50
52
  }
51
-
52
- module.exports = {
53
- run,
54
- };
@@ -0,0 +1,5 @@
1
+ import { printer } from 'amplify-prompts';
2
+
3
+ export async function run() {
4
+ printer.info('Supported commands: generateComponents, cloneComponentsFromEnv');
5
+ }
@@ -1,14 +1,15 @@
1
- const { StudioTemplateRendererManager, StudioTemplateRendererFactory } = require('@aws-amplify/codegen-ui');
2
- const {
1
+ import { StudioTemplateRendererManager, StudioTemplateRendererFactory, StudioComponent } from '@aws-amplify/codegen-ui';
2
+ import {
3
3
  AmplifyRenderer,
4
4
  ReactThemeStudioTemplateRenderer,
5
5
  ReactIndexStudioTemplateRenderer,
6
6
  ModuleKind,
7
7
  ScriptTarget,
8
8
  ScriptKind,
9
- } = require('@aws-amplify/codegen-ui-react');
10
- const { getUiBuilderComponentsPath } = require('./getUiBuilderComponentsPath');
11
- const { printer } = require('amplify-prompts');
9
+ } from '@aws-amplify/codegen-ui-react';
10
+ import { getUiBuilderComponentsPath } from './getUiBuilderComponentsPath';
11
+ import { printer } from 'amplify-prompts';
12
+ import { $TSContext } from 'amplify-cli-core';
12
13
  const config = {
13
14
  module: ModuleKind.ES2020,
14
15
  target: ScriptTarget.ES2020,
@@ -16,9 +17,9 @@ const config = {
16
17
  renderTypeDeclarations: true,
17
18
  };
18
19
 
19
- const createUiBuilderComponent = (context, schema) => {
20
+ export const createUiBuilderComponent = (context: $TSContext, schema: any) => {
20
21
  const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
21
- const rendererFactory = new StudioTemplateRendererFactory(component => new AmplifyRenderer(component, config));
22
+ const rendererFactory = new StudioTemplateRendererFactory((component: StudioComponent) => new AmplifyRenderer(component, config));
22
23
 
23
24
  const outputPathDir = uiBuilderComponentsPath;
24
25
  const outputConfig = {
@@ -31,9 +32,9 @@ const createUiBuilderComponent = (context, schema) => {
31
32
  return schema;
32
33
  };
33
34
 
34
- const createUiBuilderTheme = (context, schema) => {
35
+ export const createUiBuilderTheme = (context: $TSContext, schema: any) => {
35
36
  const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
36
- const rendererFactory = new StudioTemplateRendererFactory(component => new ReactThemeStudioTemplateRenderer(component, config));
37
+ const rendererFactory = new StudioTemplateRendererFactory((component: any) => new ReactThemeStudioTemplateRenderer(component, config));
37
38
 
38
39
  const outputPathDir = uiBuilderComponentsPath;
39
40
  const outputConfig = {
@@ -47,14 +48,15 @@ const createUiBuilderTheme = (context, schema) => {
47
48
  return schema;
48
49
  } catch (e) {
49
50
  printer.debug(e);
50
- printer.debug({ msg: 'Skipping invalid theme with schema', schema: JSON.stringify(schema, null, 2) });
51
+ printer.debug('Skipping invalid theme with schema');
52
+ printer.debug(JSON.stringify(schema, null, 2));
51
53
  throw e;
52
54
  }
53
55
  };
54
56
 
55
- const generateAmplifyUiBuilderIndexFile = (context, schemas) => {
57
+ export const generateAmplifyUiBuilderIndexFile = (context: $TSContext, schemas: any[]) => {
56
58
  const uiBuilderComponentsPath = getUiBuilderComponentsPath(context);
57
- const rendererFactory = new StudioTemplateRendererFactory(component => new ReactIndexStudioTemplateRenderer(component, config));
59
+ const rendererFactory = new StudioTemplateRendererFactory((component: any) => new ReactIndexStudioTemplateRenderer(component, config));
58
60
 
59
61
  const outputPathDir = uiBuilderComponentsPath;
60
62
  const outputConfig = {
@@ -64,16 +66,10 @@ const generateAmplifyUiBuilderIndexFile = (context, schemas) => {
64
66
  const rendererManager = new StudioTemplateRendererManager(rendererFactory, outputConfig);
65
67
 
66
68
  try {
67
- rendererManager.renderSchemaToTemplate(schemas);
69
+ return rendererManager.renderSchemaToTemplate(schemas);
68
70
  } catch (e) {
69
71
  printer.debug(e);
70
- printer.debug({ msg: 'Failed to generate component index file' });
72
+ printer.debug('Failed to generate component index file');
71
73
  throw e;
72
74
  }
73
75
  };
74
-
75
- module.exports = {
76
- createUiBuilderComponent,
77
- createUiBuilderTheme,
78
- generateAmplifyUiBuilderIndexFile,
79
- };
@@ -1,4 +1,6 @@
1
- const extractArgs = context => {
1
+ import { $TSContext } from 'amplify-cli-core';
2
+
3
+ export const extractArgs = (context: $TSContext) => {
2
4
  if (!context.input.options) {
3
5
  context.input.options = {};
4
6
  }
@@ -21,7 +23,3 @@ const extractArgs = context => {
21
23
  newEnvName,
22
24
  };
23
25
  };
24
-
25
- module.exports = {
26
- extractArgs,
27
- };
@@ -1,8 +1,9 @@
1
- const path = require('path');
2
- const fs = require('fs-extra');
3
- const { extractArgs } = require('./extractArgs');
1
+ import path from 'path';
2
+ import fs from 'fs-extra';
3
+ import { extractArgs } from './extractArgs';
4
+ import { $TSContext } from 'amplify-cli-core';
4
5
 
5
- const getUiBuilderComponentsPath = context => {
6
+ export const getUiBuilderComponentsPath = (context: $TSContext) => {
6
7
  const args = extractArgs(context);
7
8
  const srcDir = args.srcDir ? args.srcDir : context.exeInfo.projectConfig.javascript.config.SourceDir;
8
9
  const uiBuilderComponentsPath = path.resolve(path.join('.', srcDir, 'ui-components'));
@@ -13,7 +14,3 @@ const getUiBuilderComponentsPath = context => {
13
14
 
14
15
  return uiBuilderComponentsPath;
15
16
  };
16
-
17
- module.exports = {
18
- getUiBuilderComponentsPath,
19
- };
@@ -0,0 +1,40 @@
1
+ import { pathManager, JSONUtilities, $TSContext, $TSAny } from 'amplify-cli-core';
2
+ import { printer } from 'amplify-prompts';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ import { extractArgs } from './extractArgs';
6
+ const REQUIRED_PACKAGES: { [key: string]: string } = { '@aws-amplify/ui-react': '*', 'aws-amplify': '*' };
7
+
8
+ export const notifyMissingPackages = (context: $TSContext) => {
9
+ const args = extractArgs(context);
10
+ const localEnvFilePath = args.localEnvFilePath ?? pathManager.getLocalEnvFilePath();
11
+ if (!fs.existsSync(localEnvFilePath)) {
12
+ printer.debug('localEnvFilePath could not be determined - skipping dependency notification.');
13
+ return;
14
+ }
15
+ const localEnvJson = JSONUtilities.readJson(localEnvFilePath);
16
+ const packageJsonPath = path.join((localEnvJson as $TSAny).projectPath, 'package.json');
17
+ if (!fs.existsSync(packageJsonPath)) {
18
+ printer.debug('package.json file not found - skipping dependency notification.');
19
+ return;
20
+ }
21
+ const packageJson = JSONUtilities.readJson(packageJsonPath) as { dependencies: { [key: string]: string } };
22
+ Object.keys(REQUIRED_PACKAGES).forEach(packageName => {
23
+ const packageIsInstalled = Object.keys(packageJson.dependencies).includes(packageName);
24
+ if (!packageIsInstalled) {
25
+ printer.warn(`UIBuilder components required "${packageName}" that is not in your package.json. Run \`npm install ${packageName}\``);
26
+ } else if (!packageVersionMatches(REQUIRED_PACKAGES[packageName], packageJson.dependencies[packageName])) {
27
+ printer.warn(
28
+ `UIBuilder components requires version "${REQUIRED_PACKAGES[packageName]}" of "${packageName}". You currently are on version "${packageJson.dependencies[packageName]}".`,
29
+ );
30
+ }
31
+ });
32
+ };
33
+
34
+ const packageVersionMatches = (requiredVersion: string, existingVersion: string) => {
35
+ if (requiredVersion === '*') {
36
+ return true;
37
+ }
38
+
39
+ return requiredVersion === existingVersion;
40
+ };
@@ -1,15 +1,16 @@
1
- const { printer } = require('amplify-prompts');
2
- async function shouldRenderComponents(context) {
1
+ import { $TSContext } from 'amplify-cli-core';
2
+ import { printer } from 'amplify-prompts';
3
+ export async function shouldRenderComponents(context: $TSContext) {
3
4
  if (process.env.FORCE_RENDER) {
4
5
  printer.debug('Forcing component render since environment variable flag is set.');
5
6
  return true;
6
7
  }
7
- if (context.input.options && context.input.options['no-codegen']) {
8
+ if (context?.input?.options?.['no-codegen']) {
8
9
  printer.debug('Not pulling components because --no-codegen flag is set');
9
10
  return false;
10
11
  }
11
12
 
12
- if (!context.exeInfo || !context.exeInfo.projectConfig) {
13
+ if (!context?.exeInfo?.projectConfig) {
13
14
  printer.debug('Not pulling components because there is no projectConfig set for this project');
14
15
  return false;
15
16
  }
@@ -31,7 +32,3 @@ async function shouldRenderComponents(context) {
31
32
 
32
33
  return true;
33
34
  }
34
-
35
- module.exports = {
36
- shouldRenderComponents,
37
- };
@@ -1,16 +1,21 @@
1
- const aws = require('aws-sdk');
2
- const { printer } = require('amplify-prompts');
3
- const { resolveAppId } = require('amplify-provider-awscloudformation');
4
- const { createUiBuilderComponent, createUiBuilderTheme } = require('./createUiBuilderComponent');
5
- const { getUiBuilderComponentsPath } = require('./getUiBuilderComponentsPath');
6
- const { extractArgs } = require('./extractArgs');
7
- const getEnvName = (context, envName) => {
1
+ import aws from 'aws-sdk';
2
+ import { printer } from 'amplify-prompts';
3
+ import { createUiBuilderComponent, createUiBuilderTheme } from './createUiBuilderComponent';
4
+ import { getUiBuilderComponentsPath } from './getUiBuilderComponentsPath';
5
+ import { extractArgs } from './extractArgs';
6
+ import { $TSAny, $TSContext } from 'amplify-cli-core';
7
+ export const getEnvName = (context: $TSContext, envName?: string) => {
8
8
  const args = extractArgs(context);
9
9
  return envName ? envName : args.environmentName ? args.environmentName : context.exeInfo.localEnvInfo.envName;
10
10
  };
11
11
 
12
- const getAppId = (context, environmentName) => {
13
- const appId = extractArgs(context).appId || resolveAppId(context);
12
+ export const resolveAppId = async (context: $TSContext) => {
13
+ return await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'resolveAppId', [context]);
14
+ };
15
+
16
+ export const getAppId = async (context: $TSContext) => {
17
+ const appId = extractArgs(context).appId || (await resolveAppId(context));
18
+
14
19
  if (!appId) {
15
20
  throw new Error(
16
21
  'Unable to sync Studio components since appId could not be determined. This can happen when you hit the soft limit of number of apps that you can have in Amplify console.',
@@ -19,9 +24,9 @@ const getAppId = (context, environmentName) => {
19
24
  return appId;
20
25
  };
21
26
 
22
- async function listUiBuilderComponents(context, envName) {
27
+ export async function listUiBuilderComponents(context: $TSContext, envName?: string) {
23
28
  const environmentName = getEnvName(context, envName);
24
- const appId = getAppId(context, environmentName);
29
+ const appId = await getAppId(context);
25
30
 
26
31
  try {
27
32
  const amplifyUIBuilder = await getAmplifyUIBuilderService(context, environmentName, appId);
@@ -39,9 +44,9 @@ async function listUiBuilderComponents(context, envName) {
39
44
  }
40
45
  }
41
46
 
42
- async function listUiBuilderThemes(context, envName) {
47
+ export async function listUiBuilderThemes(context: $TSContext, envName?: string) {
43
48
  const environmentName = getEnvName(context, envName);
44
- const appId = getAppId(context, environmentName);
49
+ const appId = await getAppId(context);
45
50
 
46
51
  try {
47
52
  const amplifyUIBuilder = await getAmplifyUIBuilderService(context, environmentName, appId);
@@ -59,7 +64,7 @@ async function listUiBuilderThemes(context, envName) {
59
64
  }
60
65
  }
61
66
 
62
- function generateUiBuilderComponents(context, componentSchemas) {
67
+ export function generateUiBuilderComponents(context: $TSContext, componentSchemas: any[]) {
63
68
  const componentResults = componentSchemas.map(schema => {
64
69
  try {
65
70
  const component = createUiBuilderComponent(context, schema);
@@ -79,7 +84,7 @@ function generateUiBuilderComponents(context, componentSchemas) {
79
84
  return componentResults;
80
85
  }
81
86
 
82
- function generateUiBuilderThemes(context, themeSchemas) {
87
+ export function generateUiBuilderThemes(context: $TSContext, themeSchemas: any[]) {
83
88
  const themeResults = themeSchemas.map(schema => {
84
89
  try {
85
90
  const theme = createUiBuilderTheme(context, schema);
@@ -97,12 +102,12 @@ function generateUiBuilderThemes(context, themeSchemas) {
97
102
  return themeResults;
98
103
  }
99
104
 
100
- const getAmplifyUIBuilderService = async (context, environmentName, appId) => {
101
- const awsConfigInfo = await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'loadConfigurationForEnv', [
105
+ export const getAmplifyUIBuilderService = async (context: $TSContext, environmentName: string, appId: string) => {
106
+ const awsConfigInfo = (await context.amplify.invokePluginMethod(context, 'awscloudformation', undefined, 'loadConfigurationForEnv', [
102
107
  context,
103
108
  environmentName,
104
109
  appId,
105
- ]);
110
+ ])) as $TSAny;
106
111
 
107
112
  if (process.env.UI_BUILDER_ENDPOINT) {
108
113
  awsConfigInfo.endpoint = process.env.UI_BUILDER_ENDPOINT;
@@ -114,11 +119,3 @@ const getAmplifyUIBuilderService = async (context, environmentName, appId) => {
114
119
 
115
120
  return new aws.AmplifyUIBuilder(awsConfigInfo);
116
121
  };
117
-
118
- module.exports = {
119
- getAmplifyUIBuilderService,
120
- generateUiBuilderComponents,
121
- generateUiBuilderThemes,
122
- listUiBuilderComponents,
123
- listUiBuilderThemes,
124
- };
@@ -0,0 +1,6 @@
1
+ import { printer } from 'amplify-prompts';
2
+ import path from 'path';
3
+
4
+ export async function run() {
5
+ printer.info((await import(path.join(__dirname, '..', '..', 'package.json'))).version);
6
+ }
@@ -1,6 +1,7 @@
1
- const { printer } = require('amplify-prompts');
1
+ import { $TSContext } from 'amplify-cli-core';
2
+ import { printer } from 'amplify-prompts';
2
3
 
3
- async function run(context) {
4
+ export async function run(context: $TSContext) {
4
5
  try {
5
6
  await context.amplify.invokePluginMethod(context, 'ui-builder', undefined, 'executeAmplifyCommand', [
6
7
  context,
@@ -11,7 +12,3 @@ async function run(context) {
11
12
  printer.debug(e);
12
13
  }
13
14
  }
14
-
15
- module.exports = {
16
- run,
17
- };
@@ -1,6 +1,7 @@
1
- const { printer } = require('amplify-prompts');
1
+ import { $TSContext } from 'amplify-cli-core';
2
+ import { printer } from 'amplify-prompts';
2
3
 
3
- async function run(context) {
4
+ export async function run(context: $TSContext) {
4
5
  try {
5
6
  await context.amplify.invokePluginMethod(context, 'ui-builder', undefined, 'executeAmplifyCommand', [context, 'generateComponents']);
6
7
  } catch (e) {
@@ -8,7 +9,3 @@ async function run(context) {
8
9
  printer.debug(e);
9
10
  }
10
11
  }
11
-
12
- module.exports = {
13
- run,
14
- };
@@ -1,20 +1,16 @@
1
- const path = require('path');
1
+ import { $TSContext } from 'amplify-cli-core';
2
+ import path from 'path';
2
3
 
3
- async function executeAmplifyCommand(context, cmd) {
4
+ export async function executeAmplifyCommand(context: $TSContext, cmd: string) {
4
5
  const commandsDirPath = path.normalize(path.join(__dirname, 'commands'));
5
6
  const commandPath = path.join(commandsDirPath, cmd ? cmd : context.input.command);
6
7
  const commandModule = require(commandPath);
7
8
  await commandModule.run(context);
8
9
  }
9
10
 
10
- async function handleAmplifyEvent(context, args) {
11
+ export async function handleAmplifyEvent(context: $TSContext, { event }: { event: string }) {
11
12
  const eventHandlersDirPath = path.normalize(path.join(__dirname, 'event-handlers'));
12
- const eventHandlerPath = path.join(eventHandlersDirPath, `handle-${args.event}`);
13
+ const eventHandlerPath = path.join(eventHandlersDirPath, `handle-${event}`);
13
14
  const eventHandlerModule = require(eventHandlerPath);
14
- await eventHandlerModule.run(context, args);
15
+ await eventHandlerModule.run(context);
15
16
  }
16
-
17
- module.exports = {
18
- executeAmplifyCommand,
19
- handleAmplifyEvent,
20
- };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib",
5
+ "rootDir": "./src",
6
+ "strict": true,
7
+ "skipLibCheck": true
8
+ }
9
+ }