Skip to content

Commit ac70bbc

Browse files
authored
Multiple type building improvements/fixes (#225)
* Fix build-types for ScenesCollection, add error event type * build-types: fix Output Typedef not being used correctly * build-types: fix arrays of objects not actually type building correctly * build-types: hack to allow arbitrary properties inside of ExecuteBatch * build-types: remove unneeded spaces
1 parent 67dea2b commit ac70bbc

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

.travis/build-types.ts

+50-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs';
2+
import * as got from 'got';
23
import * as path from 'path';
34
import * as prettier from 'prettier';
4-
import * as got from 'got';
55
import deburr = require('lodash.deburr');
66

77
interface RawType {
@@ -50,14 +50,19 @@ interface ObjectType {
5050
}
5151

5252
interface OutputType {
53-
type: 'object';
53+
type: 'ObsWebSocket.Output';
5454
properties: Tree;
5555
optional: boolean;
5656
}
5757

58+
interface OBSStatsType {
59+
type: 'ObsWebSocket.OBSStats';
60+
optional: boolean;
61+
}
62+
5863
interface ArrayType {
5964
type: 'array';
60-
items: PrimitiveType | ObjectType | OutputType | SceneType | SceneItemType | SceneItemTransformType;
65+
items: PrimitiveType | ObjectType | OutputType | SceneType | SceneItemType | SceneItemTransformType | ScenesCollectionType;
6166
optional: boolean;
6267
}
6368

@@ -76,8 +81,8 @@ interface SceneItemTransformType {
7681
optional: boolean;
7782
}
7883

79-
interface OBSStatsType {
80-
type: 'ObsWebSocket.OBSStats';
84+
interface ScenesCollectionType {
85+
type: 'ObsWebSocket.ScenesCollection';
8186
optional: boolean;
8287
}
8388

@@ -133,7 +138,7 @@ function parseApi(raw: RawComments): void {
133138
if (request.params) {
134139
const foo = unflattenAndResolveTypes(request.params);
135140
argsString += '{';
136-
argsString += stringifyTypes(foo, {terminator: ',', finalTerminator: false});
141+
argsString += stringifyTypes(foo, {terminator: ',', finalTerminator: false, name: request.name});
137142
argsString += '}';
138143
} else {
139144
argsString += 'void';
@@ -142,7 +147,7 @@ function parseApi(raw: RawComments): void {
142147
let returnTypeString = 'void';
143148
if (request.returns) {
144149
const foo = unflattenAndResolveTypes(request.returns);
145-
returnTypeString = `{messageId: string;status: "ok";${stringifyTypes(foo, {terminator: ';', finalTerminator: false})}}`;
150+
returnTypeString = `{messageId: string;status: "ok";${stringifyTypes(foo, {terminator: ';', finalTerminator: false, name: request.name})}}`;
146151
}
147152
responseString += `${returnTypeString};`;
148153

@@ -200,6 +205,13 @@ declare module 'obs-websocket-js' {
200205
"ConnectionClosed": void;
201206
"AuthenticationSuccess": void;
202207
"AuthenticationFailure": void;
208+
"error": {
209+
error: any;
210+
message: string;
211+
type: string;
212+
// This would require importing all of the WebSocket types so leaving out for now.
213+
// target: WebSocket;
214+
};
203215
${eventOverloads.join('\n\n ')}
204216
}
205217
@@ -309,11 +321,14 @@ function unflattenAndResolveTypes(inputItems: RawType[]): Tree {
309321

310322
const firstIntermediate = (currentNode as any)[nodeName];
311323
if (firstIntermediate.type === 'array') {
312-
firstIntermediate.items = {
313-
type: 'object',
314-
properties: {},
315-
optional: false
316-
};
324+
// Not sure if needed at all, but was here before and causing issues, so added a check.
325+
if (!firstIntermediate.items.properties) {
326+
firstIntermediate.items = {
327+
type: 'object',
328+
properties: {},
329+
optional: true // Matches the "array<object>" case in "resolveType".
330+
};
331+
}
317332
currentNode = firstIntermediate.items.properties;
318333
} else {
319334
currentNode = firstIntermediate.properties;
@@ -390,7 +405,7 @@ function resolveType(inType: string): AnyType {
390405
return {
391406
type: 'array',
392407
items: {
393-
type: 'object',
408+
type: 'ObsWebSocket.Output',
394409
properties: {},
395410
optional: true
396411
},
@@ -423,6 +438,15 @@ function resolveType(inType: string): AnyType {
423438
},
424439
optional: isOptional
425440
};
441+
case 'array<scenescollection>':
442+
return {
443+
type: 'array',
444+
items: {
445+
type: 'ObsWebSocket.ScenesCollection',
446+
optional: true
447+
},
448+
optional: isOptional
449+
};
426450
case 'sceneitemtransform':
427451
return {
428452
type: 'ObsWebSocket.SceneItemTransform',
@@ -433,25 +457,25 @@ function resolveType(inType: string): AnyType {
433457
type: 'ObsWebSocket.OBSStats',
434458
optional: isOptional
435459
};
460+
case 'output':
461+
return {
462+
type: 'ObsWebSocket.Output',
463+
properties: {},
464+
optional: isOptional
465+
};
436466
case 'string | object':
437467
case 'object':
438468
return {
439469
type: 'object',
440470
properties: {},
441471
optional: isOptional
442472
};
443-
case 'output':
444-
return {
445-
type: 'object',
446-
properties: {},
447-
optional: isOptional
448-
};
449473
default:
450474
throw new Error(`Unknown type: ${inType}`);
451475
}
452476
}
453477

454-
function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = true, includePrefix = true} = {}): string {
478+
function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = true, includePrefix = true, name = ''} = {}): string {
455479
let returnString = '';
456480
Object.entries(inputTypes).forEach(([key, typeDef]) => {
457481
if (includePrefix) {
@@ -466,7 +490,12 @@ function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = t
466490
if (typeDef.items) {
467491
if (typeDef.items.type === 'object') {
468492
if (Object.keys(typeDef.items.properties).length > 0) {
469-
returnString += `${stringifyTypes(typeDef.items.properties, {includePrefix: false, terminator: ''})}[]`;
493+
returnString += `{${stringifyTypes(typeDef.items.properties, {name})}`;
494+
// Allows other arbitrary properties inside of "ExecuteBatch".
495+
if (name === 'ExecuteBatch') {
496+
returnString += '[k: string]: any;';
497+
}
498+
returnString += '}[]';
470499
} else {
471500
returnString += 'Array<{[k: string]: any}>';
472501
}

0 commit comments

Comments
 (0)