add-sibling.js 3.2 KB

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