-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·46 lines (37 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env node
const program = require('commander');
const fs = require('fs');
const path = require('path');
program
.version('0.1.1')
.option('-D, --delete', 'delete the original SVG file')
.arguments('<svg-file>')
.action((svgFile) => {
var original = fs.readFileSync(svgFile,'utf8');
var template = fs.readFileSync(__dirname + '/template', 'utf8');
var componentName = path
.basename(svgFile,'.svg')
.replace(/^[a-zA-Z]/g, (g) => (g[0].toUpperCase())) // Uppercase first letter
.replace(/-([a-z])/g, (g) => (g[1].toUpperCase())); // Convert dashes to camelCase
var svgContent = original
.replace(/"/gm, '\'') // Replace double quotes with single quotes
.replace(/-([a-z])/g, (g) => (g[1].toUpperCase())) // Convert dashes to camelCase
.replace(/^/gm,' '); // Add indentation
var output = template // Combine template with:
.replace(/SVG_CONTENT/gm, svgContent) // - SVG content
.replace(/COMPONENT_NAME/gm, componentName); // - Component name
fs.mkdir(componentName, (error) => {
if (error) throw error;
fs.writeFile(componentName + '/index.js', output, (error) => {
if (error) throw error;
console.log('React component ' + componentName + ' successfully created!');
if (program.delete) {
fs.unlink(svgFile, (error) => {
if (error) throw error;
console.log('And ' + svgFile + ' deleted.');
});
}
});
});
})
.parse(process.argv);