graph-dependencies.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const childProcess = require('child_process');
  2. const fs = require('fs-extra');
  3. const glob = require('glob');
  4. const path = require('path');
  5. const url = require('url');
  6. const basePath = path.resolve('..');
  7. const baseUrl = 'https://github.com/jupyterlab/jupyterlab/tree/master/packages';
  8. const packages = glob.sync(path.join(basePath, 'packages/*'));
  9. // Begin the graph specification
  10. let text = 'digraph G {\n';
  11. text += 'ratio = 0.6;\n';
  12. text += 'rankdir=LR;\n';
  13. packages.forEach(function(packagePath) {
  14. // Load the package.json data.
  15. const dataPath = path.join(packagePath, 'package.json');
  16. try {
  17. const data = require(dataPath); // eslint-disable-line @typescript-eslint/no-unused-vars
  18. } catch (e) {
  19. return;
  20. }
  21. // Don't include private packages.
  22. if (data.private === true) {
  23. return;
  24. }
  25. // Only include packages in the @jupyterlab namespace.
  26. if (data.name.indexOf('@jupyterlab') === -1) {
  27. return;
  28. }
  29. // In order to cut down on the number of graph nodes,
  30. // don't include "*-extension" packages.
  31. if (data.name.indexOf('-extension') !== -1) {
  32. return;
  33. }
  34. // Don't include the metapackage.
  35. if (data.name === '@jupyterlab/metapackage') {
  36. return;
  37. }
  38. // Construct a URL to the package on GitHub.
  39. const Url = url.resolve(baseUrl, 'packages/' + path.basename(packagePath));
  40. // Remove the '@jupyterlab' part of the name.
  41. const name = '"' + data.name.split('/')[1] + '"';
  42. text += name + '[URL="' + Url + '"];\n';
  43. const deps = data.dependencies || [];
  44. for (let dep in deps) {
  45. // Don't include non-jupyterlab dependencies.
  46. if (dep.indexOf('@jupyterlab') === -1) {
  47. continue;
  48. }
  49. dep = '"' + dep.split('/')[1] + '"';
  50. text += name + ' -> ' + dep + ';\n';
  51. }
  52. });
  53. text += '}\n';
  54. fs.writeFileSync('./dependencies.gv', text);
  55. childProcess.execSync(
  56. 'cat dependencies.gv | tred | dot -Tsvg -o dependency-graph.svg'
  57. );
  58. fs.unlinkSync('./dependencies.gv');