Skip to content

Commit

Permalink
fix(plugins): omit launch paths when unused [LIBS-477] (#791)
Browse files Browse the repository at this point in the history
* fix(cli): omit pluginLaunchPath when not used

* fix(pwa): allow pwa apps to be installable [LIBS-355]

* fix(plugins): don't app launchPath if unused
  • Loading branch information
KaiVandivier committed Mar 10, 2023
1 parent fb018b9 commit e49a51f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
41 changes: 36 additions & 5 deletions cli/src/lib/generateManifests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')

/**
* Gets the original `entrypoints` property in d2.config.js
* without applying defaults. Used to detect if there is actually
* supposed to be an app entrypoint for this... app. Temporary until
* the build process is redesigned to allow building plugins without
* apps (LIBS-479)
*/
const getOriginalEntrypoints = (paths) => {
try {
if (fs.existsSync(paths.config)) {
reporter.debug('Loading d2 config at', paths.config)
// NB: this import can be confounded by previous object mutations
const originalConfig = require(paths.config)
reporter.debug('loaded', originalConfig)
return originalConfig.entryPoints // may be undefined
}
} catch (e) {
reporter.error('Failed to load d2 config!')
reporter.error(e)
process.exit(1)
}
}
const parseCustomAuthorities = (authorities) => {
if (!authorities) {
return undefined
Expand Down Expand Up @@ -93,11 +115,20 @@ module.exports = (paths, config, publicUrl) => {
},
],
start_url: '.',
display: 'browser',
display: 'standalone',
theme_color: '#ffffff',
background_color: '#f4f6f8',
}

const includesPlugin = Boolean(config.entryPoints.plugin)
// If there's a plugin, there might not be an app intended to be exposed,
// in which case omit the app launch path. Check the original d2.config
// without added defaults to see if an app is intended.
// If there's not a plugin, default to 'true'
const shouldIncludeAppLaunchPath = includesPlugin
? Boolean(getOriginalEntrypoints(paths)?.app)
: true

// Legacy manifest
const manifestWebapp = {
app_hub_id: config.id,
Expand All @@ -108,8 +139,8 @@ module.exports = (paths, config, publicUrl) => {
version: config.version,
core_app: config.coreApp,

launch_path: paths.launchPath,
plugin_launch_path: paths.pluginLaunchPath,
launch_path: shouldIncludeAppLaunchPath ? paths.launchPath : undefined,
plugin_launch_path: includesPlugin ? paths.pluginLaunchPath : undefined,
default_locale: 'en',
activities: {
dhis: {
Expand Down Expand Up @@ -148,8 +179,8 @@ module.exports = (paths, config, publicUrl) => {
const appConfig = { ...config }
delete appConfig['entryPoints']
appConfig.entryPoints = {
app: paths.launchPath,
plugin: config.entryPoints.plugin ? paths.pluginLaunchPath : undefined,
app: shouldIncludeAppLaunchPath ? paths.launchPath : undefined,
plugin: includesPlugin ? paths.pluginLaunchPath : undefined,
}
delete appConfig['pwa']

Expand Down
7 changes: 5 additions & 2 deletions cli/src/lib/parseConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const { defaultsDeep, has, isPlainObject } = require('lodash')
const { defaultsDeep, cloneDeep, has, isPlainObject } = require('lodash')
const parseAuthorString = require('parse-author')

const requiredConfigFields = {
Expand Down Expand Up @@ -83,7 +83,10 @@ const parseConfig = (paths) => {

if (fs.existsSync(paths.config)) {
reporter.debug('Loading d2 config at', paths.config)
config = require(paths.config)
const importedConfig = require(paths.config)
// Make sure not to overwrite imported object
// (need to use it later in generateManifest)
config = cloneDeep(importedConfig)
reporter.debug('loaded', config)
}
if (fs.existsSync(paths.package)) {
Expand Down

0 comments on commit e49a51f

Please sign in to comment.