Skip to content

Commit

Permalink
feat: Allow use of --discovery-url parameter (#1377)
Browse files Browse the repository at this point in the history
Make it possible for the user to call the generate script passing
a --discovery-url parameter
  • Loading branch information
sgarciac authored and JustinBeckwith committed Oct 16, 2018
1 parent 83feb20 commit cf9b104
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/generator/generate.ts
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as minimist from 'minimist';
import * as path from 'path';
import * as rimraf from 'rimraf';
import {install} from 'source-map-support';
Expand All @@ -24,13 +25,16 @@ import {Generator} from './generator';
// enable source map support
install();

const argv = minimist(process.argv.slice(2));
const DEFAULT_DISCOVERY_URL = 'https://www.googleapis.com/discovery/v1/apis/';
const discoveryUrl = argv['discovery-url'];

const debug = true;
const args = process.argv.slice(2);
const gen = new Generator({debug, includePrivate: false});

async function main() {
if (args.length) {
args.forEach(async url => {
if (!discoveryUrl && argv._.length > 0) {
argv._.forEach(async url => {
await gen.generateAPI(url);
console.log('Generated API for ' + url);
});
Expand All @@ -39,7 +43,7 @@ async function main() {
const apiPath = path.join(__dirname, '../../../src/apis');
await util.promisify(rimraf)(apiPath);
console.log('Generating APIs...');
await gen.generateAllAPIs();
await gen.generateAllAPIs(discoveryUrl || DEFAULT_DISCOVERY_URL);
console.log('Finished generating APIs!');
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/generator/generator.ts
Expand Up @@ -28,10 +28,6 @@ const cliArgs = argv._;
const writeFile = util.promisify(fs.writeFile);
const readDir = util.promisify(fs.readdir);

const DISCOVERY_URL = argv['discovery-url'] ?
argv['discovery-url'] :
(cliArgs.length ? cliArgs[0] :
'https://www.googleapis.com/discovery/v1/apis/');
const FRAGMENT_URL =
'https://storage.googleapis.com/apisnippets-staging/public/';

Expand Down Expand Up @@ -177,9 +173,9 @@ export class Generator {
/**
* Generate all APIs and write to files.
*/
async generateAllAPIs() {
async generateAllAPIs(discoveryUrl: string) {
const headers = this.options.includePrivate ? {} : {'X-User-Ip': '0.0.0.0'};
const res = await this.request<Schemas>({url: DISCOVERY_URL, headers});
const res = await this.request<Schemas>({url: discoveryUrl, headers});
const apis = res.data.items;
const queue = new Q({concurrency: 10});
console.log(`Generating ${apis.length} APIs...`);
Expand Down

0 comments on commit cf9b104

Please sign in to comment.