index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ngAdd = ngAdd;
  4. const schematics_1 = require("@angular-devkit/schematics");
  5. const tasks_1 = require("@angular-devkit/schematics/tasks");
  6. const TSCONFIG_DATA = {
  7. include: ['src/**/*.ts'],
  8. exclude: ['src/**/*.spec.ts']
  9. };
  10. function safeReadJSON(path, tree) {
  11. try {
  12. return JSON.parse(tree.read(path).toString());
  13. }
  14. catch (e) {
  15. throw new schematics_1.SchematicsException(`Error when parsing ${path}: ${e.message}`);
  16. }
  17. }
  18. function ngAdd() {
  19. return (tree, context) => {
  20. const tsconfigDocFile = 'tsconfig.doc.json';
  21. if (!tree.exists(tsconfigDocFile)) {
  22. tree.create(tsconfigDocFile, JSON.stringify(TSCONFIG_DATA));
  23. }
  24. const packageJsonFile = 'package.json';
  25. const packageJson = tree.exists(packageJsonFile) && safeReadJSON(packageJsonFile, tree);
  26. if (packageJson === undefined) {
  27. throw new schematics_1.SchematicsException('Could not locate package.json');
  28. }
  29. let packageScripts = {};
  30. if (packageJson['scripts']) {
  31. packageScripts = packageJson['scripts'];
  32. }
  33. else {
  34. packageScripts = {};
  35. }
  36. if (packageScripts) {
  37. packageScripts['compodoc:build'] = 'compodoc -p tsconfig.doc.json';
  38. packageScripts['compodoc:build-and-serve'] = 'compodoc -p tsconfig.doc.json -s';
  39. packageScripts['compodoc:serve'] = 'compodoc -s';
  40. }
  41. if (tree.exists(packageJsonFile)) {
  42. tree.overwrite(packageJsonFile, JSON.stringify(packageJson, null, 2));
  43. }
  44. else {
  45. tree.create(packageJsonFile, JSON.stringify(packageJson, null, 2));
  46. }
  47. context.addTask(new tasks_1.NodePackageInstallTask());
  48. return tree;
  49. };
  50. }