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).

67 lines 2.13 kB
/** @hidden */ /** * 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 */ import { ContextService } from '../context/context-service.js'; import * as fs from '../utils/filesystem.js'; import { LoadingCache } from './loading-cache.js'; const { getContext } = ContextService; const cache = new LoadingCache(); /** * Reads a file synchronously, caching it for * the current value of the `versionKey`. * * @param {*} path The path of the file to read * @returns The file content */ function readFile(path) { const { versionKey } = getContext(); try { return cache.get(`${path}@${versionKey}`, () => { return fs.readFileSync(path, 'utf8'); }); } catch { throw new Error(`Failed to read file: ${path}`); } } /** * Reads a JSON file synchronously, caching it for * the current value of the `versionKey`. * * @param {*} path The path of the file to read. * @returns The file content as a JSON object */ export function readJsonFile(path) { return JSON.parse(readFile(path)); } /** * Reads a folder's content synchronously, caching it for * the current value of the `versionKey`. * * @param {*} path The path of the folder to read. * @param {*} extension The extension of the files to read. * @returns Map of the file name and the file content */ export function readFolder(path, extension) { const { versionKey } = getContext(); return cache.get(`${path}@${versionKey}`, () => { try { return fs.readdirSync(path).reduce((result, fileName) => { if (!fileName.endsWith(extension)) { return result; } result[fileName] = fs.readFileSync(`${path}/${fileName}`, 'utf8'); return result; }, {}); } catch { // path does not exist, return empty; faster than checking whether path exists. return {}; } }); } //# sourceMappingURL=files.js.map