Skip to content

Commit

Permalink
feat: parse additional namespaces from d2.config.js and add to `man…
Browse files Browse the repository at this point in the history
…ifest.webapp` [LIBS-638] (#860)

* feat: add `additionalNamespaces` config to manifest.webapp

* docs: additional namespaces

* docs: fix typo
  • Loading branch information
KaiVandivier authored Jul 3, 2024
1 parent 0d7d4de commit 62782fe
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 21 deletions.
5 changes: 5 additions & 0 deletions cli/src/lib/generateManifests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const { getOriginalEntrypoints } = require('./getOriginalEntrypoints')
const { parseAdditionalNamespaces } = require('./parseAdditionalNamespaces')

const parseCustomAuthorities = (authorities) => {
if (!authorities) {
Expand Down Expand Up @@ -90,6 +91,7 @@ module.exports = (paths, config, publicUrl) => {
},
{
src: 'safari-pinned-tab.svg',
sizes: '16x16',
type: 'image/svg+xml',
},
],
Expand Down Expand Up @@ -126,6 +128,9 @@ module.exports = (paths, config, publicUrl) => {
dhis: {
href: '*',
namespace: parseDataStoreNamespace(config.dataStoreNamespace),
additionalNamespaces: parseAdditionalNamespaces(
config.additionalNamespaces
),
},
},
authorities: parseCustomAuthorities(config.customAuthorities),
Expand Down
62 changes: 62 additions & 0 deletions cli/src/lib/parseAdditionalNamespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')

const parseAdditionalNamespaces = (additionalNamespaces) => {
if (!additionalNamespaces) {
return undefined
}
if (!Array.isArray(additionalNamespaces)) {
reporter.warn(
`Invalid value ${chalk.bold(
JSON.stringify(additionalNamespaces)
)} specified for ${chalk.bold(
'additionalNamespaces'
)} -- must be an array of objects, skipping.`
)
return undefined
}

const filteredNamespaces = additionalNamespaces.filter(
(additionalNamespace, index) => {
const msg = `Invalid namespace ${chalk.bold(
JSON.stringify(additionalNamespace)
)} specified at ${chalk.bold(`index ${index}`)} of ${chalk.bold(
'additionalNamespaces'
)} -- see d2.config.js documentation for the correct form. Skipping.`
const {
namespace,
authorities,
readAuthorities,
writeAuthorities,
} = additionalNamespace

const namespacePropIsString = typeof namespace === 'string'
const definedAuthsProps = [
authorities,
readAuthorities,
writeAuthorities,
].filter((auths) => auths !== undefined)
const definedAuthsPropsAreValid =
Array.isArray(definedAuthsProps) &&
definedAuthsProps.every(
(auths) =>
Array.isArray(auths) &&
auths.every((auth) => typeof auth === 'string')
)

const additionalNamespaceIsValid =
namespacePropIsString &&
definedAuthsProps.length > 0 &&
definedAuthsPropsAreValid
if (!additionalNamespaceIsValid) {
reporter.warn(msg)
return false // skip this additional namespace
}

return true
}
)

return filteredNamespaces
}

exports.parseAdditionalNamespaces = parseAdditionalNamespaces
79 changes: 79 additions & 0 deletions cli/src/lib/parseAdditionalNamespaces.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { reporter } = require('@dhis2/cli-helpers-engine')
const { parseAdditionalNamespaces } = require('./parseAdditionalNamespaces')

jest.mock('@dhis2/cli-helpers-engine', () => ({
reporter: { warn: jest.fn() },
chalk: { bold: jest.fn().mockImplementation((input) => input) },
}))

test('undefined', () => {
const output = parseAdditionalNamespaces(undefined)
expect(output).toBe(undefined)
expect(reporter.warn).toHaveBeenCalledTimes(0)
})

test('the happy path', () => {
const additionalNamespaces = [
{ namespace: 'extra1', authorities: ['M_extra1'] },
{ namespace: 'extra2', readAuthorities: ['M_extra2read'] },
{ namespace: 'extra3', writeAuthorities: ['M_extra3write'] },
{
namespace: 'extra4',
authorities: ['M_extra4readwrite'],
writeAuthotities: ['M_extra4write'],
},
]

const output = parseAdditionalNamespaces(additionalNamespaces)
expect(output).toEqual(additionalNamespaces)
expect(reporter.warn).toHaveBeenCalledTimes(0)
})

describe('handling faults', () => {
test('additionalNamespaces is not an array', () => {
const additionalNamespaces = {
namespace: 'extra1',
authorities: ['M_extra1'],
}

const output = parseAdditionalNamespaces(additionalNamespaces)

expect(output).toBe(undefined)
expect(reporter.warn).toHaveBeenCalledTimes(1)
})

test('invalid namespace options get filtered out', () => {
const testNamespaces = [
{ namespace: 'no-authorities' },
{ authorities: ['F_MISSING-NAMESPACE-STRING'] },
{
namespace: 'valid-namespace-1',
readAuthorities: ['M_extra-read'],
writeAuthorities: ['M_extra-write'],
},
{ namespace: ['not-a-string'], readAuthorities: ['M_extra2read'] },
{
namespace: 'invalid-value-type',
readAuthorities: 'should-be-an-array',
},
{
namespace: 'one-correct-auths-prop-one-error',
readAuthorities: ['M_extra-read'],
writeAuthorities: 'should-be-an-array',
},
{
namespace: 'valid-namespace-2',
writeAuthorities: ['M_extra-write'],
},
{
namespace: 'valid-namespace-3',
authorities: ['M_extra-readwrite'],
writeAuthotities: ['M_extra-write'],
},
]

const output = parseAdditionalNamespaces(testNamespaces)
expect(output).toEqual([testNamespaces[2], ...testNamespaces.slice(-2)])
expect(reporter.warn).toHaveBeenCalledTimes(6)
})
})
Loading

0 comments on commit 62782fe

Please sign in to comment.