Skip to content

Commit

Permalink
Merge pull request #124 from db-ui/fix-cli
Browse files Browse the repository at this point in the history
fix: cli
  • Loading branch information
nmerget authored Apr 5, 2024
2 parents c467d48 + c2adc70 commit b685df3
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 172 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"fs-extra": "^11.2.0",
"glob": "^10.3.10",
"oslllo-svg-fixer": "^4.0.1",
"svgtofont": "^4.0.1"
"svgtofont": "^4.2.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
Expand Down
58 changes: 34 additions & 24 deletions src/clean-icons/cli.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import { OptionsType, ProgrammOptionsType } from '../types';
import { OptionsType, ProgrammOptionsType } from "../types";
import cleanIcons from "./index";
import startProgram from "../program";


const options: ProgrammOptionsType[] = [
{
name: 'ignoreGlobs',
short: 'ig',
description: 'Path icon glob to exclude from the fonts',
array: true
},
{
name: 'src',
description: 'Src folder with all svgs',
required: true
},
{
name: 'debug',
description: 'Extra logging',
defaultValue: false
}
{
name: "ignoreGlobs",
short: "ig",
description: "Path icon glob to exclude from the fonts",
array: true,
},
{
name: "src",
description: "Src folder with all svgs",
required: true,
},
{
name: "traceResolution",
description: "Src folder with all svgs",
defaultValue: "600",
required: false,
},
{
name: "debug",
description: "Extra logging",
defaultValue: false,
},
];

const action = async (_: string, options: { _optionValues: OptionsType }) => {
const { src, ignoreGlobs, debug } = options._optionValues;
await cleanIcons(src, ignoreGlobs, debug);
const {
src,
ignoreGlobs,
debug,
traceResolution = "600",
} = options._optionValues;
await cleanIcons(src, ignoreGlobs, traceResolution, debug);
};

