UNPKG

mib-cli

Version:

CLI tool to manage projects

177 lines (141 loc) 4 kB
const autoComplete = require('../../util/auto-complete'); const projects = require('../../util/projects'); const git = require('../../util/git'); const config = require('../../util/config'); const path = require('path'); module.exports = function (vorpal, options) { const chalk = vorpal.chalk; vorpal .command('import [name]') .option('-l, --list', 'List all projects imported') .option('-R, --remove', 'Remove the given project, search by name', autoComplete.getFromProjects()) .option('-p, --path [path]', 'Path to the root folder of the project to import', autoComplete.getFromFs({directory: true})) .option('-g, --git [git]', 'Url to the git repository of the project') .description('Import a new project to the MIB team') .action(function (args, callback) { let v = this; // Check for --remove option if(args.options.remove) { if(!args.name || !projects.exists(args.name)) { v.log(chalk.red('Cannot find the name for the project to remove')); } else { projects.remove(args.name); } return callback(); } // Check for --list option if(args.options.list) { let list = projects.getRaw(); for(key in list) { v.log(key + ' :'); if (list[key].url) { v.log('\turl : ' + list[key].git); } if(list[key].path) { v.log('\tpath : ' + list[key].path); } } return callback(); } // Parse the project from the args project = parseArgs(args); // If no project found after parsing, then quit the process if(!project) { v.log(chalk.red('Cannot find the name for the project to import')); return callback(); } // No url nor path set if(!(args.options.git || args.options.path)) { v.log(chalk.red('Please provide a git url or a path in order to import the project')); return callback(); } askIfOverride(v, project, function(override) { if(override) { importProject(v, project, function(err) { if(!err) { projects.push(project); } else { v.log(chalk.red('Project not imported as errors occured !')); } callback(); }); } else { callback(); } }); }); } /** * * */ function askIfOverride(vorpal, project, callback) { if(projects.exists(project.name)) { return vorpal.prompt({ type: 'confirm', name: 'override', default: false, message: 'Project already exists, do you want to override?', }, function(result){ callback(result.override); }); } callback(true); } /** * * */ function importProject(vorpal, project, callback) { // Is a git repository, clone it if(project.git) { return importGit(vorpal, project, callback); } // Is a project already present on the disk else if (project.path) { // Only save the data return importLocalFolder(vorpal, project, callback); } // No case possible here else { return callback(new Error()); } } function importGit(vorpal, project, callback) { git.clone(vorpal, project, callback); } function importLocalFolder(vorpal, project, callback) { process.nextTick(callback); } /** * * */ function parseArgs(args) { let options = {}; // First find the name of the repository if(!args.name) { if(args.options.git) { options.name = path.basename(args.options.git, path.extname(args.options.git)); } else if (args.options.path) { options.name = args.basename(args.options.path); } } else { options.name = args.name; } // Parse path option if(args.options.path) { // If not absolute then relative to the current wd if(!path.isAbsolute(args.options.path)) { args.options.path = path.join(config.cwd(), args.options.path); } } // Then if the name is valid, try to complete the process if(options.name) { options.git = args.options.git || undefined; options.path = args.options.path || (config.root() ? path.join(config.root(), options.name) : path.join(config.cwd(), options.name)); } else { return undefined; } return options; }