@iobroker/create-adapter
Version:
Command line utility to create customized ioBroker adapters
160 lines • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkMinSelections = checkMinSelections;
exports.checkAdapterName = checkAdapterName;
exports.checkTitle = checkTitle;
exports.checkAuthorName = checkAuthorName;
exports.checkEmail = checkEmail;
exports.checkTypeScriptTools = checkTypeScriptTools;
exports.transformAdapterName = transformAdapterName;
exports.transformDescription = transformDescription;
exports.transformKeywords = transformKeywords;
exports.transformContributors = transformContributors;
const ansi_colors_1 = require("ansi-colors");
const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
/**
*
* @param category
* @param min
* @param answers
*/
async function checkMinSelections(category, min, answers) {
if (answers.length >= min) {
return true;
}
return `Please enter at least ${min} ${category}`;
}
function isAdapterNameValid(name) {
if (!isNotEmpty(name)) {
return "Please enter a valid name!";
}
const forbiddenChars = /[^a-z0-9\-_]/g;
if (forbiddenChars.test(name)) {
return `The name may only consist of lowercase letters, numbers, "-" and "_"!`;
}
if (!/^[a-z]/.test(name)) {
return `The name must start with a letter!`;
}
if (!/[a-z0-9]$/.test(name)) {
return `The name must end with a letter or number!`;
}
return true;
}
/**
*
* @param name
* @param options
*/
async function checkAdapterName(name, options) {
const validCheck = isAdapterNameValid(name);
if (typeof validCheck === "string") {
return validCheck;
}
if (options && options.checkAdapterExistence) {
const existenceCheck = await options.checkAdapterExistence(name);
if (typeof existenceCheck === "string") {
return existenceCheck;
}
}
return true;
}
/**
*
* @param title
*/
function checkTitle(title) {
if (!isNotEmpty(title)) {
return "Please enter a title!";
}
if (/iobroker|adapter/i.test(title)) {
return `The title must not contain the words "ioBroker" or "adapter"!`;
}
return true;
}
function isNotEmpty(answer) {
return answer != undefined && answer.length > 0 && answer.trim().length > 0;
}
/**
*
* @param name
*/
async function checkAuthorName(name) {
if (!isNotEmpty(name)) {
return "Please enter a valid name!";
}
return true;
}
/**
*
* @param email
*/
async function checkEmail(email) {
if (!emailRegex.test(email)) {
return "Please enter a valid email address!";
}
return true;
}
/**
*
* @param tools
*/
async function checkTypeScriptTools(tools) {
if (tools.indexOf("Prettier") > -1 && tools.indexOf("ESLint") === -1) {
return "ESLint must be selected to use Prettier!";
}
return true;
}
/**
*
* @param name
*/
function transformAdapterName(name) {
const startsWithIoBroker = /^ioBroker\./i;
if (startsWithIoBroker.test(name)) {
name = name.replace(startsWithIoBroker, "");
console.log((0, ansi_colors_1.yellow)(`You don't have to prefix the name with "ioBroker."`));
}
return name;
}
/**
*
* @param description
*/
function transformDescription(description) {
description = description.trim();
if (description.length === 0) {
return undefined;
}
return description;
}
/**
*
* @param keywords
*/
function transformKeywords(keywords) {
const keywordsArray = keywords
.trim()
.split(",")
.map(k => k.trim())
.filter(k => !!k);
if (keywordsArray.length === 0) {
return undefined;
}
return keywordsArray;
}
/**
*
* @param contributors
*/
function transformContributors(contributors) {
const contributorsArray = contributors
.trim()
.split(",")
.map(c => c.trim())
.filter(c => !!c);
if (contributorsArray.length === 0) {
return undefined;
}
return contributorsArray;
}
//# sourceMappingURL=actionsAndTransformers.js.map