UNPKG

@jsdevtools/npm-publish

Version:
112 lines 3.66 kB
import fs from "node:fs/promises"; import path from "node:path"; import semverValid from "semver/functions/valid.js"; import { list as tarList } from "tar/list"; import validatePackageName from "validate-npm-package-name"; import * as errors from "./errors.js"; const SCOPE_RE = /^(@.+)\/.+$/u; const MANIFEST_BASENAME = "package.json"; const TARBALL_EXTNAME = ".tgz"; const isManifest = (file) => { return typeof file === "string" && path.basename(file) === MANIFEST_BASENAME; }; const isDirectory = (file) => { return typeof file === "string" && path.extname(file) === ""; }; const isTarball = (file) => { return typeof file === "string" && path.extname(file) === TARBALL_EXTNAME; }; const normalizeVersion = (version) => { return semverValid(version) ?? undefined; }; const validateName = (name) => { return validatePackageName(name).validForNewPackages; }; const readPackageJson = async (...pathSegments) => { const file = path.resolve(...pathSegments); try { return await fs.readFile(file, "utf8"); } catch (error) { throw new errors.PackageJsonReadError(file, error); } }; const readTarballPackageJson = async (file) => { const data = []; const onReadEntry = (entry) => { if (entry.path === "package/package.json") { entry.on("data", (chunk) => data.push(chunk)); } }; try { await tarList({ file, onReadEntry }); if (data.length === 0) { throw new Error("package.json not found inside archive"); } } catch (error) { throw new errors.PackageTarballReadError(file, error); } return Buffer.concat(data).toString(); }; /** * Reads the package manifest (package.json) and returns its parsed contents. * * @param packagePath The path to the package being published. * @returns The parsed package metadata. */ export async function readManifest(packagePath) { let packageSpec; let manifestContents; if (!packagePath) { packageSpec = ""; manifestContents = await readPackageJson(MANIFEST_BASENAME); } else if (isManifest(packagePath)) { packageSpec = path.resolve(path.dirname(packagePath)); manifestContents = await readPackageJson(packagePath); } else if (isDirectory(packagePath)) { packageSpec = path.resolve(packagePath); manifestContents = await readPackageJson(packagePath, MANIFEST_BASENAME); } else if (isTarball(packagePath)) { packageSpec = path.resolve(packagePath); manifestContents = await readTarballPackageJson(packageSpec); } else { throw new errors.InvalidPackageError(packagePath); } let manifestJson; let name; let version; let publishConfig; try { manifestJson = JSON.parse(manifestContents); name = manifestJson.name; version = normalizeVersion(manifestJson.version); publishConfig = manifestJson.publishConfig ?? {}; } catch (error) { throw new errors.PackageJsonParseError(packageSpec, error); } if (!validateName(name)) { throw new errors.InvalidPackageNameError(name); } if (typeof version !== "string") { throw new errors.InvalidPackageVersionError(manifestJson.version); } if (typeof publishConfig !== "object" || Array.isArray(publishConfig) || !publishConfig) { throw new errors.InvalidPackagePublishConfigError(publishConfig); } return { packageSpec, name, version, publishConfig, scope: SCOPE_RE.exec(name)?.[1], }; } //# sourceMappingURL=read-manifest.js.map