-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportImages.js
51 lines (43 loc) · 1.74 KB
/
importImages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require('fs');
const { resolve } = require('path');
const baseOutputDir = '/Users/andrew.bridge/Projects/audioxide-data/data/images';
const dimensions = [];
const skipped = [];
const copied = [];
const parseFile = async (path) => {
const resizedSplit = path.match(/^(.+?)-(\d{1,4}x\d{1,4})(.[a-zA-Z]{2,4})$/);
if (resizedSplit) {
// This is a resized image, add it to the dimensions list and skip it on
const [match, filename, resizeDimensions, extension] = resizedSplit;
if (!dimensions.includes(resizeDimensions)) dimensions.push(resizeDimensions);
skipped.push(path);
return;
}
// This is an original image, copy it
await fs.promises.copyFile(resolve('./' + path), baseOutputDir + path);
copied.push(path);
};
const parseDir = async (path) => {
const files = await fs.promises.readdir(resolve('./' + path));
await Promise.all(files.map(async file => {
const filePath = `${path}/${file}`;
if (file.substr(0, 1) === '.') return;
const stat = await fs.promises.stat(resolve('./' + filePath));
if (stat.isDirectory()) {
if (!(fs.existsSync(baseOutputDir + filePath))) {
await fs.promises.mkdir(baseOutputDir + filePath, { recursive: true });
}
await parseDir(filePath);
} else if (stat.isFile()) {
await parseFile(filePath);
}
}));
};
const main = async () => {
// Parse data
await parseDir('');
await fs.promises.writeFile(`${baseOutputDir}/sizes.json`, JSON.stringify(dimensions));
await fs.promises.writeFile('report.json', JSON.stringify({ skipped, copied }));
console.log(`Completed import; Copied ${copied.length}, skipped ${skipped.length}`);
};
main();