progress.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. const chalk = require('chalk');
  20. const path = require('path');
  21. module.exports = function progress(options = {}) {
  22. const scope = options.scope || {};
  23. scope.finished = scope.finished || 0;
  24. scope.total = scope.total || 0;
  25. return {
  26. name: 'progress',
  27. buildStart() {
  28. scope.finished = 0;
  29. scope.total = 0;
  30. },
  31. load() {
  32. // TODO More accurate total number
  33. scope.total++;
  34. },
  35. transform(code, id) {
  36. scope.finished++;
  37. const filePath = path.relative(process.cwd(), id).split(path.sep).join('/');
  38. const output = `[${scope.finished}/${scope.total}]: ${chalk.grey(filePath)}`;
  39. if (process.stdout.isTTY) {
  40. process.stdout.clearLine();
  41. process.stdout.cursorTo(0);
  42. process.stdout.write(output);
  43. }
  44. else {
  45. console.log(output);
  46. }
  47. },
  48. buildEnd() {
  49. if (process.stdout.isTTY) {
  50. process.stdout.clearLine();
  51. process.stdout.cursorTo(0);
  52. }
  53. else {
  54. console.log('');
  55. }
  56. }
  57. };
  58. };