Skip to content

Commit

Permalink
Prepare 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krasnoyarov committed Jul 19, 2016
1 parent 9e7caec commit 9bb934a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 56 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 2.0.0

- Added: `--src-css-template` agument.
- Added: `--css-template-class-name` argument.
- Added: `--css-template-font-path` argument.
- Added: `--css-template-font-name` argument.
- Changed: remove `--css-template-format` argument, now format is taken from `--dest-css-template`
- Changed: remove `--css` argument, css now generated if you use `--dest-css-template` argument.
- Changed: rename `--css-template-dest` argument to `--dest-css-template`.
- Remove: `--css-template` argument.

# 1.0.1

- Fixed: get `fontId` from `fontName`, if `fontId` is `null` or `undefined`.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webfont",
"version": "1.0.1",
"version": "2.0.0",
"description": "Generator of webfonts from svg icons",
"license": "MIT",
"author": "itgalaxy <[email protected]>",
Expand Down Expand Up @@ -36,11 +36,13 @@
"CHANGELOG.md",
"LICENSE",
"dist",
"templates",
"!**/__tests__"
],
"dependencies": {
"globby": "^5.0.0",
"meow": "^3.3.0",
"mkdirp": "^0.5.1",
"svgicons2svgfont": "^5.0.0",
"svg2ttf": "^4.0.0",
"ttf2eot": "^2.0.0",
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/standalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('generate fonts with options formats `[\'svg\', \'ttf\', \'eot\']`', (t) =>

return standalone({
files: `${fixturesPath}/svg-icons/**/*`,
opts: {
config: {
formats: ['svg', 'ttf', 'eot']
}
}).then((result) => {
Expand All @@ -64,7 +64,7 @@ test('generate fonts with options formats `[\'woff2\']`', (t) => {

return standalone({
files: `${fixturesPath}/svg-icons/**/*`,
opts: {
config: {
formats: ['woff2']
}
}).then((result) => {
Expand All @@ -86,7 +86,7 @@ test('generate fonts with options css', (t) => {

return standalone({
files: `${fixturesPath}/svg-icons/**/*`,
opts: {
config: {
css: true
}
}).then((result) => {
Expand All @@ -101,9 +101,9 @@ test('generate fonts with options `css` and `css-template`', (t) => {

return standalone({
files: `${fixturesPath}/svg-icons/**/*`,
opts: {
config: {
css: true,
cssTemplate: `${fixturesPath}/templates/template.css.tpl`
srcCssTemplate: path.join(__dirname, '../../templates/template.css.tpl')
}
}).then((result) => {
t.true(typeof result.css !== 'undefined');
Expand Down
90 changes: 60 additions & 30 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fs from 'fs';
import meow from 'meow';
import mkdirp from 'mkdirp';
import path from 'path';
import standalone from './standalone';

Expand All @@ -13,13 +14,13 @@ const cli = meow(`
-f, --font-name The font family name you want, default: "webfont".
-h, --help Output usage information.
-v, --version Output the version number.
--dest Destination for generated fonts.
--formats Only this formats generate.
--css Generate css template (get build-in template).
--css-template Path to custom template.
--css-template-format Format css template "css" or "scss".
--css-template-options Accept "className", "fontPath", "fontName".
--css-template-dest Destination for generated css template.
--dest Destination for generated fonts.
--src-css-template Path to custom template.
--css-template-class-name Class name in css template.
--css-template-font-path Font path in css template.
--css-template-font-name Font name in css template.
--dest-css-template Destination for generated css template.
--verbose Tell me everything!.
For "svgicons2svgfont":
Expand All @@ -40,9 +41,11 @@ const cli = meow(`
string: [
'font-name',
'dest',
'css-template',
'css-template-format',
'css-template-dest',
'src-css-template',
'css-template-class-name',
'css-template-font-path',
'css-template-font-name',
'dest-css-template',
'font-id',
'style',
'weight',
Expand Down Expand Up @@ -86,7 +89,9 @@ const cli = meow(`

Promise.resolve().then(() => {
if (cli.input.length) {
return Object.assign({}, cli.flags, {
return Object.assign({}, {
config: cli.flags
}, {
files: cli.input
});
}
Expand All @@ -99,50 +104,75 @@ Promise.resolve().then(() => {
cli.showHelp();
}

if (!options.dest) {
if (!options.config.dest) {
return Promise.reject('Require `--dest` options');
}

if (options.css && !options.cssTemplateDest) {
return Promise.reject('Require `--css-template-dest` when `css` passed');
if (options.config.destCssTemplate) {
options.config.css = true;

const extname = path.extname(options.config.destCssTemplate);

options.config.cssFormat = extname ? extname.slice(1, extname.length) : 'css';
}

return standalone(options)
.then((result) => Promise.resolve(result))
.then((result) => {
result.options = Object.assign({}, result.options, options);
result.config = Object.assign({}, options.config);

return Promise.resolve(result);
});
})
.then((result) => {
const fontName = cli.flags.fontName;
const fontName = result.config.fontName;

return Promise.all(Object.keys(result).map((type) => {
if (type === 'options') {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
mkdirp(path.resolve(result.config.dest), (error) => {
if (error) {
return reject(error);
}

const content = result[type];
return resolve();
});
})
.then(() => {
const cssTemplateDirectory = path.resolve(path.dirname(result.config.destCssTemplate));

return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
mkdirp(cssTemplateDirectory, (error) => {
if (error) {
return reject(error);
}

return resolve();
});
});
})
.then(() => Promise.all(Object.keys(result).map((type) => {
if (type === 'config') {
return Promise.resolve();
}

const content = result[type];
let destFilename = null;

if (type !== 'css') {
destFilename = path.join(cli.flags.dest, `${fontName}.${type}`);
destFilename = path.resolve(path.join(result.config.dest, `${fontName}.${type}`));
} else {
destFilename = path.join(cli.flags.cssTemplateDest, `${fontName}.${type}`);
destFilename = path.resolve(result.config.destCssTemplate);
}

fs.writeFile(destFilename, content, (error) => {
if (error) {
return reject(new Error(error));
}
return new Promise((resolve, reject) => {
fs.writeFile(destFilename, content, (error) => {
if (error) {
return reject(new Error(error));
}

return resolve();
return resolve();
});
});
});
}))
})))
.then(() => Promise.resolve(result));
})
.catch((error) => {
Expand Down
41 changes: 21 additions & 20 deletions src/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function ttf2woff2Fn(result, options) {

export default function ({
files,
opts
config
} = {}) {
if (!files) {
return Promise.reject(new Error('You must pass webfont a `files` glob'));
Expand All @@ -103,14 +103,11 @@ export default function ({
'woff2'
],
css: false,
// Add support specify css or scss or less
cssTemplate: null,
cssTemplateFormat: 'css',
cssTemplateOptions: {
className: null,
fontPath: './',
fontName: null
},
srcCssTemplate: null,
cssFormat: 'css',
cssTemplateClassName: null,
cssTemplateFontPath: './',
cssTemplateFontName: null,
formatOptions: {
ttf: {
copyright: null,
Expand All @@ -132,7 +129,7 @@ export default function ({
prependUnicode: false,
metadata: null,
log: console.log, // eslint-disable-line
}, opts);
}, config);

if (!options.verbose) {
options.log = () => {}; // eslint-disable-line
Expand Down Expand Up @@ -183,30 +180,34 @@ export default function ({
return Promise.resolve(result);
}

if (!options.cssTemplate) {
nunjucks.configure(__dirname);
if (!options.srcCssTemplate) {
nunjucks.configure(path.join(__dirname, '../'));

options.cssTemplate = path.join(__dirname, `templates/template.${options.cssTemplateFormat}.tpl`);
options.srcCssTemplate = path.join(__dirname, `../templates/template.${options.cssFormat}.tpl`);
}

if (!options.cssTemplateOptions.className) {
options.cssTemplateOptions.className = options.fontName;
if (!options.cssTemplateClassName) {
options.cssTemplateClassName = options.fontName;
}

if (!options.cssTemplateOptions.fontName) {
options.cssTemplateOptions.fontName = options.fontName;
if (!options.cssTemplateFontName) {
options.cssTemplateFontName = options.fontName;
}

const nunjucksOptions = Object.assign(
{},
options.cssTemplateOptions,
{
glyphs
},
options
options,
{
className: options.cssTemplateClassName,
fontPath: options.cssTemplateFontPath,
fontName: options.cssTemplateFontName
}
);

result.css = nunjucks.render(path.resolve(options.cssTemplate), nunjucksOptions);
result.css = nunjucks.render(path.resolve(options.srcCssTemplate), nunjucksOptions);

return result;
})
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 9bb934a

Please sign in to comment.