Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI code refactor #665

Merged
merged 8 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9,055 changes: 4,527 additions & 4,528 deletions dist/image-sequencer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/image-sequencer.min.js

Large diffs are not rendered by default.

159 changes: 17 additions & 142 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node

require("./src/ImageSequencer");
require('./src/ImageSequencer');
sequencer = ImageSequencer({ ui: false });
var Spinner = require("ora");
var fs = require('fs')
var program = require('commander');
var utils = require('./src/CliUtils')

var program = require("commander");
var readlineSync = require("readline-sync");
var saveSequence = require('./src/cli/saveSequence.js')
var installModule = require('./src/cli/installModule.js')
var sequencerSteps = require('./src/cli/sequencerSteps.js')

function exit(message) {
console.error(message);
Expand All @@ -23,24 +26,11 @@ program
.option('--install-module [string]', "Module name space seaprated npm package name")
.parse(process.argv);

if (program.saveSequence) {
var params = program.saveSequence.split(' ');
sequencer.saveSequence(params[0], params[1]);
console.log("\x1b[32m", "Your sequence was saved successfully!!");
} else if (program.installModule) {
console.log(
"\x1b[33m%s\x1b[0m",
"Please wait while your Module is being Installed...\nThis may take a while!"
);

var params = program.installModule.split(' ');
var spinner = Spinner("Now Installing...").start();
require('child_process').execSync(`npm i ${params[1]}`)
sequencer.saveNewModule(params[0], params[1]);
sequencer.loadNewModule(params[0], require(params[1]));
spinner.stop();
console.log("\x1b[32m%s\x1b[0m", "Your module was installed successfully!!");
} else {
if (program.saveSequence) saveSequence(program, sequencer)

else if (program.installModule) installModule(program, sequencer)

else {
// Parse step into an array to allow for multiple steps.
if (!program.step) exit("No steps passed");
program.step = program.step.split(" ");
Expand All @@ -49,12 +39,12 @@ if (program.saveSequence) {
if (!program.image) exit("Can't read file.");

// User must input an image.
require("fs").access(program.image, function(err) {
fs.access(program.image, function(err) {
if (err) exit("Can't read file.");
});

// User must input a step. If steps exist, check that every step is a valid step.
if (!program.step || !validateSteps(program.step))
if (!program.step || !(utils.validateSteps(program.step, sequencer)))
exit("Please ensure all steps are valid.");

// If there's no user defined output directory, select a default directory.
Expand Down Expand Up @@ -88,124 +78,9 @@ if (program.saveSequence) {
else {
outputFilename = null;
}
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) {
var options = Object.assign({}, sequencer.modulesInfo(step).inputs);

// If inputs exists, print to console.
if (Object.keys(options).length) {
console.log("[" + step + "]: Inputs");
}

// If inputs exists, print them out with descriptions.
Object.keys(options).forEach(function(input) {
// The array below creates a variable number of spaces. This is done with (length + 1).
// The extra 4 that makes it (length + 5) is to account for the []: characters
console.log(
new Array(step.length + 5).join(" ") +
input +
": " +
options[input].desc
);
});

if (program.config) {
try {
var config = JSON.parse(program.config);
console.log(`The parsed options object: `, config);
} catch (e) {
console.error(
"\x1b[31m%s\x1b[0m",
`Options(Config) is not a not valid JSON Fallback activate`
);
program.config = false;
console.log(e);
}
}
if (program.config && validateConfig(config, options)) {
console.log("Now using Options object");
Object.keys(options).forEach(function(input) {
options[input] = config[input];
});
} else {
// If inputs exist, iterate through them and prompt for values.
Object.keys(options).forEach(function(input) {
var value = readlineSync.question(
"[" +
step +
"]: Enter a value for " +
input +
" (" +
options[input].type +
", default: " +
options[input].default +
"): "
);
options[input] = value;
});
}
// Add the step and its inputs to the sequencer.
sequencer.addSteps(step, options);
});

var spinnerObj = { succeed: () => { }, stop: () => { } };
if (!process.env.TEST)
spinnerObj = Spinner("Your Image is being processed..").start();

// Run the sequencer.
sequencer.run({ progressObj: spinnerObj }, function() {
// Export all images or final image as binary files.
sequencer.exportBin(program.output, program.basic, outputFilename);

//check if spinner was not overriden stop it
if (!spinnerObj.overrideFlag) {
spinnerObj.succeed();
console.log(`\nDone!!`);
}
});
});

sequencerSteps(program, sequencer, outputFilename)

});

// Takes an array of steps and checks if they are valid steps for the sequencer.
function validateSteps(steps) {
// Assume all are valid in the beginning.
var valid = true;
steps.forEach(function(step) {
// If any step in the array is not valid (not a property of modulesInfo), set valid to false.
if (!sequencer.modulesInfo().hasOwnProperty(step)) {
valid = false;
}
});

// Return valid. (If all of the steps are valid properties, valid will have remained true).
return valid;
}

//Takes config and options object and checks if all the keys exist in config
function validateConfig(config_, options_) {
options_ = Object.keys(options_);
if (
(function() {
for (var input in options_) {
if (!config_[options_[input]]) {
console.error(
"\x1b[31m%s\x1b[0m",
`Options Object does not have the required details "${
options_[input]
}" not specified. Fallback case activated`
);
return false;
}
}
})() == false
)
return false;
else return true;
}
}
45 changes: 41 additions & 4 deletions src/CliUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const fs = require('fs')


