Convert color code to use es6 module

This commit is contained in:
RMidhunSuresh 2022-07-11 12:17:33 +05:30
parent 9c20b6ecc1
commit 57726ddff8
2 changed files with 10 additions and 10 deletions

View file

@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const fs = require("fs"); import {readFileSync, mkdirSync, writeFileSync} from "fs";
const path = require("path"); import {resolve} from "path";
const xxhash = require('xxhashjs'); import {h32} from "xxhashjs";
function createHash(content) { function createHash(content) {
const hasher = new xxhash.h32(0); const hasher = new h32(0);
hasher.update(content); hasher.update(content);
return hasher.digest(); return hasher.digest();
} }
@ -30,8 +30,8 @@ function createHash(content) {
* @param {string} primaryColor Primary color for the new svg * @param {string} primaryColor Primary color for the new svg
* @param {string} secondaryColor Secondary color for the new svg * @param {string} secondaryColor Secondary color for the new svg
*/ */
module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondaryColor) { export function buildColorizedSVG(svgLocation, primaryColor, secondaryColor) {
const svgCode = fs.readFileSync(svgLocation, { encoding: "utf8"}); const svgCode = readFileSync(svgLocation, { encoding: "utf8"});
let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor);
coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor);
if (svgCode === coloredSVGCode) { if (svgCode === coloredSVGCode) {
@ -39,9 +39,9 @@ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondar
} }
const fileName = svgLocation.match(/.+[/\\](.+\.svg)/)[1]; const fileName = svgLocation.match(/.+[/\\](.+\.svg)/)[1];
const outputName = `${fileName.substring(0, fileName.length - 4)}-${createHash(coloredSVGCode)}.svg`; const outputName = `${fileName.substring(0, fileName.length - 4)}-${createHash(coloredSVGCode)}.svg`;
const outputPath = path.resolve(__dirname, "../../.tmp"); const outputPath = resolve(__dirname, "../../.tmp");
try { try {
fs.mkdirSync(outputPath); mkdirSync(outputPath);
} }
catch (e) { catch (e) {
if (e.code !== "EEXIST") { if (e.code !== "EEXIST") {
@ -49,6 +49,6 @@ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondar
} }
} }
const outputFile = `${outputPath}/${outputName}`; const outputFile = `${outputPath}/${outputName}`;
fs.writeFileSync(outputFile, coloredSVGCode); writeFileSync(outputFile, coloredSVGCode);
return outputFile; return outputFile;
} }

View file

@ -8,7 +8,7 @@ const path = require("path");
const manifest = require("./package.json"); const manifest = require("./package.json");
const version = manifest.version; const version = manifest.version;
const compiledVariables = new Map(); const compiledVariables = new Map();
const replacer = require("./scripts/postcss/svg-colorizer").buildColorizedSVG; import {buildColorizedSVG as replacer} from "./scripts/postcss/svg-colorizer.mjs";
import {derive} from "./scripts/postcss/color.mjs"; import {derive} from "./scripts/postcss/color.mjs";
const commonOptions = { const commonOptions = {