Skip to content

Commit 5de5652

Browse files
authored
feat(AI): Conditionally load nr-assistant dependency based on feature flag (#640)
1 parent 8a0578f commit 5de5652

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/launcher.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class Launcher {
7171
debug(`Updating package.json: ${this.files.packageJSON}`)
7272
const packageData = JSON.parse(JSON.stringify(packageJSONTemplate))
7373
packageData.dependencies = JSON.parse(JSON.stringify(this.snapshot.modules))
74+
// if assistant is explicitly disabled, remove the assistant plugin from dependencies
75+
if (this.settings?.assistant?.enabled === false) {
76+
delete packageData.dependencies['@flowfuse/nr-assistant']
77+
}
7478
// if we are working in a development env and the project nodes src is available in the right place,
7579
// then use the development version of the project nodes
7680
if (packageData.dependencies?.['@flowfuse/nr-project-nodes'] && process.env.NODE_ENV === 'development') {

test/unit/lib/launcher_spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,35 @@ describe('Launcher', function () {
416416
pkg.name.should.eqls('TEST_PROJECT')
417417
pkg.version.should.eqls('0.0.0-aaaabbbbcccc')
418418
})
419+
it('Removes nr-assistant from package.json when assistant is explicitly disabled', async function () {
420+
const snapshot = JSON.parse(JSON.stringify(setup.snapshot))
421+
snapshot.modules['@flowfuse/nr-assistant'] = '0.1.0'
422+
const settings = { assistant: { enabled: false } }
423+
const launcher = newLauncher({ config }, null, 'projectId', snapshot, settings)
424+
await launcher.writePackage()
425+
const pkgFile = await fs.readFile(path.join(config.dir, 'project', 'package.json'))
426+
const pkg = JSON.parse(pkgFile)
427+
pkg.dependencies.should.not.have.property('@flowfuse/nr-assistant')
428+
})
429+
it('Keeps nr-assistant in package.json when assistant is enabled', async function () {
430+
const snapshot = JSON.parse(JSON.stringify(setup.snapshot))
431+
snapshot.modules['@flowfuse/nr-assistant'] = '0.1.0'
432+
const settings = { assistant: { enabled: true } }
433+
const launcher = newLauncher({ config }, null, 'projectId', snapshot, settings)
434+
await launcher.writePackage()
435+
const pkgFile = await fs.readFile(path.join(config.dir, 'project', 'package.json'))
436+
const pkg = JSON.parse(pkgFile)
437+
pkg.dependencies.should.have.property('@flowfuse/nr-assistant', '0.1.0')
438+
})
439+
it('Keeps nr-assistant in package.json when assistant settings are not configured', async function () {
440+
const snapshot = JSON.parse(JSON.stringify(setup.snapshot))
441+
snapshot.modules['@flowfuse/nr-assistant'] = '0.1.0'
442+
const launcher = newLauncher({ config }, null, 'projectId', snapshot)
443+
await launcher.writePackage()
444+
const pkgFile = await fs.readFile(path.join(config.dir, 'project', 'package.json'))
445+
const pkg = JSON.parse(pkgFile)
446+
pkg.dependencies.should.have.property('@flowfuse/nr-assistant', '0.1.0')
447+
})
419448

420449
it('Updates package.json with user defined Node-RED version', async function () {
421450
const newSettings = {

0 commit comments

Comments
 (0)