|
| 1 | +#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-env --allow-net |
| 2 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 3 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 4 | +// |
| 5 | +// rescript-deno — Start ReScript projects with Deno, not npm. |
| 6 | +// |
| 7 | +// Wraps the ReScript compiler to work natively with Deno's module system. |
| 8 | +// No npm, no node_modules (unless --node-modules-dir is needed for ReScript). |
| 9 | +// |
| 10 | +// Usage: |
| 11 | +// deno run -A jsr:@hyperpolymath/rescript-deno init my-app |
| 12 | +// deno run -A jsr:@hyperpolymath/rescript-deno build |
| 13 | +// deno run -A jsr:@hyperpolymath/rescript-deno dev |
| 14 | +// deno run -A jsr:@hyperpolymath/rescript-deno clean |
| 15 | + |
| 16 | +const VERSION = "1.0.0"; |
| 17 | + |
| 18 | +// --- Templates --- |
| 19 | + |
| 20 | +const DENO_JSON_TEMPLATE = `{ |
| 21 | + "imports": { |
| 22 | + "@rescript/core": "npm:@rescript/core@1.6.1", |
| 23 | + "@rescript/runtime": "npm:@rescript/runtime@12.2.0", |
| 24 | + "rescript": "npm:rescript@12.2.0" |
| 25 | + }, |
| 26 | + "tasks": { |
| 27 | + "res:build": "deno run --node-modules-dir=auto --allow-read --allow-write --allow-env --allow-run npm:rescript@12.2.0", |
| 28 | + "res:dev": "deno run --node-modules-dir=auto --allow-read --allow-write --allow-env --allow-run npm:rescript@12.2.0 -w", |
| 29 | + "res:clean": "deno run --node-modules-dir=auto --allow-read --allow-write --allow-env --allow-run npm:rescript@12.2.0 clean", |
| 30 | + "dev": "deno task res:dev", |
| 31 | + "build": "deno task res:build" |
| 32 | + } |
| 33 | +} |
| 34 | +`; |
| 35 | + |
| 36 | +const RESCRIPT_JSON_TEMPLATE = `{ |
| 37 | + "name": "{{NAME}}", |
| 38 | + "sources": [{"dir": "src", "subdirs": true}], |
| 39 | + "package-specs": [{"module": "esmodule", "in-source": true}], |
| 40 | + "suffix": ".res.mjs", |
| 41 | + "compiler-flags": ["-open RescriptCore"], |
| 42 | + "dependencies": ["@rescript/core"] |
| 43 | +} |
| 44 | +`; |
| 45 | + |
| 46 | +const MAIN_RES_TEMPLATE = `// SPDX-License-Identifier: PMPL-1.0-or-later |
| 47 | +// {{NAME}} — entry point |
| 48 | +
|
| 49 | +let greeting = "Hello from ReScript on Deno!" |
| 50 | +
|
| 51 | +Console.log(greeting) |
| 52 | +Console.log("No npm required.") |
| 53 | +`; |
| 54 | + |
| 55 | +const GITIGNORE_TEMPLATE = `node_modules/ |
| 56 | +.deno/ |
| 57 | +*.res.mjs |
| 58 | +lib/ |
| 59 | +`; |
| 60 | + |
| 61 | +// --- Commands --- |
| 62 | + |
| 63 | +async function init(name) { |
| 64 | + if (!name) { |
| 65 | + console.error("Usage: rescript-deno init <project-name>"); |
| 66 | + Deno.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + console.log(`Creating ReScript + Deno project: ${name}`); |
| 70 | + |
| 71 | + await Deno.mkdir(`${name}/src`, { recursive: true }); |
| 72 | + |
| 73 | + await Deno.writeTextFile(`${name}/deno.json`, DENO_JSON_TEMPLATE); |
| 74 | + await Deno.writeTextFile( |
| 75 | + `${name}/rescript.json`, |
| 76 | + RESCRIPT_JSON_TEMPLATE.replaceAll("{{NAME}}", name) |
| 77 | + ); |
| 78 | + await Deno.writeTextFile( |
| 79 | + `${name}/src/Main.res`, |
| 80 | + MAIN_RES_TEMPLATE.replaceAll("{{NAME}}", name) |
| 81 | + ); |
| 82 | + await Deno.writeTextFile(`${name}/.gitignore`, GITIGNORE_TEMPLATE); |
| 83 | + |
| 84 | + console.log(` |
| 85 | +Done! Next steps: |
| 86 | +
|
| 87 | + cd ${name} |
| 88 | + deno task build # compile ReScript |
| 89 | + deno run src/Main.res.mjs # run it |
| 90 | +
|
| 91 | +No npm install needed. Deno handles everything. |
| 92 | +`); |
| 93 | +} |
| 94 | + |
| 95 | +async function build() { |
| 96 | + console.log("[rescript-deno] Building..."); |
| 97 | + const cmd = new Deno.Command("deno", { |
| 98 | + args: [ |
| 99 | + "run", |
| 100 | + "--node-modules-dir=auto", |
| 101 | + "--allow-read", |
| 102 | + "--allow-write", |
| 103 | + "--allow-env", |
| 104 | + "--allow-run", |
| 105 | + "npm:rescript@12.2.0", |
| 106 | + ], |
| 107 | + stdout: "inherit", |
| 108 | + stderr: "inherit", |
| 109 | + }); |
| 110 | + const { code } = await cmd.output(); |
| 111 | + Deno.exit(code); |
| 112 | +} |
| 113 | + |
| 114 | +async function dev() { |
| 115 | + console.log("[rescript-deno] Starting watch mode..."); |
| 116 | + const cmd = new Deno.Command("deno", { |
| 117 | + args: [ |
| 118 | + "run", |
| 119 | + "--node-modules-dir=auto", |
| 120 | + "--allow-read", |
| 121 | + "--allow-write", |
| 122 | + "--allow-env", |
| 123 | + "--allow-run", |
| 124 | + "npm:rescript@12.2.0", |
| 125 | + "-w", |
| 126 | + ], |
| 127 | + stdout: "inherit", |
| 128 | + stderr: "inherit", |
| 129 | + }); |
| 130 | + const { code } = await cmd.output(); |
| 131 | + Deno.exit(code); |
| 132 | +} |
| 133 | + |
| 134 | +async function clean() { |
| 135 | + console.log("[rescript-deno] Cleaning build artifacts..."); |
| 136 | + const cmd = new Deno.Command("deno", { |
| 137 | + args: [ |
| 138 | + "run", |
| 139 | + "--node-modules-dir=auto", |
| 140 | + "--allow-read", |
| 141 | + "--allow-write", |
| 142 | + "--allow-env", |
| 143 | + "--allow-run", |
| 144 | + "npm:rescript@12.2.0", |
| 145 | + "clean", |
| 146 | + ], |
| 147 | + stdout: "inherit", |
| 148 | + stderr: "inherit", |
| 149 | + }); |
| 150 | + const { code } = await cmd.output(); |
| 151 | + Deno.exit(code); |
| 152 | +} |
| 153 | + |
| 154 | +// --- Main --- |
| 155 | + |
| 156 | +const command = Deno.args[0]; |
| 157 | + |
| 158 | +switch (command) { |
| 159 | + case "init": |
| 160 | + await init(Deno.args[1]); |
| 161 | + break; |
| 162 | + case "build": |
| 163 | + await build(); |
| 164 | + break; |
| 165 | + case "dev": |
| 166 | + await dev(); |
| 167 | + break; |
| 168 | + case "clean": |
| 169 | + await clean(); |
| 170 | + break; |
| 171 | + case "--version": |
| 172 | + case "-v": |
| 173 | + console.log(`rescript-deno ${VERSION}`); |
| 174 | + break; |
| 175 | + case "--help": |
| 176 | + case "-h": |
| 177 | + case undefined: |
| 178 | + console.log(` |
| 179 | +rescript-deno ${VERSION} — ReScript on Deno, no npm required |
| 180 | +
|
| 181 | +Commands: |
| 182 | + init <name> Create a new ReScript + Deno project |
| 183 | + build Compile ReScript sources |
| 184 | + dev Watch mode (recompile on change) |
| 185 | + clean Remove build artifacts |
| 186 | + --version Show version |
| 187 | + --help Show this help |
| 188 | +
|
| 189 | +Example: |
| 190 | + rescript-deno init my-app |
| 191 | + cd my-app |
| 192 | + deno task build |
| 193 | + deno run src/Main.res.mjs |
| 194 | +`); |
| 195 | + break; |
| 196 | + default: |
| 197 | + console.error(`Unknown command: ${command}. Use --help for usage.`); |
| 198 | + Deno.exit(1); |
| 199 | +} |
0 commit comments