Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
feat: Env vars and more configuration for lambdas (#137)
Browse files Browse the repository at this point in the history
* feat: refactor using classes

* feat: updated tests

* feat: added possibility to add node version runtime

* fix: types

* feat: added environment variables to add to the lambdas

* docs: added clarifications

* fix(test): updated snapshot

* fix: integration test

* fix: lint problem

* chore: removed unused methods

* fix: regressions around configuration

* test: added env vars to the integration test

* fix: fixed failed tests
  • Loading branch information
ematipico authored Nov 21, 2019
1 parent 3004c41 commit 1439426
Show file tree
Hide file tree
Showing 47 changed files with 3,489 additions and 1,976 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"unicorn/filename-case": ["error", { "case": "camelCase" }],
"unicorn/prevent-abbreviations": "off",
"prettier/prettier": "error",
"indent": ["error", "tab"],
"indent": ["error", "tab", {
"SwitchCase": 1
}],
"unicorn/no-process-exit": "off",
"no-process-exit": "off",
"strict": ["error", "global"]
Expand Down
96 changes: 53 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This library supports [cosmiconfig](https://github.com/davidtheclark/cosmiconfig

### Via CLI

You can use the simple CLI available. At moment you *can't* pass the `routes` parameter, you will need to use the config object or use the [API](#via-api).
You can use the simple CLI available. At moment you _can't_ pass the `routes` parameter, you will need to use the config object or use the [API](#via-api).

Using the CLI will automatically emit the configuration files.

Expand All @@ -75,15 +75,18 @@ Usage
$ terranext
Options
--gateway-key, -g The API Gateway key of the project. Default is "Terranext"
--next-app-dir, -d The path that Terraform CLI has to follow to reach the nextjs project.
--provider The Cloud provider to use when exporting the configuration
--gateway-key, -g The API Gateway key of the project. Default is "Terranext"
--next-app-dir, -d The path that Terraform CLI has to follow to reach the nextjs project.
--provider The Cloud provider to use when exporting the configuration
--env A way for passing environment variables to the lambdas
Examples
$ terranext
$ terranext --gateway-key=CustomKey --next-app-dir=../../nextjs-project/
$ terranext --provider=AWS --next-app-dir=../../nextjs-project/
$ terranext -g=CustomKey -d=../../nextjs-project/
$ terranext --env="DEBUG,express:*"
```

### Via API
Expand All @@ -92,13 +95,19 @@ Examples
const generateResources = require("@ematipico/terraform-nextjs-plugin");

const configuration = {
gatewayKey: "AmazingWebsite",
lambdaPath: "../../project/build",
provider: "AWS"
gatewayKey: "AmazingWebsite",
lambdaPath: "../../project/build",
provider: "AWS",
env: [
{
key: "KEY",
value: "2940"
}
]
};

const resources = generateResources(configuration); // inside resources you have the terraform json configuration
generateResources(configuration, true) // it creates two files
generateResources(configuration, true); // it creates two files
```

If the second argument is a boolean and it's `true`, the library will create two files:
Expand All @@ -111,12 +120,13 @@ It will be up to you to consume them in a proper way.

## Configuration

| Name | Type | Description |
| ------------ | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `gatewayKey` | `string` | A name that will be prefixed to your resources. Usually it's the project name. _Default value: `Terranext`_. |
| `provider` | `string` | The Cloud Provider. Based on the value, a different configuration will be exported. Supported providers: `AWS` |
| `nextDirApp` | `string` | This is the path where your Next.js project is. Usually you will run `terraform` CLI from a different project/folder. So you need to tell `terraform` where this folder is. The library will take care of the rest. _Default value: `"./"`_ |
| `routes` | `Array<Mapping>`, `Mapping` | This is the structure of the routes that describe your pages. |
| Name | Type | Description |
| ------------ | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `gatewayKey` | `string` | A name that will be prefixed to your resources. Usually it's the project name. _Default value: `Terranext`_. |
| `provider` | `string` | The Cloud Provider. Based on the value, a different configuration will be exported. Supported providers: `AWS` |
| `nextDirApp` | `string` | This is the path where your Next.js project is. Usually you will run `terraform` CLI from a different project/folder. So you need to tell `terraform` where this folder is. The library will take care of the rest. _Default value: `"./"`_ |
| `routes` | `Array<Mapping>`, `Mapping` | This is the structure of the routes that describe your pages. |
| `env` | `Array<Env>` | Environments passed via CLI have to be split using `,`: `--env="KEY,VALUE"`. When using the API, you always have to pass an array of objects `{ key: "MyKeyName", "value": "MyKeyValue" }`. **_Environment variables are applied to all the lambdas_** |

### Mapping explained

Expand All @@ -132,35 +142,35 @@ Let's say we want to describe the following URLs:

```js
const routes = [
{
prefix: "/about-us",
mappings: [
{
route: "/contacts", // the URL
page: "/companyContacts" // the nextjs file, inside pages folder, that is responsible to render this page
},
{
route: "/the-company",
page: "/aboutTheCompany"
}
]
},
{
prefix: "",
mappings: [
{
route: "/blog/:url",
page: "/blogPost"
},
{
route: "/credits",
page: "/credits",
params: {
hideComments: false
}
}
]
}
{
prefix: "/about-us",
mappings: [
{
route: "/contacts", // the URL
page: "/companyContacts" // the nextjs file, inside pages folder, that is responsible to render this page
},
{
route: "/the-company",
page: "/aboutTheCompany"
}
]
},
{
prefix: "",
mappings: [
{
route: "/blog/:url",
page: "/blogPost"
},
{
route: "/credits",
page: "/credits",
params: {
hideComments: false
}
}
]
}
];
```

Expand Down
38 changes: 35 additions & 3 deletions bin/terranext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const cosmiconfig = require("cosmiconfig");
const { cosmiconfig } = require("cosmiconfig");
const meow = require("meow");
const generateResources = require("../src");

Expand All @@ -15,12 +15,14 @@ Options
--gateway-key, -g The API Gateway key of the project. Default is "Terranext"
--next-app-dir, -d The path that Terraform CLI has to follow to reach the nextjs project.
--provider The Cloud provider to use when exporting the configuration
--env A way for passing environment variables to the lambdas
Examples
$ terranext
$ terranext --gateway-key=CustomKey --next-app-dir=../../nextjs-project/
$ terranext --provider=AWS --next-app-dir=../../nextjs-project/
$ terranext -g=CustomKey -d=../../nextjs-project/
$ terranext --env="DEBUG,express:*"
`,
{
flags: {
Expand All @@ -37,6 +39,9 @@ Examples
},
provider: {
type: "string"
},
env: {
type: "string"
}
}
}
Expand All @@ -45,12 +50,18 @@ Examples
explorer
.search()
.then(async result => {
const { gatewayKey, nextAppDir, provider } = cli.flags;
const { gatewayKey, nextAppDir, provider, env } = cli.flags;
let parsedEnvs;
if (env) {
parsedEnvs = parseEnv(env, {});
}
console.log(parsedEnvs);
const options = {
...result.config,
gatewayKey,
nextAppDir,
provider
provider,
env: parsedEnvs
};
await generateResources(options, true);
})
Expand All @@ -60,3 +71,24 @@ explorer
process.exit(1);
// Do something constructive.
});

function parseEnv(unparsedEnv) {
if (Array.isArray(unparsedEnv)) {
return unparsedEnv.map(env => {
const splitEnv = env.split(",");
return {
key: splitEnv[0],
value: splitEnv[1]
};
});
}

const splitEnv = unparsedEnv.split(",");

return [
{
key: splitEnv[0],
value: splitEnv[1]
}
];
}
8 changes: 7 additions & 1 deletion integration/app/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ generateResources(
gatewayKey: "CustomKey",
nextAppDir: "../app/",
provider: "AWS",
routes
routes,
env: [
{
key: "DEBUG",
value: "express:*"
}
]
},
true
);
Loading

0 comments on commit 1439426

Please sign in to comment.