Skip to content

Commit

Permalink
chore: enable stricter compilation for the generator (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jan 25, 2018
1 parent 8318e7e commit f78d239
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 32 deletions.
53 changes: 46 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -106,6 +106,8 @@
"@types/p-queue": "^2.3.0",
"@types/pify": "^3.0.0",
"@types/qs": "^6.5.1",
"@types/rimraf": "^2.0.2",
"@types/source-map-support": "^0.4.0",
"@types/uuid": "^3.4.3",
"axios": "^0.17.1",
"clang-format": "^1.1.1",
Expand All @@ -123,8 +125,8 @@
"nunjucks": "^3.0.1",
"nyc": "11.4.1",
"opn": "5.2.0",
"pify": "^3.0.0",
"p-queue": "^2.3.0",
"pify": "^3.0.0",
"power-assert": "1.4.4",
"rimraf": "2.6.2",
"semistandard": "12.0.0",
Expand Down
47 changes: 27 additions & 20 deletions src/scripts/generator.ts
Expand Up @@ -47,8 +47,8 @@ export interface GeneratorOptions {
}

interface Api {
discoveryRestUrl?: string;
id?: string;
discoveryRestUrl: string;
id: string;
}

interface ApiResponse {
Expand All @@ -63,6 +63,10 @@ interface FragmentResponse {
interface Schema {
name: string;
version: string;
// tslint:disable-next-line no-any
methods: any[];
// tslint:disable-next-line no-any
resources: any[];
}

export class Generator {
Expand All @@ -89,8 +93,9 @@ export class Generator {
return str ? str.replace(/\*\//g, 'x/').replace(/\/\*/g, '/x') : '';
}

private getPathParams(params) {
const pathParams = [];
// tslint:disable-next-line no-any
private getPathParams(params: any) {
const pathParams = new Array<string>();
if (typeof params !== 'object') {
params = {};
}
Expand All @@ -102,7 +107,7 @@ export class Generator {
return pathParams;
}

private getSafeParamName(param) {
private getSafeParamName(param: string) {
if (RESERVED_PARAMS.indexOf(param) !== -1) {
return param + '_';
}
Expand All @@ -111,7 +116,7 @@ export class Generator {

private options: GeneratorOptions;

private state = {};
private state = new Map<string, string[]>();

/**
* Generator for generating API endpoints
Expand Down Expand Up @@ -155,11 +160,11 @@ export class Generator {
* @param id DiscoveryRestUrl of the endpoint to log
* @param message
*/
private logResult(id, message) {
if (!this.state[id]) {
this.state[id] = [];
private logResult(id: string, message: string) {
if (!this.state.has(id)) {
this.state.set(id, new Array<string>());
}
this.state[id].push(message);
this.state.get(id)!.push(message);
}

/**
Expand Down Expand Up @@ -187,10 +192,10 @@ export class Generator {
console.log(
api.id + '\n-----------\n' +
util.inspect(
this.state[api.discoveryRestUrl], {maxArrayLength: null}) +
this.state.get(api.discoveryRestUrl),
{maxArrayLength: null}) +
'\n');
}
this.state[api.discoveryRestUrl].done = true;
};
}));
try {
Expand All @@ -202,26 +207,27 @@ export class Generator {
}

async generateIndex() {
const apis = {};
// tslint:disable-next-line no-any
const apis: any = {};
const apisPath = path.join(srcPath, 'apis');
const indexPath = path.join(apisPath, 'index.ts');

// Dynamically discover available APIs
const files: string[] = await fsp.readdir(apisPath);
files.forEach(async file => {
for (const file of files) {
const filePath = path.join(apisPath, file);
if (!(await fsp.stat(filePath)).isDirectory()) {
return;
continue;
}
apis[file] = {};
const files: string[] = await fsp.readdir(path.join(apisPath, file));
files.forEach(version => {
for (const version of files) {
const parts = path.parse(version);
if (!version.endsWith('.d.ts') && parts.ext === '.ts') {
apis[file][version] = parts.name;
}
});
});
}
}
const result = this.env.render('index.njk', {apis});
await fsp.writeFile(indexPath, result, {encoding: 'utf8'});
}
Expand All @@ -231,7 +237,8 @@ export class Generator {
* embedded links.
*/
private getFragmentsForSchema(
apiDiscoveryUrl, schema, apiPath, tasks: Array<(() => Promise<void>)>) {
apiDiscoveryUrl: string, schema: Schema, apiPath: string,
tasks: Array<(() => Promise<void>)>) {
if (schema.methods) {
for (const methodName in schema.methods) {
if (schema.methods.hasOwnProperty(methodName)) {
Expand Down Expand Up @@ -285,7 +292,7 @@ export class Generator {
* Generate API file given discovery URL
* @param apiDiscoveryUri URL or filename of discovery doc for API
*/
async generateAPI(apiDiscoveryUrl) {
async generateAPI(apiDiscoveryUrl: string) {
const parts = url.parse(apiDiscoveryUrl);
if (apiDiscoveryUrl && !parts.protocol) {
this.log('Reading from file ' + apiDiscoveryUrl);
Expand Down
5 changes: 1 addition & 4 deletions tsconfig.tools.json
Expand Up @@ -2,10 +2,7 @@
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build",
"noImplicitAny": false,
"strictNullChecks": false,
"noImplicitThis": false
"outDir": "build"
},
"include": [
"src/scripts/*.ts"
Expand Down

0 comments on commit f78d239

Please sign in to comment.