create-theme.ts 2.6 KB

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