UNPKG

wireit

Version:

Upgrade your npm scripts to make them smarter and more efficient

61 lines • 1.95 kB
/** * @license * Copyright 2022 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { AsyncCache } from './async-cache.js'; import { PackageJson } from './package-json.js'; import * as pathlib from 'path'; import * as fs from './fs.js'; import { parseTree } from './ast.js'; export const astKey = Symbol('ast'); /** * Reads package.json files and caches them. */ export class CachingPackageJsonReader { #cache = new AsyncCache(); #fs; constructor(filesystem = fs) { this.#fs = filesystem; } async read(packageDir) { return this.#cache.getOrCompute(packageDir, async () => { const path = pathlib.resolve(packageDir, 'package.json'); let contents; try { contents = await this.#fs.readFile(path, 'utf8'); } catch (error) { if (error.code === 'ENOENT') { return { ok: false, error: { type: 'failure', reason: 'missing-package-json', script: { packageDir }, }, }; } throw error; } const astResult = parseTree(path, contents); if (!astResult.ok) { return astResult; } const packageJsonFile = new PackageJson({ contents, path }, astResult.value); return { ok: true, value: packageJsonFile }; }); } async *getFailures() { const values = await Promise.all([...this.#cache.values]); for (const result of values) { if (!result.ok) { yield result.error; continue; } const packageJson = result.value; yield* packageJson.failures; } } } //# sourceMappingURL=package-json-reader.js.map