UNPKG

4.14 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3import fs from "fs";
4import path from "path";
5import imagemin from "imagemin";
6import imageminWebp from "imagemin-webp";
7
8let remove_old_format_files = true;
9let change_imgs_name_in_files = true;
10
11for (let i = 2;i < process.argv.length;i++) {
12 let splited_with_equal = process.argv[i].split("=");
13 if (splited_with_equal.length < 2)
14 continue ;
15 if (splited_with_equal[0] === "remove" && splited_with_equal[1] === "false")
16 remove_old_format_files = false;
17
18 if (splited_with_equal[0] === "change" && splited_with_equal[1] === "false")
19 change_imgs_name_in_files = false;
20}
21
22const RESET_BOLD = "\u001b[22m"
23const BOLD = "\u001b[1m"
24const FG_GREEN = "\x1b[32m";
25const RESET_COLOR = "\x1b[0m";
26const FG_RED = "\x1b[34m";
27
28const BOLD_FG_GREEN = `${BOLD}${FG_GREEN}`;
29const RESET_BOLD_FG_GREEN = `${RESET_BOLD}${RESET_COLOR}`;
30const BOLD_FG_RED = `${BOLD}${FG_RED}`;
31const RESET_BOLD_FG_RED = `${RESET_BOLD}${RESET_COLOR}`;
32
33const folders_to_excld = [
34 "node_modules",
35 "dist",
36 "build",
37 "lib",
38 "libs",
39 ".git",
40 ".idea",
41 ".vscode",
42 ".vscode-test",
43 "__tests__",
44 "__mocks__",
45 "__snapshots__",
46 "__test__",
47 "__testdata__",
48 "__testfixtures__",
49];
50
51const extention_to_exclude = [
52 "png",
53 "jpg",
54 "jpeg",
55 "gif",
56 "svg",
57 "webp",
58];
59
60var allDirectories = getDirectoriesRecursive(".");
61
62for (let i = 0;i < allDirectories.length; i++) {
63 imagemin([allDirectories[i] + "/*.{jpg,png,jpeg}"], {
64 destination: allDirectories[i],
65 plugins: [
66 imageminWebp({}),
67 ],
68 })
69 .then((webp_files) => {
70 webp_files.forEach((webp_file) => {
71 const spaces = " ".repeat(50 - webp_file.sourcePath.length);
72 console.log(`${BOLD_FG_GREEN}[Converting to webp ]${RESET_BOLD_FG_GREEN} ${webp_file.sourcePath} ${spaces} ${BOLD_FG_GREEN}Success ✅${RESET_BOLD_FG_GREEN}`);
73
74 // remove the old image file
75 if (remove_old_format_files === true) {
76 fs.unlinkSync(webp_file.sourcePath);
77 console.log(`${BOLD_FG_RED}[deleting old format]${RESET_BOLD_FG_RED} ${webp_file.sourcePath} ${spaces} ${BOLD_FG_RED}Success 🚀${RESET_BOLD_FG_RED}`);
78 }
79
80 // get all files in the directory
81 if (change_imgs_name_in_files === true) {
82 for (let i = 0; i < allDirectories.length; i++) {
83 // get all files of directory allDirectories[i]
84 fs.readdirSync(allDirectories[i]).forEach((file) => {
85
86 // first we check if the file is not a directory
87 // exclude files with extension
88 if (fs.statSync(`${allDirectories[i]}/${file}`).isDirectory() === true)
89 return ;
90
91 if (isAllowdedFileExtention(file) === false) {
92 return ;
93 }
94
95 // change the image name in file to webp
96 const content_file = fs.readFileSync(`${allDirectories[i]}/${file}`, "utf8", (err, data) => { if (err) console.log(`Error =======> [${err}]`); });
97 const changed_content = content_file.replace(new RegExp("\\b" + getFileName(webp_file.sourcePath) + "\\b", 'g'), getFileName(webp_file.destinationPath));
98 fs.writeFileSync(`${allDirectories[i]}/${file}`, changed_content, "utf8", (err) => { if (err) console.log(`Error =======> [${err}]`); } );
99 });
100 }
101 }
102 });
103 })
104}
105
106function getFileName(filepath) {
107 return path.basename(filepath);
108}
109
110function flatten(lists) {
111 return lists.reduce((a, b) => a.concat(b), []);
112}
113
114function getDirectories(srcpath) {
115 // exclude folders folder
116 for (let i = 0; i < folders_to_excld.length; i++) {
117 if (srcpath.includes(folders_to_excld[i])) {
118 return [];
119 }
120 }
121
122 return fs
123 .readdirSync(srcpath)
124 .map((file) => path.join(srcpath, file))
125 .filter((path) => fs.statSync(path).isDirectory());
126}
127
128function getDirectoriesRecursive(srcpath) {
129 // exclude folders folder
130 for (let i = 0; i < folders_to_excld.length; i++) {
131 if (srcpath.includes(folders_to_excld[i])) {
132 return [];
133 }
134 }
135 return [
136 srcpath,
137 ...flatten(getDirectories(srcpath).map(getDirectoriesRecursive)),
138 ];
139}
140
141function isAllowdedFileExtention(file) {
142 const file_extension = file.split(".").pop();
143 for (let i = 0; i < extention_to_exclude.length; i++) {
144 if (file_extension && file_extension === extention_to_exclude[i]) {
145 return false;
146 }
147 }
148 return true;
149}
\No newline at end of file