Skip to content

Commit

Permalink
resolves #131 create output directory immediately (#167)
Browse files Browse the repository at this point in the history
* resolves #131 create output directory immediately

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* fix error if no steps are passed

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* add exit message if steps not passed

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* add test for creating output directory
  • Loading branch information
tech4GT authored and jywarren committed Feb 6, 2018
1 parent c5dee8a commit 32c5a29
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ program
.parse(process.argv);

// Parse step into an array to allow for multiple steps.
if(!program.step) exit("No steps passed")
program.step = program.step.split(" ");

// User must input an image.
Expand Down Expand Up @@ -58,7 +59,11 @@ sequencer.setUI({
sequencer.loadImages(program.image,function(){
console.warn('\x1b[33m%s\x1b[0m', "The execution will be async\nYou may not see the output for a few seconds or minutes")

if(program.basic) console.log("Basic mode is enabled, outputting only final image")
//Generate the Output Directory
require('./src/CliUtils').makedir(program.output,()=>{
console.log("Files will be exported to \""+program.output+"\"");

if(program.basic) console.log("Basic mode is enabled, outputting only final image")

// Iterate through the steps and retrieve their inputs.
program.step.forEach(function(step){
Expand Down Expand Up @@ -92,14 +97,15 @@ sequencer.loadImages(program.image,function(){
// Export all images or final image as binary files.
sequencer.exportBin(program.output,program.basic);

console.log("Files will be exported to \""+program.output+"\"");

});

});

});

// Takes an array of steps and checks if they are valid steps for the sequencer.
function validateSteps(steps) {
function validateSteps(steps) {

// Assume all are valid in the beginning.
var valid = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/ImageSequencer.js",
"scripts": {
"debug": "node index.js -i ../imgs/back.png -s invert",
"test": "tape test/modules/*.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/replace.js | tape-run --render=\"tap-spec\""
"test": "tape test/**/*.js test/*.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/replace.js | tape-run --render=\"tap-spec\""
},
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions src/CliUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')



/*
* This function checks if the directory exists, if not it creates one on the given path
* Callback is called with argument error if an error is encountered
*/
function makedir(path,callback){
fs.access(path,function(err){
if(err) fs.mkdir(path,function(err){
if(err) callback(err);
callback();
});
else callback()
});
};

module.exports = exports = {
makedir: makedir
}
2 changes: 1 addition & 1 deletion src/ExportBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function ExportBin(dir = "./output/",ref,basic) {
dir = (dir[dir.length-1]=="/") ? dir : dir + "/";
if(ref.options.inBrowser) return false;
fs.access(dir, function(err){
if(err) fs.mkdir(dir, function() {});
if(err) console.error(err)
});
getDirectories(dir,function(dirs){
var num = 1;
Expand Down
13 changes: 13 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const cliUtils = require('../src/CliUtils');
const test = require('tape');

test('Output directory is correctly generated',function(t){
cliUtils.makedir('./output/',function(){
require('fs').access('./output/.',function(err){
t.true(!err,"Access the created dir")
t.end()
});
});
});

0 comments on commit 32c5a29

Please sign in to comment.