Skip to content

Commit

Permalink
fix: yaml tags parsing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrajabi committed Dec 2, 2023
1 parent 2481a77 commit 494b4a4
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 8 deletions.
Binary file modified assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@semantic-release/release-notes-generator": "^12.1.0",
"commander": "^11.1.0",
"js-yaml": "^4.1.0",
"js-yaml-cloudformation-schema": "^1.0.0",
"playwright": "^1.40.1"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions src/reader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'node:fs';
import yaml from 'js-yaml';
import * as yaml from 'js-yaml';
import {CLOUDFORMATION_SCHEMA} from 'js-yaml-cloudformation-schema';

const acceptedExtensions = ['.json', '.yaml', '.yml'];

Expand Down Expand Up @@ -42,7 +43,7 @@ export const readTemplate = async (path: string) => {
case '.yaml':
case '.yml':
try {
yaml.load(fileContent);
yaml.load(fileContent, {schema: CLOUDFORMATION_SCHEMA});
} catch (e) {
throw new Error(`Invalid YAML file: ${e}`);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
197 changes: 197 additions & 0 deletions test/fixtures/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
Transform: AWS::Serverless-2016-10-31
Resources:
Api:
Type: AWS::Serverless::Api
Properties:
Name: !Sub
- ${ResourceName} From Stack ${AWS::StackName}
- ResourceName: Api
StageName: Prod
DefinitionBody:
openapi: '3.0'
info: {}
paths:
/items:
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ListItems.Arn}/invocations
responses: {}
post:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${CreateItem.Arn}/invocations
responses: {}
/items/{id}:
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetItem.Arn}/invocations
responses: {}
put:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UpdateItem.Arn}/invocations
responses: {}
delete:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DeleteItem.Arn}/invocations
responses: {}
EndpointConfiguration: REGIONAL
TracingEnabled: true
Items:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
ListItems:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-ListItems
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: ListItems
CodeUri: src/ListItems
Handler: index.handler
Runtime: nodejs18.x
MemorySize: 1024
Timeout: 30
Tracing: Active
Events:
ApiGETitems:
Type: Api
Properties:
Path: /items
Method: GET
RestApiId: !Ref Api
Environment:
Variables:
ITEMS_TABLE_NAME: !Ref Items
Policies:
- DynamoDBReadPolicy:
TableName: !Ref Items
CreateItem:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-CreateItem
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: CreateItem
CodeUri: src/CreateItem
Handler: index.handler
Runtime: nodejs18.x
MemorySize: 1024
Timeout: 30
Tracing: Active
Events:
ApiPOSTitems:
Type: Api
Properties:
Path: /items
Method: POST
RestApiId: !Ref Api
Environment:
Variables:
ITEMS_TABLE_NAME: !Ref Items
Policies:
- DynamoDBWritePolicy:
TableName: !Ref Items
GetItem:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GetItem
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: GetItem
CodeUri: src/GetItem
Handler: index.handler
Runtime: nodejs18.x
MemorySize: 1024
Timeout: 30
Tracing: Active
Events:
ApiGETitemsid:
Type: Api
Properties:
Path: /items/{id}
Method: GET
RestApiId: !Ref Api
Environment:
Variables:
ITEMS_TABLE_NAME: !Ref Items
Policies:
- DynamoDBReadPolicy:
TableName: !Ref Items
UpdateItem:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-UpdateItem
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: UpdateItem
CodeUri: src/UpdateItem
Handler: index.handler
Runtime: nodejs18.x
MemorySize: 1024
Timeout: 30
Tracing: Active
Events:
ApiPUTitemsid:
Type: Api
Properties:
Path: /items/{id}
Method: PUT
RestApiId: !Ref Api
Environment:
Variables:
ITEMS_TABLE_NAME: !Ref Items
Policies:
- DynamoDBWritePolicy:
TableName: !Ref Items
DeleteItem:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-DeleteItem
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName}
- ResourceName: DeleteItem
CodeUri: src/DeleteItem
Handler: index.handler
Runtime: nodejs18.x
MemorySize: 1024
Timeout: 30
Tracing: Active
Events:
ApiDELETEitemsid:
Type: Api
Properties:
Path: /items/{id}
Method: DELETE
RestApiId: !Ref Api
Environment:
Variables:
ITEMS_TABLE_NAME: !Ref Items
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref Items
Metadata:
AWS::Composer::Groups:
Group:
Label: API Compute
Members:
- ListItems
- CreateItem
- GetItem
- UpdateItem
- DeleteItem
18 changes: 14 additions & 4 deletions test/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import {toMatchImageSnapshot} from 'jest-image-snapshot';
expect.extend({toMatchImageSnapshot});

describe('Renderer', () => {
const inputPath = path.resolve(__dirname, './fixtures', 'template.json');
const imagePath = path.resolve(__dirname, './.out', 'template.png');
const inputPathJson = path.resolve(__dirname, './fixtures', 'template.json');
const inputPathYaml = path.resolve(__dirname, './fixtures', 'template.yaml');
const imagePath = path.resolve(__dirname, './../.out', 'template.png');

// Regression
it('should produce the same output image', async () => {
it('should produce the same output image for a given json template', async () => {
await renderTemplate({
inputPath: inputPath,
inputPath: inputPathJson,
outputPath: imagePath,
});

const image = fs.readFileSync(imagePath);
expect(image).toMatchImageSnapshot();
});
it('should produce the same output image for a given yaml template', async () => {
await renderTemplate({
inputPath: inputPathYaml,
outputPath: imagePath,
});

Expand Down

0 comments on commit 494b4a4

Please sign in to comment.