add-sibling.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. var fs = require('fs-extra');
  3. var path = require('path');
  4. var childProcess = require('child_process');
  5. /**
  6. * Add an extension to the source tree of JupyterLab.
  7. * It takes as an argument either a path to a directory
  8. * on the local filesystem or a URL to a git repository.
  9. * In the former case, it copies the directory into the
  10. * source tree, in the latter it adds the repository as
  11. * a git submodule.
  12. *
  13. * It also adds the relevant metadata to the build files.
  14. */
  15. // Make sure we have required command line arguments.
  16. if (process.argv.length < 3) {
  17. var msg = '** Must supply a target extension';
  18. process.stderr.write(msg);
  19. process.exit(1);
  20. }
  21. // Extract the desired git repository and repository name.
  22. var target = process.argv[2];
  23. var basePath = path.resolve('.');
  24. var packagePath = '';
  25. if (target[0] === '.' || target[0] === '/') {
  26. // If the target starts with a '.' or a '/', treat it as a local path.
  27. packagePath = path.resolve(target);
  28. // Possibly remove a trailing slash.
  29. if (packagePath[packagePath.length-1] === '/') {
  30. packagePath = packagePath.slice(0, -1);
  31. }
  32. packageDirName = packagePath.split('/').pop();
  33. // Copy the package directory contents to the sibling package.
  34. var newPackagePath = path.join(basePath, 'packages', packageDirName);
  35. fs.copySync(packagePath, newPackagePath);
  36. } else {
  37. // Otherwise treat it as a git reposotory and try to add it.
  38. var packageDirName = target.split('/').pop().split('.')[0];
  39. var packagePath = path.join(basePath, 'packages', packageDirName);
  40. // Add the repository as a submodule.
  41. childProcess.execSync('git submodule add --force '+ target + ' ' + packagePath);
  42. }
  43. // Get the package.json of the extension.
  44. var package = require(path.join(packagePath, 'package.json'));
  45. // Add the extension to packages/all-packages/package.json
  46. var allPackagesPath = path.join(basePath, 'packages', 'all-packages', 'package.json');
  47. var allPackages = require(allPackagesPath);
  48. allPackages.dependencies[package.name] = '~'+String(package.version);
  49. fs.writeFileSync(allPackagesPath, JSON.stringify(allPackages, null, 2) + '\n');
  50. // Add the extension to packages/all-packages/src/index.ts
  51. var indexPath = path.join(basePath, 'packages', 'all-packages', 'src', 'index.ts');
  52. var index = fs.readFileSync(indexPath, 'utf8');
  53. index = index + 'import "' + package.name + '";\n';
  54. fs.writeFileSync(indexPath, index);
  55. // Add the extension to jupyterlab/package.json
  56. var jupyterlabPackagePath = path.join(basePath, 'jupyterlab', 'package.json');
  57. var jupyterlabPackage = require(jupyterlabPackagePath);
  58. jupyterlabPackage.dependencies[package.name] = '~'+String(package.version);
  59. jupyterlabPackage.jupyterlab.extensions.push(package.name);
  60. fs.writeFileSync(jupyterlabPackagePath, JSON.stringify(jupyterlabPackage, null, 2) + '\n');