generator-levi-9-angularjs-2
Version:
Yeoman generator for levi9 angularjs2 projects
64 lines (55 loc) • 1.94 kB
JavaScript
'use strict';
var chalk = require('chalk');
var _ = require('lodash');
function addPrefixToPath(prefix, path) {
if (prefix === path || !path) {
return removeMultipleSlashesFromPath(prefix);
}
if (!prefix) {
return removeMultipleSlashesFromPath(path);
}
return removeMultipleSlashesFromPath(prefix + '/' + path);
}
function addSuffixToPath(path, end) {
if (!end) {
return removeMultipleSlashesFromPath(path);
}
return removeMultipleSlashesFromPath(path + '/' + end);
}
function addPrefixAndSuffixToPath(prefix, path, end) {
var fullPath = addSuffixToPath(addPrefixToPath(prefix, path), end);
return removeMultipleSlashesFromPath(fullPath);
}
function removeMultipleSlashesFromPath(path) {
return path.replace(/\/\/+/g, '/');
}
function insertInPathAfter(path, match, input) {
var fullPath = removeMultipleSlashesFromPath(path);
var end = path.split(match)[1];
if (end) {
fullPath = match + '/' + input + '/' + end;
}
return removeMultipleSlashesFromPath(fullPath);
}
function invalidInput(type, input, path) {
var nameRegex = /^[a-zA-Z-_]+$/;
var pathRegex = /^(\w+)[\\\\/\w]+/;
if (!nameRegex.test(input)) {
console.log(chalk.red('Not valid ' + type + ' name!') +
' Please run \'yo levi-9-angularjs-2:' + type + ' --help\' to check for available options.');
return true;
}
if (!_.isEmpty(path) && !pathRegex.test(path)) {
console.log(chalk.red('This does not look like valid path!') +
' Please run \'yo levi-9-angularjs-2:' + type + ' --help\' to check for available options.');
return true;
}
}
module.exports = {
invalidInput: invalidInput,
removeMultipleSlashesFromPath: removeMultipleSlashesFromPath,
addPrefixToPath: addPrefixToPath,
insertInPathAfter: insertInPathAfter,
addSuffixToPath: addSuffixToPath,
addPrefixAndSuffixToPath: addPrefixAndSuffixToPath
};