From 398bc596db390c13b3ad324b4b2172be8ecc979f Mon Sep 17 00:00:00 2001 From: Wassim Chegham <1699357+manekinekko@users.noreply.github.com> Date: Sun, 18 Oct 2020 14:48:55 +0200 Subject: [PATCH] feat: use existing API dev server (#19) Closes #17 --- bin/index.js | 25 ++++++++++++++++--------- readme.md | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/bin/index.js b/bin/index.js index e7285b27..2cba8731 100755 --- a/bin/index.js +++ b/bin/index.js @@ -21,11 +21,12 @@ program .option("--api-uri ", "set API uri", `http://localhost:${API_PORT}`) .option("--api-prefix ", "set API prefix", "api") .option("--app-uri ", "set APP uri", `http://localhost:${APP_PORT}`) - .option("--use-app ", "Use running APP dev server", null) - .option("--host ", "set host address", "0.0.0.0") - .option("--port ", "set port value", EMU_PORT) - .option("--verbose", "show debug logs", false) + .option("--use-api ", "Use running API dev server", null) + .option("--use-app ", "Use running APP dev server", null) + .option("--host ", "set emulator host address", "0.0.0.0") + .option("--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); @@ -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 = [ @@ -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`, ]; diff --git a/readme.md b/readme.md index c2bae7fe..418bf707 100644 --- a/readme.md +++ b/readme.md @@ -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://: +``` + +### 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://: +```bash +swa --use-app=http://: ``` Here is a list of the default ports used by popular dev servers: @@ -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` |