Skip to content

Commit 70f14ad

Browse files
committed
feat(npm): add new node versions to engine.node in package.json
1 parent 09eb471 commit 70f14ad

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

collections/arrays.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {coerce, copy, findIndex} from "@softwareventures/array";
1+
import {coerce, copy, findIndex, noneNull as _noneNull} from "@softwareventures/array";
22
import {Comparator, compare, reverse} from "@softwareventures/ordered";
33
import {all, zip} from "@softwareventures/iterable";
44

@@ -35,6 +35,12 @@ export function excludeIndex<T>(array: ArrayLike<T>, index: number): T[] {
3535
return [...a.slice(0, index), ...a.slice(index + 1)];
3636
}
3737

38+
export type NoneNull<T extends ArrayLike<unknown>> = {[K in keyof T]: NonNullable<T[K]>};
39+
40+
export function noneNull<T extends ArrayLike<unknown>>(array: T): NoneNull<T> | null {
41+
return _noneNull(array) as unknown as NoneNull<T>;
42+
}
43+
3844
export function sortFn<T>(comparator: Comparator<T>): (array: ArrayLike<T>) => T[] {
3945
return array => copy(array).sort(comparator);
4046
}

npm/add-new-versions.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

project/update.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {applyCodeStyle} from "../yarn/apply-code-style";
2222
import {PrettierFixFailureReason, prettierFixFilesIfAvailable} from "../prettier/fix";
2323
import {updateFixScript} from "../npm/update-fix-script";
2424
import {updateLintScript} from "../npm/update-lint-script";
25+
import {addNewNodeVersionsToPackageJson} from "../npm/add-new-versions";
2526
import {Project} from "./project";
2627

2728
export type Update = FsStageUpdate | DirectUpdate;
@@ -52,7 +53,8 @@ export async function updateProject(project: Project): Promise<UpdateResult> {
5253
updateFixScript,
5354
applyCodeStyle,
5455
updateCopyrightYear,
55-
addMissingLicense
56+
addMissingLicense,
57+
addNewNodeVersionsToPackageJson
5658
])
5759
.map(mapAsyncFn(step(project, git)))
5860
.map(combineAsyncResults).value;

0 commit comments

Comments
 (0)