Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add SAM Globals parsing #43

Merged
merged 4 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/cloudformation-sam-types/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../../.eslintrc", "rules": {}, "ignorePatterns": ["!**/*"] }
7 changes: 7 additions & 0 deletions libs/cloudformation-sam-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# cloudformation-sam-types

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `ng test cloudformation-sam-types` to execute the unit tests via [Jest](https://jestjs.io).
9 changes: 9 additions & 0 deletions libs/cloudformation-sam-types/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: 'cloudformation-sam-types',
preset: '../../jest.config.js',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
coverageDirectory: '../../coverage/libs/cloudformation-sam-types',
};
13 changes: 13 additions & 0 deletions libs/cloudformation-sam-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@nx-aws/cloudformation-sam-types",
"description": "Additional types for @nx-aws",
"keywords": [
"AWS",
"Serverless",
"Angular CLI",
"Workspace",
"Nx",
"Monorepo"
],
"main": "index.js"
}
1 change: 1 addition & 0 deletions libs/cloudformation-sam-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/globals';
24 changes: 24 additions & 0 deletions libs/cloudformation-sam-types/src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type GlobalsTemplate = {Globals?: Globals};
export type Globals = IGlobals;

/**
* Partial list of supported properties for the Globals property
* @since 0.7.0
*/
interface IGlobals {
Function?: IFunction;
}