var fs = require('fs')

/*
* This function checks if the directory exists, if not it creates one on the given path
Expand All @@ -16,6 +14,45 @@ function makedir(path,callback){
});
};

// Takes an array of steps and checks if they are valid steps for the sequencer.
function validateSteps(steps, sequencer) {
// Assume all are valid in the beginning.
var valid = true;
steps.forEach(function(step) {
// If any step in the array is not valid (not a property of modulesInfo), set valid to false.
if (!sequencer.modulesInfo().hasOwnProperty(step)) {
valid = false;
}
});

// Return valid. (If all of the steps are valid properties, valid will have remained true).
return valid;
}

//Takes config and options object and checks if all the keys exist in config
function validateConfig(config_, options_) {
options_ = Object.keys(options_);
if (
(function() {
for (var input in options_) {
if (!config_[options_[input]]) {
console.error(
"\x1b[31m%s\x1b[0m",
`Options Object does not have the required details "${
options_[input]
}" not specified. Fallback case activated`
);
return false;
}
}
})() == false
)
return false;
else return true;
}

module.exports = exports = {
makedir: makedir
makedir: makedir,
validateSteps: validateSteps,
validateConfig: validateConfig
}
4 changes: 2 additions & 2 deletions src/ImageSequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ ImageSequencer = function ImageSequencer(options) {
// Config is an object which contains the runtime configuration like progress bar
// information and index from which the sequencer should run
function run(config, t_image, t_from) {
let progressObj, index = 0;
var progressObj, index = 0;
config = config || { mode: 'no-arg' };
if (config.index) index = config.index;

Expand Down Expand Up @@ -455,7 +455,7 @@ ImageSequencer = function ImageSequencer(options) {
createMetaModule: createMetaModule,
saveSequence: saveSequence,
loadModules: loadModules,

//other functions
log: log,
objTypeOf: objTypeOf,
Expand Down
2 changes: 1 addition & 1 deletion src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ module.exports = {
'rotate': require('./modules/Rotate'),
'saturation': require('./modules/Saturation'),
'white-balance': require('./modules/WhiteBalance')
}
}
17 changes: 17 additions & 0 deletions src/cli/installModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var childProcess = require('child_process')
var Spinner = require('ora');

module.exports = function (program, sequencer) {
console.log(
"\x1b[33m%s\x1b[0m",
"Please wait while your Module is being Installed...\nThis may take a while!"
);

var params = program.installModule.split(' ');
var spinner = Spinner("Now Installing...").start();
childProcess.execSync(`npm i ${params[1]}`)
sequencer.saveNewModule(params[0], params[1]);
sequencer.loadNewModule(params[0], require(params[1]));
spinner.stop();
console.log("\x1b[32m%s\x1b[0m", "Your module was installed successfully!!");
}
7 changes: 7 additions & 0 deletions src/cli/saveSequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function (program, sequencer) {

var params = program.saveSequence.split(' ');
sequencer.saveSequence(params[0], params[1]);
console.log("\x1b[32m", "Your sequence was saved successfully!!");

}
88 changes: 88 additions & 0 deletions src/cli/sequencerSteps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var Spinner = require('ora');
var readlineSync = require('readline-sync');
var utils = require('../CliUtils')

module.exports = function (program, sequencer, outputFilename) {
utils.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) {
var options = Object.assign({}, sequencer.modulesInfo(step).inputs);

// If inputs exists, print to console.
if (Object.keys(options).length) {
console.log("[" + step + "]: Inputs");
}

// If inputs exists, print them out with descriptions.
Object.keys(options).forEach(function(input) {
// The array below creates a variable number of spaces. This is done with (length + 1).
// The extra 4 that makes it (length + 5) is to account for the []: characters
console.log(
new Array(step.length + 5).join(" ") +
input +
": " +
options[input].desc
);
});

if (program.config) {
try {
var config = JSON.parse(program.config);
console.log(`The parsed options object: `, config);
} catch (e) {
console.error(
"\x1b[31m%s\x1b[0m",
`Options(Config) is not a not valid JSON Fallback activate`
);
program.config = false;
console.log(e);
}
}
if (program.config && utils.validateConfig(config, options)) {
console.log("Now using Options object");
Object.keys(options).forEach(function(input) {
options[input] = config[input];
});
} else {
// If inputs exist, iterate through them and prompt for values.
Object.keys(options).forEach(function(input) {
var value = readlineSync.question(
"[" +
step +
"]: Enter a value for " +
input +
" (" +
options[input].type +
", default: " +
options[input].default +
"): "
);
options[input] = value;
});
}
// Add the step and its inputs to the sequencer.
sequencer.addSteps(step, options);
});

var spinnerObj = { succeed: () => { }, stop: () => { } };
if (!process.env.TEST)
spinnerObj = Spinner("Your Image is being processed..").start();

// Run the sequencer.
sequencer.run({ progressObj: spinnerObj }, function() {
// Export all images or final image as binary files.
sequencer.exportBin(program.output, program.basic, outputFilename);

//check if spinner was not overriden stop it
if (!spinnerObj.overrideFlag) {
spinnerObj.succeed();
console.log(`\nDone!!`);
}
});
});
}