-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-svg.js
90 lines (78 loc) · 3.47 KB
/
format-svg.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/node
const fs = require("fs");
const path = require("node:path");
var getDirName = require("path").dirname;
const readdirSync = fs.readdirSync;
const propertyToFormatFrom = /fill="\#(.*?)"/g;
const propertyToFormatTo = 'fill="currentColor"';
const logo = `
_______________________________________
│ │
│----------Formatting SVG's!-----------│
│ │
│ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣠⣤⣤⣤⣄⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀ │
│ ⠀⣠⡶⠒⠒⠶⣄⣠⡴⠚⠉⠁⠀⠀⠀⠀⠀⠉⠙⠳⢦⡀⠀⠀⠀⠀⠀⠀ │
│ ⢠⡏⠀⠀⠀⠀⠘⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢧⡀⠀⠀⠀⠀ │
│ ⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠋⢱⠀⠀⢠⠉⢡⠀⠀⠀⠀⠀⠻⡄⠀⠀⠀ │
│ ⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⣾⠄⠀⢸⣦⣾⠀⠀⠀⠀⠀⠀⢻⡄⠀⠀ │
│ ⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠈⣿⣿⠀⠀⠸⣿⡿⠀⠀⠀⠀⠀⠀⠈⠳⣄⠀ │
│ ⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠈⠁⡴⠶⡆⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠹⡄ │
│ ⠀⠀⠀⢷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠒⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣷ │
│ ⠀⠀⠀⠸⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠇ │
│ ⠀⠀⠀⣀⡿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡽⣿⡛⠁⠀ │
│ ⠀⣠⢾⣭⠀⠈⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠊⠀⢠⣝⣷⡀ │
│ ⢠⡏⠘⠋⠀⠀⠀⠈⠑⠦⣄⣀⠀⠀⠀⠀⠀⣀⡠⠔⠋⠀⠀⠀⠈⠛⠃⢻ │
│ ⠈⠷⣤⣀⣀⣀⣀⣀⣀⣀⣀⣤⡽⠟⠛⠿⣭⣄⣀⣀⣀⣀⣀⣀⣀⣀⣤⠞ │
│ ⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠉⠉⠉⠉⠀⠀⠀ │
│ │
│______________________________________│
`
console.log(logo);
/**
* Fetches all the sub directories in the current directories
*/
const getDirectories = (source) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
/**
*
* @param {*} folder The name of the folder to format files in.
* @param {*} file The name of the SVG to format
*/
const formatSvg = (folder, file) => {
const filePath = path.resolve(__dirname, "files", folder, file);
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
parsedSVG = data.replace(propertyToFormatFrom, propertyToFormatTo);
const outputFilePath = __dirname + "/out" + "/" + folder + "/" + file;
const cb = (err) => {};
fs.mkdir(getDirName(outputFilePath), { recursive: true }, function (err) {
if (err) return cb(err);
fs.writeFile(outputFilePath, parsedSVG, cb);
});
});
};
let directories;
try {
directories = getDirectories(__dirname + "/files");
} catch (error) {
console.error(`Folder with the name 'files' is missing.`);
return;
}
directories.forEach((dir) => {
const folderpath = path.resolve(__dirname + "/files", dir);
fs.readdir(folderpath, function (err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
const svgFiles = files.filter((file) => path.extname(file) == ".svg");
console.info(`\nFormatting ${svgFiles.length} files in folder: `, dir);
svgFiles.forEach(function (file) {
formatSvg(dir, file);
});
});
});