UNPKG

boostr

Version:
69 lines 2.44 kB
import { readFileSync, statSync, readdirSync } from 'fs'; import hasha from 'hasha'; import baseX from 'base-x'; import without from 'lodash/without.js'; const base62 = baseX('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); const packageURL = new URL('../package.json', import.meta.url); const { name: programName, version: programVersion } = JSON.parse(readFileSync(packageURL, 'utf-8')); export { programName, programVersion }; export function logMessage(message, { serviceName } = {}) { console.log(prefixMessageWithServiceName(message, serviceName)); } export function logError(message, { serviceName } = {}) { console.error(prefixMessageWithServiceName(message, serviceName)); } export function throwError(message, { serviceName } = {}) { throw Object.assign(new Error('Error'), { displayMessage: message, serviceName }); } export function prefixMessageWithServiceName(message, serviceName) { if (serviceName !== undefined) { message = `[${serviceName}] ${message}`; } return message; } export function resolveVariables(string, variables) { for (const [name, value] of Object.entries(variables)) { string = string.replace(new RegExp(`\\{\\{${name}\\}\\}`, 'g'), String(value)); } return string; } export function fileExists(file) { try { return statSync(file).isFile(); } catch { return false; } } export function directoryExists(directory) { try { return statSync(directory).isDirectory(); } catch { return false; } } export function getFileSize(file) { return statSync(file).size; } export function generateHashFromFile(file) { const md5 = hasha.fromFileSync(file, { encoding: 'buffer', algorithm: 'md5' }); return base62.encode(md5); } export function directoryIsEmpty(directory, { ignoreDirectoryNames = [] } = {}) { const entries = without(readdirSync(directory), ...ignoreDirectoryNames); return entries.length === 0; } export function ensureMaximumStringLength(string, maximumLength, { hashLength = 6 } = {}) { if (string.length <= maximumLength) { return string; } const head = string.slice(0, maximumLength - hashLength); const tail = string.slice(maximumLength - hashLength); const hash = hasha(tail, { algorithm: 'md5' }).slice(0, hashLength).toUpperCase(); return head + hash; } //# sourceMappingURL=utilities.js.map