Parse package.json
This example reads the package.json file in the current directory, validates its contents, and prints the package name and version.
Every step that can fail returns a Result, and each kind of failure is reported with its own message.
What This Example Shows
- Wrapping Node.js APIs:
readFilewrapsfs.readFilewithResult.fnand converts file system errors into aReadFileError. - Parsing and validation:
parsePackageJsonparses the text withResult.tryand validates it against a Zod schema withResult.parse. Any failure is converted into aParsePackageJsonErrorwithResult.mapError. - Composing steps: The entry point chains both steps with
Result.pipeandResult.andThen. - Handling the outcome:
Result.inspectprints the result, whileResult.inspectErrorusests-patternto print a message for each error type and sets the exit code. - Testing: The tests assert results with the matchers from
@praha/byethrow-testing.
