Created
December 6, 2023 16:19
-
-
Save dmmulroy/f5083ccd3db375762a2d9acfa73d85e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Template = struct | |
let root_dir = "templates" | |
let base_dir = Node.Path.join [| root_dir; "base" |] | |
let extensions_dir = Node.Path.join [| root_dir; "extensions" |] | |
let dir_to_string = function `Base -> "./" | `Extension dir -> dir | |
module type S = sig | |
type t | |
val name : string | |
val compile : t -> (unit, string) result | |
end | |
type instance = Instance : 'a * (module S with type t = 'a) -> instance | |
module Config = struct | |
module type S = sig | |
type t | |
val dir : [ `Base | `Extension of string ] | |
val name : string | |
val to_json : t -> Js.Json.t | |
end | |
end | |
module Make (M : Config.S) : S with type t = M.t = struct | |
type t = M.t | |
let validate () = | |
let template_path = Node.Path.join [| base_dir; M.name |] in | |
let template_exists = Fs.exists template_path in | |
if not template_exists then | |
Result.error | |
@@ Printf.sprintf "Template %s does not exist" template_path | |
else Ok () | |
;; | |
let name = M.name | |
let compile template = | |
let@ _ = validate () in | |
let dir = dir_to_string M.dir in | |
let name = M.name in | |
let json = M.to_json template in | |
let@ contents = Fs.read_template ~dir name in | |
let template = Handlebars.compile contents () in | |
let compiled_contents = template json () in | |
Fs.write_template ~dir name compiled_contents | |
;; | |
end | |
end | |
module Package_json_template = Template.Make (struct | |
type t = Package_json.t | |
let name = "pakage.json.tmpl" | |
let dir = `Base | |
let to_json = Package_json.to_json | |
end) | |
module Context = struct | |
type t = { | |
configuration : Configuration.t; | |
templates : Template.instance String_map.t; | |
pkg_json : Package_json.t; | |
} | |
let make configuration = | |
let templates = | |
String_map.empty | |
|> String_map.add Package_json_template.name | |
(Template.Instance | |
(Package_json.empty, (module Package_json_template))) | |
in | |
{ configuration; templates; pkg_json = Package_json.empty } | |
;; | |
let set_pkg_json pkg_json ctx = { ctx with pkg_json } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment