-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (52 loc) · 1.42 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
53
54
55
56
57
#! /usr/bin/env node
const fs = require("fs");
const argv = require("yargs")
.help()
.option("file", {
alias: "f",
description: "File you want to replace",
type: "string"
})
.option("multiplier", {
alias: "m",
description: "The rem value is * agains this number. (default is 10)",
type: "number",
default: 10
})
.alias("help", "h").argv;
if (!argv.file) {
console.log("You should specify the file");
return;
}
// read specified file
const contents = fs.readFileSync(argv.file, "utf8");
const splitOn = ":";
// split on new line
const lines = contents.split(splitOn);
// make regex which converts 'font-size: 1.15rem' to [1.15rem, 1.5, rem]
const remRegex = new RegExp("(\\d.?\\d?)(rem)");
let replacedCount = 0;
const finalLines = lines.map(line => {
if (line.includes("rem")) {
const matches = line.match(remRegex);
if (matches && matches.length === 3) {
replacedCount++;
const newLine = line.replace(
matches[0],
matches[1] * argv.multiplier + "px"
);
// console.log("replaced", line, "with", newLine);
return newLine;
}
}
return line;
});
const finalContent = finalLines.join(splitOn);
// console.log(finalContent);
try {
fs.writeFileSync(argv.file, finalContent);
} catch (error) {
console.log("[rem-to-px] Error while persisting file", error);
return;
}
console.log("[rem-to-px] Replaced", replacedCount, "rem values to px");