@vnejs/tools 0.0.28 → 0.0.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/tools",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "CLI tools of @vnejs",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -1,10 +1,12 @@
1
1
  const path = require("path");
2
2
 
3
3
  const { createDir, getFilesInDir, readDir } = require("../utils/filesystem");
4
- const { clear, ffmpeg } = require("./utils");
4
+ const { clear, ffmpeg, Semaphore } = require("./utils");
5
5
 
6
6
  const gameDir = path.join(process.env.PWD, "game");
7
7
 
8
+ const semaphore = new Semaphore(8);
9
+
8
10
  const resizeOne = async (toDirName, mediaDir, mediaTypes, quality, fromDirName, options) => {
9
11
  const promises = [];
10
12
 
@@ -46,14 +48,16 @@ const resizeOne = async (toDirName, mediaDir, mediaTypes, quality, fromDirName,
46
48
  })
47
49
  );
48
50
 
49
- await Promise.all(promises.map((i) => i()));
51
+ for (let i = 0; i < promises.length; i++) {
52
+ await semaphore.push(i, 1, promises[i]);
53
+ }
50
54
  };
51
55
  const resize = async (toDirName, mediaTypes, quality, fromDirName, options) => {
52
56
  const mods = await readDir(gameDir);
53
57
 
54
- return Promise.all(
55
- mods.map((mod) => resizeOne(toDirName, path.join(gameDir, mod, "media"), mediaTypes, quality, fromDirName, options))
56
- );
58
+ for (let i = 0; i < mods.length; i++) {
59
+ await resizeOne(toDirName, path.join(gameDir, mods[i], "media"), mediaTypes, quality, fromDirName, options);
60
+ }
57
61
  };
58
62
 
59
63
  module.exports = async (options) => {
@@ -71,4 +75,5 @@ module.exports = async (options) => {
71
75
  addPromise(["128", "all"].includes(options.target), 128);
72
76
 
73
77
  for (let i = 0; i < promises.length; i++) await promises[i]();
78
+ await semaphore.waitAll();
74
79
  };
@@ -2,10 +2,12 @@ const path = require("path");
2
2
  const sharp = require("sharp");
3
3
 
4
4
  const { createDir, getFilesInDir, readFile, readDir } = require("../utils/filesystem");
5
- const { clear } = require("./utils");
5
+ const { clear, Semaphore } = require("./utils");
6
6
 
7
7
  const gameDir = path.join(process.env.PWD, "game");
8
8
 
9
+ const semaphore = new Semaphore(8);
10
+
9
11
  const resizeOne = async (toDirName, mediaDir, mediaTypes, getSizes, fromDirName, options) => {
10
12
  const promises = [];
11
13
  const quality = options.web ? 80 : 100;
@@ -50,16 +52,16 @@ const resizeOne = async (toDirName, mediaDir, mediaTypes, getSizes, fromDirName,
50
52
  })
51
53
  );
52
54
 
53
- await Promise.all(promises.map((promiseFunc) => promiseFunc()));
55
+ for (let i = 0; i < promises.length; i++) {
56
+ await semaphore.push(i, 1, promises[i]);
57
+ }
54
58
  };
55
59
  const resize = async (toDirName, mediaTypes, getSizes, fromDirName, options) => {
56
60
  const mods = await readDir(gameDir);
57
61
 
58
- return Promise.all(
59
- mods.map((mod) =>
60
- resizeOne(toDirName, path.join(gameDir, mod, "media"), mediaTypes, getSizes, fromDirName, options)
61
- )
62
- );
62
+ for (let i = 0; i < mods.length; i++) {
63
+ await resizeOne(toDirName, path.join(gameDir, mods[i], "media"), mediaTypes, getSizes, fromDirName, options);
64
+ }
63
65
  };
64
66
 
65
67
  module.exports = async (options) => {
@@ -69,10 +71,8 @@ module.exports = async (options) => {
69
71
  if (!c) return;
70
72
  const dirName = `${height}p`;
71
73
  await clear(dirName, options.media);
72
- await Promise.all([
73
- resize(dirName, options.media, (d) => [Math.round((d.width * height) / 1080)], "original", options),
74
- resize(dirName, options.media, (d) => [Math.round((d.width * height) / 2160)], "original-4k", options),
75
- ]);
74
+ await resize(dirName, options.media, (d) => [Math.round((d.width * height) / 1080)], "original", options);
75
+ await resize(dirName, options.media, (d) => [Math.round((d.width * height) / 2160)], "original-4k", options);
76
76
  });
77
77
 
78
78
  addPromise(["all", "mobile", "360"].includes(options.target), 360);
@@ -83,4 +83,5 @@ module.exports = async (options) => {
83
83
  addPromise(["all", "desktop", "2160"].includes(options.target), 2160);
84
84
 
85
85
  for (let i = 0; i < promises.length; i++) await promises[i]();
86
+ await semaphore.waitAll();
86
87
  };
@@ -16,3 +16,65 @@ const clear = async (dirName, mediaTypes) => {
16
16
  };
17
17
 
18
18
  module.exports = { clearOne, clear, ffmpeg };
