Commit 5f55a277 authored by Marcel Dominic Bandowski's avatar Marcel Dominic Bandowski
Browse files

Switched to Typescript as this package get to big to only use classic node js.

parent 8cfbc6b1
#! /usr/bin/env node
import {WebfontConverter} from "../src";
new WebfontConverter(process.argv[2]);
\ No newline at end of file
#! /usr/bin/env node
const webfontConverter = require('../src/index');
webfontConverter(process.argv[2]);
\ No newline at end of file
......@@ -2,7 +2,7 @@
"name": "webfont-converter",
"version": "0.4.1",
"description": "Converts ttf files into formats that modern browsers need",
"main": "src/index.js",
"main": "src/index-backup.js",
"repository": {
"type": "git",
"url": "https://gitlab.white-it.net/Winschu/font-converter.git"
......@@ -39,21 +39,26 @@
"dependencies": {
"camelcase": "^5.0.0",
"colors": "^1.3.2",
"iscamelcase": "^0.5.3",
"file-extension": "^4.0.5",
"iscamelcase": "^0.6.2",
"node": "^11.0.0",
"super-trailing-slash": "^1.0.2",
"ttf2eot": "^2.0.0",
"ttf2svg": "^1.2.0",
"ttf2woff": "^2.0.1",
"ttf2woff2": "^2.0.3"
"ttf2woff2": "^2.0.3",
"yargs": "^12.0.2"
},
"devDependencies": {
"file-extension": "^4.0.5",
"@types/fs-extra": "^5.0.4",
"@types/node": "^10.12.0",
"generator-gitignore-io": "^0.1.0",
"node": "^10.12.0"
"microbuffer": "^1.0.0",
"typescript": "^3.1.3"
},
"files": [
"bin/webfont-converter.js",
"src/index.js",
"src/index-backup.js",
"./README.md"
]
}
import {
blue as blueColor,
green as greenColor,
rainbow as rainbowColor,
red as redColor,
yellow as yellowColor
} from "colors";
export function consoleError(text: string): void {
console.error(redColor(text));
}
export function consoleWarning(text: string): void {
console.warn(yellowColor(text));
}
export function consoleSuccess(text: string): void {
console.log(greenColor(text));
}
export function consoleInfo(text: string): void {
console.log(blueColor(text));
}
export function consoleSpecial(text: string): void {
console.log(rainbowColor(text));
}
import {readdir, readFile, rename, unlink, writeFile} from "fs";
import {consoleError, consoleInfo, consoleSpecial, consoleSuccess, consoleWarning} from "./Helper";
import {ByteBuffer} from "microbuffer";
import {checkFile} from "iscamelcase";
import {FontFormat} from "./Types";
const ttf2eot = require("ttf2eot");
const ttf2svg = require("ttf2svg");
const ttf2woff = require("ttf2woff");
const ttf2woff2 = require("ttf2woff2");
const fileExtension = require("file-extension");
const slash = require("super-trailing-slash");
const camelCase = require("camelcase");
export class WebfontConverter {
constructor(path: string, file?: string) {
path = slash.add(path);
if (!file) {
this.clearFiles(path).then((): void => {
this.convertFiles(path).then((): void => {
this.renameFiles(path).catch((err: NodeJS.ErrnoException): void => {
throw err;
}).finally((): void => {
consoleInfo("Completed renaming files!");
});
}).catch((err: NodeJS.ErrnoException): void => {
throw err;
}).finally((): void => {
consoleInfo("Completed converting files!");
});
}).catch((err: NodeJS.ErrnoException): void => {
throw err;
}).finally((): void => {
consoleInfo("Completed deletion of files!");
});
}
else {
//TODO: functions for single files
}
}
private async clearFiles(path: string, file?: string) {
await readdir(path, (err, files) => {
if (!err) {
files.forEach(dirFile => {
if (fileExtension(dirFile) !== "otf") {
if (fileExtension(dirFile) !== "ttf") {
if (!file) {
unlink(path + dirFile, (err) => {
if (!err) {
consoleWarning(`Removed ${path + dirFile}`);
}
else {
consoleError(`An error happened: ${err.code}`);
}
});
}
else {
//TODO: Unlink single file
}
}
}
else {
consoleError("This converter can only convert the TTF file format");
throw new Error("Process aborted!");
}
});
}
else {
consoleError(`An error happened: ${err.code}`);
throw err;
}
});
}
private async renameFiles(path: string, file?: string) {
if (!file) {
await readdir(path, (err: NodeJS.ErrnoException, files: string[]): void => {
if (!err) {
files.forEach(file => {
if (!checkFile(path + file)) {
const oldPath: string = path + file;
const newPath: string = path + camelCase(file.replace(/\.[0-9a-z]+$/i, "")) + ".ttf";
rename(oldPath, newPath, (err: NodeJS.ErrnoException): void => {
if (!err) {
consoleSuccess(`Renamed ${oldPath} to ${newPath}`);
}
else {
throw err;
}
});
}
});
}
else {
consoleError(`Error while reading from directory: ${err.code}`);
}
});
}
}
private async convertFiles(path: string, file?: string): Promise<boolean> {
if (!file) {
await readdir(path, (err: NodeJS.ErrnoException, files: string[]): void => {
if (!err) {
consoleSpecial(`Start converting ${files.length} files`);
files.forEach(file => {
consoleInfo(`Found: ${file}`);
readFile((path + file), (err: NodeJS.ErrnoException, input: Buffer): void => {
if (!err) {
const completePath: string = path + camelCase(file.replace(/\.[0-9a-z]+$/i, ""));
createFile(completePath, ttf2eot(input), "eot").then((success: boolean): void => {
if (success) {
createFile(completePath, ttf2svg(input), "svg").then((success: boolean): void => {
if (success) {
createFile(completePath, ttf2woff(input), "woff").then((): void => {
createFile(completePath, ttf2woff2(input), "woff2").then((success: boolean): void => {
if (success) {
consoleInfo(`Converting of ${completePath} started!`);
}
});
})
}
});
}
else {
consoleError("Converting was not successful!");
}
}).catch((err): void => {
throw err;
}).finally((): void => {
});
async function createFile(path: string, data: ByteBuffer, type: FontFormat): Promise<boolean> {
writeFile(`${path}.${type}`, data, (err: NodeJS.ErrnoException): void =>
writeFontFileCallback(err, `${path}.${type}`));
return true;
function writeFontFileCallback(err: NodeJS.ErrnoException, file: string): void {
if (!err) {
consoleSuccess(`Created: ${file}`);
}
else {
consoleError("Error while converting file");
throw err;
}
}
}
}
else {
throw err;
}
})
});
}
else {
throw err;
}
});
return true;
}
}
}
\ No newline at end of file
export type FontFormat = "ttf" | "otf" | "eot" | "svg" | "woff" | "woff2";
\ No newline at end of file
const fs = require('fs');
const colors = require("colors");
const camelCase = require("camelcase");
const isCamelCase = require("iscamelcase");
const slash = require('super-trailing-slash');
const fileExtension = require('file-extension');
const ttf2eot = require('ttf2eot');
const ttf2svg = require('ttf2svg');
const ttf2woff = require('ttf2woff');
const ttf2woff2 = require('ttf2woff2');
function webfontConverter(originalPath, originalFile) {
if (!originalFile) {
const fontPath = originalPath;
//clears all other files or old other font files from the directory
clearFiles(fontPath).then(() => {
console.log(colors.magenta("All other files than these in ttf format have been removed"));
//checks if there are still files that can be converted
fs.readdir((fontPath), (err, files) => {
if (files.length !== 0) {
console.log(colors.magenta(`There are ${fs.readdirSync(fontPath).length} files left`));
//Iterates trough each remaining file
convertFiles(fontPath).then((err) => {
if (!err) {
//converts remaining files to camelCase
renameFiles(fontPath).then((err) => {
if (err) {
throw err;
}
});
}
});
}
else {
console.error(colors.red("There are no files in this directory"));
}
});
});
} else {
clearOtherFiles(originalPath, originalFile).then(() => {
})
}
}
async function clearFiles(fontPath) {
//clears all other files or old other font files from the directory
fs.readdir(fontPath, (err, files) => {
if (!err) files.forEach(file => {
if (fileExtension(file) === "otf") {
throw new Error(colors.red("This converter can only convert the TTF file format"));
}
if (fileExtension(file) !== "ttf") fs.unlink(fontPath + file, (err) => {
if (!err) {
console.warn(colors.yellow(`${file} removed from chosen directory`));
}
else {
throw err;
}
});
});
else {
throw err;
}
});
}
async function clearOtherFiles(path, originalFile) {
fs.readdir(path, (err, files) => {
if (!err) {
files.forEach(file => {
if (file - `.${fileExtension(file)}` === originalFile) {
console.log(file);
}
});
}
})
}
async function renameFiles(fontPath) {
fs.readdir(fontPath, (err, files) => {
if (!err) files.forEach(file => {
if (!isCamelCase.checkFile(fontPath + file)) {
fs.renameSync(fontPath + file, fontPath + camelCase(file.replace(/\.[0-9a-z]+$/i, "")) + ".ttf");
}
});
});
}
async function convertFiles(fontPath) {
fs.readdir(fontPath, (err, files) => {
if (!err) {
console.log(colors.rainbow(`Start converting ${files.length} files`));
files.forEach(file => {
console.log(colors.blue(`Found: ${file}`));
fs.readFile((fontPath + file), (err, input) => {
if (!err) {
const fontFilePath = fontPath + camelCase(file.replace(/\.[0-9a-z]+$/i, ""));
fs.writeFile(`${fontFilePath}.eot`, ttf2eot(input), (err) => writeFontFileCallback(err, `${fontFilePath}.eot`));
fs.writeFile(`${fontFilePath}.svg`, ttf2svg(input), (err) => writeFontFileCallback(err, `${fontFilePath}.svg`));
fs.writeFile(`${fontFilePath}.woff`, ttf2woff(input), (err) => writeFontFileCallback(err, `${fontFilePath}.woff`));
fs.writeFile(`${fontFilePath}.woff2`, ttf2woff2(input), (err) => writeFontFileCallback(err, `${fontFilePath}.woff2`));
function writeFontFileCallback(err, file) {
if (!err) {
console.log(colors.green(`Created: ${file}`));
}
else {
throw err;
}
}
}
else {
throw err;
}
});
});
}
else {
throw err;
}
});
}
module.exports = webfontConverter;
\ No newline at end of file
const fs = require('fs');
const colors = require("colors");
const camelCase = require("camelcase");
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const ttf2eot_1 = require("ttf2eot");
const ttf2svg_1 = require("ttf2svg");
const ttf2woff_1 = require("ttf2woff");
const ttf2woff2_1 = require("ttf2woff2");
const file_extension_1 = require("file-extension");
const Helper_1 = require("./Helper");
const camelcase_1 = require("camelcase");
const isCamelCase = require("iscamelcase");
const slash = require('super-trailing-slash');
const fileExtension = require('file-extension');
const ttf2eot = require('ttf2eot');
const ttf2svg = require('ttf2svg');
const ttf2woff = require('ttf2woff');
const ttf2woff2 = require('ttf2woff2');
function webfontConverter(path) {
//appends trailing slash to path if needed
const fontPath = slash.add(path); //Example: ./fonts/fontFamily/
//clears all other files or old other font files from the directory
clearFiles(fontPath).then(() => {
console.log(colors.magenta("All other files than these in ttf format have been removed"));
//checks if there are still files that can be converted
fs.readdir((fontPath), (err, files) => {
if (files.length !== 0) {
console.log(colors.magenta(`There are ${fs.readdirSync(fontPath).length} files left`));
//Iterates trough each remaining file
convertFiles(fontPath).then((err) => {
//converts remaining files to camelCase
renameFiles(fontPath).then(() => {
if (err) {
throw err;
}
class WebfontConverter {
constructor(path, file) {
if (!file) {
this.clearFiles(path).then(() => {
this.convertFiles(path).then(() => {
this.renameFiles(path).catch((err) => {
throw err;
}).finally(() => {
Helper_1.consoleInfo("Completed renaming files!");
});
}).catch((err) => {
throw err;
}).finally(() => {
Helper_1.consoleInfo("Completed converting files!");
});
}).catch((err) => {
throw err;
}).finally(() => {
Helper_1.consoleInfo("Completed deletion of files!");
});
}
else {
//TODO: functions for single files
}
}
async clearFiles(path, file) {
await fs_1.readdir(path, (err, files) => {
if (!err) {
files.forEach(dirFile => {
if (file_extension_1.fileExtension(dirFile) !== "otf") {
if (file_extension_1.fileExtension(dirFile) !== "ttf") {
if (!file) {
fs_1.unlink(path + dirFile, (err) => {
if (!err) {
Helper_1.consoleWarning(`Removed ${path + dirFile}`);
}
else {
Helper_1.consoleError(`An error happened: ${err.code}`);
}
});
}
else {
//TODO: Unlink single file
}
}
else {
}
}
else {
Helper_1.consoleError("This converter can only convert the TTF file format");
throw new Error("Process aborted!");
}
});
}
else {
console.error(colors.red("There are no files in this directory"));
Helper_1.consoleError(`An error happened: ${err.code}`);
throw err;
}
});
});
}
async function clearFiles(fontPath) {
//clears all other files or old other font files from the directory
fs.readdir(fontPath, (err, files) => {
if (!err) files.forEach(file => {
if (fileExtension(file) === "otf") {
throw new Error(colors.red("This converter can only convert the TTF file format"));
}
if (fileExtension(file) !== "ttf") fs.unlink(fontPath + file, (err) => {
}
async renameFiles(path, file) {
if (!file) {
await fs_1.readdir(path, (err, files) => {
if (!err) {
console.warn(colors.yellow(`${file} removed from chosen directory`));
files.forEach(file => {
if (!isCamelCase.checkFile(path + file)) {
const oldPath = path + file;
const newPath = path + camelcase_1.default(file.replace(/\.[0-9a-z]+$/i, "")) + ".ttf";
fs_1.rename(oldPath, newPath, (err) => {
if (!err) {
Helper_1.consoleSuccess(`Renamed ${oldPath} to ${newPath}`);
}
else {
throw err;
}
});
}
});
}
else {
throw err;
Helper_1.consoleError(`Error while reading from directory: ${err.code}`);
}
});
});
else {
throw err;
}
});
}
async function renameFiles(fontPath) {
fs.readdir(fontPath, (err, files) => {
if (!err) files.forEach(file => {
if (!isCamelCase.checkFile(fontPath + file)) {
fs.renameSync(fontPath + file, fontPath + camelCase(file.replace(/\.[0-9a-z]+$/i, "")) + ".ttf");
}
});
});
}
async function convertFiles(fontPath) {
fs.readdir(fontPath, (err, files) => {
if (!err) {
console.log(colors.rainbow(`Start converting ${files.length} files`));
files.forEach(file => {
console.log(colors.blue(`Found: ${file}`));
fs.readFile((fontPath + file), (err, input) => {
if (!err) {
const fontFilePath = fontPath + camelCase(file.replace(/\.[0-9a-z]+$/i, ""));
fs.writeFile(`${fontFilePath}.eot`, ttf2eot(input), (err) => writeFontFileCallback(err, `${fontFilePath}.eot`));
fs.writeFile(`${fontFilePath}.svg`, ttf2svg(input), (err) => writeFontFileCallback(err, `${fontFilePath}.svg`));
fs.writeFile(`${fontFilePath}.woff`, ttf2woff(input), (err) => writeFontFileCallback(err, `${fontFilePath}.woff`));
fs.writeFile(`${fontFilePath}.woff2`, ttf2woff2(input), (err) => writeFontFileCallback(err, `${fontFilePath}.woff2`));
function writeFontFileCallback(err, file) {
}
async convertFiles(path, file) {
if (!file) {
await fs_1.readdir(path, (err, files) => {
if (!err) {
Helper_1.consoleSpecial(`Start converting ${files.length} files`);
files.forEach(file => {
Helper_1.consoleInfo(`Found: ${file}`);
fs_1.readFile((path + file), (err, input) => {
if (!err) {
console.log(colors.green(`Created: ${file}`));
const completePath = path + camelcase_1.default(file.replace(/\.[0-9a-z]+$/i, ""));
createFile(completePath, ttf2eot_1.ttf2eot(input), "eot").then((success) => {
if (success) {
createFile(completePath, ttf2svg_1.ttf2svg(input), "svg").then((success) => {
if (success) {
createFile(completePath, ttf2woff_1.ttf2woff(input), "woff").then(() => {
createFile(completePath, ttf2woff2_1.ttf2woff2(input), "woff2").then((success) => {
if (success) {
Helper_1.consoleSpecial("Converting completed!");
}
});
});
}
});
}
else {
Helper_1.consoleError("Converting was not successful!");
}
}).catch((err) => {
throw err;
}).finally(() => {
});
async function createFile(path, data, type) {