Skip to content

Commit dfa963d

Browse files
author
Alice
authored
Merge pull request #46 from beforeyoubid/feature/serverless-3
Feature/serverless 3
2 parents 4a1b9d2 + 0a4f932 commit dfa963d

File tree

11 files changed

+552
-2040
lines changed

11 files changed

+552
-2040
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- run: mkdir -p ./reports/jest/ ; true
3131
- run:
3232
name: Tests
33-
command: yarn test --reporters=jest-junit
33+
command: yarn test --ci --reporters=jest-junit --reporters=default
3434
environment:
3535
JEST_JUNIT_OUTPUT_DIR: ./reports/jest/
3636
- store_test_results:

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616
branches: [ main ]
1717
pull_request:
1818
# The branches below must be a subset of the branches above
19-
branches: [ main ]
19+
branches: [main, '*']
2020
schedule:
2121
- cron: '40 12 * * 2'
2222

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beforeyoubid/serverless-step-functions-offline",
3-
"version": "2.5.0",
3+
"version": "3.0.0-alpha.0",
44
"description": "Serverlesss plugin to support step function offline",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -33,7 +33,7 @@
3333
"@babel/preset-typescript": "7.17.12",
3434
"@types/jest": "28.1.1",
3535
"@types/node": "17.0.40",
36-
"@types/serverless": "1.78.33",
36+
"@types/serverless": "3.12.7",
3737
"@typescript-eslint/eslint-plugin": "5.27.0",
3838
"@typescript-eslint/parser": "5.27.0",
3939
"asl-types": "^1.2.1",
@@ -45,7 +45,7 @@
4545
"jest-junit": "13.2.0",
4646
"pre-commit": "^1.1.3",
4747
"prettier": "2.6.2",
48-
"serverless": "2.53.1",
48+
"serverless": "3.19.0",
4949
"ts-node": "10.8.1",
5050
"typescript": "4.7.3"
5151
},

src/__tests__/build.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* istanbul ignore file */
22
import setup from './setup';
3+
import logging from './logging';
34

45
const { stepFunctionsOfflinePlugin, StepFunctionsOfflinePlugin, serverless } = setup();
56

@@ -15,16 +16,19 @@ describe('build.js', () => {
1516
console.log(res);
1617
expect(res).toBeUndefined();
1718
} catch (err) {
18-
console.log(err);
19-
expect(err.message).toEqual('Function "FirstLambda" does not presented in serverless manifest');
19+
expect((err as Error).message).toEqual('Function "FirstLambda" does not presented in serverless manifest');
2020
}
2121
});
2222

