create-theme.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 inquirer from 'inquirer';
  7. import * as path from 'path';
  8. import * as utils from './utils';
  9. let questions = [
  10. {
  11. type: 'input',
  12. name: 'name',
  13. message: 'name: '
  14. },
  15. {
  16. type: 'input',
  17. name: 'title',
  18. message: 'title: '
  19. },
  20. {
  21. type: 'input',
  22. name: 'description',
  23. message: 'description: '
  24. }
  25. ];
  26. const template = `
  27. import {
  28. JupyterLab, JupyterLabPlugin
  29. } from '@jupyterlab/application';
  30. import {
  31. IThemeManager
  32. } from '@jupyterlab/apputils';
  33. /**
  34. * A plugin for the {{title}}
  35. */
  36. const plugin: JupyterLabPlugin<void> = {
  37. id: '{{name}}:plugin',
  38. requires: [IThemeManager],
  39. activate: function(app: JupyterLab, manager: IThemeManager) {
  40. manager.register({
  41. name: '{{title}}',
  42. load: function() {
  43. return manager.loadCSS('{{name}}/index.css');
  44. },
  45. unload: function() {
  46. return Promise.resolve(void 0);
  47. }
  48. });
  49. },
  50. autoStart: true
  51. };
  52. export default plugin;
  53. `;
  54. inquirer.prompt(questions).then(answers => {
  55. let { name, title, description } = answers;
  56. let dest = path.resolve(path.join('.', name));
  57. if (fs.existsSync(dest)) {
  58. console.error('Package already exists: ', name);
  59. process.exit(1);
  60. }
  61. fs.copySync(path.resolve('.', 'packages', 'theme-light-extension'), dest);
  62. let jsonPath = path.join(dest, 'package.json');
  63. let data = utils.readJSONFile(jsonPath);
  64. data.name = name;
  65. data.description = description;
  66. utils.writePackageData(jsonPath, data);
  67. // update the urls in urls.css
  68. let filePath = path.resolve('.', name, 'style', 'urls.css');
  69. let text = fs.readFileSync(filePath, 'utf8');
  70. text = text.split('@jupyterlab/theme-light-extension').join(name);
  71. fs.writeFileSync(filePath, text, 'utf8');
  72. // remove lib and node_modules.
  73. ['lib', 'node_modules'].forEach(folder => {
  74. let folderPath = path.join('.', name, folder);
  75. if (fs.existsSync(folderPath)) {
  76. fs.remove(folderPath);
  77. }
  78. });
  79. let readme = `${name}\n${description}\n`;
  80. fs.writeFileSync(path.join('.', name, 'README.md'), readme, 'utf8');
  81. let src = template.split('{{name}}').join(name);
  82. src = src.split('{{title}}').join(title);
  83. fs.writeFileSync(path.join('.', name, 'src', 'index.ts'), src, 'utf8');
  84. // Signify successful complation.
  85. console.log(`Created new theme ${name}`);
  86. });