doc-impl.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google Inc. All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const command_1 = require("../models/command");
  11. const open = require('open');
  12. class DocCommand extends command_1.Command {
  13. async run(options) {
  14. if (!options.keyword) {
  15. this.logger.error('You should specify a keyword, for instance, `ng doc ActivatedRoute`.');
  16. return 0;
  17. }
  18. let domain = 'angular.io';
  19. if (options.version) {
  20. // version can either be a string containing "next"
  21. if (options.version == 'next') {
  22. domain = 'next.angular.io';
  23. // or a number where version must be a valid Angular version (i.e. not 0, 1 or 3)
  24. }
  25. else if (!isNaN(+options.version) && ![0, 1, 3].includes(+options.version)) {
  26. domain = `v${options.version}.angular.io`;
  27. }
  28. else {
  29. this.logger.error('Version should either be a number (2, 4, 5, 6...) or "next"');
  30. return 0;
  31. }
  32. }
  33. else {
  34. // we try to get the current Angular version of the project
  35. // and use it if we can find it
  36. try {
  37. /* tslint:disable-next-line:no-implicit-dependencies */
  38. const currentNgVersion = require('@angular/core').VERSION.major;
  39. domain = `v${currentNgVersion}.angular.io`;
  40. }
  41. catch (e) { }
  42. }
  43. let searchUrl = `https://${domain}/api?query=${options.keyword}`;
  44. if (options.search) {
  45. searchUrl = `https://www.google.com/search?q=site%3A${domain}+${options.keyword}`;
  46. }
  47. // We should wrap `open` in a new Promise because `open` is already resolved
  48. await new Promise(() => {
  49. open(searchUrl, {
  50. wait: false,
  51. });
  52. });
  53. }
  54. }
  55. exports.DocCommand = DocCommand;