generate-make-graph.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2018-2022 Elyra Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { spawn } from 'child_process';
  17. import path from 'path';
  18. import chalk from 'chalk';
  19. const makeDir = path.join(__dirname, '..', '..');
  20. const cmd = process.argv[2];
  21. const make = spawn('make', [cmd, '-Bnd'], { cwd: makeDir });
  22. interface ITarget {
  23. type: 'target';
  24. name: string;
  25. depth: number;
  26. }
  27. interface ICode {
  28. type: 'code';
  29. value: string;
  30. depth: number;
  31. }
  32. interface IEnd {
  33. type: 'end';
  34. depth: number;
  35. }
  36. const graph: (ITarget | ICode | IEnd)[] = [];
  37. let depth: number | undefined = undefined;
  38. make.stdout.on('data', (data: Buffer) => {
  39. const msgs = data.toString().split('\n');
  40. for (const msg of msgs) {
  41. if (msg === '') {
  42. continue;
  43. }
  44. if (msg.includes('Considering target file')) {
  45. const [depthString, rest] = msg.split('Considering target file `');
  46. const [name] = rest.split("'.");
  47. if (name === 'Makefile') {
  48. continue;
  49. }
  50. graph.push({
  51. type: 'target',
  52. name,
  53. depth: depthString.length / 2
  54. });
  55. continue;
  56. }
  57. if (msg.includes('Must remake target')) {
  58. const [depthString] = msg.split('Must remake target `');
  59. depth = depthString.length / 2;
  60. continue;
  61. }
  62. if (msg.includes('Successfully remade target file')) {
  63. const [depthString] = msg.split('Successfully remade target file `');
  64. depth = depthString.length / 2;
  65. graph.push({
  66. type: 'end',
  67. depth
  68. });
  69. depth = undefined;
  70. continue;
  71. }
  72. if (depth !== undefined) {
  73. graph.push({
  74. type: 'code',
  75. value: msg.toString(),
  76. depth
  77. });
  78. continue;
  79. }
  80. }
  81. });
  82. const MAX_WIDTH = 120;
  83. const printGraph = (): void => {
  84. for (const g of graph) {
  85. const padLeft = '│ '.repeat(g.depth);
  86. const padRight = ' │'.repeat(g.depth);
  87. const cellWidth = MAX_WIDTH - (padLeft.length + padRight.length + 4);
  88. const bar = '─'.repeat(cellWidth);
  89. if (g.type === 'target') {
  90. const spacer = ' '.repeat(cellWidth - g.name.length - 2);
  91. console.log(`${padLeft}┌${bar}┐${padRight}`);
  92. console.log(
  93. `${padLeft}│ ${chalk.cyan.bold(g.name)}${spacer} |${padRight}`
  94. );
  95. continue;
  96. }
  97. if (g.type === 'code') {
  98. let value = g.value;
  99. if (value.length > cellWidth + 2) {
  100. value = value.slice(0, cellWidth - 5) + '...';
  101. }
  102. const spacer = ' '.repeat(Math.max(0, cellWidth - value.length - 2));
  103. console.log(`${padLeft}│ ${value}${spacer} |${padRight}`);
  104. continue;
  105. }
  106. if (g.type === 'end') {
  107. console.log(`${padLeft}└${bar}┘${padRight}`);
  108. continue;
  109. }
  110. }
  111. };
  112. make.stdout.on('end', () => {
  113. printGraph();
  114. });