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

Commit

Permalink
feat: create CLI utility
Browse files Browse the repository at this point in the history
Created a CLI utility and handling correctly the configuration via cosmiconfig
  • Loading branch information
ematipico committed Jun 20, 2019
1 parent dbb7e46 commit 0475466
Show file tree
Hide file tree
Showing 5 changed files with 565 additions and 213 deletions.
15 changes: 15 additions & 0 deletions .terranextrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"routes": {
"prefix": "",
"mappings": [
{
"page": "/blogPost",
"route": "/blog/:url"
},
{
"page": "/contact",
"route": "/contact-us"
}
]
}
}
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ A plugin to generate terraform configuration from nextjs pages
[![npm][npm]][npm-url]
[![Conventional Commits][conventional]][conventional-url]



## The reason

Nextjs supports serverless pages, where it creates files that can be used by some lambdas to render the pages.
Expand All @@ -40,6 +38,20 @@ yarn add --dev @ematipico/terraform-nextjs-plugin

## Usage

This library supports [cosmiconfig](https://github.com/davidtheclark/cosmiconfig): you just need to have a file called `terranextrc` that matches the criteria. This repository has [one](./terranextrc).

_At the moment, the library assumes that you already run `next build` inside your project._

### 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).

Using the CLI will automatically emit the configuration files.

_Arguments passed via CLI will *override* the ones that are defined inside the config file_.

### Via API

```js
const { generateResources } = require("@ematipico/terraform-nextjs-plugin");

Expand Down
52 changes: 46 additions & 6 deletions bin/terranext.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,57 @@
"use strict";

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

const explorer = cosmiconfig("terranext");
const terranext = require("../src");

const cli = meow(
`
Usage
$ terranext
Options
--gatewayKey, -g The API Gateway key of the project. Default is "Terranext"
--lambdaPath, -p The path that Terraform CLI has to follow to reach the nextjs project.
--write
Examples
$ terranext
$ terranext --gatewayKey=CustomKey --lambdaPath=../../nextjs-project/
$ terranext -g=CustomKey -p=../../nextjs-project/
`,
{
flags: {
gatewayKey: {
type: "string",
default: "Terranext",
alias: "g"
},
lambdaPath: {
type: "string",
alias: "p",
default: "./"
}
}
}
);

explorer
.search()
.then(result => {
terranext(result.config);
// result.config is the parsed configuration object.
// result.filepath is the path to the config file that was found.
// result.isEmpty is true if there was nothing to parse in the config file.
const { gatewayKey, lambdaPath } = cli.flags;
const options = {
...result.config,
gatewayKey,
lambdaPath
};

generateResources(options, true);
})
// eslint-disable-next-line no-unused-vars
.catch(error => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
// Do something constructive.
});
Loading

0 comments on commit 0475466

Please sign in to comment.