2323
it('should not throw err', async () => {
24-
const SFOP = new StepFunctionsOfflinePlugin(serverless, {
25-
...global['options'],
26-
location: '/src/__tests__',
27-
});
24+
const SFOP = new StepFunctionsOfflinePlugin(
25+
serverless,
26+
{
27+
...global['options'],
28+
location: '/src/__tests__',
29+
},
30+
logging
31+
);
2832
SFOP.variables = { FirstLambda: 'firstLambda' };
2933
SFOP._getLocation();
3034
await SFOP.hooks[global['hooks'].findState]();

src/__tests__/index.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging from './logging';
12
import setup from './setup';
23

34
const { stepFunctionsOfflinePlugin, StepFunctionsOfflinePlugin, serverless } = setup();
@@ -35,12 +36,12 @@ describe('index.js', () => {
3536
it('should throw err - unsupportable serverless version', () => {
3637
const version = '1.5';
3738
stepFunctionsOfflinePlugin.serverless.version = version;
38-
const error = `Serverless step offline requires Serverless v2.x.x but found ${version}`;
39+
const error = `Serverless Step Functions Offline requires Serverless v3.x.x but found ${version}`;
3940
expect(stepFunctionsOfflinePlugin.hooks[global['hooks'].start]).toThrowError(error);
4041
});
4142

4243
it('should be acceptable serverless version', () => {
43-
const version = '2.0';
44+
const version = '3.0';
4445
stepFunctionsOfflinePlugin.serverless.version = version;
4546
expect(stepFunctionsOfflinePlugin.hooks[global['hooks'].start]).not.toThrowError();
4647
});
@@ -65,14 +66,16 @@ describe('index.js', () => {
6566
});
6667

6768
it('should apply event file ', async () => {
68-
const SFOP = new StepFunctionsOfflinePlugin(serverless, { e: 'src/__tests__/eventFile.json' });
69+
const SFOP = new StepFunctionsOfflinePlugin(serverless, { e: 'src/__tests__/eventFile.json' }, logging);
6970
await SFOP.hooks[global['hooks'].loadEventFile]();
7071
expect(SFOP.loadedEventFile).toMatchObject({ foo: 1, bar: 2 });
7172
});
7273

7374
it('should throw error - incorrect path to event file ', () => {
74-
const SFOP = new StepFunctionsOfflinePlugin(serverless, { event: '../__tests__/eventFile2.json' });
75-
expect(SFOP.hooks[global['hooks'].loadEventFile]).rejects.toEqual({ error: /Cannot find module/ });
75+
const SFOP = new StepFunctionsOfflinePlugin(serverless, { event: '../__tests__/eventFile2.json' }, logging);
76+
expect(SFOP.hooks[global['hooks'].loadEventFile]).rejects.toEqual(
77+
expect.objectContaining({ code: 'MODULE_NOT_FOUND' })
78+
);
7679
});
7780
});
7881

@@ -92,8 +95,10 @@ describe('index.js', () => {
9295
});
9396

9497
it('should throw error - incorrect path to event file ', () => {
95-
const SFOP = new StepFunctionsOfflinePlugin(serverless, { event: '..__tests__/eventFile.json' });
96-
expect(SFOP.hooks[global['hooks'].loadEventFile]).rejects.toEqual({ error: /Cannot find module/ });
98+
const SFOP = new StepFunctionsOfflinePlugin(serverless, { event: '..__tests__/eventFile.json' }, logging);
99+
expect(SFOP.hooks[global['hooks'].loadEventFile]).rejects.toEqual(
100+
expect.objectContaining({ code: 'MODULE_NOT_FOUND' })
101+
);
97102
});
98103
describe('trying to load serverless file', () => {
99104
it('should throw err - state does not exist', () => {

src/__tests__/logging.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Logging } from 'serverless/classes/Plugin';
2+
3+
export default {
4+
log: {
5+
error: console.error,
6+
warning: console.warn,
7+
notice: console.info,
8+
verbose: console.info,
9+
success: console.info,
10+
info: console.info,
11+
debug: console.debug,
12+
},
13+
14+
writeText: console.log,
15+
progress: {
16+
get: () => {
17+
// empty function
18+
},
19+
create: () => {
20+
// empty function
21+
},
22+
} as unknown as Logging['progress'],
23+
} as Logging;

src/__tests__/serverless.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
service: ServerlessStepPlugin
22

3-
frameworkVersion: '>=<2.0.0'
3+
frameworkVersion: '>=<3.15.0'
44

55
plugins:
6-
- serverless-step-functions
7-
- serverless-step-functions-offline
8-
6+
- serverless-step-functions
7+
- serverless-step-functions-offline
98

109
provider:
1110
name: aws
@@ -17,7 +16,7 @@ provider:
1716

1817
custom:
1918
stepFunctionsOffline:
20-
FirstLambda: firstLambda
19+
FirstLambda: firstLambda
2120

2221
functions:
2322
- ${file(functions.yml)}
@@ -26,6 +25,6 @@ stepFunctions:
2625
stateMachines:
2726
foo:
2827
definition:
29-
Comment: "An example of the Amazon States Language using wait states"
28+
Comment: 'An example of the Amazon States Language using wait states'
3029
StartAt: FirstLambda
31-
States: ${file(states.yml)}
30+
States: ${file(states.yml)}

src/__tests__/setup.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
/* istanbul ignore file */
2-
import Serverless from 'serverless/lib/Serverless';
3-
import CLI from 'serverless/lib/classes/CLI';
2+
import Serverless from 'serverless';
43
import { ServerlessWithError } from '../types';
54
import StepFunctionsOfflinePlugin from '..';
5+
import Logging from './logging';
66

77
export default function setup(): {
88
stepFunctionsOfflinePlugin: StepFunctionsOfflinePlugin;
99
serverless: ServerlessWithError;
1010
StepFunctionsOfflinePlugin: typeof StepFunctionsOfflinePlugin;
1111
} {
12-
const serverless = new Serverless();
13-
serverless.cli = new CLI();
12+
const serverless = new Serverless({
13+
commands: [],
14+
options: {},
15+
});
1416
// serverless.setProvider('aws', new AwsProvider(serverless));
1517
serverless.service.functions = {
1618
firstLambda: {
1719
handler: 'examples/firstLambda/index.handler',
1820
name: 'TheFirstLambda',
21+
events: [
22+
{
23+
http: {
24+
path: 'fake-path',
25+
method: 'get',
26+
},
27+
},
28+
],
1929
},
2030
};
2131
return {
22-
serverless: serverless as any,
32+
serverless: serverless as ServerlessWithError,
2333
StepFunctionsOfflinePlugin,
24-
stepFunctionsOfflinePlugin: new StepFunctionsOfflinePlugin(serverless as any, global['options']),
34+
stepFunctionsOfflinePlugin: new StepFunctionsOfflinePlugin(
35+
serverless as ServerlessWithError,
36+
global['options'],
37+
Logging
38+
),
2539
};
2640
}

src/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import path from 'path';
33
import moment from 'moment';
4-
import Plugin from 'serverless/classes/Plugin';
4+
import Plugin, { Logging } from 'serverless/classes/Plugin';
55

66
import { StateMachine, State, Map, Fail, Succeed, Task, Parallel, Wait, Pass, Choice } from 'asl-types';
77

@@ -66,20 +66,22 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
6666
options: Options;
6767
commands: Plugin['commands'];
6868
hooks: Plugin['hooks'];
69+
cli: Logging;
6970
stateMachine: Options['stateMachine'];
7071

7172
environment = '';
7273

73-
constructor(serverless: ServerlessWithError, options: Options) {
74+
constructor(serverless: ServerlessWithError, options: Options, cli: Logging) {
7475
this.location = process.cwd();
7576
this.serverless = serverless;
77+
this.cli = cli;
7678
this.options = options;
7779
this.stateMachine = this.options.stateMachine;
7880
this.detailedLog = (this.options.detailedLog || this.options.l) ?? false;
7981
this.eventFile = this.options.event || this.options.e;
8082
this.functions = this.serverless.service.functions;
8183
this.variables = this.serverless.service.custom?.stepFunctionsOffline;
82-
this.cliLog = this.serverless.cli.log.bind(this.serverless.cli);
84+
this.cliLog = this.cli.log.info.bind(this.cli);
8385
this.contexts = {};
8486
this.commands = {
8587
'step-functions-offline': {
@@ -147,9 +149,9 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
147149

148150
_checkVersion(): void {
149151
const version = this.serverless.version;
150-
if (!version.startsWith('2.')) {
152+
if (!version.startsWith('3.')) {
151153
throw new this.serverless.classes.Error(
152-
`Serverless step offline requires Serverless v2.x.x but found ${version}`
154+
`Serverless Step Functions Offline requires Serverless v3.x.x but found ${version}`
153155
);
154156
}
155157
}
@@ -270,8 +272,9 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
270272
'us-east-1';
271273
}
272274

273-
// this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
274-
this.serverless.variables.populateService();
275+
if (this.serverless.variables.populateService) {
276+
this.serverless.variables.populateService();
277+
}
275278
return Promise.resolve();
276279
});
277280
}

tsconfig.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
/* Basic Options */
66
// "incremental": true, /* Enable incremental compilation */
7-
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8-
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9-
"moduleResolution": "node", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
7+
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
8+
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
9+
"moduleResolution": "node" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
1010
// "lib": [], /* Specify library files to be included in the compilation. */
1111
// "allowJs": true, /* Allow javascript files to be compiled. */
1212
// "checkJs": true, /* Report errors in .js files. */
1313
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
14-
"declaration": true, /* Generates corresponding '.d.ts' file. */
14+
"declaration": true /* Generates corresponding '.d.ts' file. */,
1515
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
1616
// "sourceMap": true, /* Generates corresponding '.map' file. */
1717
// "outFile": "./", /* Concatenate and emit output to single file. */
18-
"outDir": "./dist", /* Redirect output structure to the directory. */
18+
"outDir": "./dist" /* Redirect output structure to the directory. */,
1919
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
2020
// "composite": true, /* Enable project compilation */
2121
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
@@ -26,8 +26,8 @@
2626
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
2727

2828
/* Strict Type-Checking Options */
29-
"strict": true, /* Enable all strict type-checking options. */
30-
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
29+
"strict": true /* Enable all strict type-checking options. */,
30+
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
3131
// "strictNullChecks": true, /* Enable strict null checks. */
3232
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
3333
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
@@ -46,18 +46,18 @@
4646
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4747
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4848
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
49-
// "rootDirs": [],
49+
// "rootDirs": [],
5050
"types": [
5151
"serverless",
5252
"lodash",
5353
"moment",
5454
"jest",
5555
"node"
56-
], /* List of root folders whose combined content represents the structure of the project at runtime. */
57-
"typeRoots": ["./node_modules/@types"], /* List of folders to include type definitions from. */
56+
] /* List of root folders whose combined content represents the structure of the project at runtime. */,
57+
"typeRoots": ["./node_modules/@types"] /* List of folders to include type definitions from. */,
5858
// "types": [], /* Type declaration files to be included in compilation. */
5959
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
60-
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
60+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
6161
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
6262
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
6363

@@ -72,9 +72,9 @@
7272
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
7373

7474
/* Advanced Options */
75-
"skipLibCheck": true, /* Skip type checking of declaration files. */
76-
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
75+
"skipLibCheck": true /* Skip type checking of declaration files. */,
76+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
7777
},
7878
"include": ["src"],
79-
"exclude": ["node_modules", "**/__tests__/*"]
79+
"exclude": ["node_modules"]
8080
}

0 commit comments

Comments
 (0)