From 5d0ee49b07adf3dbdb872aae9851be1ee778db92 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Thu, 26 Sep 2019 21:43:41 +0200 Subject: [PATCH 1/3] feat: add support for standalone mode (default in development) --- cli/src/commands/build.js | 8 ++-- cli/src/lib/shell/env.js | 25 ------------ docs/scripts/start.md | 2 +- examples/simple-app/d2.config.js | 2 + shell/adapter/i18n/en.pot | 9 +++-- shell/adapter/src/AuthBoundary/LoginModal.js | 41 ++++++++++++++------ shell/adapter/src/AuthBoundary/index.js | 6 +-- shell/src/App.js | 4 +- 8 files changed, 48 insertions(+), 49 deletions(-) diff --git a/cli/src/commands/build.js b/cli/src/commands/build.js index ed4206537..e6c8798fd 100644 --- a/cli/src/commands/build.js +++ b/cli/src/commands/build.js @@ -52,9 +52,11 @@ const handler = async ({ const config = parseConfig(paths) const shell = makeShell({ config, paths }) - process.env.PUBLIC_URL = - process.env.PUBLIC_URL || - `/api/apps/${getUrlFriendlyName(config.title)}` + process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.' + if (!config.standalone) { + process.env.DHIS2_BASE_URL = + process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..` + } await fs.remove(paths.buildOutput) diff --git a/cli/src/lib/shell/env.js b/cli/src/lib/shell/env.js index 0a05dad99..6d0eeb82d 100644 --- a/cli/src/lib/shell/env.js +++ b/cli/src/lib/shell/env.js @@ -23,30 +23,6 @@ const prefixEnvForCRA = env => {} ) -const getDHISConfig = () => { - const dhisConfigPath = - process.env.DHIS2_HOME && `${process.env.DHIS2_HOME}/config` - - let dhisConfig - try { - dhisConfig = require(dhisConfigPath) - } catch (e) { - // Failed to load config file - use default config - reporter.debug(`\nWARNING! Failed to load DHIS config:`, e.message) - dhisConfig = { - baseUrl: 'http://localhost:8080', - authorization: 'Basic YWRtaW46ZGlzdHJpY3Q=', // admin:district - } - } - - return dhisConfig -} - -const envFromDHISConfig = config => ({ - DHIS2_BASE_URL: config.baseUrl, - DHIS2_AUTHORIZATION: config.authorization, -}) - const makeShellEnv = vars => Object.entries(vars).reduce( (out, [key, value]) => ({ @@ -59,7 +35,6 @@ const makeShellEnv = vars => module.exports = vars => { const env = { ...prefixEnvForCRA({ - ...envFromDHISConfig(getDHISConfig()), ...filterEnv(), ...makeShellEnv(vars), }), diff --git a/docs/scripts/start.md b/docs/scripts/start.md index 8326f2b04..3fb1665c8 100644 --- a/docs/scripts/start.md +++ b/docs/scripts/start.md @@ -1,6 +1,6 @@ # d2 app scripts start -Run a live-reloading development server on `localhost:3000` +Run a live-reloading development server on localhost > **NOTE**: This command is currently unsupported for libraries diff --git a/examples/simple-app/d2.config.js b/examples/simple-app/d2.config.js index 0b57ffcfd..0bf536ec9 100644 --- a/examples/simple-app/d2.config.js +++ b/examples/simple-app/d2.config.js @@ -3,6 +3,8 @@ const config = { title: 'Simple Example App', description: 'This is a simple example application', + standalone: true, // Don't bake-in a DHIS2 base URL, allow the user to choose + entryPoints: { app: './src/App', }, diff --git a/shell/adapter/i18n/en.pot b/shell/adapter/i18n/en.pot index c3307eade..e96ec3510 100644 --- a/shell/adapter/i18n/en.pot +++ b/shell/adapter/i18n/en.pot @@ -5,12 +5,15 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2019-06-18T09:28:13.445Z\n" -"PO-Revision-Date: 2019-06-18T09:28:13.445Z\n" +"POT-Creation-Date: 2019-09-26T16:40:43.483Z\n" +"PO-Revision-Date: 2019-09-26T16:40:43.483Z\n" msgid "Please sign in" msgstr "" +msgid "Server" +msgstr "" + msgid "Username" msgstr "" @@ -20,7 +23,7 @@ msgstr "" msgid "Sign in" msgstr "" -msgid "An error occurred in the DHIS2 Maps application." +msgid "An error occurred in the DHIS2 application." msgstr "" msgid "Something went wrong" diff --git a/shell/adapter/src/AuthBoundary/LoginModal.js b/shell/adapter/src/AuthBoundary/LoginModal.js index bf7da921f..fb60dee22 100644 --- a/shell/adapter/src/AuthBoundary/LoginModal.js +++ b/shell/adapter/src/AuthBoundary/LoginModal.js @@ -2,7 +2,12 @@ import React, { useState } from 'react' import i18n from '../locales' import { Modal, Button, InputField } from '@dhis2/ui-core' +const staticUrl = process.env.REACT_APP_DHIS2_BASE_URL + export const LoginModal = ({ url }) => { + const [server, setServer] = useState( + staticUrl || window.localStorage.DHIS2_BASE_URL || '' + ) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [isDirty, setIsDirty] = useState(false) @@ -12,18 +17,22 @@ export const LoginModal = ({ url }) => { const onSubmit = async e => { e.preventDefault() setIsDirty(true) - if (isValid(username) && isValid(password)) { + if (isValid(server) && isValid(username) && isValid(password)) { + window.localStorage.DHIS2_BASE_URL = server try { - await fetch(`${url}/dhis-web-commons-security/login.action`, { - method: 'POST', - credentials: 'include', - body: `j_username=${username}&j_password=${password}`, - headers: { - 'X-Requested-With': 'XMLHttpRequest', - Accept: 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded', - }, - }) + await fetch( + `${server}/dhis-web-commons-security/login.action`, + { + method: 'POST', + credentials: 'include', + body: `j_username=${username}&j_password=${password}`, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } + ) } catch (e) { console.log( 'TODO: This will always error and cancel the request until we get a real login endpoint!' @@ -40,6 +49,16 @@ export const LoginModal = ({ url }) => {
{i18n.t('Please sign in')} + {!staticUrl && ( + setServer(_ref.target.value)} + /> + )} { } if (error) { - if (error.type === 'access') { - return - } - console.log(JSON.stringify(error, undefined, 2)) - throw error + return } i18n.changeLanguage(data.userSettings.keyUiLocale) diff --git a/shell/src/App.js b/shell/src/App.js index fceec46d8..d006703c9 100644 --- a/shell/src/App.js +++ b/shell/src/App.js @@ -8,7 +8,9 @@ const D2App = React.lazy(() => ) // Automatic bundle splitting! const appConfig = { - url: process.env.REACT_APP_DHIS2_BASE_URL || 'http://localhost:8080', + url: + process.env.REACT_APP_DHIS2_BASE_URL || + window.localStorage.DHIS2_BASE_URL, appName: process.env.REACT_APP_DHIS2_APP_NAME || '', apiVersion: parseInt(process.env.REACT_APP_DHIS2_API_VERSION) || 32, } From fe4da92612c54fb7c4fc7c3f66e838a369c1ff90 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Thu, 26 Sep 2019 22:03:58 +0200 Subject: [PATCH 2/3] feat: allow cli overrides, print build parameters --- cli/src/commands/build.js | 28 ++++++++++++++++++++++++++-- examples/simple-app/yarn.lock | 2 +- shell/adapter/yarn.lock | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cli/src/commands/build.js b/cli/src/commands/build.js index e6c8798fd..c8c738b29 100644 --- a/cli/src/commands/build.js +++ b/cli/src/commands/build.js @@ -40,6 +40,7 @@ const handler = async ({ mode, dev, watch, + standalone, shell: shellSource, force, }) => { @@ -48,14 +49,31 @@ const handler = async ({ mode = mode || (dev && 'development') || getNodeEnv() || 'production' loadEnvFiles(paths, mode) - reporter.info(`Build mode: ${chalk.bold(mode)}`) + reporter.print(chalk.green('\tbuild mode\t', chalk.yellow(mode))) const config = parseConfig(paths) const shell = makeShell({ config, paths }) process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.' - if (!config.standalone) { + reporter.print( + chalk.green('\tPUBLIC_URL\t'), + chalk.bold(process.env.PUBLIC_URL) + ) + + if ( + standalone === false || + (typeof standalone === 'undefined' && !config.standalone) + ) { process.env.DHIS2_BASE_URL = process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..` + reporter.print( + chalk.green('\tDHIS2_BASE_URL\t'), + chalk.yellow(process.env.DHIS2_BASE_URL) + ) + } else { + reporter.print( + chalk.green('\tDHIS2_BASE_URL\t'), + chalk.yellow('standalone mode') + ) } await fs.remove(paths.buildOutput) @@ -141,6 +159,12 @@ const command = { description: 'Watch source files for changes', default: false, }, + standalone: { + type: 'boolean', + description: + 'Build in standalone mode (overrides the d2.config.js setting)', + default: undefined, + }, }, handler, } diff --git a/examples/simple-app/yarn.lock b/examples/simple-app/yarn.lock index db63f7c17..6c2ca58f5 100644 --- a/examples/simple-app/yarn.lock +++ b/examples/simple-app/yarn.lock @@ -923,7 +923,7 @@ integrity sha512-+Rmw+QshbgiBielN7UH4VXfEeSnSj1N3d4HGqf3iekyUXCNdpZX2bD9QrH43KvdfH+lDp71Uh7y3FKEhzSbRLA== "@dhis2/cli-app-scripts@file:../../cli": - version "1.4.2" + version "1.4.3" dependencies: "@babel/core" "^7.6.0" "@babel/plugin-proposal-class-properties" "^7.4.4" diff --git a/shell/adapter/yarn.lock b/shell/adapter/yarn.lock index 79b10d265..070a328d6 100644 --- a/shell/adapter/yarn.lock +++ b/shell/adapter/yarn.lock @@ -851,7 +851,7 @@ integrity sha512-+Rmw+QshbgiBielN7UH4VXfEeSnSj1N3d4HGqf3iekyUXCNdpZX2bD9QrH43KvdfH+lDp71Uh7y3FKEhzSbRLA== "@dhis2/cli-app-scripts@file:../../cli": - version "1.4.2" + version "1.4.3" dependencies: "@babel/core" "^7.6.0" "@babel/plugin-proposal-class-properties" "^7.4.4" From 1fcdd8e341dbdc578c32f400dfecabb3204ef301 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Thu, 26 Sep 2019 22:16:43 +0200 Subject: [PATCH 3/3] chore: update log format --- cli/src/commands/build.js | 51 +++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/cli/src/commands/build.js b/cli/src/commands/build.js index c8c738b29..f5e24edda 100644 --- a/cli/src/commands/build.js +++ b/cli/src/commands/build.js @@ -27,13 +27,24 @@ const getNodeEnv = () => { return null } -// This is nasty and frankly wrong (also don't use long unweildy capitalized names in a URL!) but has to match the current DHIS2 app server -// From https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-api/src/main/java/org/hisp/dhis/appmanager/App.java#L360-L371 -const getUrlFriendlyName = name => - name - .trim() - .replace(/[^A-Za-z0-9\s-]/g, '') - .replace(/ /g, '-') +const printBuildParam = (key, value) => { + reporter.print(chalk.green(` - ${key} =`), chalk.yellow(value)) +} +const setAppParameters = (standalone, config) => { + process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.' + printBuildParam('PUBLIC_URL', process.env.PUBLIC_URL) + + if ( + standalone === false || + (typeof standalone === 'undefined' && !config.standalone) + ) { + process.env.DHIS2_BASE_URL = + process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..` + printBuildParam('DHIS2_BASE_URL', process.env.DHIS2_BASE_URL) + } else { + printBuildParam('DHIS2_BASE_URL', '') + } +} const handler = async ({ cwd, @@ -49,31 +60,13 @@ const handler = async ({ mode = mode || (dev && 'development') || getNodeEnv() || 'production' loadEnvFiles(paths, mode) - reporter.print(chalk.green('\tbuild mode\t', chalk.yellow(mode))) + printBuildParam('Build Mode', mode) + const config = parseConfig(paths) const shell = makeShell({ config, paths }) - process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.' - reporter.print( - chalk.green('\tPUBLIC_URL\t'), - chalk.bold(process.env.PUBLIC_URL) - ) - - if ( - standalone === false || - (typeof standalone === 'undefined' && !config.standalone) - ) { - process.env.DHIS2_BASE_URL = - process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..` - reporter.print( - chalk.green('\tDHIS2_BASE_URL\t'), - chalk.yellow(process.env.DHIS2_BASE_URL) - ) - } else { - reporter.print( - chalk.green('\tDHIS2_BASE_URL\t'), - chalk.yellow('standalone mode') - ) + if (config.type === 'app') { + setAppParameters(standalone, config) } await fs.remove(paths.buildOutput)