Skip to content

Commit

Permalink
feat: Allow passing options to nomnoml-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Jan 30, 2022
1 parent f947b0b commit 0c64205
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
node_modules
npm-debug.log
test/piracy.png
test/piracy.svg
10 changes: 9 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ module.exports = function (grunt) {
},

nomnoml: {
task: {
png: {
files: {
'test/piracy.png': ['test/piracy.nomnoml']
}
},
svg: {
options: {
format: 'svg'
},
files: {
'test/piracy.svg': ['test/piracy.nomnoml']
}
}
},

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ grunt.initConfig({
}
},
all: {
options: { format: 'svg' },
src: ['doc/images/*.nomnoml']
dest: 'dist/doc/images'
}
}
});
```

The configuration consists of key-value pairs with the output image path
as a key pointing to the nomnoml input file. If you specify more source
files by wildcards, the destination should be a directory; the source file
extension wil lbe replaced by "png" in the output file name.
extension will be replaced by "png" or other one depending on the format
in the output file name.

The `options` can contain one or more options supported by the [nomnoml-cli API].
For example, `format` can be `png` (default), `jpg`, `svg` or `pdf`.

Then, load the plugin:

Expand Down Expand Up @@ -103,3 +109,4 @@ Licensed under the MIT license.
[Getting Gtarted]: https://github.com/gruntjs/grunt/wiki/Getting-started
[nomnoml]: http://www.nomnoml.com/
[nomnoml-cli]: https://github.com/prantlf/nomnoml-cli
[nomnoml-cli API]: https://github.com/prantlf/nomnoml-cli#programmatic-usage
48 changes: 26 additions & 22 deletions tasks/nomnoml.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,43 @@ const generateDiagram = require('nomnoml-cli');
const { join, parse, dirname } = require('path');

module.exports = function (grunt) {
function processDiagram(fileSrc, fileDest) {
function processDiagram(fileSrc, fileDest, options) {
try {
grunt.log.subhead('Processing diagram "' + fileSrc + '"');
return generateDiagram({
return generateDiagram(Object.assign({
inputFile: fileSrc,
output: fileDest
});
}, options));
} catch (error) {
grunt.log.error(error);
grunt.fail.warn('Processing diagram "' + fileSrc + '" failed\n');
}
}

grunt.registerMultiTask('nomnoml', "Generate images from nomnoml diagram sources", function () {
var done = this.async(),
promises = this.files.map(function (file) {
// If multiple source files are specified, the destination
// path should point to a directory
var single = file.orig.src.length === 1 &&
!file.orig.src.some(function (src) {
return src.indexOf('*') >= 0 || src.indexOf('?') >= 0;
}),
promises = file.src.map(function (src) {
// If the destination is a directory, use the source file name
// with the '.png' extension
const dest = single ? file.dest : join(file.dest, parse(src).name + '.png');
const dir = dirname(dest);
grunt.file.mkdir(dir);
return processDiagram(src, dest);
});
return Promise.all(promises);
const done = this.async();
const options = this.options({});
let { format = 'png' } = options;
format = format.toLowerCase();
const promises = this.files.map(function (file) {
// If multiple source files are specified, the destination
// path should point to a directory
const single = file.orig.src.length === 1 &&
!file.orig.src.some(function (src) {
return src.indexOf('*') >= 0 || src.indexOf('?') >= 0;
});
Promise.all(promises)
.then(done);
const promises = file.src.map(function (src) {
// If the destination is a directory, use the source file name
// with the '.png' or format-dependent extension
const dest = single ? file.dest : join(file.dest, parse(src).name + '.' + format);
const dir = dirname(dest);
grunt.file.mkdir(dir);
return processDiagram(src, dest, options);
});
return Promise.all(promises);
});
Promise
.all(promises)
.then(done);
});
};
9 changes: 7 additions & 2 deletions test/nomnoml_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const { statSync } = require('fs');
const { join } = require('path');

exports.nomnoml = {
task: function (test) {
var output = statSync(join(__dirname, 'piracy.png'));
png: function (test) {
const output = statSync(join(__dirname, 'piracy.png'));
test.ok(output.isFile() && output.size > 0, 'creates a PNG file');
test.done();
},
svg: function (test) {
const output = statSync(join(__dirname, 'piracy.svg'));
test.ok(output.isFile() && output.size > 0, 'creates a SVG file');
test.done();
}
};

0 comments on commit 0c64205

Please sign in to comment.