From 50d1606105c015c000bfe622ffbe94545ebf7744 Mon Sep 17 00:00:00 2001 From: Borys_Levytskyi Date: Thu, 14 Jan 2021 20:48:19 +0200 Subject: [PATCH] Refactor towards modular structure --- src/commands.tsx | 86 ------------------- .../components}/BinaryString.tsx | 0 src/{ => core}/components/CommandLink.tsx | 2 +- .../components}/BitwiseExpressionModel.ts | 4 +- .../BitwiseOperationExpressionView.tsx | 8 +- src/expression/module.tsx | 21 +++++ src/index.tsx | 72 ++++++---------- .../components}/IpAddressView.css | 0 .../components}/IpAddressView.tsx | 4 +- src/{ipaddress => networking}/ip.test.ts | 0 src/{ipaddress => networking}/ip.ts | 0 src/networking/module.tsx | 47 ++++++++++ src/{core => shell}/AppState.ts | 0 src/{core => shell}/appStateStore.ts | 0 src/{core => shell}/cmd.test.ts | 1 - src/{core => shell}/cmd.ts | 2 +- .../components}/AboutResultView.tsx | 0 src/{ => shell/components}/AppRoot.tsx | 14 +-- .../components/DisplayResultView.tsx | 2 +- .../components}/ErrorResultView.tsx | 0 .../components}/HelpResultView.css | 0 .../components}/HelpResultView.tsx | 2 +- src/{ => shell}/components/Indicators.tsx | 2 +- src/{ => shell}/components/InputBox.tsx | 0 .../components}/TextResultView.tsx | 0 .../components}/UnknownInputResultView.tsx | 0 .../components}/WhatsNewResultView.css | 0 .../components}/WhatsNewResultView.tsx | 2 +- src/shell/interfaces.ts | 8 ++ src/shell/module.tsx | 36 ++++++++ src/shell/startup.ts | 62 +++++++++++++ 31 files changed, 221 insertions(+), 154 deletions(-) delete mode 100644 src/commands.tsx rename src/{components/results => core/components}/BinaryString.tsx (100%) rename src/{ => core}/components/CommandLink.tsx (89%) rename src/{components/results/expressions => expression/components}/BitwiseExpressionModel.ts (97%) rename src/{components/results/expressions => expression/components}/BitwiseOperationExpressionView.tsx (93%) create mode 100644 src/expression/module.tsx rename src/{components/results => networking/components}/IpAddressView.css (100%) rename src/{components/results => networking/components}/IpAddressView.tsx (95%) rename src/{ipaddress => networking}/ip.test.ts (100%) rename src/{ipaddress => networking}/ip.ts (100%) create mode 100644 src/networking/module.tsx rename src/{core => shell}/AppState.ts (100%) rename src/{core => shell}/appStateStore.ts (100%) rename src/{core => shell}/cmd.test.ts (96%) rename src/{core => shell}/cmd.ts (98%) rename src/{components/results => shell/components}/AboutResultView.tsx (100%) rename src/{ => shell/components}/AppRoot.tsx (89%) rename src/{ => shell}/components/DisplayResultView.tsx (94%) rename src/{components/results => shell/components}/ErrorResultView.tsx (100%) rename src/{components/results => shell/components}/HelpResultView.css (100%) rename src/{components/results => shell/components}/HelpResultView.tsx (98%) rename src/{ => shell}/components/Indicators.tsx (93%) rename src/{ => shell}/components/InputBox.tsx (100%) rename src/{components/results => shell/components}/TextResultView.tsx (100%) rename src/{components/results => shell/components}/UnknownInputResultView.tsx (100%) rename src/{components/results => shell/components}/WhatsNewResultView.css (100%) rename src/{components/results => shell/components}/WhatsNewResultView.tsx (97%) create mode 100644 src/shell/interfaces.ts create mode 100644 src/shell/module.tsx create mode 100644 src/shell/startup.ts diff --git a/src/commands.tsx b/src/commands.tsx deleted file mode 100644 index 7923b38..0000000 --- a/src/commands.tsx +++ /dev/null @@ -1,86 +0,0 @@ -import * as expression from './expression/expression'; -import React from 'react'; -import uuid from 'uuid/v4'; -import { CommandInput, CmdShell } from './core/cmd'; -import AppState from './core/AppState'; -import {ParsingError, IpAddress, ipAddressParser, IpAddressWithSubnetMask, ParsedIpObject} from './ipaddress/ip' -import ErrorResultView from './components/results/ErrorResultView'; -import HelpResultView from './components/results/HelpResultView'; -import AboutResultView from './components/results/AboutResultView'; -import WhatsnewResultView from './components/results/WhatsNewResultView'; -import TextResultView from './components/results/TextResultView'; -import IpAddressView from './components/results/IpAddressView'; -import UnknownInputResultView from './components/results/UnknownInputResultView'; -import BitwiseOperationExpressionView from './components/results/expressions/BitwiseOperationExpressionView'; - -export default { - initialize (cmd: CmdShell, appState: AppState) { - - cmd.debugMode = appState.debugMode; - appState.onChange(() => cmd.debugMode = appState.debugMode); - - cmd.command("help", (c: CommandInput) => appState.addCommandResult(c.input, )); - cmd.command("clear", () => appState.clearCommandResults()); - cmd.command("em", () => appState.toggleEmphasizeBytes()); - cmd.command("dark", () => appState.setUiTheme('dark')); - cmd.command("light", () => appState.setUiTheme('light')); - cmd.command("midnight", () => appState.setUiTheme('midnight')); - cmd.command("about", (c: CommandInput) => appState.addCommandResult(c.input, )); - cmd.command("whatsnew", (c: CommandInput) => appState.addCommandResult(c.input, )); - cmd.command("guid", (c: CommandInput) => appState.addCommandResult(c.input, )); - cmd.command("-notrack", () => {}); - cmd.command("-debug", (c: CommandInput) => { - appState.toggleDebugMode(); - appState.addCommandResult(c.input, ); - }); - - - // Ip Addresses - cmd.command({ - canHandle: (input:string) => ipAddressParser.parse(input) != null, - handle: function(c: CommandInput) { - var result = ipAddressParser.parse(c.input); - - if(result == null) - return; - - if(result instanceof ParsingError) { - appState.addCommandResult(c.input, ); - return; - } - - const ipAddresses : IpAddress[] = []; - - (result as ParsedIpObject[]).forEach(r => { - if(r instanceof IpAddressWithSubnetMask) - { - ipAddresses.push(r.ipAddress); - ipAddresses.push(r.createSubnetMaskIp()); - } - else if(r instanceof IpAddress) { - ipAddresses.push(r); - } - }); - - appState.addCommandResult(c.input, ); - } - }) - - // Bitwise Expressions - cmd.command({ - canHandle: (input:string) => expression.parser.canParse(input), - handle: function(c: CommandInput) { - var expr = expression.parser.parse(c.input); - appState.addCommandResult(c.input, ); - } - }) - - // Last command handler reports that input is unknown - cmd.command({ - canHandle: () => true, - handle: (c: CommandInput) => appState.addCommandResult(c.input, ) - }); - - cmd.onError((input: string, err: Error) => appState.addCommandResult(input, )); - } - } \ No newline at end of file diff --git a/src/components/results/BinaryString.tsx b/src/core/components/BinaryString.tsx similarity index 100% rename from src/components/results/BinaryString.tsx rename to src/core/components/BinaryString.tsx diff --git a/src/components/CommandLink.tsx b/src/core/components/CommandLink.tsx similarity index 89% rename from src/components/CommandLink.tsx rename to src/core/components/CommandLink.tsx index f6f8dd7..1f76e20 100644 --- a/src/components/CommandLink.tsx +++ b/src/core/components/CommandLink.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import cmd from '../core/cmd'; +import cmd from '../../shell/cmd'; type CommandLinkProps = { command?:string; diff --git a/src/components/results/expressions/BitwiseExpressionModel.ts b/src/expression/components/BitwiseExpressionModel.ts similarity index 97% rename from src/components/results/expressions/BitwiseExpressionModel.ts rename to src/expression/components/BitwiseExpressionModel.ts index 70c6369..ee1734f 100644 --- a/src/components/results/expressions/BitwiseExpressionModel.ts +++ b/src/expression/components/BitwiseExpressionModel.ts @@ -1,5 +1,5 @@ -import { NumericOperand, ListOfNumbersExpression, BitwiseOperationExpression, ExpressionOperand } from '../../../expression/expression'; -import { ExpressionInputItem, ExpressionInput } from '../../../expression/expression-interfaces'; +import { NumericOperand, ListOfNumbersExpression, BitwiseOperationExpression, ExpressionOperand } from '../expression'; +import { ExpressionInputItem, ExpressionInput } from '../expression-interfaces'; type Config = { emphasizeBytes: boolean; diff --git a/src/components/results/expressions/BitwiseOperationExpressionView.tsx b/src/expression/components/BitwiseOperationExpressionView.tsx similarity index 93% rename from src/components/results/expressions/BitwiseOperationExpressionView.tsx rename to src/expression/components/BitwiseOperationExpressionView.tsx index fa70079..4da004a 100644 --- a/src/components/results/expressions/BitwiseOperationExpressionView.tsx +++ b/src/expression/components/BitwiseOperationExpressionView.tsx @@ -1,9 +1,9 @@ import React from 'react'; -import formatter from '../../../core/formatter'; -import BinaryStringView, { FlipBitEventArg } from '../BinaryString'; +import formatter from '../../core/formatter'; +import BinaryStringView, { FlipBitEventArg } from '../../core/components/BinaryString'; import BitwiseExpressionViewModel from './BitwiseExpressionModel'; -import { ExpressionInput, ExpressionInputItem } from '../../../expression/expression-interfaces'; -import { ExpressionOperand, NumericOperand } from '../../../expression/expression'; +import { ExpressionInput, ExpressionInputItem } from '../expression-interfaces'; +import { ExpressionOperand, NumericOperand } from '../expression'; type BitwiseOperationExpressionViewProps = { expression: ExpressionInput; diff --git a/src/expression/module.tsx b/src/expression/module.tsx new file mode 100644 index 0000000..eb37fe1 --- /dev/null +++ b/src/expression/module.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import AppState from '../shell/AppState'; +import { CmdShell, CommandInput } from '../shell/cmd'; +import BitwiseOperationExpressionView from './components/BitwiseOperationExpressionView'; +import {parser} from './expression'; + +const expressionAppModule = { + setup: function(appState: AppState, cmd: CmdShell) { + + // Bitwise Expressions + cmd.command({ + canHandle: (input:string) => parser.canParse(input), + handle: function(c: CommandInput) { + var expr = parser.parse(c.input); + appState.addCommandResult(c.input, ); + } + }); + } +} + +export default expressionAppModule; \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 0c11900..1934c22 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,61 +1,41 @@ -import AppState from './core/AppState'; -import appStateStore from './core/appStateStore'; -import cmd from './core/cmd'; -import commands from './commands'; -import AppRoot from './AppRoot'; -import hash from './core/hash'; +import cmd, { CommandInput } from './shell/cmd'; +import AppRoot from './shell/components/AppRoot'; import log from 'loglevel'; import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; +import networkingAppModule from './networking/module'; +import expressionAppModule from './expression/module'; +import shellModule from './shell/module'; +import bootstrapAppData from './shell/startup'; +import UnknownInputResultView from './shell/components/UnknownInputResultView'; -const env = window.location.host === "bitwisecmd.com" ? 'prod' : 'stage'; -setupLogger(env); - -const appState = createAppState(env); - -commands.initialize(cmd, appState); +const appData = bootstrapAppData(); +initializeModules(); executeStartupCommands(); -var root = ; +var root = ; ReactDOM.render(root, document.getElementById('root')); log.debug("started"); -function createAppState(env:string) { - var stateData = appStateStore.getPersistedData(); - const appState = new AppState(stateData, env); - appStateStore.watch(appState); - log.debug("appState initialized", appState); - return appState; -} - -function setupLogger(env: Env) { - if(env != 'prod'){ - log.setLevel("debug"); - log.debug(`Log level is set to debug. Env: ${env}`) - } else { - log.setLevel("warn"); - } -} - function executeStartupCommands() { - var hashArgs = hash.getArgs(window.location.hash); - - var startupCommands = ['help', '127.0.0.1 192.168.0.0/8', '1|2&6','4 0b1000000 0x80']; - - if(appState.wasOldVersion) { - startupCommands = ["whatsnew"]; - } - - if(hashArgs.length > 0) { - startupCommands = hashArgs; - } - - log.debug('Executing startup commands', startupCommands); - - startupCommands.forEach(cmd.execute.bind(cmd)); + log.debug("Executing startup commands", appData.startupCommands); + appData.startupCommands.forEach(cmd.execute.bind(cmd)); } -type Env = 'prod' | 'stage'; +function initializeModules() { + shellModule.setup(appData.appState, cmd); + networkingAppModule.setup(appData.appState, cmd); + expressionAppModule.setup(appData.appState, cmd); + + // Last command handler reports that input is unknown + cmd.command({ + canHandle: () => true, + handle: (c: CommandInput) => appData.appState.addCommandResult(c.input, ) + }); +} + + + diff --git a/src/components/results/IpAddressView.css b/src/networking/components/IpAddressView.css similarity index 100% rename from src/components/results/IpAddressView.css rename to src/networking/components/IpAddressView.css diff --git a/src/components/results/IpAddressView.tsx b/src/networking/components/IpAddressView.tsx similarity index 95% rename from src/components/results/IpAddressView.tsx rename to src/networking/components/IpAddressView.tsx index a6aea21..57d7631 100644 --- a/src/components/results/IpAddressView.tsx +++ b/src/networking/components/IpAddressView.tsx @@ -1,7 +1,7 @@ import React from 'react'; -import { IpAddress, OctetNumber, getNetworkClass } from '../../ipaddress/ip'; +import { IpAddress, OctetNumber, getNetworkClass } from '../ip'; import formatter from '../../core/formatter' -import BinaryStringView from './BinaryString'; +import BinaryStringView from '../../core/components/BinaryString'; import './IpAddressView.css'; type IpAddressViewProps = { ipAddresses: IpAddress[] diff --git a/src/ipaddress/ip.test.ts b/src/networking/ip.test.ts similarity index 100% rename from src/ipaddress/ip.test.ts rename to src/networking/ip.test.ts diff --git a/src/ipaddress/ip.ts b/src/networking/ip.ts similarity index 100% rename from src/ipaddress/ip.ts rename to src/networking/ip.ts diff --git a/src/networking/module.tsx b/src/networking/module.tsx new file mode 100644 index 0000000..f75ea14 --- /dev/null +++ b/src/networking/module.tsx @@ -0,0 +1,47 @@ +import React from 'react'; +import AppState from '../shell/AppState'; +import { CmdShell, CommandInput } from '../shell/cmd'; +import ErrorResultView from '../shell/components/ErrorResultView'; +import IpAddressView from './components/IpAddressView'; +import { ipAddressParser, ParsingError, IpAddress, ParsedIpObject, IpAddressWithSubnetMask } from './ip'; +import log from 'loglevel'; + +const networkingAppModule = { + setup: function(appState: AppState, cmd: CmdShell) { + + // Add Ip Address commands + cmd.command({ + canHandle: (input:string) => ipAddressParser.parse(input) != null, + handle: function(c: CommandInput) { + var result = ipAddressParser.parse(c.input); + + if(result == null) + return; + + if(result instanceof ParsingError) { + appState.addCommandResult(c.input, ); + return; + } + + const ipAddresses : IpAddress[] = []; + + (result as ParsedIpObject[]).forEach(r => { + if(r instanceof IpAddressWithSubnetMask) + { + ipAddresses.push(r.ipAddress); + ipAddresses.push(r.createSubnetMaskIp()); + } + else if(r instanceof IpAddress) { + ipAddresses.push(r); + } + }); + + appState.addCommandResult(c.input, ); + } + }); + + log.debug(); + } +} + +export default networkingAppModule; \ No newline at end of file diff --git a/src/core/AppState.ts b/src/shell/AppState.ts similarity index 100% rename from src/core/AppState.ts rename to src/shell/AppState.ts diff --git a/src/core/appStateStore.ts b/src/shell/appStateStore.ts similarity index 100% rename from src/core/appStateStore.ts rename to src/shell/appStateStore.ts diff --git a/src/core/cmd.test.ts b/src/shell/cmd.test.ts similarity index 96% rename from src/core/cmd.test.ts rename to src/shell/cmd.test.ts index 25fb8da..06bbde0 100644 --- a/src/core/cmd.test.ts +++ b/src/shell/cmd.test.ts @@ -1,5 +1,4 @@ import { CmdShell, ICommandHandler } from "./cmd"; -import { func } from "prop-types"; describe("CmdShell", () => { it("simple command", () => { diff --git a/src/core/cmd.ts b/src/shell/cmd.ts similarity index 98% rename from src/core/cmd.ts rename to src/shell/cmd.ts index 025137d..3f9f73f 100644 --- a/src/core/cmd.ts +++ b/src/shell/cmd.ts @@ -1,4 +1,4 @@ -import is from './is'; +import is from '../core/is'; import log from 'loglevel'; export type CommandInput = { diff --git a/src/components/results/AboutResultView.tsx b/src/shell/components/AboutResultView.tsx similarity index 100% rename from src/components/results/AboutResultView.tsx rename to src/shell/components/AboutResultView.tsx diff --git a/src/AppRoot.tsx b/src/shell/components/AppRoot.tsx similarity index 89% rename from src/AppRoot.tsx rename to src/shell/components/AppRoot.tsx index d471bca..75f7b87 100644 --- a/src/AppRoot.tsx +++ b/src/shell/components/AppRoot.tsx @@ -1,11 +1,11 @@ import React from 'react'; -import InputBox from './components/InputBox'; -import DisplayResultView from './components/DisplayResultView'; -import AppState, { CommandResultView } from './core/AppState'; -import cmd from './core/cmd'; +import InputBox from './InputBox'; +import DisplayResultView from './DisplayResultView'; +import AppState, { CommandResultView } from '../AppState'; +import cmd from '../cmd'; import log from 'loglevel'; -import Indicators from './components/Indicators'; -import hash from './core/hash'; +import Indicators from './Indicators'; +import hash from '../../core/hash'; type AppRootProps = { appState: AppState, @@ -33,7 +33,7 @@ export default class AppRoot extends React.Component } getResultViews() : JSX.Element[] { - log.debug('getting result views') + var results = this.state.commandResults.map((r, i) => {r.view} diff --git a/src/components/DisplayResultView.tsx b/src/shell/components/DisplayResultView.tsx similarity index 94% rename from src/components/DisplayResultView.tsx rename to src/shell/components/DisplayResultView.tsx index fd06d95..f055e78 100644 --- a/src/components/DisplayResultView.tsx +++ b/src/shell/components/DisplayResultView.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import AppState from '../core/AppState'; +import AppState from '../AppState'; type DisplayResultProps = { diff --git a/src/components/results/ErrorResultView.tsx b/src/shell/components/ErrorResultView.tsx similarity index 100% rename from src/components/results/ErrorResultView.tsx rename to src/shell/components/ErrorResultView.tsx diff --git a/src/components/results/HelpResultView.css b/src/shell/components/HelpResultView.css similarity index 100% rename from src/components/results/HelpResultView.css rename to src/shell/components/HelpResultView.css diff --git a/src/components/results/HelpResultView.tsx b/src/shell/components/HelpResultView.tsx similarity index 98% rename from src/components/results/HelpResultView.tsx rename to src/shell/components/HelpResultView.tsx index 56c5216..ffd510f 100644 --- a/src/components/results/HelpResultView.tsx +++ b/src/shell/components/HelpResultView.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import CommandLink from '../CommandLink'; +import CommandLink from '../../core/components/CommandLink'; import './HelpResultView.css'; function HelpResultView() { diff --git a/src/components/Indicators.tsx b/src/shell/components/Indicators.tsx similarity index 93% rename from src/components/Indicators.tsx rename to src/shell/components/Indicators.tsx index fd0d517..3f98eb7 100644 --- a/src/components/Indicators.tsx +++ b/src/shell/components/Indicators.tsx @@ -1,4 +1,4 @@ -import AppState from "../core/AppState"; +import AppState from "../AppState"; import React from "react"; type IndicatorsProps = { diff --git a/src/components/InputBox.tsx b/src/shell/components/InputBox.tsx similarity index 100% rename from src/components/InputBox.tsx rename to src/shell/components/InputBox.tsx diff --git a/src/components/results/TextResultView.tsx b/src/shell/components/TextResultView.tsx similarity index 100% rename from src/components/results/TextResultView.tsx rename to src/shell/components/TextResultView.tsx diff --git a/src/components/results/UnknownInputResultView.tsx b/src/shell/components/UnknownInputResultView.tsx similarity index 100% rename from src/components/results/UnknownInputResultView.tsx rename to src/shell/components/UnknownInputResultView.tsx diff --git a/src/components/results/WhatsNewResultView.css b/src/shell/components/WhatsNewResultView.css similarity index 100% rename from src/components/results/WhatsNewResultView.css rename to src/shell/components/WhatsNewResultView.css diff --git a/src/components/results/WhatsNewResultView.tsx b/src/shell/components/WhatsNewResultView.tsx similarity index 97% rename from src/components/results/WhatsNewResultView.tsx rename to src/shell/components/WhatsNewResultView.tsx index dba2011..9d5f615 100644 --- a/src/components/results/WhatsNewResultView.tsx +++ b/src/shell/components/WhatsNewResultView.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import CommandLink from '../CommandLink'; +import CommandLink from '../../core/components/CommandLink'; import './WhatsNewResultView.css'; function WhatsnewResultView() { diff --git a/src/shell/interfaces.ts b/src/shell/interfaces.ts new file mode 100644 index 0000000..d967c23 --- /dev/null +++ b/src/shell/interfaces.ts @@ -0,0 +1,8 @@ +import AppState from "./AppState"; +import { CmdShell } from "./cmd"; + +export type Env = 'prod' | 'stage'; + +export type AppModule = { + setup: (appState: AppState, cmd: CmdShell) => void; +}; \ No newline at end of file diff --git a/src/shell/module.tsx b/src/shell/module.tsx new file mode 100644 index 0000000..9d38426 --- /dev/null +++ b/src/shell/module.tsx @@ -0,0 +1,36 @@ +import React from 'react'; +import uuid from 'uuid'; +import AppState from './AppState'; +import { CmdShell, CommandInput } from './cmd'; +import AboutResultView from './components/AboutResultView'; +import ErrorResultView from './components/ErrorResultView'; +import HelpResultView from './components/HelpResultView'; +import TextResultView from './components/TextResultView'; +import WhatsnewResultView from './components/WhatsNewResultView'; + +const shellModule = { + setup: function(appState: AppState, cmd: CmdShell) { + + cmd.debugMode = appState.debugMode; + appState.onChange(() => cmd.debugMode = appState.debugMode); + + cmd.command("help", (c: CommandInput) => appState.addCommandResult(c.input, )); + cmd.command("clear", () => appState.clearCommandResults()); + cmd.command("em", () => appState.toggleEmphasizeBytes()); + cmd.command("dark", () => appState.setUiTheme('dark')); + cmd.command("light", () => appState.setUiTheme('light')); + cmd.command("midnight", () => appState.setUiTheme('midnight')); + cmd.command("about", (c: CommandInput) => appState.addCommandResult(c.input, )); + cmd.command("whatsnew", (c: CommandInput) => appState.addCommandResult(c.input, )); + cmd.command("guid", (c: CommandInput) => appState.addCommandResult(c.input, )); + cmd.command("-notrack", () => {}); + cmd.command("-debug", (c: CommandInput) => { + appState.toggleDebugMode(); + appState.addCommandResult(c.input, ); + }); + + cmd.onError((input: string, err: Error) => appState.addCommandResult(input, )); + } +} + +export default shellModule; \ No newline at end of file diff --git a/src/shell/startup.ts b/src/shell/startup.ts new file mode 100644 index 0000000..251ef2f --- /dev/null +++ b/src/shell/startup.ts @@ -0,0 +1,62 @@ +import log from 'loglevel'; +import hash from '../core/hash'; +import AppState from './AppState'; +import { Env } from './interfaces'; +import appStateStore from './appStateStore'; + +export type StartupAppData = { + appState: AppState, + startupCommands: string[] +} + +function bootstrapAppData() : StartupAppData { + const env = window.location.host === "bitwisecmd.com" ? 'prod' : 'stage'; + + setupLogger(env); + + const appState = createAppState(env); + const startupCommands = getStartupCommands(appState); + + return { + appState, + startupCommands + } +} + + +function createAppState(env:string) { + var stateData = appStateStore.getPersistedData(); + const appState = new AppState(stateData, env); + appStateStore.watch(appState); + log.debug("appState initialized", appState); + return appState; +} + +function getStartupCommands(appState : AppState) : string[] { + var hashArgs = hash.getArgs(window.location.hash); + + var startupCommands = ['help', '127.0.0.1 192.168.0.0/8', '1|2&6','4 0b1000000 0x80']; + + if(appState.wasOldVersion) { + startupCommands = ["whatsnew"]; + } + + if(hashArgs.length > 0) { + startupCommands = hashArgs; + } + + log.debug('Executing startup commands', startupCommands); + + return startupCommands; +} + +function setupLogger(env: Env) { + if(env != 'prod'){ + log.setLevel("debug"); + log.debug(`Log level is set to debug. Env: ${env}`) + } else { + log.setLevel("warn"); + } +} + +export default bootstrapAppData; \ No newline at end of file