UNPKG

@communities-webruntime/services

Version:

If you would like to run Lightning Web Runtime without the CLI, we expose some of our programmatic APIs available in Node.js. If you're looking for the CLI documentation [you can find that here](https://www.npmjs.com/package/@communities-webruntime/cli).

119 lines 4.51 kB
"use strict"; /** * Copyright (c) 2019, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextService = void 0; require("colors"); const crypto_1 = __importDefault(require("crypto")); const api_1 = require("@webruntime/api"); const assert_1 = require("../utils/assert"); const observable_folder_hash_1 = __importDefault(require("../utils/observable-folder-hash")); const filesystem_1 = __importDefault(require("../utils/filesystem")); const webruntime_context_1 = __importDefault(require("./webruntime-context")); const { log } = console; const state = { currentContext: null, }; const startContextCallbacks = []; class ContextService extends api_1.AddressableService { constructor({ projectDir, server: { basePath = '' }, additionalProperties: communitiesConfig = {}, }) { super([]); this.projectDir = projectDir; this.basePath = basePath; this.communitiesConfig = communitiesConfig; } async initialize() { var _a, _b; await ContextService.startContext({ templateDir: this.projectDir, basePath: this.basePath, isDesignMode: (_a = this.communitiesConfig.compiler) === null || _a === void 0 ? void 0 : _a.isDesignMode, isLockerEnabled: (_b = this.communitiesConfig.locker) === null || _b === void 0 ? void 0 : _b.isEnabled, }); } async shutdown() { await ContextService.endContext(); } request() { } /** * Start the Web Runtime context and compute the template version key * based on the given configuration by computing a hash for srcDir * and watching it for changes. * * @public * @param {Object} config The template configuration */ static async startContext(config) { assert_1.assert(!state.currentContext, 'Context already started'); const context = (state.currentContext = new webruntime_context_1.default(config)); const sourcePath = context.srcDir; return observable_folder_hash_1.default(sourcePath) .then(observable => { state.folderHashSubscription = observable.subscribe({ next(hash) { // include folder mtime so that the returned hash changes // when the folder is touched, this is useful for tests const { mtime } = filesystem_1.default.statSync(sourcePath); const versionKey = crypto_1.default .createHash('md5') .update(hash + mtime) .digest('hex') .substring(0, 10); if (!('versionKey' in context) || context.versionKey !== versionKey) { log(`Created template version hash: ${versionKey}`.cyan); context.versionKey = versionKey; } }, }); }) .then(() => { // notify people waiting for the context startContextCallbacks .splice(0, startContextCallbacks.length) .forEach(callback => callback.call(null, context)); return context; }); } /** * Returns the current context * @public */ static getContext() { if (state.currentContext != null) { return state.currentContext; } throw new Error('Context not started'); } /** * Returns the current context * @public */ static async waitForContext() { if (state.currentContext) { return ContextService.getContext(); } return new Promise(resolve => { startContextCallbacks.push(resolve); }); } /** * Ends the current context and stops watching srcDir for changes. * @public */ static endContext() { if (state.folderHashSubscription) { state.folderHashSubscription.unsubscribe(); state.folderHashSubscription = null; } state.currentContext = null; } } exports.ContextService = ContextService; //# sourceMappingURL=context-service.js.map