a zip code crypto-currency system good for red ONLY

ProgressPlugin.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class ProgressPlugin {
  7. constructor(options) {
  8. if(typeof options === "function") {
  9. options = {
  10. handler: options
  11. };
  12. }
  13. options = options || {};
  14. this.profile = options.profile;
  15. this.handler = options.handler;
  16. }
  17. apply(compiler) {
  18. const handler = this.handler || defaultHandler;
  19. const profile = this.profile;
  20. if(compiler.compilers) {
  21. const states = new Array(compiler.compilers.length);
  22. compiler.compilers.forEach(function(compiler, idx) {
  23. compiler.apply(new ProgressPlugin(function(p, msg) {
  24. states[idx] = Array.prototype.slice.apply(arguments);
  25. handler.apply(null, [
  26. states.map(state => state && state[0] || 0).reduce((a, b) => a + b) / states.length,
  27. `[${idx}] ${msg}`
  28. ].concat(Array.prototype.slice.call(arguments, 2)));
  29. }));
  30. });
  31. } else {
  32. let lastModulesCount = 0;
  33. let moduleCount = 500;
  34. let doneModules = 0;
  35. const activeModules = [];
  36. const update = function update(module) {
  37. handler(
  38. 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6,
  39. "building modules",
  40. `${doneModules}/${moduleCount} modules`,
  41. `${activeModules.length} active`,
  42. activeModules[activeModules.length - 1]
  43. );
  44. };
  45. const moduleDone = function moduleDone(module) {
  46. doneModules++;
  47. const ident = module.identifier();
  48. if(ident) {
  49. const idx = activeModules.indexOf(ident);
  50. if(idx >= 0) activeModules.splice(idx, 1);
  51. }
  52. update();
  53. };
  54. compiler.plugin("compilation", function(compilation) {
  55. if(compilation.compiler.isChild()) return;
  56. lastModulesCount = moduleCount;
  57. moduleCount = 0;
  58. doneModules = 0;
  59. handler(0, "compiling");
  60. compilation.plugin("build-module", function(module) {
  61. moduleCount++;
  62. const ident = module.identifier();
  63. if(ident) {
  64. activeModules.push(ident);
  65. }
  66. update();
  67. });
  68. compilation.plugin("failed-module", moduleDone);
  69. compilation.plugin("succeed-module", moduleDone);
  70. const syncHooks = {
  71. "seal": [0.71, "sealing"],
  72. "optimize": [0.72, "optimizing"],
  73. "optimize-modules-basic": [0.73, "basic module optimization"],
  74. "optimize-modules": [0.74, "module optimization"],
  75. "optimize-modules-advanced": [0.75, "advanced module optimization"],
  76. "optimize-chunks-basic": [0.76, "basic chunk optimization"],
  77. "optimize-chunks": [0.77, "chunk optimization"],
  78. "optimize-chunks-advanced": [0.78, "advanced chunk optimization"],
  79. // optimize-tree
  80. "optimize-chunk-modules": [0.80, "chunk modules optimization"],
  81. "optimize-chunk-modules-advanced": [0.81, "advanced chunk modules optimization"],
  82. "revive-modules": [0.82, "module reviving"],
  83. "optimize-module-order": [0.83, "module order optimization"],
  84. "optimize-module-ids": [0.84, "module id optimization"],
  85. "revive-chunks": [0.85, "chunk reviving"],
  86. "optimize-chunk-order": [0.86, "chunk order optimization"],
  87. "optimize-chunk-ids": [0.87, "chunk id optimization"],
  88. "before-hash": [0.88, "hashing"],
  89. "before-module-assets": [0.89, "module assets processing"],
  90. "before-chunk-assets": [0.90, "chunk assets processing"],
  91. "additional-chunk-assets": [0.91, "additional chunk assets processing"],
  92. "record": [0.92, "recording"]
  93. };
  94. Object.keys(syncHooks).forEach(name => {
  95. let pass = 0;
  96. const settings = syncHooks[name];
  97. compilation.plugin(name, () => {
  98. if(pass++ > 0)
  99. handler(settings[0], settings[1], `pass ${pass}`);
  100. else
  101. handler(settings[0], settings[1]);
  102. });
  103. });
  104. compilation.plugin("optimize-tree", (chunks, modules, callback) => {
  105. handler(0.79, "module and chunk tree optimization");
  106. callback();
  107. });
  108. compilation.plugin("additional-assets", callback => {
  109. handler(0.91, "additional asset processing");
  110. callback();
  111. });
  112. compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
  113. handler(0.92, "chunk asset optimization");
  114. callback();
  115. });
  116. compilation.plugin("optimize-assets", (assets, callback) => {
  117. handler(0.94, "asset optimization");
  118. callback();
  119. });
  120. });
  121. compiler.plugin("emit", (compilation, callback) => {
  122. handler(0.95, "emitting");
  123. callback();
  124. });
  125. compiler.plugin("done", () => {
  126. handler(1, "");
  127. });
  128. }
  129. let lineCaretPosition = 0,
  130. lastState, lastStateTime;
  131. function defaultHandler(percentage, msg) {
  132. let state = msg;
  133. const details = Array.prototype.slice.call(arguments, 2);
  134. if(percentage < 1) {
  135. percentage = Math.floor(percentage * 100);
  136. msg = `${percentage}% ${msg}`;
  137. if(percentage < 100) {
  138. msg = ` ${msg}`;
  139. }
  140. if(percentage < 10) {
  141. msg = ` ${msg}`;
  142. }
  143. details.forEach(detail => {
  144. if(!detail) return;
  145. if(detail.length > 40) {
  146. detail = `...${detail.substr(detail.length - 37)}`;
  147. }
  148. msg += ` ${detail}`;
  149. });
  150. }
  151. if(profile) {
  152. state = state.replace(/^\d+\/\d+\s+/, "");
  153. if(percentage === 0) {
  154. lastState = null;
  155. lastStateTime = Date.now();
  156. } else if(state !== lastState || percentage === 1) {
  157. const now = Date.now();
  158. if(lastState) {
  159. const stateMsg = `${now - lastStateTime}ms ${lastState}`;
  160. goToLineStart(stateMsg);
  161. process.stderr.write(stateMsg + "\n");
  162. lineCaretPosition = 0;
  163. }
  164. lastState = state;
  165. lastStateTime = now;
  166. }
  167. }
  168. goToLineStart(msg);
  169. process.stderr.write(msg);
  170. }
  171. function goToLineStart(nextMessage) {
  172. let str = "";
  173. for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) {
  174. str += "\b \b";
  175. }
  176. for(var i = 0; i < lineCaretPosition; i++) {
  177. str += "\b";
  178. }
  179. lineCaretPosition = nextMessage.length;
  180. if(str) process.stderr.write(str);
  181. }
  182. }
  183. }
  184. module.exports = ProgressPlugin;