Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Rescript: How to model package.json / collection of two strings

$
0
0

Forgive the primitiveness of my question. I’m exploring Rescript and I’d like to open my package.json file into a typed datastore.

I’ve made some progress, but I need some direction:

@module("fs")
external readFileSync: (
  ~name: string,
   [#utf8],
) => string = "readFileSync"

type script = {
  name: string,
  value: string
}

type packageData = {
  name: string,
  version: string,
  scripts: array<script>,
  keywords: array<string>,
  author: option<string>,  
  license: string,
  dependencies: array<(string, string)>,
}

let file = readFileSync(~name="package.json", #utf8)
Console.log("File: " ++ file);

@scope("JSON") @val
external parseIntoMyData: string => packageData = "parse"

let result = parseIntoMyData(file)

Console.log("deps: " ++ result.scripts[0].name)

But the last line: 30 │ Console.log("deps: " ++ result.scripts[0].name) gives an error:

This has type: option<script>
But it's expected to have type: packageData

Here is the package.json content:

{
  "name": "cli",
  "version": "0.0.0",
  "scripts": {
    "res:build": "rescript",
    "res:clean": "rescript clean",
    "res:dev": "rescript -w"
  },
  "keywords": [
    "rescript"
  ],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@rescript/core": "1.4.0",
    "rescript": "11.1.1"
  }
}

I also posted this at SO but realized this forum might be the better choice.


Viewing all articles
Browse latest Browse all 2592

Trending Articles