Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Forked from chrisbull/svg-to-react-native-cli and added options to override width, height, fill and stroke of the compiled SVG.

# svg-to-react-native-cli
Based on svg-to-react-cli.
A command line utility that takes a svg image file and outputs a fully formatted stateless functional React Native component with `height` and `width` for props. With flags to toggle formatting and remove style attributes.
Expand Down
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ const generateComponent = require('./src/generateComponent');
const printErrors = require('./src/output').printErrors;
const removeStyle = require('./src/removeStyle');
const replaceAllStrings = require('./src/replaceAllStrings');
const replaceFill = require('./src/replaceFill');
const replaceStroke = require('./src/replaceStroke');
const replaceWidthHeight = require('./src/replaceWidthHeight');

// Argument setup
const args = yargs
.option('dir', { alias: 'd', default: false })
.option('format', { default: true })
.option('output', { alias: 'o' })
.option('fillProp', { type: 'boolean', default: true })
.option('strokeProp', { type: 'boolean', default: true })
.option('widthHeightProp', { type: 'boolean', default: true })
.option('rm-style', { default: false })
.option('force', { alias: 'f', default: false }).argv;

Expand All @@ -35,6 +41,9 @@ const firstArg = args._[0];
const newFileName = args._[1] || 'MyComponent';
const outputPath = args.output;
const directoryPath = args.dir;
const fillProp = args.fillProp;
const strokeProp = args.strokeProp;
const widthHeightProp = args.widthHeightProp;
const rmStyle = args.rmStyle;
const format = args.format;

Expand Down Expand Up @@ -132,6 +141,21 @@ const runUtil = (fileToRead, fileToWrite) => {
SVGtoJSX(output).then(jsx => {
// Convert any html tags to react-native-svg tags
jsx = replaceAllStrings(jsx);

// replace Fill
if (fillProp) {
jsx = replaceFill(jsx);
}

// replace Stroke
if (strokeProp) {
jsx = replaceStroke(jsx);
}

// replace widthHeightProp
if (widthHeightProp) {
jsx = replaceWidthHeight(jsx);
}

// Wrap it up in a React component
jsx = generateComponent(jsx, fileToWrite);
Expand Down
5 changes: 5 additions & 0 deletions src/replaceFill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function replaceFill(str) {
return str.replace(/fill=(".*?")/gm, function (match, color) {
return 'fill={props.fill || ' + color + '}';
});
};
5 changes: 5 additions & 0 deletions src/replaceStroke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function replaceStroke(str) {
return str.replace(/stroke=(".*?")/gm, function (match, color) {
return 'stroke={props.stroke || ' + color + '}';
});
};
5 changes: 5 additions & 0 deletions src/replaceWidthHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function replaceFill(str) {
return str.replace(/(width|height)=(".*?")/gm, function (match, key, value) {
return `${key}={props.${key} || ${value}}`;
});
};
Loading