diginext-img-magic-cli 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +154 -154
- package/dist/plugins/getAllFiles.d.ts +2 -2
- package/dist/plugins/getAllFiles.js +23 -23
- package/esm/index.d.ts +1 -1
- package/esm/index.js +149 -149
- package/esm/plugins/getAllFiles.d.ts +2 -2
- package/esm/plugins/getAllFiles.js +18 -18
- package/package.json +2 -3
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const yargs_1 = __importDefault(require("yargs"));
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const diginext_img_magic_1 = require("diginext-img-magic");
|
|
9
|
-
const isImageSupported_1 = __importDefault(require("diginext-img-magic/dist/compress/isImageSupported"));
|
|
10
|
-
const url_1 = require("diginext-utils/dist/string/url");
|
|
11
|
-
const getAllFiles_1 = __importDefault(require("./plugins/getAllFiles"));
|
|
12
|
-
const { version } = require("../package.json");
|
|
13
|
-
// import { hideBin } from "yargs/helpers";
|
|
14
|
-
(async () => {
|
|
15
|
-
//
|
|
16
|
-
const argv = (0, yargs_1.default)(process.argv.slice(2))
|
|
17
|
-
.usage("Usage: $0 <command> [options]")
|
|
18
|
-
.example('$0 --dir "dir"', "")
|
|
19
|
-
// .demandOption(["p"])
|
|
20
|
-
.alias("d", "dir")
|
|
21
|
-
.describe("d", "Directory")
|
|
22
|
-
.option("dir", {
|
|
23
|
-
string: true,
|
|
24
|
-
})
|
|
25
|
-
.help("h")
|
|
26
|
-
.alias("h", "help")
|
|
27
|
-
.epilog(`verion: ${version}`).argv;
|
|
28
|
-
let dirName;
|
|
29
|
-
if (Array.isArray(argv.dir)) {
|
|
30
|
-
dirName = argv.dir[1] === true ? "" : argv.dir[1];
|
|
31
|
-
if (!dirName) {
|
|
32
|
-
console.log("NEED DIRECTORY!");
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
dirName = argv === true ? "" : argv.dir;
|
|
38
|
-
if (!dirName) {
|
|
39
|
-
console.log("NEED DIRECTORY!");
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (!fs_1.default.lstatSync(dirName).isDirectory()) {
|
|
44
|
-
//
|
|
45
|
-
console.log("Please use a directory!");
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
console.log("dirName :>> ", dirName);
|
|
49
|
-
dirName = dirName.replace(/\\/g, "/");
|
|
50
|
-
const folderName = (0, url_1.getFileNameWithoutExtension)(dirName);
|
|
51
|
-
console.log("folderName :>> ", folderName);
|
|
52
|
-
// const arr = dirName.split("/");
|
|
53
|
-
// console.log("arr :>> ", dirName.split("/").pop());
|
|
54
|
-
const folderNameWebp = `${folderName}-webp`;
|
|
55
|
-
const dirFolderWebp = [...dirName.split("/").slice(0, -1), folderNameWebp].join("/");
|
|
56
|
-
const folderNameThumb = `${folderName}-thumb-webp`;
|
|
57
|
-
const dirFolderThumb = [...dirName.split("/").slice(0, -1), folderNameThumb].join("/");
|
|
58
|
-
console.log("dirFolderWebp :>> ", dirFolderWebp);
|
|
59
|
-
console.log("dirFolderThumb :>> ", dirFolderThumb);
|
|
60
|
-
const list = (0, getAllFiles_1.default)(dirName);
|
|
61
|
-
const checkSameName = () => {
|
|
62
|
-
const __list = list
|
|
63
|
-
.map((item) => {
|
|
64
|
-
const supported = (0, isImageSupported_1.default)(item);
|
|
65
|
-
if (supported) {
|
|
66
|
-
return [...item.split("/").slice(0, -1), `${(0, url_1.getFileNameWithoutExtension)(item)}`].join("/");
|
|
67
|
-
// return getFileNameWithoutExtension(item);
|
|
68
|
-
}
|
|
69
|
-
return null;
|
|
70
|
-
})
|
|
71
|
-
.filter((x) => x);
|
|
72
|
-
const duplicate = __list.filter((item, index) => __list.indexOf(item) !== index);
|
|
73
|
-
if (duplicate.length >= 1) {
|
|
74
|
-
console.log("!!!");
|
|
75
|
-
console.error("Tìm thấy hình giống tên nhau nhưng khác extension, không thực hiện convert !");
|
|
76
|
-
}
|
|
77
|
-
for (let i = 0; i < duplicate.length; i++) {
|
|
78
|
-
const element = duplicate[i];
|
|
79
|
-
//
|
|
80
|
-
const ___list = list.filter((x) => {
|
|
81
|
-
return x.indexOf(`${element}.`) >= 0;
|
|
82
|
-
});
|
|
83
|
-
console.log("Vui lòng đổi tên :>> ", ___list);
|
|
84
|
-
}
|
|
85
|
-
return duplicate.length >= 1;
|
|
86
|
-
};
|
|
87
|
-
if (checkSameName()) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const MAX_PROCESSING = 10;
|
|
91
|
-
let index = 0;
|
|
92
|
-
const startConvert = () => {
|
|
93
|
-
if (fs_1.default.existsSync(dirFolderWebp)) {
|
|
94
|
-
fs_1.default.rmSync(dirFolderWebp, { recursive: true });
|
|
95
|
-
}
|
|
96
|
-
fs_1.default.mkdirSync(dirFolderWebp, { recursive: true });
|
|
97
|
-
if (fs_1.default.existsSync(dirFolderThumb)) {
|
|
98
|
-
fs_1.default.rmSync(dirFolderThumb, { recursive: true });
|
|
99
|
-
}
|
|
100
|
-
fs_1.default.mkdirSync(dirFolderThumb, { recursive: true });
|
|
101
|
-
//
|
|
102
|
-
index = -1;
|
|
103
|
-
for (let i = 0; i < MAX_PROCESSING; i++) {
|
|
104
|
-
walkOne();
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const walkOne = async () => {
|
|
108
|
-
//
|
|
109
|
-
index++;
|
|
110
|
-
if (index == list.length) {
|
|
111
|
-
index++;
|
|
112
|
-
setTimeout(() => {
|
|
113
|
-
try {
|
|
114
|
-
fs_1.default.rmSync(".temp", { recursive: true });
|
|
115
|
-
console.log("DONE !");
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
console.error(`metname error`, error);
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (index >= list.length) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
await convertOneObject(list[index]);
|
|
127
|
-
walkOne();
|
|
128
|
-
};
|
|
129
|
-
const convertOneObject = async (url) => {
|
|
130
|
-
if (!url) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
console.log(`${index + 1}/${list.length}: ${url}`);
|
|
134
|
-
url = url.replace("\\", "/");
|
|
135
|
-
const fileExt = (0, url_1.getFileExtension)(url);
|
|
136
|
-
const supported = (0, isImageSupported_1.default)(url);
|
|
137
|
-
if (!supported) {
|
|
138
|
-
fs_1.default.cpSync(url, url.replace(dirName, dirFolderWebp), { recursive: true });
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const { large, small } = await (0, diginext_img_magic_1.convertToWebpByPath)(url);
|
|
142
|
-
fs_1.default.cpSync(large, url.replace(dirName, dirFolderWebp).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
143
|
-
fs_1.default.cpSync(small, url.replace(dirName, dirFolderThumb).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
144
|
-
try {
|
|
145
|
-
fs_1.default.rmSync(large);
|
|
146
|
-
fs_1.default.rmSync(small);
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
console.error(`metname error`, error);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
startConvert();
|
|
153
|
-
//
|
|
154
|
-
})();
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const diginext_img_magic_1 = require("diginext-img-magic");
|
|
9
|
+
const isImageSupported_1 = __importDefault(require("diginext-img-magic/dist/compress/isImageSupported"));
|
|
10
|
+
const url_1 = require("diginext-utils/dist/string/url");
|
|
11
|
+
const getAllFiles_1 = __importDefault(require("./plugins/getAllFiles"));
|
|
12
|
+
const { version } = require("../package.json");
|
|
13
|
+
// import { hideBin } from "yargs/helpers";
|
|
14
|
+
(async () => {
|
|
15
|
+
//
|
|
16
|
+
const argv = (0, yargs_1.default)(process.argv.slice(2))
|
|
17
|
+
.usage("Usage: $0 <command> [options]")
|
|
18
|
+
.example('$0 --dir "dir"', "")
|
|
19
|
+
// .demandOption(["p"])
|
|
20
|
+
.alias("d", "dir")
|
|
21
|
+
.describe("d", "Directory")
|
|
22
|
+
.option("dir", {
|
|
23
|
+
string: true,
|
|
24
|
+
})
|
|
25
|
+
.help("h")
|
|
26
|
+
.alias("h", "help")
|
|
27
|
+
.epilog(`verion: ${version}`).argv;
|
|
28
|
+
let dirName;
|
|
29
|
+
if (Array.isArray(argv.dir)) {
|
|
30
|
+
dirName = argv.dir[1] === true ? "" : argv.dir[1];
|
|
31
|
+
if (!dirName) {
|
|
32
|
+
console.log("NEED DIRECTORY!");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
dirName = argv === true ? "" : argv.dir;
|
|
38
|
+
if (!dirName) {
|
|
39
|
+
console.log("NEED DIRECTORY!");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!fs_1.default.lstatSync(dirName).isDirectory()) {
|
|
44
|
+
//
|
|
45
|
+
console.log("Please use a directory!");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
console.log("dirName :>> ", dirName);
|
|
49
|
+
dirName = dirName.replace(/\\/g, "/");
|
|
50
|
+
const folderName = (0, url_1.getFileNameWithoutExtension)(dirName);
|
|
51
|
+
console.log("folderName :>> ", folderName);
|
|
52
|
+
// const arr = dirName.split("/");
|
|
53
|
+
// console.log("arr :>> ", dirName.split("/").pop());
|
|
54
|
+
const folderNameWebp = `${folderName}-webp`;
|
|
55
|
+
const dirFolderWebp = [...dirName.split("/").slice(0, -1), folderNameWebp].join("/");
|
|
56
|
+
const folderNameThumb = `${folderName}-thumb-webp`;
|
|
57
|
+
const dirFolderThumb = [...dirName.split("/").slice(0, -1), folderNameThumb].join("/");
|
|
58
|
+
console.log("dirFolderWebp :>> ", dirFolderWebp);
|
|
59
|
+
console.log("dirFolderThumb :>> ", dirFolderThumb);
|
|
60
|
+
const list = (0, getAllFiles_1.default)(dirName);
|
|
61
|
+
const checkSameName = () => {
|
|
62
|
+
const __list = list
|
|
63
|
+
.map((item) => {
|
|
64
|
+
const supported = (0, isImageSupported_1.default)(item);
|
|
65
|
+
if (supported) {
|
|
66
|
+
return [...item.split("/").slice(0, -1), `${(0, url_1.getFileNameWithoutExtension)(item)}`].join("/");
|
|
67
|
+
// return getFileNameWithoutExtension(item);
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
})
|
|
71
|
+
.filter((x) => x);
|
|
72
|
+
const duplicate = __list.filter((item, index) => __list.indexOf(item) !== index);
|
|
73
|
+
if (duplicate.length >= 1) {
|
|
74
|
+
console.log("!!!");
|
|
75
|
+
console.error("Tìm thấy hình giống tên nhau nhưng khác extension, không thực hiện convert !");
|
|
76
|
+
}
|
|
77
|
+
for (let i = 0; i < duplicate.length; i++) {
|
|
78
|
+
const element = duplicate[i];
|
|
79
|
+
//
|
|
80
|
+
const ___list = list.filter((x) => {
|
|
81
|
+
return x.indexOf(`${element}.`) >= 0;
|
|
82
|
+
});
|
|
83
|
+
console.log("Vui lòng đổi tên :>> ", ___list);
|
|
84
|
+
}
|
|
85
|
+
return duplicate.length >= 1;
|
|
86
|
+
};
|
|
87
|
+
if (checkSameName()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const MAX_PROCESSING = 10;
|
|
91
|
+
let index = 0;
|
|
92
|
+
const startConvert = () => {
|
|
93
|
+
if (fs_1.default.existsSync(dirFolderWebp)) {
|
|
94
|
+
fs_1.default.rmSync(dirFolderWebp, { recursive: true });
|
|
95
|
+
}
|
|
96
|
+
fs_1.default.mkdirSync(dirFolderWebp, { recursive: true });
|
|
97
|
+
if (fs_1.default.existsSync(dirFolderThumb)) {
|
|
98
|
+
fs_1.default.rmSync(dirFolderThumb, { recursive: true });
|
|
99
|
+
}
|
|
100
|
+
fs_1.default.mkdirSync(dirFolderThumb, { recursive: true });
|
|
101
|
+
//
|
|
102
|
+
index = -1;
|
|
103
|
+
for (let i = 0; i < MAX_PROCESSING; i++) {
|
|
104
|
+
walkOne();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const walkOne = async () => {
|
|
108
|
+
//
|
|
109
|
+
index++;
|
|
110
|
+
if (index == list.length) {
|
|
111
|
+
index++;
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
try {
|
|
114
|
+
fs_1.default.rmSync(".temp", { recursive: true });
|
|
115
|
+
console.log("DONE !");
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.error(`metname error`, error);
|
|
119
|
+
}
|
|
120
|
+
}, 5000);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (index >= list.length) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
await convertOneObject(list[index]);
|
|
127
|
+
walkOne();
|
|
128
|
+
};
|
|
129
|
+
const convertOneObject = async (url) => {
|
|
130
|
+
if (!url) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
console.log(`${index + 1}/${list.length}: ${url}`);
|
|
134
|
+
url = url.replace("\\", "/");
|
|
135
|
+
const fileExt = (0, url_1.getFileExtension)(url);
|
|
136
|
+
const supported = (0, isImageSupported_1.default)(url);
|
|
137
|
+
if (!supported) {
|
|
138
|
+
fs_1.default.cpSync(url, url.replace(dirName, dirFolderWebp), { recursive: true });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const { large, small } = await (0, diginext_img_magic_1.convertToWebpByPath)(url);
|
|
142
|
+
fs_1.default.cpSync(large, url.replace(dirName, dirFolderWebp).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
143
|
+
fs_1.default.cpSync(small, url.replace(dirName, dirFolderThumb).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
144
|
+
try {
|
|
145
|
+
fs_1.default.rmSync(large);
|
|
146
|
+
fs_1.default.rmSync(small);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
console.error(`metname error`, error);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
startConvert();
|
|
153
|
+
//
|
|
154
|
+
})();
|
|
155
155
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const getAllFiles: (dirPath: string, arrayOfFiles?: Array<string>) => string[];
|
|
2
|
-
export default getAllFiles;
|
|
1
|
+
declare const getAllFiles: (dirPath: string, arrayOfFiles?: Array<string>) => string[];
|
|
2
|
+
export default getAllFiles;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const fs_1 = __importDefault(require("fs"));
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const getAllFiles = function (dirPath, arrayOfFiles) {
|
|
9
|
-
const files = fs_1.default.readdirSync(dirPath);
|
|
10
|
-
arrayOfFiles = arrayOfFiles || [];
|
|
11
|
-
files.forEach(function (file) {
|
|
12
|
-
if (fs_1.default.statSync(dirPath + "/" + file).isDirectory()) {
|
|
13
|
-
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
const __path = path_1.default.join(dirPath, "/", file).replace(/\\/g, "/");
|
|
17
|
-
if (arrayOfFiles)
|
|
18
|
-
arrayOfFiles.push(__path);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
return arrayOfFiles;
|
|
22
|
-
};
|
|
23
|
-
exports.default = getAllFiles;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const getAllFiles = function (dirPath, arrayOfFiles) {
|
|
9
|
+
const files = fs_1.default.readdirSync(dirPath);
|
|
10
|
+
arrayOfFiles = arrayOfFiles || [];
|
|
11
|
+
files.forEach(function (file) {
|
|
12
|
+
if (fs_1.default.statSync(dirPath + "/" + file).isDirectory()) {
|
|
13
|
+
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const __path = path_1.default.join(dirPath, "/", file).replace(/\\/g, "/");
|
|
17
|
+
if (arrayOfFiles)
|
|
18
|
+
arrayOfFiles.push(__path);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return arrayOfFiles;
|
|
22
|
+
};
|
|
23
|
+
exports.default = getAllFiles;
|
|
24
24
|
//# sourceMappingURL=getAllFiles.js.map
|
package/esm/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/esm/index.js
CHANGED
|
@@ -1,150 +1,150 @@
|
|
|
1
|
-
import yargs from "yargs";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import { convertToWebpByPath } from "diginext-img-magic";
|
|
4
|
-
import isImageSupported from "diginext-img-magic/dist/compress/isImageSupported";
|
|
5
|
-
import { getFileExtension, getFileNameWithoutExtension } from "diginext-utils/dist/string/url";
|
|
6
|
-
import getAllFiles from "./plugins/getAllFiles";
|
|
7
|
-
const { version } = require("../package.json");
|
|
8
|
-
// import { hideBin } from "yargs/helpers";
|
|
9
|
-
(async () => {
|
|
10
|
-
//
|
|
11
|
-
const argv = yargs(process.argv.slice(2))
|
|
12
|
-
.usage("Usage: $0 <command> [options]")
|
|
13
|
-
.example('$0 --dir "dir"', "")
|
|
14
|
-
// .demandOption(["p"])
|
|
15
|
-
.alias("d", "dir")
|
|
16
|
-
.describe("d", "Directory")
|
|
17
|
-
.option("dir", {
|
|
18
|
-
string: true,
|
|
19
|
-
})
|
|
20
|
-
.help("h")
|
|
21
|
-
.alias("h", "help")
|
|
22
|
-
.epilog(`verion: ${version}`).argv;
|
|
23
|
-
let dirName;
|
|
24
|
-
if (Array.isArray(argv.dir)) {
|
|
25
|
-
dirName = argv.dir[1] === true ? "" : argv.dir[1];
|
|
26
|
-
if (!dirName) {
|
|
27
|
-
console.log("NEED DIRECTORY!");
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
dirName = argv === true ? "" : argv.dir;
|
|
33
|
-
if (!dirName) {
|
|
34
|
-
console.log("NEED DIRECTORY!");
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (!fs.lstatSync(dirName).isDirectory()) {
|
|
39
|
-
//
|
|
40
|
-
console.log("Please use a directory!");
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
console.log("dirName :>> ", dirName);
|
|
44
|
-
dirName = dirName.replace(/\\/g, "/");
|
|
45
|
-
const folderName = getFileNameWithoutExtension(dirName);
|
|
46
|
-
console.log("folderName :>> ", folderName);
|
|
47
|
-
// const arr = dirName.split("/");
|
|
48
|
-
// console.log("arr :>> ", dirName.split("/").pop());
|
|
49
|
-
const folderNameWebp = `${folderName}-webp`;
|
|
50
|
-
const dirFolderWebp = [...dirName.split("/").slice(0, -1), folderNameWebp].join("/");
|
|
51
|
-
const folderNameThumb = `${folderName}-thumb-webp`;
|
|
52
|
-
const dirFolderThumb = [...dirName.split("/").slice(0, -1), folderNameThumb].join("/");
|
|
53
|
-
console.log("dirFolderWebp :>> ", dirFolderWebp);
|
|
54
|
-
console.log("dirFolderThumb :>> ", dirFolderThumb);
|
|
55
|
-
const list = getAllFiles(dirName);
|
|
56
|
-
const checkSameName = () => {
|
|
57
|
-
const __list = list
|
|
58
|
-
.map((item) => {
|
|
59
|
-
const supported = isImageSupported(item);
|
|
60
|
-
if (supported) {
|
|
61
|
-
return [...item.split("/").slice(0, -1), `${getFileNameWithoutExtension(item)}`].join("/");
|
|
62
|
-
// return getFileNameWithoutExtension(item);
|
|
63
|
-
}
|
|
64
|
-
return null;
|
|
65
|
-
})
|
|
66
|
-
.filter((x) => x);
|
|
67
|
-
const duplicate = __list.filter((item, index) => __list.indexOf(item) !== index);
|
|
68
|
-
if (duplicate.length >= 1) {
|
|
69
|
-
console.log("!!!");
|
|
70
|
-
console.error("Tìm thấy hình giống tên nhau nhưng khác extension, không thực hiện convert !");
|
|
71
|
-
}
|
|
72
|
-
for (let i = 0; i < duplicate.length; i++) {
|
|
73
|
-
const element = duplicate[i];
|
|
74
|
-
//
|
|
75
|
-
const ___list = list.filter((x) => {
|
|
76
|
-
return x.indexOf(`${element}.`) >= 0;
|
|
77
|
-
});
|
|
78
|
-
console.log("Vui lòng đổi tên :>> ", ___list);
|
|
79
|
-
}
|
|
80
|
-
return duplicate.length >= 1;
|
|
81
|
-
};
|
|
82
|
-
if (checkSameName()) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const MAX_PROCESSING = 10;
|
|
86
|
-
let index = 0;
|
|
87
|
-
const startConvert = () => {
|
|
88
|
-
if (fs.existsSync(dirFolderWebp)) {
|
|
89
|
-
fs.rmSync(dirFolderWebp, { recursive: true });
|
|
90
|
-
}
|
|
91
|
-
fs.mkdirSync(dirFolderWebp, { recursive: true });
|
|
92
|
-
if (fs.existsSync(dirFolderThumb)) {
|
|
93
|
-
fs.rmSync(dirFolderThumb, { recursive: true });
|
|
94
|
-
}
|
|
95
|
-
fs.mkdirSync(dirFolderThumb, { recursive: true });
|
|
96
|
-
//
|
|
97
|
-
index = -1;
|
|
98
|
-
for (let i = 0; i < MAX_PROCESSING; i++) {
|
|
99
|
-
walkOne();
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
const walkOne = async () => {
|
|
103
|
-
//
|
|
104
|
-
index++;
|
|
105
|
-
if (index == list.length) {
|
|
106
|
-
index++;
|
|
107
|
-
setTimeout(() => {
|
|
108
|
-
try {
|
|
109
|
-
fs.rmSync(".temp", { recursive: true });
|
|
110
|
-
console.log("DONE !");
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
113
|
-
console.error(`metname error`, error);
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (index >= list.length) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
await convertOneObject(list[index]);
|
|
122
|
-
walkOne();
|
|
123
|
-
};
|
|
124
|
-
const convertOneObject = async (url) => {
|
|
125
|
-
if (!url) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
console.log(`${index + 1}/${list.length}: ${url}`);
|
|
129
|
-
url = url.replace("\\", "/");
|
|
130
|
-
const fileExt = getFileExtension(url);
|
|
131
|
-
const supported = isImageSupported(url);
|
|
132
|
-
if (!supported) {
|
|
133
|
-
fs.cpSync(url, url.replace(dirName, dirFolderWebp), { recursive: true });
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
const { large, small } = await convertToWebpByPath(url);
|
|
137
|
-
fs.cpSync(large, url.replace(dirName, dirFolderWebp).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
138
|
-
fs.cpSync(small, url.replace(dirName, dirFolderThumb).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
139
|
-
try {
|
|
140
|
-
fs.rmSync(large);
|
|
141
|
-
fs.rmSync(small);
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
console.error(`metname error`, error);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
startConvert();
|
|
148
|
-
//
|
|
149
|
-
})();
|
|
1
|
+
import yargs from "yargs";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { convertToWebpByPath } from "diginext-img-magic";
|
|
4
|
+
import isImageSupported from "diginext-img-magic/dist/compress/isImageSupported";
|
|
5
|
+
import { getFileExtension, getFileNameWithoutExtension } from "diginext-utils/dist/string/url";
|
|
6
|
+
import getAllFiles from "./plugins/getAllFiles";
|
|
7
|
+
const { version } = require("../package.json");
|
|
8
|
+
// import { hideBin } from "yargs/helpers";
|
|
9
|
+
(async () => {
|
|
10
|
+
//
|
|
11
|
+
const argv = yargs(process.argv.slice(2))
|
|
12
|
+
.usage("Usage: $0 <command> [options]")
|
|
13
|
+
.example('$0 --dir "dir"', "")
|
|
14
|
+
// .demandOption(["p"])
|
|
15
|
+
.alias("d", "dir")
|
|
16
|
+
.describe("d", "Directory")
|
|
17
|
+
.option("dir", {
|
|
18
|
+
string: true,
|
|
19
|
+
})
|
|
20
|
+
.help("h")
|
|
21
|
+
.alias("h", "help")
|
|
22
|
+
.epilog(`verion: ${version}`).argv;
|
|
23
|
+
let dirName;
|
|
24
|
+
if (Array.isArray(argv.dir)) {
|
|
25
|
+
dirName = argv.dir[1] === true ? "" : argv.dir[1];
|
|
26
|
+
if (!dirName) {
|
|
27
|
+
console.log("NEED DIRECTORY!");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
dirName = argv === true ? "" : argv.dir;
|
|
33
|
+
if (!dirName) {
|
|
34
|
+
console.log("NEED DIRECTORY!");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!fs.lstatSync(dirName).isDirectory()) {
|
|
39
|
+
//
|
|
40
|
+
console.log("Please use a directory!");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
console.log("dirName :>> ", dirName);
|
|
44
|
+
dirName = dirName.replace(/\\/g, "/");
|
|
45
|
+
const folderName = getFileNameWithoutExtension(dirName);
|
|
46
|
+
console.log("folderName :>> ", folderName);
|
|
47
|
+
// const arr = dirName.split("/");
|
|
48
|
+
// console.log("arr :>> ", dirName.split("/").pop());
|
|
49
|
+
const folderNameWebp = `${folderName}-webp`;
|
|
50
|
+
const dirFolderWebp = [...dirName.split("/").slice(0, -1), folderNameWebp].join("/");
|
|
51
|
+
const folderNameThumb = `${folderName}-thumb-webp`;
|
|
52
|
+
const dirFolderThumb = [...dirName.split("/").slice(0, -1), folderNameThumb].join("/");
|
|
53
|
+
console.log("dirFolderWebp :>> ", dirFolderWebp);
|
|
54
|
+
console.log("dirFolderThumb :>> ", dirFolderThumb);
|
|
55
|
+
const list = getAllFiles(dirName);
|
|
56
|
+
const checkSameName = () => {
|
|
57
|
+
const __list = list
|
|
58
|
+
.map((item) => {
|
|
59
|
+
const supported = isImageSupported(item);
|
|
60
|
+
if (supported) {
|
|
61
|
+
return [...item.split("/").slice(0, -1), `${getFileNameWithoutExtension(item)}`].join("/");
|
|
62
|
+
// return getFileNameWithoutExtension(item);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
})
|
|
66
|
+
.filter((x) => x);
|
|
67
|
+
const duplicate = __list.filter((item, index) => __list.indexOf(item) !== index);
|
|
68
|
+
if (duplicate.length >= 1) {
|
|
69
|
+
console.log("!!!");
|
|
70
|
+
console.error("Tìm thấy hình giống tên nhau nhưng khác extension, không thực hiện convert !");
|
|
71
|
+
}
|
|
72
|
+
for (let i = 0; i < duplicate.length; i++) {
|
|
73
|
+
const element = duplicate[i];
|
|
74
|
+
//
|
|
75
|
+
const ___list = list.filter((x) => {
|
|
76
|
+
return x.indexOf(`${element}.`) >= 0;
|
|
77
|
+
});
|
|
78
|
+
console.log("Vui lòng đổi tên :>> ", ___list);
|
|
79
|
+
}
|
|
80
|
+
return duplicate.length >= 1;
|
|
81
|
+
};
|
|
82
|
+
if (checkSameName()) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const MAX_PROCESSING = 10;
|
|
86
|
+
let index = 0;
|
|
87
|
+
const startConvert = () => {
|
|
88
|
+
if (fs.existsSync(dirFolderWebp)) {
|
|
89
|
+
fs.rmSync(dirFolderWebp, { recursive: true });
|
|
90
|
+
}
|
|
91
|
+
fs.mkdirSync(dirFolderWebp, { recursive: true });
|
|
92
|
+
if (fs.existsSync(dirFolderThumb)) {
|
|
93
|
+
fs.rmSync(dirFolderThumb, { recursive: true });
|
|
94
|
+
}
|
|
95
|
+
fs.mkdirSync(dirFolderThumb, { recursive: true });
|
|
96
|
+
//
|
|
97
|
+
index = -1;
|
|
98
|
+
for (let i = 0; i < MAX_PROCESSING; i++) {
|
|
99
|
+
walkOne();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const walkOne = async () => {
|
|
103
|
+
//
|
|
104
|
+
index++;
|
|
105
|
+
if (index == list.length) {
|
|
106
|
+
index++;
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
try {
|
|
109
|
+
fs.rmSync(".temp", { recursive: true });
|
|
110
|
+
console.log("DONE !");
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
console.error(`metname error`, error);
|
|
114
|
+
}
|
|
115
|
+
}, 5000);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (index >= list.length) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
await convertOneObject(list[index]);
|
|
122
|
+
walkOne();
|
|
123
|
+
};
|
|
124
|
+
const convertOneObject = async (url) => {
|
|
125
|
+
if (!url) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
console.log(`${index + 1}/${list.length}: ${url}`);
|
|
129
|
+
url = url.replace("\\", "/");
|
|
130
|
+
const fileExt = getFileExtension(url);
|
|
131
|
+
const supported = isImageSupported(url);
|
|
132
|
+
if (!supported) {
|
|
133
|
+
fs.cpSync(url, url.replace(dirName, dirFolderWebp), { recursive: true });
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const { large, small } = await convertToWebpByPath(url);
|
|
137
|
+
fs.cpSync(large, url.replace(dirName, dirFolderWebp).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
138
|
+
fs.cpSync(small, url.replace(dirName, dirFolderThumb).replace(`.${fileExt}`, ".webp"), { recursive: true });
|
|
139
|
+
try {
|
|
140
|
+
fs.rmSync(large);
|
|
141
|
+
fs.rmSync(small);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error(`metname error`, error);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
startConvert();
|
|
148
|
+
//
|
|
149
|
+
})();
|
|
150
150
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const getAllFiles: (dirPath: string, arrayOfFiles?: Array<string>) => string[];
|
|
2
|
-
export default getAllFiles;
|
|
1
|
+
declare const getAllFiles: (dirPath: string, arrayOfFiles?: Array<string>) => string[];
|
|
2
|
+
export default getAllFiles;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
const getAllFiles = function (dirPath, arrayOfFiles) {
|
|
4
|
-
const files = fs.readdirSync(dirPath);
|
|
5
|
-
arrayOfFiles = arrayOfFiles || [];
|
|
6
|
-
files.forEach(function (file) {
|
|
7
|
-
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
|
|
8
|
-
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
const __path = path.join(dirPath, "/", file).replace(/\\/g, "/");
|
|
12
|
-
if (arrayOfFiles)
|
|
13
|
-
arrayOfFiles.push(__path);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
return arrayOfFiles;
|
|
17
|
-
};
|
|
18
|
-
export default getAllFiles;
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
const getAllFiles = function (dirPath, arrayOfFiles) {
|
|
4
|
+
const files = fs.readdirSync(dirPath);
|
|
5
|
+
arrayOfFiles = arrayOfFiles || [];
|
|
6
|
+
files.forEach(function (file) {
|
|
7
|
+
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
|
|
8
|
+
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
const __path = path.join(dirPath, "/", file).replace(/\\/g, "/");
|
|
12
|
+
if (arrayOfFiles)
|
|
13
|
+
arrayOfFiles.push(__path);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return arrayOfFiles;
|
|
17
|
+
};
|
|
18
|
+
export default getAllFiles;
|
|
19
19
|
//# sourceMappingURL=getAllFiles.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diginext-img-magic-cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"readme": "README.md",
|
|
6
6
|
"bin": {
|
|
@@ -30,10 +30,9 @@
|
|
|
30
30
|
"build": "npm run prebuild && tsc --module commonjs --outDir dist && tsc --module esnext --outDir esm && npm run test",
|
|
31
31
|
"build_tsup": "tsup src/**/*.ts --format esm,cjs --dts --legacy-output",
|
|
32
32
|
"prebuild": "rm -rf dist && rm -rf esm",
|
|
33
|
-
"release": "npm run
|
|
33
|
+
"release": "npm run build && npm publish",
|
|
34
34
|
"dev": "npm run prebuild && concurrently \"tsup src/**/*.ts --format esm,cjs --dts --watch\"",
|
|
35
35
|
"test": "mocha test/*.test.js",
|
|
36
|
-
"publish": "npm run build && npm run test && npm publish",
|
|
37
36
|
"lint": "TIMING=1 eslint src --fix",
|
|
38
37
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
39
38
|
},
|