Skip to content

Commit

Permalink
Add prettier to CI/CD (#18)
Browse files Browse the repository at this point in the history
* Add prettier to GitHub Actions
Fix prettier errors

* remove settings.json and add to .gitignore

* ci: rename formatting workflow

* doc: fix comments from review. run prettier
  • Loading branch information
jhowlett-scottlogic authored Oct 11, 2022
1 parent f64d981 commit a22964e
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 55 deletions.
36 changes: 17 additions & 19 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
module.exports = {
env: {
node: true
node: true,
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"parserOptions": {
ecmaVersion: "latest",
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-explicit-any": "off"
}
};
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
parserOptions: {
ecmaVersion: "latest",
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
rules: {
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-explicit-any": "off",
},
};
31 changes: 31 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Format

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
run-eslint:
name: Format
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]

steps:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Install Node.js dependencies
run: npm ci

- name: Code Format Check
run: npm run format:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
test/api
other-examples
.vscode/settings.json
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.github
example
*.handlebars
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
endOfLine: "lf",
};
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
}
}
]
}
}
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@ This repository is the TypeScript template for the [OpenAPI Forge](https://githu

https://github.com/ColinEberhardt/openapi-forge


## Quick Start

Clone and then navigate to root directory of the repository.

Install all the dependencies needed:
~~~

```
npm install
~~~
Once you have a local version, you can reference it's location as the 'generator' argument of the 'forge' command of openapi-forge.
~~~
```

Once you have a local version, you can reference its location as the 'generator' argument of the 'forge' command of openapi-forge.

```
$ openapi-forge forge
\ https://petstore3.swagger.io/api/v3/openapi.json
\ {location of local generator}
\ -o api
~~~
```

## Testing
There are two scripts that can be used for testing, one that uses preset values for file paths to *.feature files and the generate.js file of the forge:

There are two scripts that can be used for testing, one that uses preset values for file paths to \*.feature files and the generate.js file of the forge:

Using default values:
~~~

```
npm run test:defaultPaths
~~
This method uses:
Expand All @@ -35,9 +39,11 @@ featurePath: node_modules/openapi-forge/features/*.feature
generatorPath: openapi-forge/src/generate
The second script requires values for the featurePath & generatePath:
~~~
```

npm test {featurePath} {generatorPath}
~~~

```
## Linting
Expand All @@ -47,7 +53,7 @@ Two scripts are available to help you find linting errors:
npm run eslint:check
~~~
This runs eslint in check mode which will raise errors found but not try and fix them.
This is also ran on a PR and a push to main. It will fail if any errors were found.
This is also ran on a PR and a push to main. It will fail if any errors were found.
~~~
npm run eslint:write
Expand All @@ -56,4 +62,5 @@ This runs eslint in write mode which will raise errors found and try to fix them
## Notes
The openapi-forge dependency is pointing to commit:0fb044b3a2808e8faf82786f168a12763f5aaeca. If openapi-forge is updated and openapi-forge-typescript requires this updated version then the commit reference in package.json will have to be updated. This is a temporary measure and will be fixed once the packages are properly versioned and hosted on npm.
The openapi-forge dependency is pointing to commit:0fb044b3a2808e8faf82786f168a12763f5aaeca. If openapi-forge is updated and openapi-forge-typescript requires this updated version then the commit reference in package.json will have to be updated. This is a temporary measure and will be fixed once the packages are properly versioned and hosted on npm.
```
10 changes: 6 additions & 4 deletions features/support/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ export class ModelSteps extends BaseModelStep {

@then(/the request should have a header property with value (.*)/)
public checkRequestHeaders(headerParam: string) {
expect(this.requestParams.headers).to.have.property("test", headerParam)
expect(this.requestParams.headers).to.have.property("test", headerParam);
}

@then(/the request header should have a cookie property with value (.*)/)
public checkRequestCookie(cookieParam: string) {
expect(this.requestParams.headers).to.have.property("cookie", cookieParam)
expect(this.requestParams.headers).to.have.property("cookie", cookieParam);
}

@when(/calling the method ([a-zA-Z]*) and the server provides an empty response/)
@when(
/calling the method ([a-zA-Z]*) and the server provides an empty response/
)
public async callWithoutResponse(methodName: string) {
this.serverResponseObject = null;
this.apiResponse = await this.api[methodName]();
Expand Down Expand Up @@ -149,4 +151,4 @@ export class ModelSteps extends BaseModelStep {
public extractAtIndex(index: string) {
this.apiResponse = this.apiResponse[parseInt(index)];
}
}
}
2 changes: 1 addition & 1 deletion features/support/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class BaseModelStep {
output: "./features/api",
testRun: true,
skipValidation: true,
logLevel: "quiet"
logLevel: "quiet",
});
}
}
6 changes: 2 additions & 4 deletions features/support/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import { binding, after, then, when } from "cucumber-tsflow";
import { assert } from "chai";
import * as ts from "typescript";
Expand Down Expand Up @@ -36,12 +34,12 @@ export class ModelSteps extends BaseModelStep {
});
return found;
}

@after()
public async after() {
return this.cleanup();
}

@when("generating an API from the following specification")
public async generate(schema: string) {
await this.generateApi(schema);
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"test": "\"./node_modules/.bin/cucumber-js\" -p default",
"test:defaultPath": "\"./node_modules/.bin/cucumber-js\" -p default \"node_modules/openapi-forge/features/*.feature\" \"openapi-forge/src/generate\"",
"format:check": "prettier --check .",
"format:write": "prettier --write .",
"lint:check": "eslint .",
"lint:write": "eslint --fix ."
},
Expand All @@ -21,6 +23,7 @@
"cucumber-tsflow": "^3.2.0",
"openapi-forge": "github:ColinEberhardt/openapi-forge#4aecbe5d8f47369ed13f1e2315a4999d78cd00cb",
"eslint": "^8.24.0",
"prettier": "^2.7.1",
"ts-node": "^8.0.3",
"typescript": "^4.7.4"
}
Expand Down
2 changes: 1 addition & 1 deletion template/nodeFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export async function transport(params: RequestParameters) {
throw new Error(`${response.status} ${response.statusText}`);
}
return await response.json();
}
}
2 changes: 1 addition & 1 deletion template/parameterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ export class ParameterBuilder {
});
}
}
}
}
20 changes: 10 additions & 10 deletions template/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export async function request(

// add cookies to headers
const cookies = params
.filter((p) => p.location === "cookie")
.reduce<Headers>((acc, param, index, array) => {
acc["cookie"] ??= "";
acc["cookie"] += `${param.name}=${param.value}`;
if(index !== array.length - 1){
acc["cookie"] += ";"
}
return acc;
}, {});
.filter((p) => p.location === "cookie")
.reduce<Headers>((acc, param, index, array) => {
acc["cookie"] ??= "";
acc["cookie"] += `${param.name}=${param.value}`;
if (index !== array.length - 1) {
acc["cookie"] += ";";
}
return acc;
}, {});

// provide a bearer token if required
if (config.bearerToken) {
Expand All @@ -79,6 +79,6 @@ export async function request(
if (bodyParam) {
requestParams.body = bodyParam.value;
}

return await config.transport(requestParams);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"target": "ES5",
"noImplicitAny": true
}
}
}

0 comments on commit a22964e

Please sign in to comment.