figlet
Version:
Creates ASCII Art from text. A full implementation of the FIGfont spec.
99 lines (98 loc) • 2.73 kB
JavaScript
import * as fs from "fs";
import * as path from "path";
import { f as figlet, g as getFontName } from "./figlet-C8Ns3Vyn.js";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fontPath = path.join(__dirname, "/../fonts/");
const nodeFiglet = figlet;
nodeFiglet.defaults({ fontPath });
nodeFiglet.loadFont = function(name, callback) {
const actualFontName = getFontName(name);
return new Promise((resolve, reject) => {
if (nodeFiglet.figFonts[actualFontName]) {
if (callback) {
callback(null, nodeFiglet.figFonts[actualFontName].options);
}
resolve(nodeFiglet.figFonts[actualFontName].options);
return;
}
fs.readFile(
path.join(nodeFiglet.defaults().fontPath, actualFontName + ".flf"),
{ encoding: "utf-8" },
(err, fontData) => {
if (err) {
if (callback) {
callback(err);
}
reject(err);
return;
}
fontData = fontData + "";
try {
const font = nodeFiglet.parseFont(
actualFontName,
fontData
);
if (callback) {
callback(null, font);
}
resolve(font);
} catch (error) {
const typedError = error instanceof Error ? error : new Error(String(error));
if (callback) {
callback(typedError);
}
reject(typedError);
}
}
);
});
};
nodeFiglet.loadFontSync = function(font) {
const actualFontName = getFontName(font);
if (nodeFiglet.figFonts[actualFontName]) {
return nodeFiglet.figFonts[actualFontName].options;
}
const fontData = fs.readFileSync(
path.join(nodeFiglet.defaults().fontPath, actualFontName + ".flf"),
{
encoding: "utf-8"
}
) + "";
return nodeFiglet.parseFont(actualFontName, fontData);
};
nodeFiglet.fonts = function(next) {
return new Promise((resolve, reject) => {
const fontList = [];
fs.readdir(
nodeFiglet.defaults().fontPath,
(err, files) => {
if (err) {
next && next(err);
reject(err);
return;
}
files.forEach((file) => {
if (/\.flf$/.test(file)) {
fontList.push(file.replace(/\.flf$/, ""));
}
});
next && next(null, fontList);
resolve(fontList);
}
);
});
};
nodeFiglet.fontsSync = function() {
const fontList = [];
fs.readdirSync(nodeFiglet.defaults().fontPath).forEach((file) => {
if (/\.flf$/.test(file)) {
fontList.push(file.replace(/\.flf$/, ""));
}
});
return fontList;
};
export {
nodeFiglet as default
};