startProgram(
'@db-ui/foundations - clean icons',
'CLI to clean icon for icon fonts to work',
options,
action
"@db-ui/foundations - clean icons",
"CLI to clean icon for icon fonts to work",
options,
action,
);
6 changes: 5 additions & 1 deletion src/clean-icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { error } from "console";
const cleanIcons = async (
src: string,
ignoreGlobs?: string[],
traceResolution?: string,
debug?: boolean,
) => {
const paths = `${src}/**/*.svg`;
Expand All @@ -23,7 +24,10 @@ const cleanIcons = async (
const promises: Promise<unknown>[] = globPaths.map(async (path) => {
try {
// eslint-disable-next-line no-await-in-loop,new-cap
return await SVGFixer(path, path, { showProgressBar: debug }).fix();
return await SVGFixer(path, path, {
showProgressBar: debug,
traceResolution: Number(traceResolution || "600"),
}).fix();
} catch (catchError) {
error(path, catchError);
return catchError;
Expand Down
117 changes: 62 additions & 55 deletions src/generate-icon-fonts/data.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
import { ProgrammOptionsType } from '../types';
import { ProgrammOptionsType } from "../types";

export const gifOptions: ProgrammOptionsType[] = [
{
name: 'ignoreGlobs',
description: 'Path icon glob to exclude from the fonts',
array: true
},
{
name: 'variants',
description:
'Font variants e.g. solid, inverted, etc. We always add a "default" variant for icons.',
array: true,
defaultValue: []
},
{
name: 'cleanIgnoreVariants',
description:
'Ignore variants which should not be cleaned automatically',
array: true,
defaultValue: []
},
{
name: 'withSizes',
description: 'Splits the font into different sizes'
},
{
name: 'src',
description: 'Src folder with all svgs',
required: true
},
{
name: 'prefix',
description: 'Prefix of icons to delete for icons'
},
{
name: 'fontName',
description: 'The name of your font',
required: true
},
{
name: 'dryRun',
description: 'prints the output of the command'
},
{
name: 'debug',
description: 'Extra logging',
defaultValue: false
}
{
name: "ignoreGlobs",
description: "Path icon glob to exclude from the fonts",
array: true,
},
{
name: "variants",
description:
'Font variants e.g. solid, inverted, etc. We always add a "default" variant for icons.',
array: true,
defaultValue: [],
},
{
name: "cleanIgnoreVariants",
description: "Ignore variants which should not be cleaned automatically",
array: true,
defaultValue: [],
},
{
name: "withSizes",
description: "Splits the font into different sizes",
},
{
name: "src",
description: "Src folder with all svgs",
required: true,
},
{
name: "prefix",
description: "Prefix of icons to delete for icons",
},
{
name: "fontName",
description: "The name of your font",
required: true,
},
{
name: "dryRun",
short: "dry",
description: "prints the output of the command",
},
{
name: "debug",
description: "Extra logging",
defaultValue: false,
},
// TODO: This is buggy we should fix it by making a PR to https://github.com/jaywcjlove/svgtofont
/* {
name: "outSVGReact",
description: "Creates react svg components",
short: "react",
defaultValue: false,
},*/
];

export const fileEndingsToDelete = [
'eot',
'less',
'module.less',
'styl',
'svg',
'symbol.svg',
'ttf',
'woff'
"eot",
"less",
"module.less",
"styl",
"svg",
"symbol.svg",
"ttf",
"woff",
];
8 changes: 7 additions & 1 deletion src/generate-icon-fonts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const generateIconFonts = async (values: OptionsType): Promise<unknown> => {
cleanIgnoreVariants,
debug = false,
overwriteSources = false,
traceResolution = "600",
} = values;
const dist = `${src}/fonts`;
const temporaryDirectory = `${src}/tmp`;
Expand All @@ -46,7 +47,12 @@ const generateIconFonts = async (values: OptionsType): Promise<unknown> => {
gatherIcons(temporaryDirectory, values);

debugLog(debug, "---Start cleaning icon---");
await cleanIcons(`${temporaryDirectory}/*`, ignoreVariants, debug);
await cleanIcons(
`${temporaryDirectory}/*`,
ignoreVariants,
traceResolution,
debug,
);

debugLog(debug, "---Start svg to font ---");
const allTemporaryDirectories = FSE.readdirSync(temporaryDirectory);
Expand Down
89 changes: 45 additions & 44 deletions src/generate-icon-fonts/svg-to-font.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import path from 'node:path';
import path from "node:path";

import svgtofont from 'svgtofont';
import { OptionsType } from '../types';
import svgtofont from "svgtofont";
import { OptionsType } from "../types";
import { log } from "console";

const svgToFont = async (
temporaryDirectory: string,
dist: string,
options: OptionsType
temporaryDirectory: string,
dist: string,
options: OptionsType,
) => {
const { fontName, debug } = options;
const fileName = __filename;
let lastSlashIndex = fileName.lastIndexOf('\\');
if (lastSlashIndex === -1) {
lastSlashIndex = fileName.lastIndexOf('/');
}
const { fontName, debug, svgoOptions, outSVGReact, svgicons2svgfont } =
options;
const fileName = __filename;
let lastSlashIndex = fileName.lastIndexOf("\\");
if (lastSlashIndex === -1) {
lastSlashIndex = fileName.lastIndexOf("/");
}

const generateIconFontsDir = fileName.slice(0, Math.max(0, lastSlashIndex));
const generateIconFontsDir = fileName.slice(0, Math.max(0, lastSlashIndex));

try {
return svgtofont({
src: temporaryDirectory,
dist,
fontName,
log: debug,
css: true,
outSVGReact: false, // TODO: Consider if we want to give this to users
outSVGPath: false,
useNameAsUnicode: true,
generateInfoData: true,
// SvgoOptions: TODO: https://github.com/svg/svgo#configuration,
svgicons2svgfont: {
fontHeight: 1000,
normalize: true,
centerHorizontally: true
},
website: {
index: 'font-class',
template: path.resolve(
generateIconFontsDir,
'templates/template.ejs'
),
links: [{ title: '', url: '' }]
},
styleTemplates: path.resolve(generateIconFontsDir, 'styles')
});
} catch (error) {
console.error(error);
}
try {
return svgtofont({
src: temporaryDirectory,
dist,
fontName,
log: debug,
logger: (message) => log(message),
css: true,
outSVGReact,
outSVGPath: true,
useNameAsUnicode: true,
generateInfoData: true,
svgoOptions,
svgicons2svgfont: {
fontHeight: 1000,
normalize: true,
centerHorizontally: true,
...svgicons2svgfont,
},
website: {
index: "font-class",
template: path.resolve(generateIconFontsDir, "templates/template.ejs"),
links: [{ title: "", url: "" }],
},
styleTemplates: path.resolve(generateIconFontsDir, "styles"),
});
} catch (error) {
console.error(error);
}

return true;
return true;
};

export default svgToFont;
Loading

0 comments on commit b685df3

Please sign in to comment.