bitwig-types-generator
Version:
Generated Types for Bitwig scripting API
79 lines • 3.13 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitwigApiInterface = void 0;
const BitwigApiEntity_1 = require("./BitwigApiEntity");
const Global = __importStar(require("./BitwigApiGlobal"));
const BitwigApiMethod_1 = require("./BitwigApiMethod");
const CALLBACK_REGEX = /^\w*Callback(\<\w+\>)?/;
class BitwigApiInterface extends BitwigApiEntity_1.BitwigApiEntity {
get sort() {
return CALLBACK_REGEX.test(this.declaration.value) ? 150 : 300;
}
attributes = new Set();
classes = new Set();
methods = new Map();
setPublicAttributes({ def }) {
def = def.replace("=", ":");
this.attributes.add(`readonly ${def};\n`);
}
setClasses({ def }) {
this.classes.add(def);
}
setPublicMemberFunctions({ id, def, type }) {
const method = new BitwigApiMethod_1.BitwigApiMethod(def, type);
this.methods.set(id, method);
}
setMemberFunctionDocumentation(id, desc) {
const method = this.methods.get(id);
if (!method)
throw new Error(`Unmatched method id ${id}`);
method.setDescription(desc);
}
toType() {
let body = this.description;
const match = this.declaration.value.match(CALLBACK_REGEX);
if (match) {
const methods = [...this.methods.values()];
const method = methods.shift();
// if (methods.length)
// methods.forEach((m) => console.warn(`ignored ${m.name}`));
return `type ${match[0]} = ${method?.toCallbackType() ?? "Function"};`;
}
body += `${this.declaration.type} ${this.declaration.value} {\n`;
for (const line of this.attributes.values()) {
body += line;
}
for (const method of this.methods.values()) {
body += method.toType();
}
body += "}";
if (this.classes.size) {
body += "\n\n";
body += `namespace ${this.declaration.name} {`;
[...this.classes].forEach((cl) => (body += `export type ${cl} = ${Global.namespace}.${cl};`));
body += "}";
}
return body;
}
}
exports.BitwigApiInterface = BitwigApiInterface;
//# sourceMappingURL=BitwigApiInterface.js.map