UNPKG

counterfact

Version:

a library for building a fake REST API for testing

31 lines (30 loc) 1.04 kB
/* eslint-disable n/no-sync */ import { existsSync } from "node:fs"; import fs from "node:fs/promises"; import path from "node:path"; const DEFAULT_MODULE_KIND = "commonjs"; export async function determineModuleKind(modulePath) { if (modulePath.endsWith(".cjs")) { return "commonjs"; } if (modulePath.endsWith(".mjs")) { return "module"; } if (modulePath === path.parse(modulePath).root) { return DEFAULT_MODULE_KIND; } const packageJsonPath = path.join(modulePath, "package.json"); if (existsSync(packageJsonPath)) { try { const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8")); if (typeof packageJson.type !== "string") { return DEFAULT_MODULE_KIND; } return packageJson.type; } catch (error) { process.stderr.write(`Error reading or parsing package.json: ${String(error)}`); } } return await determineModuleKind(path.dirname(modulePath)); }