UNPKG

frontity

Version:

Frontity cli and entry point to other packages

151 lines (150 loc) 4.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasGit = exports.isInGitRepository = exports.log = exports.fetchPackageVersion = exports.isFrontityProjectRoot = exports.errorLogger = exports.toCamelCase = exports.isNamespaceValid = exports.isThemeNameValid = exports.isPackageNameValid = void 0; const chalk_1 = __importDefault(require("chalk")); const node_fetch_1 = __importDefault(require("node-fetch")); const child_process_1 = require("child_process"); const fs_extra_1 = require("fs-extra"); /** * Validation for package name. * * @param name - The package name to be checked. * @returns Whether the given name is valid. */ const isPackageNameValid = (name) => { if (name.length >= 214) return false; if ("._".includes(name[0])) return false; const specialCharacters = /[/:*?"<>|]/; if (specialCharacters.test(name)) return false; const nameConventionMatch = /^(?:@[a-z0-9_.-]+\/)?[a-z0-9_.-]+$/; return nameConventionMatch.test(name); }; exports.isPackageNameValid = isPackageNameValid; /** * Theme name validation. * * @param name - The theme name to be checked. * @returns Wheather the given name is valid. */ const isThemeNameValid = (name) => { const nameConventionMatch = /^(?:@[\w-]+\/)?[\w-]+$/; return nameConventionMatch.test(name); }; exports.isThemeNameValid = isThemeNameValid; /** * Namespace value validation. * * @param name - Namespace value. * @returns Weather the given name is valid. */ const isNamespaceValid = (name) => { // This one matches only words. Meaning, dashes, spaces, etc. are invalid const rule = /^\w+$/g; return rule.test(name); }; exports.isNamespaceValid = isNamespaceValid; /** * Takes a name, as string, and converts it to camelCase if it contains non alpha chars. * * @param name - The name value to be converted. * @returns The converted name. */ const toCamelCase = (name) => { // This one matches the dash separated values: my-namespace-is-cool const rule = /-[a-z]/gi; return name.replace(rule, (group) => { // Here we're grabbing the dash plus the following letter // and return the uppercased value of that letter. return group[1].toUpperCase(); }); }; exports.toCamelCase = toCamelCase; /** * Logs an error to the console preformatted with the proper color. * * @param error - The error instance. * @param message - The optional message. */ const errorLogger = (error, message) => { console.error(chalk_1.default.bold.red("\nError: ") + chalk_1.default.red(error.message) + "\n\n" + (message || "") + `If you need help please visit ${chalk_1.default.underline.magenta("https://community.frontity.org/")}.\n`); process.exit(1); }; exports.errorLogger = errorLogger; /** * Checks if the given path is a valid frontity project. * * @param path - The path to check. * @returns If the path is a valid project root. */ const isFrontityProjectRoot = async (path) => { const dirContent = await (0, fs_extra_1.readdir)(path); return dirContent.some((content) => /^frontity\.settings\.(js|ts)$/i.test(content)); }; exports.isFrontityProjectRoot = isFrontityProjectRoot; /** * Fetches the given package version. The latest one. * * @param pkg - The package name. * @returns The latest version. */ const fetchPackageVersion = async (pkg) => { const response = await (0, node_fetch_1.default)(`https://registry.npmjs.com/${pkg}`); const data = await response.json(); if (data.error) throw new Error(`Package "${pkg}" not found on NPM.`); const version = data["dist-tags"].latest; return version; }; exports.fetchPackageVersion = fetchPackageVersion; /** * Utility function to log to console. * * @param msg - The message to be logged. * @param optionalParams - Optional parameters. */ const log = (msg, ...optionalParams) => { if (process.env.NODE_ENV !== "test") { console.log(msg, ...optionalParams); } }; exports.log = log; /** * Checks if the current directory is a git repo. * * @returns True if the current directory is a git repo, false otherwise. */ const isInGitRepository = () => { try { (0, child_process_1.execSync)("git rev-parse --is-inside-work-tree", { stdio: "ignore" }); return true; } catch (e) { return false; } }; exports.isInGitRepository = isInGitRepository; /** * Checks if git is installed on the current machine. * * @returns True if the git command exists, false otherwise. */ const hasGit = () => { try { (0, child_process_1.execSync)("git --version", { stdio: "ignore" }); return true; } catch (e) { return false; } }; exports.hasGit = hasGit;