Skip to content

Commit

Permalink
Format code using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
oblador committed May 7, 2017
1 parent 3a23936 commit 585e56d
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 147 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"import/no-extraneous-dependencies": [0],
"import/no-unresolved": [2, { ignore: ['^react(-native)?$'] }],
"import/extensions": [2, { "js": "never", "json": "always" }]
"import/extensions": [2, { "js": "never", "json": "always" }],
"arrow-parens": ["error", "as-needed"],
"comma-dangle": ["error", {
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "ignore",
}]
}
}
2 changes: 1 addition & 1 deletion RNIMigration.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Icon extends React.Component {

iconRef = null;

handleComponentRef = (ref) => {
handleComponentRef = ref => {
this.iconRef = ref;
};

Expand Down
27 changes: 12 additions & 15 deletions generate-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
'use strict';

var argv = require('yargs')
.usage('Usage: $0 [options] path/to/styles.css \nFor default template please provide --componentName and --fontFamily')
.usage(
'Usage: $0 [options] path/to/styles.css \nFor default template please provide --componentName and --fontFamily'
)
.demand(1)
.default('p', '.icon-')
.describe('p', 'CSS selector prefix')
Expand All @@ -13,34 +15,29 @@ var argv = require('yargs')
.describe('o', 'Save output to file, defaults to STDOUT')
.alias('o', 'output')
.describe('g', 'Save glyphmap JSON to file')
.alias('g', 'glyphmap')
.argv;
.alias('g', 'glyphmap').argv;

var _ = require('lodash');
var fs = require('fs');
var generateIconSetFromCss = require('./lib/generate-icon-set-from-css');

var template;
if(argv.template) {
if (argv.template) {
template = fs.readFileSync(argv.template, { encoding: 'utf8' });
}

var data = _.omit(argv, '_ $0 o output p prefix t template g glyphmap'.split(' '));

var data = _.omit(
argv,
'_ $0 o output p prefix t template g glyphmap'.split(' ')
);

var content = generateIconSetFromCss(argv._, argv.prefix, template, data);
if(argv.output) {
fs.writeFileSync(
argv.output,
content
);
if (argv.output) {
fs.writeFileSync(argv.output, content);
} else {
console.log(content);
}

if (argv.glyphmap) {
fs.writeFileSync(
argv.glyphmap,
generateIconSetFromCss(argv._, argv.prefix)
);
fs.writeFileSync(argv.glyphmap, generateIconSetFromCss(argv._, argv.prefix));
}
25 changes: 10 additions & 15 deletions generate-material-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
'use strict';

var argv = require('yargs')
.usage('Usage: $0 [options] path/to/codepoints \nFor default template please provide --componentName and --fontFamily')
.usage(
'Usage: $0 [options] path/to/codepoints \nFor default template please provide --componentName and --fontFamily'
)
.demand(1)
.default('t', __dirname + '/templates/bundled-icon-set.tpl')
.describe('t', 'Template in lodash format')
.alias('t', 'template')
.describe('o', 'Save output to file, defaults to STDOUT')
.alias('o', 'output')
.describe('g', 'Save glyphmap JSON to file')
.alias('g', 'glyphmap')
.argv;
.alias('g', 'glyphmap').argv;

var _ = require('lodash');
var fs = require('fs');
Expand All @@ -21,7 +22,7 @@ var extractGlyphMapFromCodepoints = function(fileName) {
var glyphMap = {};
codepoints.forEach(function(point) {
var parts = point.split(' ');
if(parts.length === 2) {
if (parts.length === 2) {
glyphMap[parts[0].replace(/_/g, '-')] = parseInt(parts[1], 16);
}
});
Expand All @@ -30,33 +31,27 @@ var extractGlyphMapFromCodepoints = function(fileName) {
};

var template;
if(argv.template) {
if (argv.template) {
template = fs.readFileSync(argv.template, { encoding: 'utf8' });
}

var data = _.omit(argv, '_ $0 o output t template g glyphmap'.split(' '));
var glyphMap = extractGlyphMapFromCodepoints(argv._[0]);

var content = JSON.stringify(glyphMap, null, ' ');
if(template) {
if (template) {
var compiled = _.template(template);
data = data || {};
data.glyphMap = content;
content = compiled(data);
}

if(argv.output) {
fs.writeFileSync(
argv.output,
content
);
if (argv.output) {
fs.writeFileSync(argv.output, content);
} else {
console.log(content);
}

if (argv.glyphmap) {
fs.writeFileSync(
argv.glyphmap,
JSON.stringify(glyphMap, null, ' ')
);
fs.writeFileSync(argv.glyphmap, JSON.stringify(glyphMap, null, ' '));
}
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { default as createIconSet } from './lib/create-icon-set';
export { default as createIconSetFromFontello } from './lib/create-icon-set-from-fontello';
export { default as createIconSetFromIcoMoon } from './lib/create-icon-set-from-icomoon';
export {
default as createIconSetFromFontello,
} from './lib/create-icon-set-from-fontello';
export {
default as createIconSetFromIcoMoon,
} from './lib/create-icon-set-from-icomoon';
14 changes: 7 additions & 7 deletions lib/create-icon-set-from-fontello.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import createIconSet from './create-icon-set';

export default function createIconSetFromFontello(config, fontFamilyArg, fontFile) {
export default function createIconSetFromFontello(
config,
fontFamilyArg,
fontFile
) {
const glyphMap = {};
config.glyphs.forEach((glyph) => {
config.glyphs.forEach(glyph => {
glyphMap[glyph.css] = glyph.code;
});

const fontFamily = fontFamilyArg || config.name || 'fontello';

return createIconSet(
glyphMap,
fontFamily,
fontFile || `${fontFamily}.ttf`,
);
return createIconSet(glyphMap, fontFamily, fontFile || `${fontFamily}.ttf`);
}
17 changes: 9 additions & 8 deletions lib/create-icon-set-from-icomoon.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import createIconSet from './create-icon-set';

export default function createIconSetFromIcoMoon(config, fontFamilyArg, fontFile) {
export default function createIconSetFromIcoMoon(
config,
fontFamilyArg,
fontFile
) {
const glyphMap = {};
config.icons.forEach((icon) => {
config.icons.forEach(icon => {
glyphMap[icon.properties.name] = icon.properties.code;
});

const fontFamily = fontFamilyArg || config.preferences.fontPref.metadata.fontFamily;
const fontFamily =
fontFamilyArg || config.preferences.fontPref.metadata.fontFamily;

return createIconSet(
glyphMap,
fontFamily,
fontFile || `${fontFamily}.ttf`,
);
return createIconSet(glyphMap, fontFamily, fontFile || `${fontFamily}.ttf`);
}
59 changes: 38 additions & 21 deletions lib/create-icon-set.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React, {
Component,
} from 'react';

import React, { Component } from 'react';
import PropTypes from 'prop-types';

import {
NativeModules,
Platform,
Expand All @@ -16,7 +12,8 @@ import createIconButtonComponent from './icon-button';
import createTabBarItemIOSComponent from './tab-bar-item-ios';
import createToolbarAndroidComponent from './toolbar-android';

const NativeIconAPI = (NativeModules.RNVectorIconsManager || NativeModules.RNVectorIconsModule);
const NativeIconAPI =
NativeModules.RNVectorIconsManager || NativeModules.RNVectorIconsModule;

const DEFAULT_ICON_SIZE = 12;
const DEFAULT_ICON_COLOR = 'black';
Expand Down Expand Up @@ -55,7 +52,7 @@ export default function createIconSet(glyphMap, fontFamily, fontFile) {
}

root = null;
handleRef = (ref) => {
handleRef = ref => {
this.root = ref;
};

Expand All @@ -81,18 +78,26 @@ export default function createIconSet(glyphMap, fontFamily, fontFile) {
props.style = [styleDefaults, style, styleOverrides];
props.ref = this.handleRef;

return (<Text {...props}>{glyph}{this.props.children}</Text>);
return <Text {...props}>{glyph}{this.props.children}</Text>;
}
}

const imageSourceCache = {};

function getImageSource(name, size = DEFAULT_ICON_SIZE, color = DEFAULT_ICON_COLOR) {
function getImageSource(
name,
size = DEFAULT_ICON_SIZE,
color = DEFAULT_ICON_COLOR
) {
if (!NativeIconAPI) {
if (Platform.OS === 'android') {
throw new Error('RNVectorIconsModule not available, did you properly integrate the module?');
throw new Error(
'RNVectorIconsModule not available, did you properly integrate the module?'
);
}
throw new Error('RNVectorIconsManager not available, did you add the library to your project and link with libRNVectorIcons.a?');
throw new Error(
'RNVectorIconsManager not available, did you add the library to your project and link with libRNVectorIcons.a?'
);
}

let glyph = glyphMap[name] || '?';
Expand All @@ -113,23 +118,35 @@ export default function createIconSet(glyphMap, fontFamily, fontFile) {
resolve({ uri: cached, scale });
}
} else {
NativeIconAPI.getImageForFont(fontReference, glyph, size, processedColor, (err, image) => {
const error = (typeof err === 'string' ? new Error(err) : err);
imageSourceCache[cacheKey] = image || error || false;
if (!error && image) {
resolve({ uri: image, scale });
} else {
reject(error);
NativeIconAPI.getImageForFont(
fontReference,
glyph,
size,
processedColor,
(err, image) => {
const error = typeof err === 'string' ? new Error(err) : err;
imageSourceCache[cacheKey] = image || error || false;
if (!error && image) {
resolve({ uri: image, scale });
} else {
reject(error);
}
}
});
);
}
});
}

Icon.Button = createIconButtonComponent(Icon);
Icon.TabBarItem = createTabBarItemIOSComponent(IconNamePropType, getImageSource);
Icon.TabBarItem = createTabBarItemIOSComponent(
IconNamePropType,
getImageSource
);
Icon.TabBarItemIOS = Icon.TabBarItem;
Icon.ToolbarAndroid = createToolbarAndroidComponent(IconNamePropType, getImageSource);
Icon.ToolbarAndroid = createToolbarAndroidComponent(
IconNamePropType,
getImageSource
);
Icon.getImageSource = getImageSource;

return Icon;
Expand Down
14 changes: 9 additions & 5 deletions lib/generate-icon-set-from-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const _ = require('lodash');
const fs = require('fs');

function extractGlyphMapFromCss(files, selectorPattern) {
const styleRulePattern = '(\\.[A-Za-z0-9_.:, \\n\\t-]+)\\{[^}]*content: ?["\\\'](?:\\\\([A-Fa-f0-9]+)|([^"\\\']+))["\\\'][^}]*\\}';
const styleRulePattern =
'(\\.[A-Za-z0-9_.:, \\n\\t-]+)\\{[^}]*content: ?["\\\'](?:\\\\([A-Fa-f0-9]+)|([^"\\\']+))["\\\'][^}]*\\}';
const allStyleRules = new RegExp(styleRulePattern, 'g');
const singleStyleRules = new RegExp(styleRulePattern);
const allSelectors = new RegExp(selectorPattern, 'g');
const singleSelector = new RegExp(selectorPattern);

const extractGlyphFromRule = (rule) => {
const extractGlyphFromRule = rule => {
const ruleParts = rule.match(singleStyleRules);
if (ruleParts[2]) {
// Hex value in CSS
Expand All @@ -21,7 +22,7 @@ function extractGlyphMapFromCss(files, selectorPattern) {
return ruleParts[3].charCodeAt();
};

const extractSelectorsFromRule = (rule) => {
const extractSelectorsFromRule = rule => {
const ruleParts = rule.match(singleStyleRules);
const selectors = ruleParts[1].match(allSelectors) || [];
return selectors.map(selector => selector.match(singleSelector)[1]);
Expand All @@ -31,7 +32,7 @@ function extractGlyphMapFromCss(files, selectorPattern) {
.map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' }))
.map(contents => contents.match(allStyleRules) || [])
.reduce((acc, rules) => acc.concat(rules), [])
.map((rule) => {
.map(rule => {
const glyph = extractGlyphFromRule(rule);
const selectors = extractSelectorsFromRule(rule);
return selectors.map(selector => [selector, glyph]);
Expand All @@ -44,7 +45,10 @@ function escapeRegExp(str) {
}

function generateIconSetFromCss(cssFiles, selectorPrefix, template, data = {}) {
const glyphMap = extractGlyphMapFromCss(cssFiles, `${escapeRegExp(selectorPrefix)}([A-Za-z0-9_-]+):before`);
const glyphMap = extractGlyphMapFromCss(
cssFiles,
`${escapeRegExp(selectorPrefix)}([A-Za-z0-9_-]+):before`
);
const content = JSON.stringify(glyphMap, null, ' ');
if (template) {
return _.template(template)(Object.assign({ glyphMap: content }, data));
Expand Down
Loading

0 comments on commit 585e56d

Please sign in to comment.