|
1 | | -import fs from "node:fs"; |
2 | | - |
| 1 | +import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"; |
| 2 | +import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 3 | +import * as FileSystem from "@effect/platform/FileSystem"; |
3 | 4 | import ansis from "ansis"; |
| 5 | +import * as Effect from "effect/Effect"; |
4 | 6 | import { isMatching, match, P } from "ts-pattern"; |
5 | 7 |
|
6 | | -import { glob } from "./lib/glob"; |
7 | | -import { ignores } from "./lib/ignores"; |
8 | | -import { version } from "./lib/version"; |
| 8 | +import { ignores } from "./effects/ignores"; |
| 9 | +import { version } from "./effects/version"; |
| 10 | +import { glob } from "./utils/glob"; |
9 | 11 |
|
10 | 12 | const GLOB_PACKAGE_JSON = [ |
11 | 13 | "package.json", |
12 | 14 | "packages/*/package.json", |
13 | 15 | "packages/*/*/package.json", |
14 | 16 | ]; |
15 | 17 |
|
16 | | -async function update(path: string) { |
17 | | - const packageJson = JSON.parse(fs.readFileSync(path, "utf8")); |
18 | | - if (!isMatching({ version: P.string }, packageJson)) { |
19 | | - throw new Error(`Invalid package.json at ${path}`); |
20 | | - } |
21 | | - const newVersion = version; |
22 | | - const oldVersion = match(packageJson) |
23 | | - .with({ version: P.select(P.string) }, (v) => v) |
24 | | - .otherwise(() => "0.0.0"); |
25 | | - if (oldVersion === newVersion) { |
26 | | - console.info(ansis.greenBright(`Skipping ${path} as it's already on version ${newVersion}`)); |
27 | | - return; |
28 | | - } |
29 | | - const packageJsonUpdated = { |
30 | | - ...packageJson, |
31 | | - version: newVersion, |
32 | | - }; |
33 | | - fs.writeFileSync(path, `${JSON.stringify(packageJsonUpdated, null, 2)}\n`); |
34 | | - console.info(ansis.green(`Updated ${path} to version ${packageJsonUpdated.version}`)); |
| 18 | +function update(path: string) { |
| 19 | + return Effect.gen(function*() { |
| 20 | + const fs = yield* FileSystem.FileSystem; |
| 21 | + const packageJsonText = yield* fs.readFileString(path, "utf8"); |
| 22 | + const packageJson = JSON.parse(packageJsonText); |
| 23 | + if (!isMatching({ version: P.string }, packageJson)) { |
| 24 | + yield* Effect.fail(new Error(`Invalid package.json at ${path}: invalid or missing version field`)); |
| 25 | + } |
| 26 | + const newVersion = yield* version; |
| 27 | + const oldVersion = match(packageJson) |
| 28 | + .with({ version: P.select(P.string) }, (v) => v) |
| 29 | + .otherwise(() => "0.0.0"); |
| 30 | + if (oldVersion === newVersion) { |
| 31 | + yield* Effect.log(ansis.greenBright(`Skipping ${path} as it's already on version ${newVersion}`)); |
| 32 | + return false; |
| 33 | + } |
| 34 | + const packageJsonUpdated = { |
| 35 | + ...packageJson, |
| 36 | + version: newVersion, |
| 37 | + }; |
| 38 | + yield* fs.writeFileString(path, `${JSON.stringify(packageJsonUpdated, null, 2)}\n`); |
| 39 | + yield* Effect.log(`Updated ${path} to version ${packageJsonUpdated.version}`); |
| 40 | + return true; |
| 41 | + }); |
35 | 42 | } |
36 | 43 |
|
37 | | -async function main() { |
38 | | - const tasks = glob(GLOB_PACKAGE_JSON, ignores); |
39 | | - await Promise.all(tasks.map((path) => update(path))); |
40 | | -} |
| 44 | +const program = Effect.gen(function*() { |
| 45 | + const ignorePatterns = yield* ignores; |
| 46 | + return yield* Effect.all(glob(GLOB_PACKAGE_JSON, ignorePatterns).map(update), { concurrency: 8 }); |
| 47 | +}); |
41 | 48 |
|
42 | | -await main(); |
| 49 | +program.pipe( |
| 50 | + Effect.provide(NodeFileSystem.layer), |
| 51 | + NodeRuntime.runMain, |
| 52 | +); |
0 commit comments