UNPKG

@jsdevtools/npm-publish

Version:
74 lines 2.79 kB
import os from "node:os"; import * as errors from "./errors.js"; import { ACCESS_PUBLIC, ACCESS_RESTRICTED, STRATEGY_ALL, STRATEGY_UPGRADE, } from "./options.js"; const REGISTRY_NPM = "https://registry.npmjs.org/"; export const TAG_LATEST = "latest"; /** * Normalizes and sanitizes options, and fills-in any default values. * * @param manifest Package metadata from package.json. * @param options User-input options. * @returns Validated auth and publish configuration. */ export function normalizeOptions(manifest, options) { const defaultTag = manifest.publishConfig?.tag ?? TAG_LATEST; const defaultRegistry = manifest.publishConfig?.registry ?? REGISTRY_NPM; const defaultAccess = manifest.publishConfig?.access ?? (manifest.scope === undefined ? ACCESS_PUBLIC : undefined); const defaultProvenance = manifest.publishConfig?.provenance ?? false; return { token: validateToken(options.token), registry: validateRegistry(options.registry ?? defaultRegistry), tag: setValue(options.tag, defaultTag, validateTag), access: setValue(options.access, defaultAccess, validateAccess), provenance: setValue(options.provenance, defaultProvenance, Boolean), ignoreScripts: setValue(options.ignoreScripts, true, Boolean), dryRun: setValue(options.dryRun, false, Boolean), strategy: setValue(options.strategy, STRATEGY_ALL, validateStrategy), logger: options.logger, temporaryDirectory: options.temporaryDirectory ?? os.tmpdir(), }; } const setValue = (value, defaultValue, validate) => ({ value: validate(value ?? defaultValue), isDefault: value === undefined, }); const validateToken = (value) => { if (typeof value === "string" && value.length > 0) { return value; } throw new errors.InvalidTokenError(); }; const validateRegistry = (value) => { try { return new URL(value); } catch { throw new errors.InvalidRegistryUrlError(value); } }; const validateTag = (value) => { if (typeof value === "string") { const trimmedValue = value.trim(); const encodedValue = encodeURIComponent(trimmedValue); if (trimmedValue.length > 0 && trimmedValue === encodedValue) { return value; } } throw new errors.InvalidTagError(value); }; const validateAccess = (value) => { if (value === undefined || value === ACCESS_PUBLIC || value === ACCESS_RESTRICTED) { return value; } throw new errors.InvalidAccessError(value); }; const validateStrategy = (value) => { if (value === STRATEGY_ALL || value === STRATEGY_UPGRADE) { return value; } throw new errors.InvalidStrategyError(value); }; //# sourceMappingURL=normalize-options.js.map