UNPKG

mongoose-management

Version:
73 lines (72 loc) 3.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const prompts_1 = require("../../prompts"); const group_1 = __importDefault(require("../dataset/group")); const errors_1 = require("../errors"); exports.call = (prompts, groups) => __awaiter(this, void 0, void 0, function* () { const questions = exports.getQuestions(groups); const answersMain = yield prompts.call(questions); if (answersMain.name === '') { throw new errors_1.CancelPromptError('cancel'); } return answersMain; }); exports.getQuestions = (groups) => { const nameValues = groups.getGroups().map((d) => d.getPath().toLowerCase()); return [ { type: 'fuzzypath', name: 'path', message: 'Target path for the group:', excludePath: exports.excludePath, itemType: 'directory', suggestOnly: false, rootPath: groups.getPathProject(), }, { type: 'input', name: 'name', default: 'odm', message: 'Collection group name:', validate: exports.validateName(nameValues, groups.getPathProject()), }, ]; }; exports.evaluation = (answers, groups) => { return (group) => { const path = exports.pathRelative(answers.name, answers.path, groups.getPathProject()); if (!group) { return groups.addGroup(new group_1.default({ path, collections: [] }, groups)); } group.setPath(path); return group; }; }; exports.validateName = (nameValues, pathProject) => (value, { path }) => { const name = value.trim(); const mergedPath = exports.pathRelative(name, path, pathProject); if (!prompts_1.regexpName.test(name)) { return prompts_1.regexpNameMessage; } if (nameValues.indexOf(mergedPath.toLowerCase()) >= 0) { return `The path and the name already exist as a group [group: ${mergedPath}]!`; } return true; }; exports.pathRelative = (name, pathDocuments, pathProject) => { const path = pathDocuments.replace(pathProject, ''); return path_1.join(path.substr(0, 1) === '/' ? path.substr(1) : path, name); }; exports.excludePath = (path) => /node_modules|\/\.|\\\.|^\../.test(path);