UNPKG

sharec-core

Version:

[![.github/workflows/main.yml](https://github.com/lamartire/sharec/workflows/.github/workflows/main.yml/badge.svg)](https://github.com/lamartire/sharec/actions) [![npm](https://img.shields.io/npm/v/sharec)](https://npmjs.com/sharec) ![MIT License](https:/

52 lines (38 loc) 1.23 kB
// @ts-check const { readFile } = require('sharec-utils').std const { join } = require('sharec-utils').path const { find } = require('sharec-utils').fs /** * @typedef {import('../').FlowContext} FlowContext * @typedef {import('../').Semaphore} Semaphore */ /** * @param {FlowContext} context * @param {Semaphore} semaphore * @returns {Promise<FlowContext>} */ const readCache = async (context, semaphore) => { const { targetPath, options } = context if (!options.cache) return context const cachePath = options.cache === 'include' ? join(targetPath, '.sharec/cache') : join(targetPath, 'node_modules/.cache/sharec') semaphore.start('Reading cache') try { const cachedFiles = await find(cachePath, '**/*') if (cachedFiles.length === 0) { semaphore.success("There isn't cache") return context } for (const configPath of cachedFiles) { const configKey = configPath.replace(cachePath, '').replace(/^\//, '') context.cache[configKey] = await readFile(configPath, 'utf8') } semaphore.start('Cache has been loaded') return context } catch (err) { semaphore.error("Cache hasn't been loaded") return context } } module.exports = readCache