reclass-doc
Version:
Reclass model documentation generator.
180 lines (179 loc) • 6.58 kB
JavaScript
;
/**
* Reclass doc generator
*
* @author Jiri Hybek <jiri@hybek.cz>
* @license Apache-2.0 (c) 2017 Jiri Hybek
*/
var __extends = (this && this.__extends) || (function () {
var 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 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 });
var fs = require("fs");
var path = require("path");
var events_1 = require("events");
var Inventory_1 = require("./Inventory");
var Renderer_1 = require("./Renderer");
var Server_1 = require("./Server");
/**
* Application class
*/
var ReclassDoc = (function (_super) {
__extends(ReclassDoc, _super);
/**
* Constructor
*
* @param config ReclassDoc configuration
* @param logger Logger instance
*/
function ReclassDoc(config, logger) {
var _this = _super.call(this) || this;
/** Server instance */
_this.server = null;
_this.watchLock = false;
//Set default configuration
config.outputDir = config.outputDir || (config.reclassDir + "/doc");
config.classDir = config.classDir || '/classes';
config.nodeDir = config.nodeDir || '/nodes';
config.mediaSrcDir = config.mediaSrcDir || (config.reclassDir + "/util/media");
config.mediaOutDir = config.mediaOutDir || "/media";
config.assetsSrcDir = config.assetsSrcDir || "/assets/dist";
config.assetsOutDir = config.assetsOutDir || "/assets";
config.templateDir = config.templateDir || path.resolve(__dirname + "/../template");
config.title = config.title || 'Reclass Reference';
config.serverPort = config.serverPort || 8080;
config.pugOptions = config.pugOptions || { pretty: true };
config.globals = config.globals || {};
config.logoUrl = config.logoUrl || null;
if (config.watchReclass === undefined)
config.watchReclass = true;
if (config.watchMedia === undefined)
config.watchMedia = true;
if (config.watchTemplate === undefined)
config.watchTemplate = true;
if (config.startServer)
config.globals['_watchChanges'] = true;
//Init logger
_this.logger = logger;
_this.logger.debug("Using configuration:", config);
//Init inventory
_this.inventory = new Inventory_1.Inventory({
reclassDir: config.reclassDir,
classesDir: config.classDir,
nodesDir: config.nodeDir
}, _this.logger.facility("Inventory"));
//Init renderer
_this.renderer = new Renderer_1.Renderer({
outputDir: config.outputDir,
nodesDir: config.nodeDir,
classesDir: config.classDir,
mediaSrcDir: config.mediaSrcDir,
mediaOutDir: config.mediaOutDir,
assetsSrcDir: config.assetsSrcDir,
assetsOutDir: config.assetsOutDir,
templateDir: config.templateDir,
globals: config.globals,
options: config.pugOptions,
title: config.title,
logoUrl: config.logoUrl
}, _this.inventory, _this.logger.facility("Renderer"));
//Setup server?
if (config.startServer) {
_this.server = new Server_1.Server({
outputDir: config.outputDir,
port: config.serverPort
}, _this.logger.facility("Server"));
}
//Start watching changes?
if (config.watch) {
if (config.watchReclass) {
_this.watch(config.reclassDir + config.nodeDir);
_this.watch(config.reclassDir + config.classDir);
if (fs.existsSync(config.reclassDir + "/README.md"))
_this.watch(config.reclassDir + "/README.md");
if (fs.existsSync(config.reclassDir + "/readme.md"))
_this.watch(config.reclassDir + "/readme.md");
}
if (config.watchTemplate)
_this.watch(config.templateDir);
if (config.watchMedia)
_this.watch(config.mediaSrcDir);
}
return _this;
}
/**
* Starts application
*/
ReclassDoc.prototype.start = function () {
this.emit("start");
//Start server
if (this.server)
this.server.start();
//Build documentation
this.build();
};
/**
* Builds documentation
*/
ReclassDoc.prototype.build = function () {
this.logger.info("Building documentation...");
//Load inventory
this.logger.debug("Loading inventory...");
this.inventory.load();
var index = this.inventory.getIndex();
var treeFinger = this.inventory.getTreeIndex();
//Render documentation
this.logger.debug("Rendering...");
this.renderer.render(index, treeFinger);
//Update server modification time
if (this.server)
this.server.setModified();
this.emit("build");
};
/**
* Start watching for changes
*
* @param path Filename or directory to watch
*/
ReclassDoc.prototype.watch = function (path) {
var _this = this;
this.logger.info("Watching for changes of directory '" + path + "'...");
var stat = fs.statSync(path);
if (stat && stat.isDirectory()) {
fs.watch(path, { recursive: true }, function () {
_this.watchTrigger();
});
}
else {
fs.watch(path, function () {
_this.watchTrigger();
});
}
};
/**
* Called when watches items has changed
*/
ReclassDoc.prototype.watchTrigger = function () {
var _this = this;
this.logger.debug("Watch triggered.");
if (this.watchLock)
return;
this.watchLock = true;
this.logger.info("Some files changed, rebuilding...");
setTimeout(function () {
_this.watchLock = false;
_this.logger.debug("Watch lock released.");
}, 1);
this.build();
this.emit("watchChange");
};
return ReclassDoc;
}(events_1.EventEmitter));
exports.ReclassDoc = ReclassDoc;