-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render form utils and add forms to index file (#536)
* fix: remove locale dependency on date formatting * feat: add utils file renderer Co-authored-by: Kevin Pranoto <kpranoto@amazon.com> Co-authored-by: Justin Shih <jushih@amazon.com>
- Loading branch information
1 parent
5978feb
commit 9ceb6bc
Showing
16 changed files
with
1,647 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/codegen-ui-react/lib/react-utils-studio-template-renderer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"). | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
import { EOL } from 'os'; | ||
import ts, { EmitHint } from 'typescript'; | ||
import { StudioTemplateRenderer } from '@aws-amplify/codegen-ui'; | ||
import { ReactRenderConfig, scriptKindToFileExtensionNonReact } from './react-render-config'; | ||
import { ImportCollection } from './imports'; | ||
import { ReactOutputManager } from './react-output-manager'; | ||
import { RequiredKeys } from './utils/type-utils'; | ||
import { transpile, buildPrinter, defaultRenderConfig } from './react-studio-template-renderer-helper'; | ||
import { generateValidationFunction } from './utils/forms/validation'; | ||
|
||
export class ReactUtilsStudioTemplateRenderer extends StudioTemplateRenderer< | ||
string, | ||
string[], | ||
ReactOutputManager, | ||
{ | ||
componentText: string; | ||
renderComponentToFilesystem: (outputPath: string) => Promise<void>; | ||
} | ||
> { | ||
protected importCollection = new ImportCollection(); | ||
|
||
protected renderConfig: RequiredKeys<ReactRenderConfig, keyof typeof defaultRenderConfig>; | ||
|
||
fileName: string; | ||
|
||
/* | ||
* list of util functions to generate | ||
*/ | ||
utils: string[]; | ||
|
||
constructor(utils: string[], renderConfig: ReactRenderConfig) { | ||
super(utils, new ReactOutputManager(), renderConfig); | ||
this.utils = utils; | ||
this.renderConfig = { | ||
...defaultRenderConfig, | ||
...renderConfig, | ||
renderTypeDeclarations: false, // Never render type declarations for index.js|ts file. | ||
}; | ||
this.fileName = `utils.${scriptKindToFileExtensionNonReact(this.renderConfig.script)}`; | ||
} | ||
|
||
renderComponentInternal() { | ||
const { printer, file } = buildPrinter(this.fileName, this.renderConfig); | ||
const utilsStatements: (ts.TypeAliasDeclaration | ts.VariableStatement)[] = []; | ||
|
||
this.utils.forEach((util) => { | ||
if (util === 'validation') { | ||
utilsStatements.push(...generateValidationFunction()); | ||
} | ||
}); | ||
|
||
const { componentText } = transpile( | ||
utilsStatements.map((util) => printer.printNode(EmitHint.Unspecified, util, file)).join(EOL), | ||
this.renderConfig, | ||
); | ||
|
||
return { | ||
componentText, | ||
renderComponentToFilesystem: async (outputPath: string) => { | ||
await this.renderComponentToFilesystem(componentText)(this.fileName)(outputPath); | ||
}, | ||
}; | ||
} | ||
|
||
// no-op | ||
validateSchema() {} | ||
} |
Oops, something went wrong.