clasp-types
Version:
A d.ts generator for clasp projects
88 lines (87 loc) • 3.62 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.LibraryBuilder = void 0;
var Namespace_1 = require("../Namespace");
var Builder_1 = require("./Builder");
var LibraryBuilder = /** @class */ (function (_super) {
__extends(LibraryBuilder, _super);
function LibraryBuilder(kind, claspJson, packageJson) {
var _this = _super.call(this) || this;
_this.rootKind = kind;
_this.claspJson = claspJson;
_this.packageJson = packageJson;
return _this;
}
LibraryBuilder.prototype.build = function () {
var rootNamespace = new Namespace_1.Namespace(this.prepare(this.rootKind), 0);
this.append("// Type definitions for " + this.claspJson.library.name).line();
this.append("// Generated using clasp-types").doubleLine();
if (this.packageJson.dependencies) {
for (var key in this.packageJson.dependencies) {
key = key.replace('@types/', '');
console.log(key);
this.append("/// <reference types=\"" + key + "\" />").doubleLine();
}
}
rootNamespace.render(this);
this.append("declare var " + this.claspJson.library.name + ": " + this.claspJson.library.namespace + "." + this.claspJson.library.name + ";");
return this;
};
/**
* Prepare kind with library class from functions and enum
*/
LibraryBuilder.prototype.prepare = function (kind) {
kind.kindString = 'Module';
kind.flags.isPublic = true;
kind.name = this.claspJson.library.namespace;
var functions = kind.children.filter(function (kind) { return kind.flags.isPublic; }).filter(function (kind) { return kind.kindString === 'Function'; });
var library = {
name: this.claspJson.library.name,
comment: {
shortText: "The main entry point to interact with " + this.claspJson.library.name,
text: "Script ID: **" + this.claspJson.scriptId + "**"
},
kindString: 'Class',
children: functions,
flags: {
isPublic: true
},
signatures: []
};
var enums = kind.children.filter(function (kind) { return kind.flags.isPublic; }).filter(function (kind) { return kind.kindString === 'Enumeration'; });
enums.forEach(function (e) {
var property = {
name: e.name,
kindString: "Property",
flags: {
isPublic: true,
isTypeof: true
},
type: {
type: "reference",
name: e.name,
},
children: [],
signatures: [],
};
library.children.unshift(property);
});
kind.children.unshift(library);
return kind;
};
return LibraryBuilder;
}(Builder_1.Builder));
exports.LibraryBuilder = LibraryBuilder;