| 76507 |      1 | /*  Title:      Pure/System/nodejs.scala
 | 
|  |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for the Node.js platform, as provided by Isabelle/VSCodium component.
 | 
|  |      5 | 
 | 
|  |      6 | See also: https://nodejs.org/docs/latest-v16.x
 | 
|  |      7 | */
 | 
|  |      8 | 
 | 
|  |      9 | package isabelle
 | 
|  |     10 | 
 | 
|  |     11 | 
 | 
|  |     12 | object Nodejs {
 | 
|  |     13 |   /* require modules */
 | 
|  |     14 | 
 | 
|  |     15 |   def require_module(name: JS.Source, module: JS.Source): JS.Source =
 | 
| 76510 |     16 |     name + " = require(" + module + ")"
 | 
| 76507 |     17 | 
 | 
|  |     18 |   def require_path(name: JS.Source, path: Path, dir: Boolean = false): JS.Source =
 | 
|  |     19 |     require_module(name, JS.platform_path(path, dir = dir))
 | 
|  |     20 | 
 | 
| 76510 |     21 |   def require_builtin(name: String): JS.Source =
 | 
|  |     22 |     require_module("const " + name, JS.string(name))
 | 
|  |     23 | 
 | 
| 76507 |     24 | 
 | 
|  |     25 |   /* file-system operations */
 | 
|  |     26 | 
 | 
|  |     27 |   def require_fs: JS.Source = require_builtin("fs")
 | 
|  |     28 | 
 | 
|  |     29 |   val encoding_utf8: JSON.T = JSON.Object("encoding" -> "utf8")
 | 
|  |     30 | 
 | 
|  |     31 |   def read_file(path: Path): JS.Source =
 | 
|  |     32 |     JS.function("fs.readFileSync", JS.platform_path(path), JS.value(encoding_utf8))
 | 
|  |     33 | 
 | 
|  |     34 |   def write_file(path: Path, arg: JS.Source): JS.Source =
 | 
|  |     35 |     JS.function("fs.writeFileSync", JS.platform_path(path), arg, JS.value(encoding_utf8))
 | 
|  |     36 | 
 | 
|  |     37 | 
 | 
|  |     38 |   /* external process */
 | 
|  |     39 | 
 | 
|  |     40 |   def execute(js: String): Process_Result =
 | 
|  |     41 |     Isabelle_System.bash("isabelle node -", input = js,
 | 
|  |     42 |       description = "Node.js").check
 | 
|  |     43 | }
 |