Skip to content

style(layers): apply standardized formatting #1453

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

Merged
merged 2 commits into from
May 11, 2023
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
76 changes: 40 additions & 36 deletions layers/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,67 @@ module.exports = {
jest: true,
node: true,
},
extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
plugins: ['@typescript-eslint', 'prettier'],
settings: {
'import/resolver': {
node: {},
typescript: {
project: './tsconfig.es.json',
project: './tsconfig.json',
alwaysTryTypes: true,
},
},
},
rules: {
'@typescript-eslint/ban-ts-ignore': ['off'],
'@typescript-eslint/camelcase': ['off'],
'@typescript-eslint/explicit-function-return-type': [ 'error', { allowExpressions: true } ],
'@typescript-eslint/explicit-member-accessibility': 'error',
'@typescript-eslint/indent': [ 'error', 2, { SwitchCase: 1 } ],
'@typescript-eslint/interface-name-prefix': ['off'],
'@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'none' } } ],
'@typescript-eslint/explicit-function-return-type': [
'error',
{ allowExpressions: true },
], // Enforce return type definitions for functions
'@typescript-eslint/explicit-member-accessibility': 'error', // Enforce explicit accessibility modifiers on class properties and methods (public, private, protected)
'@typescript-eslint/member-ordering': [
// Standardize the order of class members
'error',
{
default: {
memberTypes: [
'signature',
'public-field', // = ["public-static-field", "public-instance-field"]
'protected-field', // = ["protected-static-field", "protected-instance-field"]
'private-field', // = ["private-static-field", "private-instance-field"]
'public-field',
'protected-field',
'private-field',
'constructor',
'public-method', // = ["public-static-method", "public-instance-method"]
'protected-method', // = ["protected-static-method", "protected-instance-method"]
'private-method', // = ["private-static-method", "private-instance-method"]
'public-method',
'protected-method',
'private-method',
],
order: 'alphabetically',
},
},
],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-inferrable-types': ['off'],
'@typescript-eslint/no-unused-vars': [ 'error', { argsIgnorePattern: '^_' } ],
'@typescript-eslint/no-use-before-define': ['off'],
'@typescript-eslint/semi': [ 'error', 'always' ],
'array-bracket-spacing': [ 'error', 'always', { singleValue: false } ],
'arrow-body-style': [ 'error', 'as-needed' ],
'computed-property-spacing': [ 'error', 'never' ],
'func-style': [ 'warn', 'expression' ],
indent: [ 'error', 2, { SwitchCase: 1 } ],
'keyword-spacing': 'error',
'newline-before-return': 2,
'no-console': 0,
'no-multi-spaces': [ 'error', { ignoreEOLComments: false } ],
'no-multiple-empty-lines': [ 'error', { max: 1, maxBOF: 0 } ],
'no-throw-literal': 'error',
'object-curly-spacing': [ 'error', 'always' ],
'prefer-arrow-callback': 'error',
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
semi: [ 'error', 'always' ]
'@typescript-eslint/no-explicit-any': 'error', // Disallow usage of the any type
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], // Disallow unused variables, except for variables starting with an underscore
'@typescript-eslint/no-use-before-define': ['off'], // Check if this rule is needed
'no-unused-vars': 'off', // Disable eslint core rule, since it's replaced by @typescript-eslint/no-unused-vars
// Rules from eslint core https://eslint.org/docs/latest/rules/
'array-bracket-spacing': ['error', 'never'], // Disallow spaces inside of array brackets
'computed-property-spacing': ['error', 'never'], // Disallow spaces inside of computed properties
'func-style': ['warn', 'expression'], // Enforce function expressions instead of function declarations
'keyword-spacing': 'error', // Enforce spaces after keywords and before parenthesis, e.g. if (condition) instead of if(condition)
'padding-line-between-statements': [
// Require an empty line before return statements
'error',
{ blankLine: 'always', prev: '*', next: 'return' },
],
'no-console': 0, // Allow console.log statements
'no-multi-spaces': ['error', { ignoreEOLComments: false }], // Disallow multiple spaces except for comments
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 0 }], // Enforce no empty line at the beginning & end of files and max 1 empty line between consecutive statements
'no-throw-literal': 'error', // Disallow throwing literals as exceptions, e.g. throw 'error' instead of throw new Error('error')
'object-curly-spacing': ['error', 'always'], // Enforce spaces inside of curly braces in objects
'prefer-arrow-callback': 'error', // Enforce arrow functions instead of anonymous functions for callbacks
quotes: ['error', 'single', { allowTemplateLiterals: true }], // Enforce single quotes except for template strings
semi: ['error', 'always'], // Require semicolons instead of ASI (automatic semicolon insertion) at the end of statements
},
};
2 changes: 1 addition & 1 deletion layers/bin/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ new LayerPublisherStack(app, 'LayerPublisherStack', {
powertoolsPackageVersion: app.node.tryGetContext('PowertoolsPackageVersion'),
layerName: 'AWSLambdaPowertoolsTypeScript',
ssmParameterLayerArn: SSM_PARAM_LAYER_ARN,
});
});
49 changes: 16 additions & 33 deletions layers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,21 @@ module.exports = {
transform: {
'^.+\\.ts?$': 'ts-jest',
},
moduleFileExtensions: [ 'js', 'ts' ],
'collectCoverageFrom': [
'**/src/**/*.ts',
'!**/node_modules/**',
],
'testMatch': ['**/?(*.)+(spec|test).ts'],
'roots': [
'<rootDir>/src',
'<rootDir>/tests',
],
'testPathIgnorePatterns': [
'/node_modules/',
],
'testEnvironment': 'node',
'coveragePathIgnorePatterns': [
'/node_modules/',
'/types/',
],
'coverageThreshold': {
'global': {
'statements': 100,
'branches': 100,
'functions': 100,
'lines': 100,
moduleFileExtensions: ['js', 'ts'],
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],
testMatch: ['**/?(*.)+(spec|test).ts'],
roots: ['<rootDir>/src', '<rootDir>/tests'],
testPathIgnorePatterns: ['/node_modules/'],
testEnvironment: 'node',
coveragePathIgnorePatterns: ['/node_modules/', '/types/'],
coverageThreshold: {
global: {
statements: 100,
branches: 100,
functions: 100,
lines: 100,
},
},
'coverageReporters': [
'json-summary',
'text',
'lcov'
],
'setupFiles': [
'<rootDir>/tests/helpers/populateEnvironmentVariables.ts'
]
};
coverageReporters: ['json-summary', 'text', 'lcov'],
setupFiles: ['<rootDir>/tests/helpers/populateEnvironmentVariables.ts'],
};
9 changes: 5 additions & 4 deletions layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"test": "echo 'Not applicable'",
"cdk": "cdk",
"package": "echo 'Not applicable'",
"lint": "eslint --ext .ts --no-error-on-unmatched-pattern src tests",
"lint-fix": "eslint --fix --ext .ts --fix --no-error-on-unmatched-pattern src tests",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --fix --no-error-on-unmatched-pattern .",
"test:unit": "jest --group=unit",
"test:e2e": "RUNTIME=nodejs14x jest --group=e2e"
},
"lint-staged": {
"*.ts": "npm run lint-fix"
"*.ts": "npm run lint-fix",
"*.js": "npm run lint-fix"
},
"repository": {
"type": "git",
Expand All @@ -36,4 +37,4 @@
"devDependencies": {
"source-map-support": "^0.5.21"
}
}
}
39 changes: 22 additions & 17 deletions layers/src/layer-publisher-stack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
CfnOutput,
RemovalPolicy,
Stack,
StackProps
} from 'aws-cdk-lib';
import { CfnOutput, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {
LayerVersion,
Expand All @@ -14,39 +9,49 @@ import {
import { StringParameter } from 'aws-cdk-lib/aws-ssm';

export interface LayerPublisherStackProps extends StackProps {
readonly layerName?: string
readonly powertoolsPackageVersion?: string
readonly ssmParameterLayerArn: string
readonly layerName?: string;
readonly powertoolsPackageVersion?: string;
readonly ssmParameterLayerArn: string;
}

export class LayerPublisherStack extends Stack {
public readonly lambdaLayerVersion: LayerVersion;
public constructor(scope: Construct, id: string, props: LayerPublisherStackProps) {
public constructor(
scope: Construct,
id: string,
props: LayerPublisherStackProps
) {
super(scope, id, props);

const { layerName, powertoolsPackageVersion } = props;

console.log(`publishing layer ${layerName} version : ${powertoolsPackageVersion}`);
console.log(
`publishing layer ${layerName} version : ${powertoolsPackageVersion}`
);

this.lambdaLayerVersion = new LayerVersion(this, 'LambdaPowertoolsLayer', {
layerVersionName: props?.layerName,
description: `AWS Lambda Powertools for TypeScript version ${powertoolsPackageVersion}`,
compatibleRuntimes: [
Runtime.NODEJS_14_X,
Runtime.NODEJS_16_X,
Runtime.NODEJS_18_X
Runtime.NODEJS_18_X,
],
license: 'MIT-0',
// This is needed because the following regions do not support the compatibleArchitectures property #1400
// ...(![ 'eu-south-2', 'eu-central-2', 'ap-southeast-4' ].includes(Stack.of(this).region) ? { compatibleArchitectures: [Architecture.X86_64] } : {}),
code: Code.fromAsset('../tmp'),
});

const layerPermission = new CfnLayerVersionPermission(this, 'PublicLayerAccess', {
action: 'lambda:GetLayerVersion',
layerVersionArn: this.lambdaLayerVersion.layerVersionArn,
principal: '*',
});
const layerPermission = new CfnLayerVersionPermission(
this,
'PublicLayerAccess',
{
action: 'lambda:GetLayerVersion',
layerVersionArn: this.lambdaLayerVersion.layerVersionArn,
principal: '*',
}
);

layerPermission.applyRemovalPolicy(RemovalPolicy.RETAIN);
this.lambdaLayerVersion.applyRemovalPolicy(RemovalPolicy.RETAIN);
Expand Down
2 changes: 1 addition & 1 deletion layers/tests/e2e/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export const RESOURCE_NAME_PREFIX = 'Layers-E2E';
export const ONE_MINUTE = 60 * 1000;
export const TEST_CASE_TIMEOUT = 3 * ONE_MINUTE;
export const SETUP_TIMEOUT = 5 * ONE_MINUTE;
export const TEARDOWN_TIMEOUT = 5 * ONE_MINUTE;
export const TEARDOWN_TIMEOUT = 5 * ONE_MINUTE;
17 changes: 9 additions & 8 deletions layers/tests/e2e/layerPublisher.class.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import { Metrics } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';

const logger = new Logger({
logLevel: 'DEBUG'
logLevel: 'DEBUG',
});
const metrics = new Metrics();
const tracer = new Tracer();

export const handler = (): void => {

// Check that the packages version matches the expected one
try {
const packageJSON = JSON.parse(
readFileSync('/opt/nodejs/node_modules/@aws-lambda-powertools/logger/package.json', {
encoding: 'utf8',
flag: 'r',
})
readFileSync(
'/opt/nodejs/node_modules/@aws-lambda-powertools/logger/package.json',
{
encoding: 'utf8',
flag: 'r',
}
)
);

if (packageJSON.version != process.env.POWERTOOLS_PACKAGE_VERSION) {
Expand All @@ -42,5 +44,4 @@ export const handler = (): void => {
tracer.annotateColdStart();
handlerSegment.close();
tracer.setSegment(segment);

};
};
Loading