Skip to content

Commit

Permalink
feat: add subcommands validate and apply
Browse files Browse the repository at this point in the history
Use `element-templates-cli validate [filename(s)]` to validate template(s).

BREAKING CHANGE: The tool expects subcommand `apply` to apply a template.
  • Loading branch information
barmac committed Feb 8, 2024
1 parent d05e737 commit 7a9cf9b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ npm install --global element-templates-cli
## Usage

```sh
element-templates-cli --diagram diagram.bpmn --template template.json --element ServiceTask --output diagram_2.bpmn
# Apply a template to an element in a BPMN diagram
element-templates-cli apply--diagram diagram.bpmn --template template.json --element ServiceTask --output diagram_2.bpmn

# Validate a template
element-templates-cli validate template.json
```

## Additional resources
Expand Down
54 changes: 45 additions & 9 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ import { parseArgs } from 'node:util';
import { readFile, writeFile } from 'node:fs/promises';

import {
applyTemplate
applyTemplate,
validate as validateTemplate
} from '../dist/index.js';

run();

async function run() {
function run() {
const command = process.argv[2];

switch (command) {
case 'apply':
return apply();
case 'validate':
return validate();
}

throw new Error(`unknown command: ${command}`);


}

async function apply() {
const options = {
diagram: {
type: 'string'
Expand All @@ -28,13 +43,7 @@ async function run() {
}
};

const { values } = parseArgs({ options });

for (const option in options) {
if (!values[option]) {
throw new Error(`Missing option "${option}"`);
}
}
const { values } = parse({ options });

const diagram = await readFile(values.diagram, 'utf8');
const template = await readFile(values.template, 'utf8');
Expand All @@ -48,3 +57,30 @@ async function run() {

await writeFile(values.output, xml);
}

async function validate() {
const {
positionals: templates
} = parse({
allowPositionals: true,
});

for (const templatePath of templates) {
const template = await readFile(templatePath, 'utf8');

const result = validateTemplate(template);
console.log(result);
}
}

function parse(config) {
const result = parseArgs({ ...config, allowPositionals: true });

for (const option in config.options || []) {
if (!result.values[option]) {
throw new Error(`Missing option "${option}"`);
}
}

return { ...result, positionals: result.positionals.slice(1) || [] };
}
1 change: 1 addition & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/bpmn-io/element-templates-cli#readme",
"devDependencies": {
"@bpmn-io/element-templates-validator": "^1.7.0",
"bpmn-js-element-templates": "^1.12.1",
"bpmn-js-headless": "^0.0.1",
"chai": "^4.3.7",
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { applyTemplate } from './applyTemplate.js';
export { validate } from './validate.js';
8 changes: 8 additions & 0 deletions src/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import { validateZeebe as _validate } from '@bpmn-io/element-templates-validator';

export function validate(template) {
const parsedTemplate = JSON.parse(template);

return _validate(parsedTemplate);
}
49 changes: 32 additions & 17 deletions test/cliSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,34 @@ const execFile = promisify(childProcess.execFile);

describe('cli', function() {

test.only = function(testName, element) {
return test(testName, element, true);
};

describe('apply template', function() {

test('rest-conditional');
testApply('rest-conditional');


testApply('easy-post-connector');


testApply('task-header-with-condition');
});


describe('validate template', function() {

testValidate('rest-conditional');


test('easy-post-connector');
testValidate('easy-post-connector');


test('task-header-with-condition');
testValidate('task-header-with-condition');
});
});

function test(testName, element = 'ServiceTask', only = false) {
const fn = only ? it.only : it;


// helpers //////////////////////
function testApply(testName, element = 'ServiceTask', fn = it) {
fn(`should work for: ${testName}`, async function() {

// given
Expand All @@ -41,32 +50,32 @@ function test(testName, element = 'ServiceTask', only = false) {
const expected = await fs.readFile(`test/fixtures/diagrams/${testName}_expected.bpmn`, 'utf8');

// when
const { stdout } = await exec({
const { stdout } = await exec(prepareApplyArgs({
diagram,
template,
element
});
}));

// then
expect(withoutDiagram(stdout.trim())).to.eql(withoutDiagram(expected.trim()));
});
}

function exec(argsMap) {
const args = prepareArgs(argsMap);
function testValidate(testName, fn = it) {
const template = `test/fixtures/templates/${testName}.json`;

return execFile(CLI_PATH, args, {
cwd: path.resolve(__dirname, '..')
fn(`should validate for: ${testName}`, async function() {
return exec([ 'validate', template ]);
});
}

function prepareArgs({
function prepareApplyArgs({
diagram,
template,
element,
output
}) {
const args = [];
const args = [ 'apply' ];

if (diagram) {
args.push('--diagram', diagram);
Expand Down Expand Up @@ -99,3 +108,9 @@ function withoutDiagram(xml) {

return xml.slice(0, startPosition) + xml.slice(endPosition + END_CLAUSE.length);
}

function exec(args) {
return execFile(CLI_PATH, args, {
cwd: path.resolve(__dirname, '..')
});
}

0 comments on commit 7a9cf9b

Please sign in to comment.