nativescript
Version:
Command-line interface for building NativeScript projects
89 lines • 3.71 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectNameValidator = void 0;
const path = require("path");
const _ = require("lodash");
const helpers = require("../helpers");
const ValidationResult = require("./validation-result");
const yok_1 = require("../yok");
class ProjectNameValidator {
constructor($errors) {
this.$errors = $errors;
}
validateName(name) {
const validNameRegex = /^[a-zA-Z0-9_.\- ]*$/g;
const ext = path.extname(name);
if (helpers.isNullOrWhitespace(name)) {
return new ValidationResult.ValidationResult(ProjectNameValidator.EMPTY_FILENAME_ERROR_MESSAGE);
}
if (!validNameRegex.test(name)) {
return new ValidationResult.ValidationResult(ProjectNameValidator.NOT_VALID_NAME_ERROR_MESSAGE);
}
if (_.includes(ProjectNameValidator.INVALID_FILENAMES, name.split(".")[0])) {
return new ValidationResult.ValidationResult(ProjectNameValidator.RESERVED_NAME_ERROR_MESSAGE);
}
if (ext !== "" &&
_.includes(ProjectNameValidator.INVALID_EXTENSIONS, ext)) {
return new ValidationResult.ValidationResult(ProjectNameValidator.INVALID_EXTENSION_ERROR_MESSAGE);
}
if (name.length > ProjectNameValidator.MAX_FILENAME_LENGTH) {
return new ValidationResult.ValidationResult(ProjectNameValidator.TOO_LONG_NAME_ERROR_MESSAGE);
}
if (_.startsWith(name, " ")) {
return new ValidationResult.ValidationResult(ProjectNameValidator.LEADING_SPACES_ERROR_MESSAGE);
}
if (_.endsWith(name, ".")) {
return new ValidationResult.ValidationResult(ProjectNameValidator.TRAILING_DOTS_ERROR_MESSAGE);
}
if (_.endsWith(name, " ")) {
return new ValidationResult.ValidationResult(ProjectNameValidator.TRAILING_SPACES_ERROR_MESSAGE);
}
return ValidationResult.ValidationResult.Successful;
}
validate(name) {
const validationResult = this.validateName(name);
const isSuccessful = validationResult.isSuccessful;
if (!isSuccessful) {
this.$errors.fail(validationResult.error);
}
return isSuccessful;
}
}
exports.ProjectNameValidator = ProjectNameValidator;
ProjectNameValidator.MAX_FILENAME_LENGTH = 30;
ProjectNameValidator.EMPTY_FILENAME_ERROR_MESSAGE = "Name cannot be empty.";
ProjectNameValidator.NOT_VALID_NAME_ERROR_MESSAGE = "Name should contain only the following symbols: A-Z, a-z, 0-9, _, ., - and space";
ProjectNameValidator.RESERVED_NAME_ERROR_MESSAGE = "Name is among the reserved names: ~, CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.";
ProjectNameValidator.INVALID_EXTENSION_ERROR_MESSAGE = "Unsupported file type.";
ProjectNameValidator.TOO_LONG_NAME_ERROR_MESSAGE = "Name is too long.";
ProjectNameValidator.TRAILING_DOTS_ERROR_MESSAGE = "Name cannot contain trailing dots.";
ProjectNameValidator.LEADING_SPACES_ERROR_MESSAGE = "Name cannot contain leading spaces.";
ProjectNameValidator.TRAILING_SPACES_ERROR_MESSAGE = "Name cannot contain trailing spaces.";
ProjectNameValidator.INVALID_FILENAMES = [
"~",
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
];
ProjectNameValidator.INVALID_EXTENSIONS = [];
yok_1.injector.register("projectNameValidator", ProjectNameValidator);
//# sourceMappingURL=project-name-validator.js.map
;