Skip to content

Commit 206dfef

Browse files
authored
fix: Ensure correct theme name choice for nr5 and legacy versions (#672)
1 parent c3d21b1 commit 206dfef

2 files changed

Lines changed: 84 additions & 9 deletions

File tree

lib/launcher.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,30 @@ class Launcher {
248248
}
249249
}
250250

251-
// Mirror the NR5+ theme gate from nr-launcher's runtimeSettings.js.
251+
// Ensure the theme is compatible with the Node-RED version.
252252
const storedTheme = this.config.theme || 'forge-light'
253253
const snapshotNRVersion = this.snapshot?.modules?.['node-red']
254-
const nrMajor = parseInt((snapshotNRVersion || '0.0.0').split('.')[0], 10) || 0
254+
const oldThemes = ['forge-light', 'forge-dark']
255+
const newTheme = 'forge'
255256
let themeName = storedTheme
256-
if (nrMajor >= 5) {
257-
if (themeName === 'forge-light' || themeName === 'forge-dark' || themeName === 'forge') {
258-
themeName = 'forge'
259-
}
260-
} else if (themeName === 'forge') {
261-
themeName = 'forge-light'
257+
let useNewThemes = false
258+
259+
// at the time of writing, latest is NR5.
260+
if (snapshotNRVersion === 'latest' || snapshotNRVersion === 'next') {
261+
useNewThemes = true
262+
} else {
263+
// get version info from semver
264+
const nrMajor = parseInt((snapshotNRVersion || '0.0.0').split('.')[0], 10) || 0
265+
useNewThemes = nrMajor >= 5
262266
}
267+
if (useNewThemes && oldThemes.includes(themeName)) {
268+
// switch old theme to new theme
269+
themeName = newTheme
270+
} else if (!useNewThemes && themeName === newTheme) {
271+
// switch new theme to old theme
272+
themeName = oldThemes[0]
273+
}
274+
263275
const assistant = {
264276
enabled: this.settings?.assistant?.enabled || false, // overall enable/disable
265277
url: `${this.config.forgeURL}/api/v1/assistant/`, // URL for the assistant service

test/unit/lib/launcher_spec.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,70 @@ describe('Launcher', function () {
520520
const npmrc = await fs.readFile(path.join(config.dir, 'project', '.npmrc'))
521521
npmrc.toString().should.eql('// test\n')
522522
})
523-
523+
it('Keeps legacy theme name', async function () {
524+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
525+
copyOfSnapshot.modules['node-red'] = '4.2.0'
526+
copyOfSnapshot.editorTheme = { theme: 'forge-dark' } // old theme
527+
const launcher = newLauncher({ config: { ...config, theme: 'forge-dark' } }, null, 'projectId', copyOfSnapshot)
528+
await launcher.writeSettings()
529+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
530+
const settings = JSON.parse(setFile)
531+
settings.should.have.property('editorTheme')
532+
settings.editorTheme.should.have.property('theme', 'forge-dark')
533+
})
534+
it('Updates legacy theme name for Node-RED >= 5.0.0', async function () {
535+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
536+
copyOfSnapshot.modules['node-red'] = '5.0.0'
537+
copyOfSnapshot.editorTheme = { theme: 'forge-light' } // old theme name
538+
const launcher = newLauncher({ config: { ...config, theme: 'forge-light' } }, null, 'projectId', copyOfSnapshot)
539+
await launcher.writeSettings()
540+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
541+
const settings = JSON.parse(setFile)
542+
settings.should.have.property('editorTheme')
543+
settings.editorTheme.should.have.property('theme', 'forge') // updated to forge
544+
})
545+
it('Updates legacy theme name for Node-RED "latest"', async function () {
546+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
547+
copyOfSnapshot.modules['node-red'] = 'latest'
548+
copyOfSnapshot.editorTheme = { theme: 'forge-light' } // old theme name
549+
const launcher = newLauncher({ config: { ...config, theme: 'forge-light' } }, null, 'projectId', copyOfSnapshot)
550+
await launcher.writeSettings()
551+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
552+
const settings = JSON.parse(setFile)
553+
settings.should.have.property('editorTheme')
554+
settings.editorTheme.should.have.property('theme', 'forge') // updated to forge
555+
})
556+
it('Updates legacy theme name for Node-RED "next"', async function () {
557+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
558+
copyOfSnapshot.modules['node-red'] = 'next'
559+
copyOfSnapshot.editorTheme = { theme: 'forge-light' } // old theme name
560+
const launcher = newLauncher({ config: { ...config, theme: 'forge-light' } }, null, 'projectId', copyOfSnapshot)
561+
await launcher.writeSettings()
562+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
563+
const settings = JSON.parse(setFile)
564+
settings.should.have.property('editorTheme')
565+
settings.editorTheme.should.have.property('theme', 'forge') // updated to forge
566+
})
567+
it('Keeps custom theme name for Node-RED < 5.0.0', async function () {
568+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
569+
copyOfSnapshot.modules['node-red'] = '4.2.0'
570+
const launcher = newLauncher({ config: { ...config, theme: 'custom-theme' } }, null, 'projectId', copyOfSnapshot)
571+
await launcher.writeSettings()
572+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
573+
const settings = JSON.parse(setFile)
574+
settings.should.have.property('editorTheme')
575+
settings.editorTheme.should.have.property('theme', 'custom-theme') // keeps custom theme name
576+
})
577+
it('Keeps custom theme name for Node-RED >= 5.0.0', async function () {
578+
const copyOfSnapshot = JSON.parse(JSON.stringify(setup.snapshot))
579+
copyOfSnapshot.modules['node-red'] = '5.0.0'
580+
const launcher = newLauncher({ config: { ...config, theme: 'custom-theme' } }, null, 'projectId', copyOfSnapshot)
581+
await launcher.writeSettings()
582+
const setFile = await fs.readFile(path.join(config.dir, 'project', 'settings.json'))
583+
const settings = JSON.parse(setFile)
584+
settings.should.have.property('editorTheme')
585+
settings.editorTheme.should.have.property('theme', 'custom-theme') // keeps custom theme name
586+
})
524587
it('Uses custom catalogue when licensed', async function () {
525588
const licensedConfig = {
526589
...config,

0 commit comments

Comments
 (0)