add-sibling.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 packageDirName;
  25. var packagePath = '';
  26. if (target[0] === '.' || target[0] === '/') {
  27. // If the target starts with a '.' or a '/', treat it as a local path.
  28. packagePath = path.resolve(target);
  29. // Possibly remove a trailing slash.
  30. if (packagePath[packagePath.length-1] === '/') {
  31. packagePath = packagePath.slice(0, -1);
  32. }
  33. packageDirName = packagePath.split('/').pop();
  34. // Copy the package directory contents to the sibling package.
  35. var newPackagePath = path.join(basePath, 'packages', packageDirName);
  36. fs.copySync(packagePath, newPackagePath);
  37. } else {
  38. // Otherwise treat it as a git reposotory and try to add it.
  39. packageDirName = target.split('/').pop().split('.')[0];
  40. var packagePath = path.join(basePath, 'packages', packageDirName);
  41. // Add the repository as a submodule.
  42. childProcess.execSync('git submodule add --force '+ target + ' ' + packagePath);
  43. }
  44. // Get the package.json of the extension.
  45. var package = require(path.join(packagePath, 'package.json'));
  46. // Add the extension to packages/all-packages/package.json
  47. var allPackagesPath = path.join(basePath, 'packages', 'all-packages', 'package.json');
  48. var allPackages = require(allPackagesPath);
  49. allPackages.dependencies[package.name] = '~'+String(package.version);
  50. fs.writeFileSync(allPackagesPath, JSON.stringify(allPackages, null, 2) + '\n');
  51. // Add the extension path to packages/all-packages/tsconfig.json
  52. var tsconfigPath = path.join(basePath, 'packages', 'all-packages', 'tsconfig.json');
  53. var tsconfig = require(tsconfigPath);
  54. tsconfig.compilerOptions.paths[package.name] = [path.join('..', packageDirName, 'src')];
  55. fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + '\n');
  56. // Add the extension to packages/all-packages/src/index.ts
  57. var indexPath = path.join(basePath, 'packages', 'all-packages', 'src', 'index.ts');
  58. var index = fs.readFileSync(indexPath, 'utf8');
  59. index = index + 'import "' + package.name + '";\n';
  60. fs.writeFileSync(indexPath, index);
  61. // Add the extension to jupyterlab/package.json
  62. var jupyterlabPackagePath = path.join(basePath, 'jupyterlab', 'package.json');
  63. var jupyterlabPackage = require(jupyterlabPackagePath);
  64. jupyterlabPackage.dependencies[package.name] = '~'+String(package.version);
  65. jupyterlabPackage.jupyterlab.extensions.push(package.name);
  66. fs.writeFileSync(jupyterlabPackagePath, JSON.stringify(jupyterlabPackage, null, 2) + '\n');