cexbrayat 371bd6194a fix(@angular/cli): validate version in doc command
As the JSON Schema validation is minimum in the CLI, we have to also validate in code.
This PR:
- updates the JSON Schema to use `number` and `enum` instead of `integer` and `const` that are not supported.
- adds validation in the doc command implementation
- adds error reporting if the version is not valid
- fixes a typo in an error message in the parser
2019-06-24 17:30:44 -07:00

53 lines
1.5 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Command } from '../models/command';
import { Arguments } from '../models/interface';
import { Schema as DocCommandSchema } from './doc';
const open = require('open');
export class DocCommand extends Command<DocCommandSchema> {
public async run(options: DocCommandSchema & Arguments) {
if (!options.keyword) {
this.logger.error('You should specify a keyword, for instance, `ng doc ActivatedRoute`.');
return 0;
}
let domain = 'angular.io';
if (options.version) {
// version can either be a string containing "next"
if (options.version == 'next') {
domain = 'next.angular.io';
// or a number where version must be a valid Angular version (i.e. not 0, 1 or 3)
} else if (!isNaN(+options.version) && ![0, 1, 3].includes(+options.version)) {
domain = `v${options.version}.angular.io`;
} else {
this.logger.error('Version should either be a number (2, 4, 5, 6...) or "next"');
return 0;
}
}
let searchUrl = `https://${domain}/api?query=${options.keyword}`;
if (options.search) {
searchUrl = `https://www.google.com/search?q=site%3A${domain}+${options.keyword}`;
}
// We should wrap `open` in a new Promise because `open` is already resolved
await new Promise(() => {
open(searchUrl, {
wait: false,
});
});
}
}