-
Notifications
You must be signed in to change notification settings - Fork 2
/
extractor.js
72 lines (61 loc) · 2.42 KB
/
extractor.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable no-nested-ternary */
const fs = require('fs');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const pLimit = require('p-limit');
const { readScFile } = require('./src/sc/scFormat');
const logger = require('./logger');
const lzham = require('./src/lzham');
const { argv } = yargs(hideBin(process.argv));
const getAllFilesRecursively = (folder) => {
const files = fs.readdirSync(folder, { withFileTypes: true });
const allFiles = [];
files.forEach((file) => {
if (file.isDirectory()) {
allFiles.push(...getAllFilesRecursively(`${folder}/${file.name}`));
} else if (file.name.endsWith('.sc') && !file.name.endsWith('_tex.sc')) {
allFiles.push(`${folder}/${file.name}`);
}
});
return allFiles;
};
const main = async () => {
await lzham.init();
const startTime = new Date().getTime();
const promises = [];
const filesToExtract = !argv.file ? [] : (Array.isArray(argv.file) ? argv.file : [argv.file]);
const folders = !argv.folder ? [] : (Array.isArray(argv.folder) ? argv.folder : [argv.folder]);
const { extractMovieClips } = argv;
folders.forEach((folder) => {
// fs.rmSync(folder, { recursive: true });
// fs.mkdirSync(folder, { recursive: true });
filesToExtract.push(...getAllFilesRecursively(folder));
});
if (fs.existsSync('out')) {
fs.rmSync('out', { recursive: true });
}
const limit = pLimit(1);
// filesToExtract.forEach((scFile) => {
// fs.mkdirSync(`out/${scFile.substring(0, scFile.lastIndexOf('/'))}`, { recursive: true });
// promises.push(limit(() => readScFile(`${scFile.substring(0, scFile.indexOf('.sc'))}`)));
// });
for (let i = 0; i < filesToExtract.length; i += 1) {
const scFile = filesToExtract[i];
await readScFile(`${scFile.substring(0, scFile.indexOf('.sc'))}`, extractMovieClips);
}
// const sharp = require('sharp');
// const ex = await sharp('./out/sc/background_basic-shape0.png')
// .affine([1, 0, 0, 1], { background: 'black', odx: 20 })
// .affine([1, 0, 0, 1], { background: 'black', odx: -20 })
// .toFile('result.png');
const results = await Promise.allSettled(promises);
// const results = await Promise.all(promises);
results.forEach((result) => {
if (result.status === 'rejected') {
// logger.error(result.reason);
console.error(result.reason);
}
});
console.log(`extract all time - ${new Date().getTime() - startTime}ms`);
};
main();