Skip to content

Commit

Permalink
fix: --api-devserver-url not working if --api-location not set (#523, #…
Browse files Browse the repository at this point in the history
…579) (#620)

* fix: --api-devserver-url not working if --api-location not set (#523, #579)

* docs: fix documentation regarding --api-devserver-url usage (#579)
  • Loading branch information
sinedied committed Dec 16, 2022
1 parent 7ecbae2 commit 02fdb90
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions docs/www/docs/cli/swa-start.md
Expand Up @@ -51,7 +51,7 @@ Here is a list of the default ports used by some popular dev servers:
| [Next.js](https://nextjs.org/) | 3000 | `swa start http://localhost:3000` |
| [React (Create React App)](https://reactjs.org/docs/create-a-new-react-app.html) | 3000 | `swa start http://localhost:3000` |
| [Svelte (sirv-cli)](https://github.com/lukeed/sirv/tree/master/packages/sirv-cli/) | 5000 | `swa start http://localhost:5000` |
| [Vue](https://github.com/vuejs/create-vue) | 3000 | `swa start http://localhost:3000` |
| [Vue](https://github.com/vuejs/create-vue) | 3000 | `swa start http://localhost:3000` |

Instead of starting a dev server separately, you can provide the startup command to the CLI.

Expand Down Expand Up @@ -94,10 +94,10 @@ When developing your backend locally, sometimes it's useful to run Azure Functio
To use the CLI with your local API backend dev server, follow these two steps:

1. Start your API using Azure Functions Core Tools: `func host start` or start debugging in VS Code.
2. In a separate terminal, run the SWA CLI with the `--api-location` flag and the URI of the local API server, in the following format:
2. In a separate terminal, run the SWA CLI with the `--api-devserver-url` flag and the URI of the local API server, in the following format:

```bash
swa start ./my-dist --api-location http://localhost:7071
swa start ./my-dist --api-devserver-url http://localhost:7071
```

## Options
Expand All @@ -108,7 +108,7 @@ Here are the options you can use with `swa start`:
- `-i, --api-location <path>`: the folder containing the source code of the API application
- `-O, --output-location <path>`: the folder containing the built source of the front-end application. The path is relative to `--app-location` (default: ".")
- `-D, --app-devserver-url <url>`: connect to the app dev server at this URL instead of using output location
- `-is, --api-devserver-url <url>`: connect to the api server at this URL instead of using output location
- `-is, --api-devserver-url <url>`: connect to the api server at this URL instead of using api location
- `-j, --api-port <apiPort>`: the API server port passed to `func start` (default: 7071)
- `-q, --host <host>`: the host address to use for the CLI dev server (default: "localhost")
- `-p, --port <port>`: the port value to use for the CLI dev server (default: 4280)
Expand Down Expand Up @@ -157,7 +157,7 @@ swa start http://localhost:3000 --run-build "npm start"
Connect both front-end and the API to running development server

```bash
swa start http://localhost:3000 --api-location http://localhost:7071
swa start http://localhost:3000 --api-devserver-url http://localhost:7071
```

## See Also
Expand Down
4 changes: 2 additions & 2 deletions docs/www/docs/use/3-api-server.md
Expand Up @@ -29,10 +29,10 @@ To use the SWA emulator services alongside the API server:
func host start
```

2. Start SWA CLI in a separate terminal and use the `--api-location` option to pass it the relevant local API Server URI. _For example:_
2. Start SWA CLI in a separate terminal and use the `--api-devserver-url` option to pass it the relevant local API Server URI. _For example:_

```bash
swa start ./my-dist --api-location http://localhost:7071
swa start ./my-dist --api-devserver-url http://localhost:7071
```

## 4.3 Start API Server Automatically
Expand Down
23 changes: 13 additions & 10 deletions src/cli/commands/start.ts
Expand Up @@ -35,8 +35,12 @@ export default function registerCommand(program: Command) {
.option("-a, --app-location <path>", "the folder containing the source code of the front-end application", DEFAULT_CONFIG.appLocation)
.option("-i, --api-location <path>", "the folder containing the source code of the API application", DEFAULT_CONFIG.apiLocation)
.option("-O, --output-location <path>", "the folder containing the built source of the front-end application", DEFAULT_CONFIG.outputLocation)
.option("-D, --app-devserver-url <url>", "connect to the app dev server at this URL instead of using output location", DEFAULT_CONFIG.appDevserverUrl)
.option("-is, --api-devserver-url <url>", "connect to the api server at this URL instead of using output location", DEFAULT_CONFIG.apiDevserverUrl)
.option(
"-D, --app-devserver-url <url>",
"connect to the app dev server at this URL instead of using output location",
DEFAULT_CONFIG.appDevserverUrl
)
.option("-is, --api-devserver-url <url>", "connect to the api server at this URL instead of using api location", DEFAULT_CONFIG.apiDevserverUrl)
.option<number>("-j, --api-port <apiPort>", "the API server port passed to `func start`", parsePort, DEFAULT_CONFIG.apiPort)
.option("-q, --host <host>", "the host address to use for the CLI dev server", DEFAULT_CONFIG.host)
.option<number>("-p, --port <port>", "the port value to use for the CLI dev server", parsePort, DEFAULT_CONFIG.port)
Expand Down Expand Up @@ -107,7 +111,7 @@ Use a custom command to run framework development server at startup
swa start http://localhost:3000 --run-build "npm start"
Connect both front-end and the API to running development server
swa start http://localhost:3000 --api-location http://localhost:7071
swa start http://localhost:3000 --api-devserver-url http://localhost:7071
`
);
}
Expand Down Expand Up @@ -183,17 +187,16 @@ export async function start(options: SWACLIConfig) {
logger.silly(` ${outputLocation}`);
}

if (apiLocation) {
if (apiDevserverUrl) {
// TODO: properly refactor this after GA to send apiDevserverUrl to the server
useApiDevServer = apiDevserverUrl;
apiLocation = apiDevserverUrl;
} else if (apiLocation) {
// resolves to the absolute path of the apiLocation
let resolvedApiLocation = path.resolve(apiLocation);

if (apiDevserverUrl) {
// TODO: properly refactor this after GA to send apiDevserverUrl to the server
useApiDevServer = apiDevserverUrl;
apiLocation = apiDevserverUrl;
}
// make sure api folder exists
else if (fs.existsSync(resolvedApiLocation)) {
if (fs.existsSync(resolvedApiLocation)) {
apiLocation = resolvedApiLocation;
} else {
logger.info(`Skipping API because folder "${resolvedApiLocation}" is missing`, "swa");
Expand Down

0 comments on commit 02fdb90

Please sign in to comment.