Skip to content

Commit

Permalink
Merge pull request #254 from FlowFuse/2802-device-node-red-version
Browse files Browse the repository at this point in the history
Update `package.json` with user defined node-red version
  • Loading branch information
knolleary authored May 1, 2024
2 parents c00f048 + 3d9d6be commit e55e1ad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,36 @@ class Launcher {
* @param {boolean} options.updateSettings Update the settings (settings.js, settings.json)
*/
async writeConfiguration (options = { updateSnapshot: true, updateSettings: true }) {
let fullWrite = !options // default to full write if no options are provided

// If this is an application owned device, the NR version might be user defined.
// When the updateSettings flag is set, the user defined version is specified
// and the versions differ, set the fullWrite flag to cause the package.json
// to be updated and the installDependencies function to be run.
const userDefinedNRVersion = this.settings?.editor?.nodeRedVersion
if (userDefinedNRVersion && options?.updateSettings && this.agent?.currentOwnerType === 'application') {
const pkg = await this.readPackage()
const pkgNRVersion = pkg.modules?.['node-red'] || 'latest'
const snapshotNRVersion = this.snapshot?.modules?.['node-red']
if ((pkgNRVersion !== userDefinedNRVersion || snapshotNRVersion !== userDefinedNRVersion)) {
// package.json dependencies will be updated with snapshot.modules when writePackage is called
// so here, we need to update the snapshot modules node-red version with the user defined version
this.snapshot.modules['node-red'] = userDefinedNRVersion
fullWrite = true
}
}

info('Updating configuration files')
await fs.mkdir(this.projectDir, { recursive: true })
if (!options || options.updateSnapshot) {
if (fullWrite || options.updateSnapshot) {
this.state = States.INSTALLING
await this.writeNPMRCFile()
await this.writePackage()
await this.installDependencies()
await this.writeFlow()
await this.writeCredentials()
}
if (!options || options.updateSettings === true) {
if (fullWrite || options.updateSettings === true) {
await this.writeSettings()
await this.writeNPMRCFile()
}
Expand Down
32 changes: 32 additions & 0 deletions test/unit/lib/launcher_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const should = require('should')
const sinon = require('sinon')
const { newLauncher } = require('../../../lib/launcher')
const setup = require('../setup')
const fs = require('fs/promises')
Expand Down Expand Up @@ -138,6 +139,37 @@ describe('Launcher', function () {
pkg.version.should.eqls('0.0.0-aaaabbbbcccc')
})

it.only('Updates package.json with user defined Node-RED version', async function () {
const newSettings = {
editor: {
nodeRedVersion: '3.1.9'
}
}
// simulate agent update settings. Essentially, when updated settings are available, the agent will
// create a new launcher instance then call writeConfiguration with the `updateSettings` flag set to true
const launcher = newLauncher({ config }, 'application', null, setup.snapshot, newSettings)
// mock relevant parts of launcher and launcher.agent:
sinon.spy(launcher, 'writePackage')
sinon.spy(launcher, 'writeSettings')
sinon.spy(launcher, 'writeFlow')
sinon.stub(launcher, 'installDependencies').resolves()
launcher.agent.currentOwnerType = 'application'

// simulate agent update settings
await launcher.writeConfiguration({ updateSettings: true })
launcher.settings.should.have.property('editor').and.be.an.Object()
launcher.settings.editor.should.have.property('nodeRedVersion', '3.1.9')
launcher.writePackage.calledOnce.should.be.true()
launcher.writeSettings.calledOnce.should.be.true()
launcher.writeFlow.calledOnce.should.be.true()
launcher.installDependencies.calledOnce.should.be.true()

// check written package.json
const pkgFileAfter = await fs.readFile(path.join(config.dir, 'project', 'package.json'))
const pkgAfter = JSON.parse(pkgFileAfter)
pkgAfter.dependencies.should.have.property('node-red', '3.1.9')
})

it('Write Settings - with HTTPS, raw values', async function () {
const launcher = newLauncher({
config: {
Expand Down

0 comments on commit e55e1ad

Please sign in to comment.