Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
djvs committed May 15, 2020
0 parents commit 88ba7e0
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openapi-generator-cli.jar
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Client Generator

Simply invoke the "generate" script with the name of the desired repo, a folder to output the client folder into, and an optional URL for the OpenAPI documentation (defaults to http://localhost:3000/documentation/json):

```
./generator issuer-phone-twilio /my/packages
# or
./generator issuer-phone-twilio /my/packages http://localhost:3000/documentation/json
```

The `FORCE_OVERWRITE` env var can also be specified to force overwriting an existing package.
64 changes: 64 additions & 0 deletions generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/zsh

SERVICE_NAME="$1"
OUTPUT_DIR="$2"

CLIENT_NAME="$SERVICE_NAME-client"
CLIENT_DIR="$OUTPUT_DIR/$CLIENT_NAME"
GENERATOR_DIR="./"
OPENAPI_GENERATOR_JAR_PATH="$GENERATOR_DIR/openapi-generator-cli.jar"
if [[ ! -v DOCUMENTATION_URL ]]; then
DOCUMENTATION_URL="http://localhost:3000/documentation/json"
fi

echo "Output directory: $OUTPUT_DIR"
echo "Service name: $SERVICE_NAME"
echo "Client name: $CLIENT_NAME"
echo "Client dir: $CLIENT_DIR"

if [[ -d "$CLIENT_DIR" ]]; then
if [[ -v FORCE_OVERWRITE ]]; then
echo 'Overwriting existing client...'
else
read CONF\?"Client dir \"$CLIENT_DIR\" exists, delete and regenerate? (y/N) "
if [[ $CONF == 'y' ]]; then
echo 'Overwriting existing client...'
else
exit
fi
fi
rm -rf "$CLIENT_DIR"
fi

# Get the OpenAPI generator CLI tool and save to ./generators/client/
if [[ ! -f "$OPENAPI_GENERATOR_JAR_PATH" ]]; then
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/4.3.1/openapi-generator-cli-4.3.1.jar -O "$OPENAPI_GENERATOR_JAR_PATH"
fi

# Create project from TSDX template in OUTPUT_DIR
pushd "$OUTPUT_DIR";
tsdx create --template basic "$CLIENT_NAME"
popd;

# Pull API JSON from the (hopefully running) service
# mkdir -p "$CLIENT_DIR/src"
wget $DOCUMENTATION_URL -O "$CLIENT_DIR/api.json"
java -jar "$OPENAPI_GENERATOR_JAR_PATH" generate -o "$CLIENT_DIR/src/" -i "$CLIENT_DIR/api.json" -g typescript-axios --package-name "$CLIENT_NAME"

# Project prep
# Rename package within @affinityproject scope
perl -pi -e 's/^ "name": "/ "name": "\@affinityproject\//' "$CLIENT_DIR/package.json"

# mv "$CLIENT_DIR/src/.gitignore" "$CLIENT_DIR/"
rm "$CLIENT_DIR/src/.gitignore"
cp -rvf "$GENERATOR_DIR"/template/* "$GENERATOR_DIR"/template/.* "$CLIENT_DIR/"

perl -pi -e "s/import (.*) = require\((.*)\)/import \1 from \2/g" "$CLIENT_DIR/src"/**/*
perl -pi -e "s/ == / === /g" "$CLIENT_DIR/src"/**/*
perl -pi -e "s/http.ClientResponse/http.IncomingMessage/g" "$CLIENT_DIR/src"/**/*

pushd "$CLIENT_DIR"
npm install --save axios '@types/axios'
npx prettier --write ./src/**/*.ts
tsdx lint src test --fix
npm run build
14 changes: 14 additions & 0 deletions template/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
rules: {
'@typescript-eslint/no-unused-vars': ['off'],
'@typescript-eslint/consistent-type-assertions': ['off'],
'@typescript-eslint/consistent-type-assertions': ['off'],
'@typescript-eslint/interface-name-prefix': [
'off',
{
prefixWithI: 'always',
},
],
'@typescript-eslint/no-namespace': ['off', {allowDeclarations: false, allowDefinitionFiles: false}],
},
}
1 change: 1 addition & 0 deletions template/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@bloomprotocol/prettier-config')
21 changes: 21 additions & 0 deletions template/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 djvs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions template/tsconfig-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const tsConfig = require('./tsconfig.json')
const tsConfigPaths = require('tsconfig-paths')

// this is needed for paths to work when running files in the build folder
const baseUrl = './build'
tsConfigPaths.register({
baseUrl,
paths: tsConfig.compilerOptions.paths,
})
7 changes: 7 additions & 0 deletions template/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"allowJs": true
},
"include": ["src", "knexfile.ts"]
}
33 changes: 33 additions & 0 deletions template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"include": ["src", "types", "test"],
"compilerOptions": {
"target": "es5",
"module": "esnext",
"lib": ["dom", "esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"outDir": "build",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"],
".*": ["src/*"],
"@src/*": ["src/*"]
},
"jsx": "react",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}

0 comments on commit 88ba7e0

Please sign in to comment.