From 5daf175120fff343dccfc2128bff964db077ff92 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Thu, 12 Dec 2024 00:31:38 +0100 Subject: [PATCH] feat: add elementTemplates as default engine `elementTemplates` engine is by default set to `bpmn-js-elements-template` version. This allows templates authors to indicate compatibility with specific library versions. --- karma.config.js | 6 ++++ package-lock.json | 33 +++++++++++++++++++ package.json | 1 + rollup.config.mjs | 7 ++++ .../rules/element-templates-compatibility.js | 9 ----- src/element-templates/ElementTemplates.js | 13 ++++++++ .../ElementTemplates.spec.js | 26 +++++++++++++++ 7 files changed, 86 insertions(+), 9 deletions(-) diff --git a/karma.config.js b/karma.config.js index ead36993..9db925dc 100644 --- a/karma.config.js +++ b/karma.config.js @@ -1,6 +1,9 @@ /* eslint-env node */ const path = require('path'); + +const pkg = require('./package.json'); + const { DefinePlugin, NormalModuleReplacementPlugin @@ -93,6 +96,9 @@ module.exports = function(karma) { plugins: [ new DefinePlugin({ + // @nikku needs to be defined + 'process.env.PKG_VERSION': JSON.stringify(pkg.version), + // @barmac: process.env has to be defined to make @testing-library/preact work 'process.env': {} }), diff --git a/package-lock.json b/package-lock.json index c91a9140..4fcf02c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,7 @@ "@rollup/plugin-commonjs": "^28.0.1", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.3.0", + "@rollup/plugin-replace": "^6.0.1", "@testing-library/preact": "^2.0.1", "@testing-library/preact-hooks": "^1.1.0", "assert": "^2.1.0", @@ -1357,6 +1358,28 @@ } } }, + "node_modules/@rollup/plugin-replace": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-6.0.1.tgz", + "integrity": "sha512-2sPh9b73dj5IxuMmDAsQWVFT7mR+yoHweBaXG2W/R8vQ+IWZlnaI7BR7J6EguVQUp1hd8Z7XuozpDjEKQAAC2Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "@rollup/pluginutils": "^5.0.1", + "magic-string": "^0.30.3" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, "node_modules/@rollup/pluginutils": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.1.0.tgz", @@ -11234,6 +11257,16 @@ "resolve": "^1.22.1" } }, + "@rollup/plugin-replace": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-6.0.1.tgz", + "integrity": "sha512-2sPh9b73dj5IxuMmDAsQWVFT7mR+yoHweBaXG2W/R8vQ+IWZlnaI7BR7J6EguVQUp1hd8Z7XuozpDjEKQAAC2Q==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^5.0.1", + "magic-string": "^0.30.3" + } + }, "@rollup/pluginutils": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.1.0.tgz", diff --git a/package.json b/package.json index d72cf44e..01b329bf 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "@rollup/plugin-commonjs": "^28.0.1", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.3.0", + "@rollup/plugin-replace": "^6.0.1", "@testing-library/preact": "^2.0.1", "@testing-library/preact-hooks": "^1.1.0", "assert": "^2.1.0", diff --git a/rollup.config.mjs b/rollup.config.mjs index df420c17..74f091e7 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -4,6 +4,7 @@ import commonjs from '@rollup/plugin-commonjs'; import copy from 'rollup-plugin-copy'; import json from '@rollup/plugin-json'; import resolve from '@rollup/plugin-node-resolve'; +import replace from '@rollup/plugin-replace'; import { readFileSync @@ -81,6 +82,12 @@ function pgl(plugins = []) { function corePlugins(plugins = []) { return [ ...plugins, + replace({ + preventAssignment: true, + values: { + 'process.env.PKG_VERSION': JSON.stringify(pkg.version) + } + }), json(), resolve({ mainFields: [ diff --git a/src/cloud-element-templates/linting/rules/element-templates-compatibility.js b/src/cloud-element-templates/linting/rules/element-templates-compatibility.js index 7577f39b..9a10499f 100644 --- a/src/cloud-element-templates/linting/rules/element-templates-compatibility.js +++ b/src/cloud-element-templates/linting/rules/element-templates-compatibility.js @@ -83,11 +83,6 @@ export default function({ templates = [] }) { // helpers ////////////////////// function getEnginesConfig(definitions) { - const { - exporter, - exporterVersion - } = definitions; - const engines = {}; const executionPlatform = definitions.get('modeler:executionPlatform'); @@ -97,10 +92,6 @@ function getEnginesConfig(definitions) { engines.camunda = executionPlatformVersion; } - if (exporter === 'Camunda Modeler' && exporterVersion) { - engines.camundaDesktopModeler = exporterVersion; - } - return engines; } diff --git a/src/element-templates/ElementTemplates.js b/src/element-templates/ElementTemplates.js index 55102108..8e92e474 100644 --- a/src/element-templates/ElementTemplates.js +++ b/src/element-templates/ElementTemplates.js @@ -24,6 +24,10 @@ import { coerce } from 'semver'; +// eslint-disable-next-line no-undef +const packageVersion = process.env.PKG_VERSION; + + /** * Registry for element templates. */ @@ -155,6 +159,7 @@ export default class ElementTemplates { } setEngines(engines) { + this._engines = this._coerceEngines(engines); this._fire('engines.changed'); @@ -169,6 +174,14 @@ export default class ElementTemplates { */ _coerceEngines(engines) { + // we provide engine with the current + // package version; templates may use that engine to declare + // compatibility with this library + engines = { + elementTemplates: packageVersion, + ...engines + }; + return reduce(engines, (validEngines, version, engine) => { const coercedVersion = coerce(version); diff --git a/test/spec/cloud-element-templates/ElementTemplates.spec.js b/test/spec/cloud-element-templates/ElementTemplates.spec.js index 7e8bdf96..69bcb0e6 100644 --- a/test/spec/cloud-element-templates/ElementTemplates.spec.js +++ b/test/spec/cloud-element-templates/ElementTemplates.spec.js @@ -34,6 +34,9 @@ import { findExtensions, findExtension } from 'src/cloud-element-templates/Helpe import { getLabel } from 'bpmn-js/lib/features/label-editing/LabelUtil'; import { findMessage } from 'src/cloud-element-templates/Helper'; +// eslint-disable-next-line no-undef +const packageVersion = process.env.PKG_VERSION; + describe('provider/cloud-element-templates - ElementTemplates', function() { @@ -785,6 +788,15 @@ describe('provider/cloud-element-templates - ElementTemplates', function() { describe('getEngines', function() { + it('should provide default engine', inject(function(elementTemplates) { + + // then + expect( + elementTemplates.getEngines() + ).to.have.property('elementTemplates', packageVersion); + })); + + it('should provide set engines', inject(function(elementTemplates) { // when @@ -795,9 +807,11 @@ describe('provider/cloud-element-templates - ElementTemplates', function() { // then expect(elementTemplates.getEngines()).to.eql({ + 'elementTemplates': packageVersion, 'camunda': '8.0.0', 'other': '100.5.0' }); + })); }); @@ -819,6 +833,18 @@ describe('provider/cloud-element-templates - ElementTemplates', function() { expect(spy).to.have.been.calledOnce; })); + + it('should override engine', inject(function(elementTemplates) { + + // when + elementTemplates.setEngines({ + elementTemplates: '1.0.0' + }); + + // then + expect(elementTemplates.getEngines()).to.have.property('elementTemplates', '1.0.0'); + })); + });