Skip to content

Commit

Permalink
feat: use existing API dev server (#19)
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
manekinekko committed Oct 18, 2020
1 parent 6f64f02 commit 398bc59
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
25 changes: 16 additions & 9 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ program
.option("--api-uri <apiUri>", "set API uri", `http://localhost:${API_PORT}`)
.option("--api-prefix <apiPrefix>", "set API prefix", "api")
.option("--app-uri <appUri>", "set APP uri", `http://localhost:${APP_PORT}`)
.option("--use-app <useAppServer>", "Use running APP dev server", null)
.option("--host <host>", "set host address", "0.0.0.0")
.option("--port <port>", "set port value", EMU_PORT)
.option("--verbose", "show debug logs", false)
.option("--use-api <useApi>", "Use running API dev server", null)
.option("--use-app <useApp>", "Use running APP dev server", null)
.option("--host <host>", "set emulator host address", "0.0.0.0")
.option("--port <port>", "set emulator port value", EMU_PORT)
.option("--build", "build the API and APP before starting the emulator", false)
.option("--verbose", "show debug logs", false)
.option("--ui", "enable dashboard UI", false)
.parse(process.argv);

Expand Down Expand Up @@ -55,18 +56,24 @@ const envVarsObj = {
GITHUB_CLIENT_ID: "",
GITHUB_CLIENT_SECRET: "",
SWA_EMU_AUTH_URI: program.authUri,
SWA_EMU_API_URI: program.apiUri,
SWA_EMU_API_URI: program.useApi || program.apiUri,
SWA_EMU_API_PREFIX: program.apiPrefix,
SWA_EMU_APP_URI: program.useAppServer || program.appUri,
SWA_EMU_APP_URI: program.useApp || program.appUri,
SWA_EMU_APP_LOCATION: app_artifact_location,
SWA_EMU_HOST: program.host,
SWA_EMU_PORT: program.port,
};

const { command: hostCommand, args: hostArgs } = createRuntimeHost(appUriPort, program.host, program.port);

let serveApiContent = `[ -d '${api_location}' ] && (cd ${api_location}; func start --cors *) || echo 'No API found. Skipping.'`;
if (program.useApi) {
serveApiContent = `echo 'using API dev server at ${program.useApi}'`;
}

let serveStaticContent = `${hostCommand} ${hostArgs.join(" ")}`;
if (program.useAppServer) {
serveStaticContent = `echo 'using dev server at ${program.useAppServer}'`;
if (program.useApp) {
serveStaticContent = `echo 'using APP dev server at ${program.useApp}'`;
}

const startCommand = [
Expand All @@ -86,7 +93,7 @@ const startCommand = [
`"${serveStaticContent}"`,

// serve the api, if it's available
`"[ -d '${api_location}' ] && (cd ${api_location}; func start --cors *) || echo 'No API found. Skipping.'"`,
`"${serveApiContent}"`,

`--color=always`,
];
Expand Down
20 changes: 16 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,28 @@ Using `npx`:
- Start the emulator: `npx @manekinekko/swa-emu@latest`
- Access your SWA app from `http://localhost`

### Use with a local dev server
### Use with a local API dev server

When developing locally on your front-end application, it might be usefull to use your local application dev server, that comes with your application CLI, to serve your app content and benefit from the built-in feature like the livereload or HMR (hot module reload) features.
When developing locally on your back-end application, it might be useful to use your local API dev server, to serve your API content and benefit from the built-in features like debugging. In order to use SWA EMU with your local API dev server, follow these two steps:

1. Start your local API dev server (as usual). For example: `func start host`.
1. Run `swa` with the `--use-api` flag of the URI provided by the API dev server, in the following format:

```bash
swa --use-api=http://<api-dev-server-host>:<api-dev-server-port>
```

### Use with a local APP dev server

When developing locally on your front-end application, it might be useful to use your local application dev server, that comes with your application CLI, to serve your app content and benefit from the built-in feature like the livereload or HMR (hot module reload) features.

In order to use SWA EMU with your local dev server, follow these two steps:

1. Start your local dev server (as usual). For example: `ng serve`
1. Run `swa` with the `--use-app` flag of the URI provided by the dev server, in the following format:

```
$ swa --use-app=http://<dev-server-host>:<dev-server-port>
```bash
swa --use-app=http://<app-dev-server-host>:<app-dev-server-port>
```

Here is a list of the default ports used by popular dev servers:
Expand Down Expand Up @@ -104,6 +115,7 @@ If you need to override the default values, provide the following options:
| `--api-uri` | the API URI | `http://localhost:7071` | `swa --api-uri=http://localhost:8082` |
| `--app-uri` | the app URI | `http://localhost:4200` | `swa --app-uri=http://localhost:8081` |
| `--use-app` | use the app dev server | `null` | `swa --use-app=http://localhost:8080` |
| `--use-api` | use the api dev server | `null` | `swa --use-api=http://localhost:3000` |
| `--host` | the emulator host address | `0.0.0.0` | `swa --host=192.168.68.80` |
| `--port` | the emulator port value | `80` | `swa --port=8080` |
| `--build` | build the api and app before starting | `false` | `swa --build` |
Expand Down

0 comments on commit 398bc59

Please sign in to comment.