add-sibling.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. packagePath = newPackagePath;
  34. } else {
  35. // Otherwise treat it as a git reposotory and try to add it.
  36. packageDirName = target
  37. .split('/')
  38. .pop()
  39. .split('.')[0];
  40. packagePath = path.join(basePath, 'packages', packageDirName);
  41. utils.run('git clone ' + target + ' ' + packagePath);
  42. }
  43. // Remove any existing node_modules in the extension.
  44. if (fs.existsSync(path.join(packagePath, 'node_modules'))) {
  45. fs.removeSync(path.join(packagePath, 'node_modules'));
  46. }
  47. // Make sure composite is set to true in the new package.
  48. let packageTsconfigPath = path.join(packagePath, 'tsconfig.json');
  49. if (fs.existsSync(packageTsconfigPath)) {
  50. let packageTsconfig = utils.readJSONFile(packageTsconfigPath);
  51. packageTsconfig.compilerOptions.composite = true;
  52. utils.writeJSONFile(packageTsconfigPath, packageTsconfig);
  53. }
  54. // Get the package.json of the extension.
  55. let pkgJSONPath = path.join(packagePath, 'package.json');
  56. let data = utils.readJSONFile(pkgJSONPath);
  57. if (data.private !== true) {
  58. data.publishConfig = {};
  59. data.publishConfig.access = 'public';
  60. utils.writeJSONFile(pkgJSONPath, data);
  61. }
  62. // Add the extension path to packages/metapackage/tsconfig.json
  63. let tsconfigPath = path.join(
  64. basePath,
  65. 'packages',
  66. 'metapackage',
  67. 'tsconfig.json'
  68. );
  69. let tsconfig = utils.readJSONFile(tsconfigPath);
  70. tsconfig.references.push({
  71. path: path.join('..', '..', packageDirName)
  72. });
  73. utils.writeJSONFile(tsconfigPath, tsconfig);
  74. // Update the core jupyterlab build dependencies.
  75. utils.run('jlpm run integrity');