add-sibling.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 clone '+ 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. // Add the extension path to packages/all-packages/tsconfig.json
  50. var tsconfigPath = path.join(basePath, 'packages', 'all-packages', 'tsconfig.json');
  51. var tsconfig = require(tsconfigPath);
  52. tsconfig.compilerOptions.paths[package.name] = [path.join('..', packageDirName, 'src')];
  53. fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + '\n');
  54. // Update the core jupyterlab build dependencies.
  55. childProcess.execSync('npm run update:core', {stdio:[0,1,2]});
  56. // Update the lerna symlinks.
  57. childProcess.execSync('npm install', {stdio:[0,1,2]});