@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
1 lines • 7.68 kB
Source Map (JSON)
{"version":3,"file":"semver-fns.mjs","names":[],"sources":["../src/semver-fns.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { CoerceOptions, Range, SemVer } from \"semver\";\nimport { coerce, inc, parse, satisfies, valid, validRange } from \"semver\";\n\nexport type ReleaseType =\n | \"major\"\n | \"premajor\"\n | \"minor\"\n | \"preminor\"\n | \"patch\"\n | \"prepatch\"\n | \"prerelease\"\n | \"release\";\n\nexport const RELEASE_TYPES: ReleaseType[] = [\n \"major\",\n \"premajor\",\n \"minor\",\n \"preminor\",\n \"patch\",\n \"prepatch\",\n \"prerelease\",\n \"release\"\n];\n\n/**\n * Parse a semver string into a SemVer object\n *\n * @param semver - The semver string to parse\n * @param loose - Whether to use loose parsing\n * @returns The parsed SemVer object\n */\nexport const parseVersion = (semver: string, loose = true) =>\n parse(semver, loose);\n\n/**\n * Coerce a version string into a valid SemVer string\n *\n * @param version - The version string or number or {@link SemVer} to coerce\n * @param options - Options to use when coercing the version\n * @returns The coerced SemVer string or null if invalid\n */\nexport const coerceVersion = (\n version: string | number | SemVer | null | undefined,\n options?: CoerceOptions\n): SemVer | null => {\n return coerce(version, options);\n};\n\n/**\n * Type check for {@link SemVer}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link SemVer}\n */\nexport const isSemver = (val: any): val is SemVer => {\n return isObject(val) && \"version\" in val;\n};\n\n/**\n * Type check for {@link Range}\n *\n * @param val - The value to check\n * @returns Whether the value is a valid {@link Range}\n */\nexport const isRange = (val: any): val is Range => {\n return isObject(val) && \"range\" in val;\n};\n\n/**\n * Check if a {@link SemVer} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isSemver} function.\n *\n * @param semver - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidSemver = (semver: any, loose = true): boolean => {\n return (\n (isString(semver) || isSemver(semver)) && valid(semver, loose) !== null\n );\n};\n\n/**\n * Check if a {@link Range} string is valid\n *\n * @remarks\n * If you're looking for type checking, please use the {@link isRange} function.\n *\n * @param range - The range string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the range string is valid\n */\nexport const isValidRange = (range: any, loose = true): boolean => {\n return (\n (isString(range) || isRange(range)) && validRange(range, loose) !== null\n );\n};\n\n/**\n * Check if a {@link SemVer} or {@link Range} string is valid\n *\n * @param version - The semver string to check\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string is valid\n */\nexport const isValidVersion = (\n version: string | SemVer | Range | null | undefined,\n loose = true\n) => {\n return isValidSemver(version, loose) || isValidRange(version, loose);\n};\n\n/**\n * Check if a semver string satisfies a range\n *\n * @param version - The semver string to check\n * @param range - The range to check against\n * @param loose - Whether to use loose parsing\n * @returns Whether the semver string satisfies the range\n */\nexport const satisfiesVersion = (\n version: string | SemVer | null | undefined,\n range: string | Range | null | undefined,\n loose = true\n) => {\n if (\n !version ||\n !range ||\n !isValidSemver(version, loose) ||\n !isValidRange(range, loose)\n ) {\n return false;\n }\n\n return satisfies(version, range, { loose });\n};\n\n/**\n * Check if a string is a valid relative version keyword\n *\n * @param val - The string to check\n * @returns Whether the string is a valid relative version keyword\n */\nexport const isRelativeVersionKeyword = (val: string): val is ReleaseType => {\n return RELEASE_TYPES.includes(val as ReleaseType);\n};\n\n/**\n * Derive a new semver version from the current version and a version specifier\n *\n * @param currentSemverVersion - The current semver version\n * @param semverSpecifier - The semver specifier to use\n * @param preid - The pre-release identifier to use\n * @returns The derived new semver version\n */\nexport const deriveNewSemverVersion = (\n currentSemverVersion: string,\n semverSpecifier: string,\n preid?: string\n) => {\n if (!valid(currentSemverVersion)) {\n throw new Error(\n `Invalid semver version \"${currentSemverVersion}\" provided.`\n );\n }\n\n let newVersion = semverSpecifier;\n\n if (isRelativeVersionKeyword(semverSpecifier)) {\n // Derive the new version from the current version combined with the new version specifier.\n const derivedVersion = inc(currentSemverVersion, semverSpecifier, preid!);\n\n if (!derivedVersion) {\n throw new Error(\n `Unable to derive new version from current version \"${currentSemverVersion}\" and version specifier \"${semverSpecifier}\"`\n );\n }\n newVersion = derivedVersion;\n } else if (!valid(semverSpecifier)) {\n // Ensure the new version specifier is a valid semver version, given it is not a valid semver keyword\n throw new Error(\n `Invalid semver version specifier \"${semverSpecifier}\" provided. Please provide either a valid semver version or a valid semver version keyword.`\n );\n }\n\n return newVersion;\n};\n"],"mappings":";;;;;AAiCA,MAAa,gBAA+B;CAC1C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;;;;;;;AASD,MAAa,gBAAgB,QAAgB,QAAQ,SACnD,MAAM,QAAQ,MAAM;;;;;;;;AAStB,MAAa,iBACX,SACA,YACkB;AAClB,QAAO,OAAO,SAAS,QAAQ;;;;;;;;AASjC,MAAa,YAAY,QAA4B;AACnD,QAAO,SAAS,IAAI,IAAI,aAAa;;;;;;;;AASvC,MAAa,WAAW,QAA2B;AACjD,QAAO,SAAS,IAAI,IAAI,WAAW;;;;;;;;;;;;AAarC,MAAa,iBAAiB,QAAa,QAAQ,SAAkB;AACnE,SACG,SAAS,OAAO,IAAI,SAAS,OAAO,KAAK,MAAM,QAAQ,MAAM,KAAK;;;;;;;;;;;;AAcvE,MAAa,gBAAgB,OAAY,QAAQ,SAAkB;AACjE,SACG,SAAS,MAAM,IAAI,QAAQ,MAAM,KAAK,WAAW,OA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