UNPKG

@meese-os/server

Version:
180 lines (153 loc) 4.34 kB
/** * OS.js - JavaScript Cloud/Web Desktop Platform * * Copyright (c) 2011-Present, Anders Evenrud <andersevenrud@gmail.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Anders Evenrud <andersevenrud@gmail.com> * @licence Simplified BSD License */ const path = require("path"); const chokidar = require("chokidar"); /** * TODO: typedef * @typedef {Object} PackageMetadata */ /** * Package Options * @typedef {Object} PackageOptions * @property {String} filename * @property {PackageMetadata} metadata */ /** * MeeseOS Package Abstraction */ class Package { /** * Create new instance. * @param {Core} core MeeseOS Core instance reference * @param {PackageOptions} [options={}] Instance options */ constructor(core, options = {}) { /** * @type {Core} */ this.core = core; this.script = options.metadata.server ? path.resolve(path.dirname(options.filename), options.metadata.server) : null; /** * @type {String} */ this.filename = options.filename; /** * @type {PackageMetadata} */ this.metadata = options.metadata; this.handler = null; this.watcher = null; } /** * Destroys instance. */ async destroy() { this.action("destroy"); if (this.watcher) { await this.watcher.close(); this.watcher = null; } } /** * Run method on package script. * @param {String} method Method name * @param {*} [...args] Pass arguments * @returns {Boolean} */ action(method, ...args) { try { if (this.handler && typeof this.handler[method] === "function") { this.handler[method](...args); return true; } } catch (e) { this.core.logger.warn(e); } return false; } /** * Validates this package. * @param {PackageMetadata[]} manifest Global manifest * @returns {Boolean} */ validate(manifest) { return ( this.script && this.metadata && Boolean(manifest.find((iter) => iter.name === this.metadata.name)) ); } /** * Initializes this package. * @returns {Promise<undefined>} */ init() { const mod = require(this.script); const handler = typeof mod.default === "function" ? mod.default : mod; this.handler = handler(this.core, this); if (typeof this.handler.init === "function") { return this.handler.init(); } return Promise.resolve(); } /** * Starts server scripts. * @returns {Promise<undefined>} */ start() { return this.action("start"); } /** * Creates a watch in package dist. * @param {Function} cb Callback function on watch changes * @returns {String} Watched path */ watch(cb) { const pub = this.core.config("public"); const dist = path.join(pub, "apps", this.metadata.name); this.watcher = chokidar.watch(dist); this.watcher.on("change", () => cb(this.metadata)); return dist; } /** * Resolve an URL for resource. * @param {String} path Input path * @returns {String} */ resource(path) { if (path.charAt(0) !== "/") { path = `/${path}`; } return `/apps/${this.metadata.name}${path}`; } } module.exports = Package;