add-sibling.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import * as fs from 'fs-extra';
  6. import * as path from 'path';
  7. import * as utils from './utils';
  8. /**
  9. * Add an extension to the source tree of JupyterLab.
  10. * It takes as an argument either a path to a directory
  11. * on the local filesystem or a URL to a git repository.
  12. * In the former case, it copies the directory into the
  13. * source tree, in the latter it adds the repository as
  14. * a git submodule.
  15. *
  16. * It also adds the relevant metadata to the build files.
  17. */
  18. // Make sure we have required command line arguments.
  19. if (process.argv.length < 3) {
  20. let msg = '** Must supply a target extension';
  21. process.stderr.write(msg);
  22. process.exit(1);
  23. }
  24. // Extract the desired git repository and repository name.
  25. let target = process.argv[2];
  26. let basePath = path.resolve('.');
  27. let packageDirName = path.basename(target);
  28. let packagePath = path.resolve(target);
  29. if (fs.existsSync(packagePath)) {
  30. // Copy the package directory contents to the sibling package.
  31. let newPackagePath = path.join(basePath, 'packages', packageDirName);
  32. fs.copySync(packagePath, newPackagePath);
  33. } else {
  34. // Otherwise treat it as a git reposotory and try to add it.
  35. packageDirName = target.split('/').pop().split('.')[0];
  36. let packagePath = path.join(basePath, 'packages', packageDirName);
  37. utils.run('git clone ' + target + ' ' + packagePath);
  38. }
  39. // Remove any existing node_modules in the extension.
  40. if (fs.existsSync(path.join(packagePath, 'node_modules'))) {
  41. fs.removeSync(path.join(packagePath, 'node_modules'));
  42. }
  43. // Get the package.json of the extension.
  44. let data = utils.readJSONFile(path.join(packagePath, 'package.json'));
  45. // Add the extension path to packages/all-packages/tsconfig.json
  46. let tsconfigPath = path.join(basePath, 'packages', 'all-packages', 'tsconfig.json');
  47. let tsconfig = utils.readJSONFile(tsconfigPath);
  48. tsconfig.compilerOptions.paths[data.name] = [path.join('..', packageDirName, 'src')];
  49. fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + '\n');
  50. // Update the core jupyterlab build dependencies.
  51. try {
  52. utils.run('npm run integrity');
  53. } catch (e) {
  54. if (!process.env.TRAVIS_BRANCH) {
  55. console.error(e);
  56. process.exit(1);
  57. }
  58. }