|
| 1 | +import chain from "@softwareventures/chain"; |
| 2 | +import {excludeFn} from "@softwareventures/array"; |
| 3 | +import {coerce, intersects, ltr} from "semver"; |
| 4 | +import {mapNullableFn} from "@softwareventures/nullable"; |
| 5 | +import {Project} from "../project/project"; |
| 6 | +import {FsStageUpdate} from "../project/update"; |
| 7 | +import {readProjectJson} from "../project/read-json"; |
| 8 | +import { |
| 9 | + allAsyncResults, |
| 10 | + bindAsyncResultFn, |
| 11 | + mapResultFn, |
| 12 | + success, |
| 13 | + toAsyncNullable |
| 14 | +} from "../result/result"; |
| 15 | +import {nodeVersionRange} from "../node/version-range"; |
| 16 | +import {noneNull} from "../collections/arrays"; |
| 17 | +import {insert} from "../fs-stage/fs-stage"; |
| 18 | +import {modifyPackageJson} from "./modify-package-json"; |
| 19 | + |
| 20 | +export async function addNewNodeVersionsToPackageJson( |
| 21 | + project: Project |
| 22 | +): Promise<FsStageUpdate | null> { |
| 23 | + const packageJson = readProjectJson(project, "package.json"); |
| 24 | + const oldVersionRange = packageJson.then( |
| 25 | + mapResultFn(packageJson => String(packageJson?.engines?.node ?? "")) |
| 26 | + ); |
| 27 | + const newVersionRange = oldVersionRange.then( |
| 28 | + mapResultFn(range => |
| 29 | + range === "" |
| 30 | + ? null |
| 31 | + : chain(project.node.currentReleases) |
| 32 | + .map(excludeFn(release => intersects(range, `^${release}`))) |
| 33 | + .map(excludeFn(release => ltr(coerce(release) ?? "0.0.0", range))) |
| 34 | + .map(nodeVersionRange).value |
| 35 | + ) |
| 36 | + ); |
| 37 | + const file = allAsyncResults([oldVersionRange, newVersionRange]).then( |
| 38 | + bindAsyncResultFn(async ([oldVersionRange, newVersionRange]) => |
| 39 | + newVersionRange == null || newVersionRange === "" |
| 40 | + ? success(null) |
| 41 | + : modifyPackageJson(project, packageJson => ({ |
| 42 | + ...packageJson, |
| 43 | + engines: { |
| 44 | + ...packageJson?.engines, |
| 45 | + node: `${oldVersionRange} || ${newVersionRange}` |
| 46 | + } |
| 47 | + })) |
| 48 | + ) |
| 49 | + ); |
| 50 | + |
| 51 | + return toAsyncNullable(allAsyncResults([newVersionRange, file])) |
| 52 | + .then(mapNullableFn(noneNull)) |
| 53 | + .then( |
| 54 | + mapNullableFn(([newVersionRange, file]) => ({ |
| 55 | + type: "fs-stage-update", |
| 56 | + log: `fix(node): declare support for node ${newVersionRange}`, |
| 57 | + apply: async stage => insert(stage, "package.json", file) |
| 58 | + })) |
| 59 | + ); |
| 60 | +} |
0 commit comments