/**
* Partial list of supported values the Function attribute of the Globals property
*
* @since 0.7.0
*/
interface IFunction {
Handler?: string;
CodeUri?: string | {
Bucket: string
Key: string
Version: string
};
}
7 changes: 7 additions & 0 deletions libs/cloudformation-sam-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": ["**/*.ts"]
}
12 changes: 12 additions & 0 deletions libs/cloudformation-sam-types/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"rootDir": "./src",
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
15 changes: 15 additions & 0 deletions libs/cloudformation-sam-types/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
22 changes: 15 additions & 7 deletions libs/sam/src/build/get-entries-from-cloudformation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
Globals,
} from '@nx-aws/cloudformation-sam-types';
import Resource from 'cloudform-types/types/resource';
import { resolve, parse, join, relative } from 'path';
import { loadCloudFormationTemplate } from '../utils/load-cloud-formation-template';
import { loadCloudFormationTemplate } from '@nx-aws/sam';
import { ExtendedBuildBuilderOptions } from './build';
import { isFile } from '@angular-devkit/core/node/fs';
import { readFileSync, writeFileSync } from 'fs';
Expand All @@ -16,21 +19,23 @@ export function getEntriesFromCloudFormation(
context: BuilderContext
): Array<Entry> {
const cf = loadCloudFormationTemplate(options.template);
const globals = cf.Globals;
const resources = cf.Resources;
if (!resources) {
throw new Error("CloudFormation template didn't contain any resources");
}
return Object.keys(resources)
.map(name => {
return getEntry(resources[name], options, context);
return getEntry(resources[name], options, context, globals);
})
.filter((s): s is Entry => !!s);
}

function getEntry(
resource: Resource,
options: ExtendedBuildBuilderOptions,
context: BuilderContext
context: BuilderContext,
globalProperties?: Globals,
): Entry | undefined {
const properties = resource.Properties;
if (!properties) {
Expand All @@ -42,7 +47,7 @@ function getEntry(

const srcMapInstall = resolve(__dirname, 'source-map-install.js');
if (resource.Type === 'AWS::Serverless::Function') {
return getEntryForFunction(properties, options, srcMapInstall);
return getEntryForFunction(properties, options, srcMapInstall, globalProperties?.Function);
} else if (resource.Type === 'AWS::Serverless::LayerVersion') {
return getEntryForLayer(properties, options, context, srcMapInstall);
} else {
Expand All @@ -52,11 +57,14 @@ function getEntry(
function getEntryForFunction(
properties: { [key: string]: any },
options: ExtendedBuildBuilderOptions,
srcMapInstall: string
srcMapInstall: string,
globalProperties?: { [key: string]: any },
) {
const { dir } = parse(options.template);
const codeUri: string = properties.CodeUri;
const handler: string = properties.Handler;
// fallback to global CodeUri if function doesn't specify one
const codeUri: string = properties.CodeUri || globalProperties?.CodeUri;
// fallback to global Handler if function doesn't specify one
const handler: string = properties.Handler || globalProperties?.Handler;
const handlerParts = handler.split('.');
handlerParts.pop();
const fileName = [...handlerParts, 'ts'].join('.');
Expand Down
9 changes: 6 additions & 3 deletions libs/sam/src/utils/load-cloud-formation-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { readFileSync } from 'fs';
import { load } from 'js-yaml';
import { CLOUDFORMATION_SCHEMA } from 'cloudformation-js-yaml-schema';
import Template from 'cloudform-types/types/template';
import {
GlobalsTemplate,
} from '@nx-aws/cloudformation-sam-types';

export function loadCloudFormationTemplate(templatePath: string): Template {
export function loadCloudFormationTemplate(templatePath: string): Template & GlobalsTemplate {
const yaml = readFileSync(templatePath, { encoding: 'utf-8' });
const cf: Template = load(yaml, {
schema: CLOUDFORMATION_SCHEMA
const cf: Template & GlobalsTemplate = load(yaml, {
schema: CLOUDFORMATION_SCHEMA,
});
return cf;
}
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"s3": {
"tags": []
},
"cloudformation-sam-types":{
"tags": []
}
}
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nx-aws/sam",
"version": "0.6.2",
"version": "0.7.0",
"description": "AWS SAM plugin for nx",
"keywords": [
"AWS",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"paths": {
"@nx-aws/sam": ["libs/sam/src/index.ts"],
"@nx-aws/core": ["libs/core/src/index.ts"],
"@nx-aws/s3": ["libs/s3/src/index.ts"]
"@nx-aws/s3": ["libs/s3/src/index.ts"],
"@nx-aws/cloudformation-sam-types": ["libs/cloudformation-sam-types/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down
66 changes: 66 additions & 0 deletions workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,72 @@
}
}
}
},
"cloudformation-sam-types": {
"root": "libs/cloudformation-sam-types",
"sourceRoot": "libs/cloudformation-sam-types/src",
"projectType": "library",
"schematics": {},
"architect": {
"lint": {
"builder": "@nrwl/linter:lint",
"options": {
"linter": "eslint",
"config": "libs/cloudformation-sam-types/.eslintrc",
"tsConfig": [
"libs/cloudformation-sam-types/tsconfig.lib.json",
"libs/cloudformation-sam-types/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**", "!libs/cloudformation-sam-types/**"]
}
},
"test": {
"builder": "@nrwl/jest:jest",
"options": {
"jestConfig": "libs/cloudformation-sam-types/jest.config.js",
"tsConfig": "libs/cloudformation-sam-types/tsconfig.spec.json",
"passWithNoTests": true
}
},
"build": {
"builder": "@nrwl/node:package",
"options": {
"outputPath": "dist/libs/cloudformation-sam-types",
"tsConfig": "libs/cloudformation-sam-types/tsconfig.lib.json",
"packageJson": "libs/cloudformation-sam-types/package.json",
"main": "libs/cloudformation-sam-types/src/index.ts",
"assets": ["libs/cloudformation-sam-types/*.md"]
}
},
"publish": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"parallel": false,
"commands": [
{
"command": "npx monorepo-package-tool --rootModuleDir libs/cloudformation-sam-types --destDir dist/libs/cloudformation-sam-types"
},
{
"command": "npm publish dist/libs/cloudformation-sam-types --access public"
}
]
}
},
"pack": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"parallel": false,
"commands": [
{
"command": "npx monorepo-package-tool --rootModuleDir libs/cloudformation-sam-types --destDir dist/libs/cloudformation-sam-types"
},
{
"command": "npm pack dist/libs/cloudformation-sam-types"
}
]
}
}
}
}
},
"cli": {
Expand Down