19
+
20
+ const getPromiseField = (e) => e.promise;
21
+ const onlyNotInProcessWithResolve = (e) => !e.inProcess && e.resolve;
22
+ const byWeight = (a, b) => b.weight - a.weight;
23
+ const race = (e) => (e.length ? Promise.race(e.map(getPromiseField)) : new Promise(setImmediate));
24
+
25
+ class Semaphore {
26
+ queue = {};
27
+ queueCurrent = [];
28
+ queueSorted = [];
29
+ length = 0;
30
+ weight = 0;
31
+ maxWeight = 999;
32
+ isQueueStarted = false;
33
+ constructor(maxWeight) {
34
+ this.maxWeight = maxWeight;
35
+ }
36
+
37
+ push = (key, weight, func) => {
38
+ const element = this.queue[key];
39
+ if (element) {
40
+ if (element.weight > weight && !element.inProcess) Object.assign(element, { weight, func });
41
+ return element.promise;
42
+ }
43
+ const obj = { key, weight, func, inProcess: false };
44
+ const promise = new Promise((resolve) => (obj.resolve = resolve));
45
+ obj.promise = promise;
46
+ this.queueSorted = [];
47
+ this.queue[key] = obj;
48
+ this.length++;
49
+ !this.isQueueStarted && this._startQueue();
50
+ return promise;
51
+ };
52
+
53
+ waitAll = () => Promise.all(this.queueCurrent.map(getPromiseField));
54
+
55
+ _startQueue = async () => {
56
+ this.isQueueStarted = true;
57
+ while (this.length) {
58
+ this.queueSorted = this.queueSorted.length ? this.queueSorted : this._recalcSortedQueue();
59
+ while (this._isCanAddNewElement(this.queueSorted)) this._start(this.queueSorted.pop());
60
+ await race(this.queueCurrent);
61
+ }
62
+ this.isQueueStarted = false;
63
+ };
64
+ _start = (e) => {
65
+ this.weight += e.weight;
66
+ e.inProcess = true;
67
+ this.queueCurrent.push(e);
68
+ e.func().then(() => {
69
+ this.weight -= e.weight;
70
+ this.queueCurrent = this.queueCurrent.filter((a) => a.key !== e.key);
71
+ this.length--;
72
+ delete this.queue[e.key];
73
+ e.resolve();
74
+ });
75
+ };
76
+ _isCanAddNewElement = (queue) => queue.length && this.maxWeight >= this.weight + queue[queue.length - 1].weight;
77
+ _recalcSortedQueue = () => Object.values(this.queue).filter(onlyNotInProcessWithResolve).sort(byWeight);
78
+ }
79
+
80
+ module.exports.Semaphore = Semaphore;
@@ -1,10 +1,12 @@
1
1
  const path = require("path");
2
2
 
3
3
  const { createDir, getFilesInDir, readDir } = require("../utils/filesystem");
4
- const { clear, ffmpeg } = require("./utils");
4
+ const { clear, ffmpeg, Semaphore } = require("./utils");
5
5
 
6
6
  const gameDir = path.join(process.env.PWD, "game");
7
7
 
8
+ const semaphore = new Semaphore(8);
9
+
8
10
  const resizeOne = async (toDirName, mediaDir, mediaTypes, height, fromDirName, options) => {
9
11
  const promises = [];
10
12
 
@@ -46,14 +48,16 @@ const resizeOne = async (toDirName, mediaDir, mediaTypes, height, fromDirName, o
46
48
  })
47
49
  );
48
50
 
49
- await Promise.all(promises.map((i) => i()));
51
+ for (let i = 0; i < promises.length; i++) {
52
+ await semaphore.push(i, 1, promises[i]);
53
+ }
50
54
  };
51
55
  const resize = async (toDirName, mediaTypes, height, fromDirName, options) => {
52
56
  const mods = await readDir(gameDir);
53
57
 
54
- return Promise.all(
55
- mods.map((mod) => resizeOne(toDirName, path.join(gameDir, mod, "media"), mediaTypes, height, fromDirName, options))
56
- );
58
+ for (let i = 0; i < mods.length; i++) {
59
+ await resizeOne(toDirName, path.join(gameDir, mods[i], "media"), mediaTypes, height, fromDirName, options);
60
+ }
57
61
  };
58
62
 
59
63
  module.exports = async (options) => {
@@ -64,14 +68,10 @@ module.exports = async (options) => {
64
68
 
65
69
  const dirName = `${height}p`;
66
70
  await clear(dirName, options.media);
67
- await Promise.all([
68
- resize(dirName, options.media, height, "original", options),
69
- resize(dirName, options.media, height, "original-4k", options),
70
- ]);
71
+ await resize(dirName, options.media, height, "original", options);
72
+ await resize(dirName, options.media, height, "original-4k", options);
71
73
  });
72
74
 
73
- console.log(9999);
74
-
75
75
  addPromise(["all", "mobile", "360"].includes(options.target), 360);
76
76
  addPromise(["all", "mobile", "540"].includes(options.target), 540);
77
77
  addPromise(["all", "mobile", "desktop", "test", "720"].includes(options.target), 720);
@@ -80,4 +80,5 @@ module.exports = async (options) => {
80
80
  addPromise(["all", "desktop", "2160"].includes(options.target), 2160);
81
81
 
82
82
  for (let i = 0; i < promises.length; i++) await promises[i]();
83
+ await semaphore.waitAll();
83
84
  };