Skip to content

Commit

Permalink
feat(app): load YAML element templates
Browse files Browse the repository at this point in the history
* Uses `js-yaml` to transform YAML files to JSON element templates
  • Loading branch information
Niklas Kiefer committed Jul 29, 2022
1 parent 5e34d91 commit a23c8a0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 8 deletions.
27 changes: 25 additions & 2 deletions app/lib/config/__tests__/config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ describe('Config', function() {
// then
expect(templates).to.eql([
{ id: 'com.foo.Bar' }, // local
{ id: 'com.yml.Bar' }, // local (YAML)
{ id: 'com.foo.Bar', FOO: 'BAR' }, // global
{ id: 'single', FOO: 'BAR' } // global
{ id: 'single', FOO: 'BAR' }, // global
{ id: 'com.yml.Bar', FOO: 'BAR' }, // global (YAML)
{ id: 'single-yml', FOO: 'BAR' } // global (YAML)
]);
});

Expand All @@ -181,7 +184,9 @@ describe('Config', function() {
// then
expect(templates).to.eql([
{ id: 'com.foo.Bar', FOO: 'BAR' },
{ id: 'single', FOO: 'BAR' }
{ id: 'single', FOO: 'BAR' },
{ id: 'com.yml.Bar', FOO: 'BAR' },
{ id: 'single-yml', FOO: 'BAR' }
]);
});

Expand All @@ -203,6 +208,24 @@ describe('Config', function() {
.to.throw(/template .* parse error: Unexpected token I.*/);
});


it('should throw if YML#load errors', function() {

// given
const file = null;

const config = new Config({
resourcesPaths: [
getAbsolutePath('fixtures/broken-yml')
],
userPath: 'foo'
});

// when
expect(() => config.get('bpmn.elementTemplates', file))
.to.throw(/template .* parse error: end of the stream or a document separator is expected */);
});

});


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
text foobar
number: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- id: com.yml.Bar
FOO: BAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: single-yml
FOO: BAR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- id: com.yml.Bar
52 changes: 46 additions & 6 deletions app/lib/config/providers/ElementTemplatesProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ const glob = require('glob');
const parents = require('parents');
const path = require('path');

const { isArray } = require('min-dash');
const { flatten, isArray, keys } = require('min-dash');

const YAML = require('js-yaml');

const log = require('../../log')('app:config:element-templates');

const FILE_HANDLERS = {
'json': getJSONTemplatesForPath,
'yml': getYAMLTemplatesForPath
};


/**
* Get element templates.
Expand Down Expand Up @@ -80,7 +87,9 @@ function getTemplates(paths) {

// do not throw if file not accessible or no such file
try {
files = globTemplates(path);
files = flatten(keys(FILE_HANDLERS).map(type => {
return globTemplates(path, type);
}));
} catch (error) {
log.error(`templates ${ path } glob error`, error);

Expand All @@ -105,19 +114,24 @@ function getTemplatesForPaths(paths) {
return paths.reduce((templates, path) => {
return [
...templates,
...getTemplatesForPath(path)
...getFileHandler(path)(path)
];
}, []);
}

function getFileHandler(file) {
const extension = path.extname(file).slice(1);
return FILE_HANDLERS[extension];
}

/**
* Get element templates from paths.
*
* @param {string} path
*
* @return {Array<Template>}
*/
function getTemplatesForPath(path) {
function getJSONTemplatesForPath(path) {
let templates;

try {
Expand All @@ -139,15 +153,41 @@ function getTemplatesForPath(path) {
* Glob element templates from `<path>/resources`.
*
* @param {string} path
* @param {string} type
*
* @return {Array<string>}
*/
function globTemplates(path) {
function globTemplates(path, type) {
const globOptions = {
cwd: path,
nodir: true,
realpath: true
};

return glob.sync('element-templates/**/*.json', globOptions);
return glob.sync('element-templates/**/*.' + type, globOptions);
}

/**
* Get element templates from paths.
*
* @param {string} path
*
* @return {Array<Template>}
*/
function getYAMLTemplatesForPath(path) {
let templates;

try {
templates = YAML.load(fs.readFileSync(path, 'utf8'));

if (!isArray(templates)) {
templates = [ templates ];
}

return templates;
} catch (error) {
log.error(`template ${ path } parse error`, error);

throw new Error(`template ${ path } parse error: ${ error.message }`);
}
}
13 changes: 13 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"form-data": "^2.5.1",
"glob": "^7.1.6",
"ids": "^1.0.0",
"js-yaml": "^4.1.0",
"min-dash": "^3.8.1",
"mri": "^1.1.6",
"node-fetch": "^2.6.1",
Expand Down

0 comments on commit a23c8a0

Please sign in to comment.