icon.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #! /usr/local/bin/node
  2. const path = require("path");
  3. const fs = require("fs");
  4. if (process.argv.length < 3) throw new Error("missing args");
  5. const action = process.argv[2];
  6. if (action === "copy") {
  7. const dir = process.argv[3];
  8. fs.readdirSync(dir).forEach((file) => {
  9. if (file.startsWith(".")) return;
  10. if (/\(\d\)\./.test(file)) return;
  11. const ext = path.extname(file);
  12. const filename = file.substr(0, file.length - ext.length);
  13. const match = /(?<basename>.+)@(?<scale>\d)x$/.exec(filename);
  14. if (match) {
  15. fs.copyFileSync(
  16. path.resolve(dir, file),
  17. path.resolve(
  18. __dirname,
  19. "img",
  20. match.groups.scale + "x",
  21. match.groups.basename + ext
  22. )
  23. );
  24. console.log(match.groups.basename, match.groups.scale);
  25. } else {
  26. fs.copyFileSync(
  27. path.resolve(dir, file),
  28. path.resolve(__dirname, "img", file)
  29. );
  30. }
  31. });
  32. }
  33. if (action === "rename") {
  34. const src = process.argv[3];
  35. const dest = process.argv[4];
  36. if (!(src && dest)) {
  37. throw new Error("missing args");
  38. }
  39. ["", "2x", "3x"].forEach((scale) => {
  40. [".png", ".jpg"].forEach((ext) => {
  41. const oldPath = path.resolve(__dirname, "img", scale, src + ext);
  42. const newPath = path.resolve(__dirname, "img", scale, dest + ext);
  43. if (fs.existsSync(oldPath)) {
  44. fs.renameSync(oldPath, newPath);
  45. console.log("rename " + oldPath + " -> " + newPath);
  46. }
  47. });
  48. });
  49. }
  50. if (action === "rm") {
  51. const src = process.argv[3];
  52. if (!src) {
  53. throw new Error("missing args");
  54. }
  55. ["", "2x", "3x"].forEach((scale) => {
  56. [".png", ".jpg"].forEach((ext) => {
  57. const path = path.resolve(__dirname, "img", scale, src + ext);
  58. if (fs.existsSync(path)) {
  59. console.log("delete " + path);
  60. }
  61. });
  62. });
  63. }
  64. if (action === "optimize") {
  65. function readdir(dir) {
  66. fs.readdirSync(dir).forEach((file) => {
  67. const childDir = path.resolve(dir, file);
  68. console.log(childDir);
  69. });
  70. }
  71. readdir(path.resolve(__dirname, "lib"));
  72. }