Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universal Support #63

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
build
# temporarily as it's not using npm
# dist
# build
83 changes: 83 additions & 0 deletions bin/universalTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const fs = require('fs');
const path = require('path');

const iconsPath = path.join(__dirname, '../src/icons');

function transform(input) {
const replacements = {
svgs: 'Svg',
circle: 'Circle',
clipPath: 'ClipPath',
defs: 'Defs',
ellipse: 'Ellipse',
g: 'G',
image: 'Image',
line: 'Line',
linearGradient: 'LinearGradient',
mask: 'Mask',
path: 'Path',
pattern: 'Pattern',
polygon: 'Polygon',
polyline: 'Polyline',
radialGradient: 'RadialGradient',
rect: 'Rect',
stop: 'Stop',
svg: 'Svg',
symbol: 'Symbol',
tSpan: 'TSpan',
text: 'Text',
textPath: 'TextPath',
use: 'Use',
};

let used = [];

const transformed = Object.entries(replacements).reduce(
(result, [key, value]) => {
const regex = new RegExp(`(<(\/)?)(${key})(>)?`, 'g');

if (regex.test(result)) {
if (key !== 'svg') {
used.push(value);
}

return result.replace(regex, `$1${value}$4`);
}

return result;
},
input
);

const subModules = used.map((u) => ` ${u},\n`).join('');

const imports = `import Svg, {\n${subModules}} from 'svgs';`;

const lines = transformed.split(/\n/g);

lines.splice(2, 0, imports);

return lines.join('\n');
}

function catchError(err) {
if (err) {
console.error(err);
}
}

fs.readdir(iconsPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}

files.forEach(function (file) {
const filePath = path.resolve(iconsPath, file);

const text = fs.readFileSync(filePath, 'utf8', catchError);

const transformedText = transform(text);

fs.writeFile(filePath, transformedText, 'utf8', catchError);
});
});
Loading