Skip to content

Commit

Permalink
feat(core): add dashboard UI
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Aug 1, 2020
1 parent 761812d commit 0831999
Showing 1 changed file with 71 additions and 45 deletions.
116 changes: 71 additions & 45 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@ const path = require("path");
const program = require("commander");
const builder = require("../src/builder");
const { readConfigFile } = require("../src/utils");
const { dashboard } = require("../src/dashboard");
const { spawn } = require("child_process");
const spawnx = (command, args) =>
spawn(command, args, {
// stdio: [null, null, null, null],
shell: true,
cwd: path.resolve(__dirname, ".."),
});

const EMU_PORT = 80;
const AUTH_PORT = 4242;
const API_PORT = 7071;
const APP_PORT = 4200;

program
.name("swa")
.usage("<command>")
.version(require("../package.json").version)
.option("--auth-uri <authUri>", "set Auth uri", "http://localhost:4242")
.option("--api-uri <apiUri>", "set API uri", "http://localhost:7071")
.option("--auth-uri <authUri>", "set Auth uri", `http://localhost:${AUTH_PORT}`)
.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:4200")
.option("--app-uri <appUri>", "set APP uri", `http://localhost:${APP_PORT}`)
.option("--host <host>", "set host address", "0.0.0.0")
.option("--port <port>", "set port value", 80)
.option("--debug", "set port value", false)
.option("--port <port>", "set port value", EMU_PORT)
.option("--verbose", "show more logs", false)
.option("--build", "build the API and APP before starting the emulator", false)
.option("--ui", "enable dashboard UI", false)
.parse(process.argv);

if (program.debug) {
if (program.verbose) {
process.env.DEBUG = "*";
}

// parse the Auth URI port or default to 4242
const authUriPort = program.authUri.split(":").map(Number)[2] || 4242;
const authUriPort = program.authUri.split(":").map(Number)[2] || AUTH_PORT;

// parse the APP URI port or default to 4200
const appUriSegments = program.appUri.split(":");
const appUriPort = appUriSegments[2] || 4200;
const appUriPort = appUriSegments[2] || APP_PORT;

// provide binaries
const concurrentlyBin = path.resolve(__dirname, "..", "./node_modules/.bin/concurrently");
Expand All @@ -49,23 +48,27 @@ if (program.build) {
builder();
}

const startCommand = [
const envVarsObj = {
// set env vars for current command
"StaticWebAppsAuthCookie=123",
"StaticWebAppsAuthContextCookie=abc",
"AppServiceAuthSession=1a2b3c",
`DEBUG=${program.debug ? "*" : ""}`,
StaticWebAppsAuthCookie: 123,
StaticWebAppsAuthContextCookie: "abc",
AppServiceAuthSession: "1a2b3c",
DEBUG: program.debug ? "*" : "",

// use the default dev token
`GITHUB_CLIENT_ID=`,
`GITHUB_CLIENT_SECRET=`,
GITHUB_CLIENT_ID: "",
GITHUB_CLIENT_SECRET: "",
SWA_EMU_AUTH_URI: program.authUri,
SWA_EMU_API_URI: program.apiUri,
SWA_EMU_API_PREFIX: program.apiPrefix,
SWA_EMU_APP_URI: program.appUri,
SWA_EMU_APP_LOCATION: app_artifact_location,
SWA_EMU_HOST: program.host,
SWA_EMU_PORT: program.port,
};

`SWA_EMU_AUTH_URI="${program.authUri}"`,
`SWA_EMU_API_URI="${program.apiUri}"`,
`SWA_EMU_API_PREFIX="${program.apiPrefix}"`,
`SWA_EMU_APP_URI="${program.appUri}"`,
`SWA_EMU_HOST="${program.host}"`,
`SWA_EMU_PORT="${program.port}"`,
const startCommand = [
...Object.keys(envVarsObj).map((key) => `${key}="${envVarsObj[key]}"`),

// run concurrent commands
concurrentlyBin,
Expand Down Expand Up @@ -94,24 +97,47 @@ if (process.env.DEBUG) {
console.log(startCommand);
}

// start hosting
const hosting = spawnx(`${httpServerBin}`, `${app_artifact_location} -p ${appUriPort} -c-1`.split(" "));
dashboard.stream("hosting", hosting);
// hosting.stdout.on("data", (data) => dashboard.hosting.log(data.toString("utf8")));
// hosting.stderr.on("data", (data) => dashboard.hosting.log(data.toString("utf8")));
if (program.ui) {
// print the dashboard UI

// start functions
const functions = spawnx(`[ -d '${api_location}' ] && (cd ${api_location}; func start --cors *) || echo 'No API found. Skipping.'`, []);
dashboard.stream("functions", functions);
const { dashboard } = require("../src/dashboard");
const spawnx = (command, args) =>
spawn(`${command}`, args, {
shell: true,
env: { ...process.env, ...envVarsObj },
cwd: path.resolve(__dirname, ".."),
});

// start auth
const auth = spawnx(`(cd ./src/auth/; func start --cors=* --port=${authUriPort})`, []);
dashboard.stream("auth", auth);
// start hosting
const hosting = spawnx(`${httpServerBin}`, `${app_artifact_location} -p ${appUriPort} -c-1`.split(" "));
dashboard.stream("hosting", hosting);

// start proxy
const status = spawnx(`node`, [`./src/proxy`]);
dashboard.stream("status", status);
// start functions
const functions = spawnx(`[ -d '${api_location}' ] && (cd ${api_location}; func start --cors *) || echo 'No API found. Skipping.'`, []);
dashboard.stream("functions", functions);

process.on("exit", () => {
process.exit(process.pid);
});
// start auth
const auth = spawnx(`(cd ./src/auth/; func start --cors=* --port=${authUriPort})`, []);
dashboard.stream("auth", auth);

// start proxy
const status = spawnx(`node`, [`./src/proxy`]);
dashboard.stream("status", status);

process.on("exit", () => {
process.exit(process.pid);
});
} else {
shell.exec(
startCommand.join(" "),
{
// set the cwd to the installation folder
cwd: path.resolve(__dirname, ".."),
},
(code, stdout, stderr) => {
if (stderr.length) {
console.error(stderr);
}
}
);
}

0 comments on commit 0831999

Please sign in to comment.