Skip to content

Commit

Permalink
Added build command
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Laberge authored and Francois Laberge committed Aug 27, 2017
1 parent ad2cb4c commit 8fe4d5d
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 97 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ npm install -g diagrams
## watch
The diagrams CLI provides the `watch` command that will automatically generate the .svg visualization of each diagram file format it supports. Run the following:

Watches the current directory and generates an .svg file in the same directory as any found file.
Generates all of the .svg files, then starts watching the current directory and regenerates any diagram file that changes.
```
diagrams watch
```

Same as above but also first generates all .svg files before starting watch process
Generates all .svg files, but don't start watching after.
```
diagrams watch --build
diagrams build
```

To provide the target directory to watch just pass it as an argument right after the `watch` command.
Expand Down
37 changes: 37 additions & 0 deletions bin/diagrams-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

var program = require('commander'),
build = require('../src/build')
fs = require('fs');

program
.parse(process.argv);

// There should not be more than one directory provided
if( process.argv.length>=4 ){
reportError('Only one directory argument expected');
}

// Validate the directory if it was provided, otherwise default to current directory
var directory = ".";
if( process.argv.length === 3 ) {
directory = process.argv[2];

try {
var dirInfo = fs.readdirSync(directory);
} catch( err ) {
if(err.code === "ENOENT"){
reportError("Provided directory does not exist");
}
if(err.code === "ENOTDIR"){
reportError("Provided directory is a file");
}
}
}

build(false, directory, (err) => {
if(err){
console.log("Error: " + explanation );
process.exit(1);
}
});
42 changes: 5 additions & 37 deletions bin/diagrams-watch.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#!/usr/bin/env node

var program = require('commander'),
build = require('../src/build')
fs = require('fs');
program
.option('b --build', 'build')
.parse(process.argv);

// There should not be more than one directory provided
var buildPlusManyParameters = process.argv.length>=5 && typeof program.build !== 'undefined';
var noBuildTooManyParameters = !buildPlusManyParameters && process.argv.length>=5;
if( buildPlusManyParameters || noBuildTooManyParameters){
if( process.argv.length>=4 ){
reportError('Only one directory argument expected');
}

// Validate the directory if it was provided, otherwise default to current directory
var watchDirectory = ".";
if( (program.build && process.argv.length === 5) ||
process.argv.length === 4 ) {
if( process.argv.length === 3 ) {
watchDirectory = process.argv[2];

try {
Expand All @@ -31,37 +28,8 @@ if( (program.build && process.argv.length === 5) ||
}
}


var path = require('path');
var chokidar = require('chokidar');

// Chokidar didn't seem to respect a glob pattern properly, so I have implemented
// the filtering on a watch of the entire directory. I filed an issue/question here:
// https://github.com/paulmillr/chokidar/issues/544
chokidar.watch(watchDirectory, {ignoreInitial:!program.build}).on('all', (event, inputPath) => {
if( inputPath.match(/^.*\.(dot|sequence|flowchart|railroad)$/) !== null ){
if( event==='add' || event==='change' ) {
convertFile(inputPath);
}
}
});

function convertFile(inputPath) {
var extension = inputPath.match(/^.*\.(dot|sequence|flowchart|railroad)$/)[1];
var outputPath = inputPath+'.svg';

console.log(inputPath + ' -> ' + outputPath);

// Create dynamic path to matching generator for extension. Example for 'sequence' it ends
// up as: var generator = require('../src/sequence/sequence')
var generator = require('../src/'+extension+'/'+extension);
generator(inputPath, outputPath, function(err){
if(err){
reportError(err);
}
})
}

function reportError(explanation){
console.log("Error: " + explanation);
process.exit(1);
}
build(true, watchDirectory, ()=>{});
3 changes: 2 additions & 1 deletion bin/diagrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ program
.command('sequence [inputFilePath] [outputFilePath.svg]', 'Generate a network sequence diagram in svg')
.command('dot [inputFilePath] [outputFilePath.svg]', 'Generate a dot in svg')
.command('railroad [inputFilePath] [outputFilePath.svg]', 'Generate a railroad diagram in svg')
.command('watch [directory] [--build]', 'Watch a directory for diagram files and automatically generate/update their matching .svg. If --build is provided existing diagram files will first generate their match .svg.')
.command('build [directory]', 'Scans a directory for diagram files and generates their matching .svg')
.command('watch [directory]', 'Watch a directory for diagram files and automatically generate/update their matching .svg')
.parse(process.argv);

// Look at src/debugger.js for the implementation
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"test": "mocha tests/index.js"
},
"dependencies": {
"chokidar": "^1.6.1",
"commander": "2.8.1",
"electron-prebuilt": "^0.36.2",
"glob": "^7.1.2",
"railroad-diagrams": "1.0.0",
"viz.js": "^1.3.0",
"chokidar": "^1.6.1"
"viz.js": "^1.3.0"
},
"repository": {
"type": "git",
Expand Down
49 changes: 49 additions & 0 deletions src/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var path = require('path');

function build(watch, directory, cb){

if(watch){
var chokidar = require('chokidar');
// Chokidar didn't seem to respect a glob pattern properly, so I have implemented
// the filtering on a watch of the entire directory. I filed an issue/question here:
// https://github.com/paulmillr/chokidar/issues/544
chokidar.watch(directory, {ignoreInitial:false}).on('all', (event, inputPath) => {
if( inputPath.match(/^.*\.(dot|sequence|flowchart|railroad)$/) !== null ){
if( event==='add' || event==='change' ) {
convertFile(inputPath);
}
}
});
} else {
var glob = require('glob');
let buildPattern = `${directory}/**/*.@(dot|sequence|flowchart|railroad)`;
glob(buildPattern, {}, function (err, files) {
if(err){
return cb(err);
}
files.forEach( (file)=>{
convertFile(file);
});
cb(null);
})
}
}

function convertFile(inputPath) {
var extension = inputPath.match(/^.*\.(dot|sequence|flowchart|railroad)$/)[1];
var outputPath = inputPath+'.svg';

console.log(inputPath + ' -> ' + outputPath);

// Create dynamic path to matching generator for extension. Example for 'sequence' it ends
// up as: var generator = require('../src/sequence/sequence')
var generator = require('../src/'+extension+'/'+extension);
generator(inputPath, outputPath, function(err){
if(err){
reportError(err);
}
})
}


module.exports = build;
123 changes: 70 additions & 53 deletions tests/fixtures/dot/simple.dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8fe4d5d

Please sign in to comment.