-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (45 loc) · 1.87 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
47
48
49
50
51
52
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const mkdirp = require('mkdirp');
const svgComponentTemplate = require('./ComponentTemplate');
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function convert() {
const svgFolderPath = process.argv[2];
const outputComponentPath = process.argv[3];
if (!svgFolderPath || !outputComponentPath) {
console.error('Usage: node index.js <SVG_FOLDER_PATH> <OUTPUT_COMPONENT_PATH>');
process.exit(1);
}
fs.readdir(svgFolderPath, (err, files) => {
if (err) {
console.error(`Error reading SVG folder: ${err}`);
process.exit(1);
}
files.forEach((file) => {
if (path.extname(file).toLowerCase() === '.svg') {
const svgFilePath = path.join(svgFolderPath, file);
fs.readFile(svgFilePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading SVG file: ${err}`);
return;
}
const $ = cheerio.load(data);
const svgContent = $('svg').html();
if (!svgContent) {
console.error(`SVG content not found in ${svgFilePath}. Skipping.`);
return;
}
const svgFileName = path.basename(file, path.extname(file));
const componentCode = svgComponentTemplate(svgContent, svgFileName);
mkdirp.sync(outputComponentPath);
fs.writeFileSync(`${outputComponentPath}/${capitalize(svgFileName)}Icon.vue`, componentCode);
console.log(`Generated Vue component for ${svgFileName}`);
});
}
});
});
}
module.exports = { convert }