Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created November 5, 2022 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/4c672b9b8be2bf9ae38407e7ba7982a6 to your computer and use it in GitHub Desktop.
Save dongyuwei/4c672b9b8be2bf9ae38407e7ba7982a6 to your computer and use it in GitHub Desktop.
/**
1. 有@3x的优先取@3x, 如 add@3x.png
2. 没有@3x的优先取@2x,如 add@2x.png
3. 没有@3x和@2x的,取图片名称本身,如 add.png
*/
const path = require("path");
const shell = require("shelljs");
const files = {};
function portImages(dir) {
const dirPath = path.join(__dirname, "../../app", dir);
shell.cd(dirPath);
console.log("dirPath", dirPath);
shell
.exec("fd png") // brew install fd , jpg, svg
.toString()
.split("\n")
.filter((item) => !!item)
.forEach((file) => {
const filePath = path.join(dirPath, file);
files[filePath] = true;
});
}
function processAllImages() {
["icons", "images"].forEach((dir) => {
portImages(dir);
});
for (const file in files) {
if (!file.includes("@2x") && !file.includes("@3x")) {
const newFile = file
.replace("/icons/", "/icons2/")
.replace("/images/", "/images2/");
shell.mkdir("-p", path.dirname(newFile));
shell.cp(file, newFile);
} else if (file.includes("@2x")) {
const newFile = file
.replace("@2x", "")
.replace("/icons/", "/icons2/")
.replace("/images/", "/images2/");
shell.mkdir("-p", path.dirname(newFile));
shell.cp(file, newFile);
} else if (file.includes("@3x")) {
const newFile = file
.replace("@3x", "")
.replace("/icons/", "/icons2/")
.replace("/images/", "/images2/");
// console.log("3x", file, newFile);
shell.mkdir("-p", path.dirname(newFile));
shell.cp(file, newFile);
}
}
}
processAllImages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment