Commit a31aa61e authored by Marcel Dominic Bandowski's avatar Marcel Dominic Bandowski
Browse files

Added export to ttf2eot typings.

Added consoleErrorCode method.
parent c5af7b99
.idea
bin
fonts
node_modules
src
.yo-rc.json
tsconfig.json
yarn.lock
......
......@@ -19,7 +19,13 @@ npm install -g webfont-converter --dev
### usage example
```c
webfont-converter ./fontpath/
webfont-converter --path=./fontpath
```
or
```c
webfont-converter --p=./fontpath
```
# TODO
......
......@@ -10,6 +10,10 @@ export function consoleError(text: string): void {
console.error(redColor(text));
}
export function consoleErrorCode(err: NodeJS.ErrnoException) {
consoleError(`An error happened: ${err.code}`);
}
export function consoleWarning(text: string): void {
console.warn(yellowColor(text));
}
......
import {readdir, readFile, rename, unlink, writeFile} from "fs";
import {consoleError, consoleInfo, consoleSpecial, consoleSuccess, consoleWarning} from "./Helper";
import {readdir, readFile, rename, writeFile} from "fs";
import {remove} from "fs-extra";
import {consoleError, consoleErrorCode, consoleInfo, consoleSpecial, consoleSuccess, consoleWarning} from "./Helper";
import {ByteBuffer} from "microbuffer";
import {checkFile} from "iscamelcase";
......@@ -19,22 +21,18 @@ 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 => {
this.clearFiles(path).then((success: boolean): void => {
if (success) {
this.convertFiles(path).then((): void => {
this.renameFiles(path).catch((err: NodeJS.ErrnoException): void => {
throw err;
});
}).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 {
......@@ -42,51 +40,60 @@ export class WebfontConverter {
}
}
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
private async clearFiles(path: string, file?: string): Promise<boolean> {
return await readDirectory(path);
async function readDirectory(path): Promise<boolean> {
readdir(path, (err, files): boolean => {
if (!err) {
files.forEach(dirFile => {
if (fileExtension(dirFile) !== "otf") {
if (fileExtension(dirFile) !== "ttf") {
if (!file) {
remove(path + dirFile, (err: NodeJS.ErrnoException) => {
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("This converter can only convert the TTF file format");
throw new Error("Process aborted!");
}
});
}
else {
consoleError(`An error happened: ${err.code}`);
throw err;
}
});
});
return true;
} else {
consoleError(`An error happened: ${err.code}`);
throw err;
}
});
return true;
}
}
private async renameFiles(path: string, file?: string) {
private async renameFiles(path: string, file?: string): Promise<boolean> {
if (!file) {
await readdir(path, (err: NodeJS.ErrnoException, files: string[]): void => {
await renamingFiles(path);
} else {
return false;
}
async function renamingFiles(path): Promise<boolean> {
readdir(path, (err: NodeJS.ErrnoException, files: string[]): boolean => {
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 => {
rename(oldPath, newPath, (err: NodeJS.ErrnoException): boolean => {
if (!err) {
consoleSuccess(`Renamed ${oldPath} to ${newPath}`);
return true;
}
else {
throw err;
......@@ -94,11 +101,13 @@ export class WebfontConverter {
});
}
});
return true;
}
else {
consoleError(`Error while reading from directory: ${err.code}`);
}
});
return true;
}
}
......@@ -112,56 +121,66 @@ export class WebfontConverter {
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 => {
this.createFile(completePath, ttf2eot(input), "eot").then((success: boolean): void => {
if (success) {
createFile(completePath, ttf2svg(input), "svg").then((success: boolean): void => {
this.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!`);
}
});
})
this.createFile(completePath, ttf2woff(input), "woff").then((success: boolean): void => {
if (success) {
this.createFile(completePath, ttf2woff2(input), "woff2").then((success: boolean): void => {
if (success) {
consoleInfo(`Converting of ${completePath} started!`);
} else {
consoleError("Converting was not successful!");
}
}).catch((err: NodeJS.ErrnoException): void => {
consoleErrorCode(err);
});
} else {
consoleError("Converting was not successful!");
}
}).catch((err: NodeJS.ErrnoException): void => {
consoleErrorCode(err);
});
} else {
consoleError("Converting was not successful!");
}
}).catch((err: NodeJS.ErrnoException): void => {
consoleErrorCode(err);
});
}
else {
consoleError("Converting was not successful!");
}
}).catch((err): void => {
throw err;
}).finally((): void => {
consoleErrorCode(err);
});
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 {
} else {
throw err;
}
})
});
}
else {
} else {
throw err;
}
});
return true;
}
}
private async createFile(path: string, data: ByteBuffer, type: FontFormat): Promise<boolean> {
writeFile(`${path}.${type}`, data, (err: NodeJS.ErrnoException): boolean =>
writeFontFileCallback(err, `${path}.${type}`));
return true;
function writeFontFileCallback(err: NodeJS.ErrnoException, file: string): boolean {
if (!err) {
consoleSuccess(`Created: ${file}`);
return true;
} else {
consoleError("Error while converting file");
throw err;
}
}
}
}
\ 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
import {ByteBuffer} from "microbuffer";
declare module "ttf2eot" {
export function ttf2eot(input: Buffer): ByteBuffer
function ttf2eot(input: Buffer): ByteBuffer
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment