graph-dependencies.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 {', 'ratio = 0.6;', 'rankdir=LR;'];
  11. packages.forEach(function (packagePath) {
  12. // Load the package.json data.
  13. const dataPath = path.join(packagePath, 'package.json');
  14. let data;
  15. try {
  16. data = require(dataPath);
  17. } catch (e) {
  18. return;
  19. }
  20. const name = data.name ?? 'UNKNOWN';
  21. // Don't include private packages.
  22. if (data.private === true) {
  23. return;
  24. }
  25. // Only include packages in the @jupyterlab namespace.
  26. if (!name.startsWith('@jupyterlab')) {
  27. return;
  28. }
  29. // In order to cut down on the number of graph nodes,
  30. // don't include "*-extension" packages.
  31. if (name.endsWith('-extension')) {
  32. return;
  33. }
  34. // Don't include the metapackage.
  35. if (name === '@jupyterlab/metapackage') {
  36. return;
  37. }
  38. const shortName = name.split('/')[1];
  39. const urlLink = url.resolve(
  40. baseUrl,
  41. 'packages/' + path.basename(packagePath)
  42. );
  43. // Remove the '@jupyterlab' part of the name.
  44. text.push(`"${shortName}" [URL="${urlLink}"];\n`);
  45. const deps = data.dependencies ?? [];
  46. for (let dep in deps) {
  47. // Only include JupyterLab dependencies
  48. if (dep.startsWith('@jupyterlab')) {
  49. text.push(`"${shortName}" -> "${dep.split('/')[1]}";\n`);
  50. }
  51. }
  52. });
  53. text.push('}');
  54. fs.writeFileSync('./dependencies.gv', text.join('\n'));
  55. childProcess.execSync(
  56. 'cat dependencies.gv | tred | dot -Tsvg -o dependency-graph.svg'
  57. );
  58. fs.unlinkSync('./dependencies.gv');