| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #! /usr/local/bin/node
- const path = require("path");
- const fs = require("fs");
- if (process.argv.length < 3) throw new Error("missing args");
- const action = process.argv[2];
- if (action === "copy") {
- const dir = process.argv[3];
- fs.readdirSync(dir).forEach((file) => {
- if (file.startsWith(".")) return;
- if (/\(\d\)\./.test(file)) return;
- const ext = path.extname(file);
- const filename = file.substr(0, file.length - ext.length);
- const match = /(?<basename>.+)@(?<scale>\d)x$/.exec(filename);
- if (match) {
- fs.copyFileSync(
- path.resolve(dir, file),
- path.resolve(
- __dirname,
- "img",
- match.groups.scale + "x",
- match.groups.basename + ext
- )
- );
- console.log(match.groups.basename, match.groups.scale);
- } else {
- fs.copyFileSync(
- path.resolve(dir, file),
- path.resolve(__dirname, "img", file)
- );
- }
- });
- }
- if (action === "rename") {
- const src = process.argv[3];
- const dest = process.argv[4];
- if (!(src && dest)) {
- throw new Error("missing args");
- }
- ["", "2x", "3x"].forEach((scale) => {
- [".png", ".jpg"].forEach((ext) => {
- const oldPath = path.resolve(__dirname, "img", scale, src + ext);
- const newPath = path.resolve(__dirname, "img", scale, dest + ext);
- if (fs.existsSync(oldPath)) {
- fs.renameSync(oldPath, newPath);
- console.log("rename " + oldPath + " -> " + newPath);
- }
- });
- });
- }
- if (action === "rm") {
- const src = process.argv[3];
- if (!src) {
- throw new Error("missing args");
- }
- ["", "2x", "3x"].forEach((scale) => {
- [".png", ".jpg"].forEach((ext) => {
- const path = path.resolve(__dirname, "img", scale, src + ext);
- if (fs.existsSync(path)) {
- console.log("delete " + path);
- }
- });
- });
- }
- if (action === "optimize") {
- function readdir(dir) {
- fs.readdirSync(dir).forEach((file) => {
- const childDir = path.resolve(dir, file);
- console.log(childDir);
- });
- }
- readdir(path.resolve(__dirname, "lib"));
- }
|