forked from Templarian/MaterialDesign-Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
110 lines (93 loc) · 3 KB
/
util.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fs = require("fs");
const path = require("path");
/**
* Retrieve the absolute path to the `@mdi/svg` package.
*
* If using from the root of a project, it will return the `node_modules/@mdi/svg` path.
* Otherwise, it will fallback to 0.3.2 behavior (without validating that it exists).
*
* This help for pnpm in isolated mode where the 0.3.2 behavior try to find the `@mdi/svg` package
* somewhere in the `.pnpm` store.
*
* @param {string} overridePackageName
* @returns the absolute path to the `@mdi/svg` package.
*/
const getSVGPackagePath = (overridePackageName = undefined) => {
const pkgName = overridePackageName || "svg";
const mdi_svg_path = path.resolve(
process.cwd(),
"node_modules",
"@mdi",
pkgName
);
if (fs.existsSync(mdi_svg_path)) {
return path.resolve(mdi_svg_path);
} else {
// Did not find the folder of the package.
// Fallback to the 0.3.2 behavior.
return path.resolve(parent_folder, pkgName);
}
};
/**
* Read a file and return its content.
*
* @param {string} filename
* @param {string} overridePackageName
* @returns content of the file
*/
const readFile = (filename, overridePackageName = undefined) => {
let filepath = path.resolve(svg_package, filename);
if (overridePackageName !== undefined) {
filepath = path.resolve(getSVGPackagePath(overridePackageName), filename);
}
return fs.readFileSync(filepath, { encoding: "utf8" });
};
/**
* Read a JSON and return an object of the parsed JSON.
*
* @param {string} filename
* @param {string} overridePackageName
* @returns object of the parsed json
*/
const readJSONtoObject = (filename, overridePackageName = undefined) => {
const file = readFile(filename, overridePackageName);
return JSON.parse(file);
};
const parent_folder = path.resolve(`${__dirname}/../`);
const svg_package = getSVGPackagePath();
const getVersion = (overridePackageName = undefined) => {
const file = readJSONtoObject("package.json", overridePackageName);
return JSON.parse(file).version;
};
const getMeta = (withPaths, overridePackageName = undefined) => {
const meta = readJSONtoObject("meta.json", overridePackageName);
if (withPaths) {
const total = meta.length;
meta.forEach((icon, i) => {
let svgFilename = path.join("svg", `${icon.name}.svg`);
const svg = readFile(svgFilename, overridePackageName);
icon.path = svg.match(/ d="([^"]+)"/)[1];
});
}
return meta;
};
exports.getVersionLight = () => {
return getVersion("light-svg");
};
exports.getMetaLight = (withPaths) => {
return getMeta(withPaths, "light-svg");
};
exports.getVersion = getVersion;
exports.getMeta = getMeta;
exports.closePath = (path) => {
return path.replace(/(\d)M/g, "$1ZM");
};
exports.write = (file, data) => {
fs.writeFileSync(file, data);
};
exports.read = (file) => {
return fs.readFileSync(file, "utf8");
};
exports.exists = (file) => {
return fs.existsSync(file);
};