0 people like it.

Use node.js packages from F#

For example executable, install NPM and stylus package.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
open System.IO
open System.Diagnostics

let RunNodeJS nodePath initial (script:string) =
   let info = ProcessStartInfo(
               fileName = "node.exe",
               Arguments = sprintf "-e \"%s\"" initial,
               RedirectStandardError  = true,
               RedirectStandardOutput = true,
               RedirectStandardInput  = true,
               UseShellExecute        = false,
               CreateNoWindow         = true,
               WorkingDirectory = nodePath)
   use proc = new Process(StartInfo = info)
   proc.Start() |> ignore 
   proc.StandardInput.Write script
   proc.StandardInput.Flush()
   proc.StandardInput.Close()
   let output = proc.StandardOutput.ReadToEnd()
   proc.WaitForExit()
   if proc.ExitCode <> 0 then
      failwith ("The execution of the command failed: \r\n" + proc.StandardError.ReadToEnd())
   output

// usage example: stylus, css preprocessor

let RunStylus = 
   RunNodeJS @"C:\Program Files\nodejs\node_modules\npm\" 
        "var stylus = require('stylus');
         var stdin = process.openStdin();
         stdin.setEncoding('utf8');
         var acc = '';
         stdin.on('data', function (chunk) {
            acc = acc + chunk;
         });
         stdin.on('end', function () {
            stylus(acc, {}).render(function (err, res) {
               if (err) throw err;
               console.log(res);
            });
         });"

RunStylus "
#prompt
   position absolute
   top 150px
   left 50%
   width 200px
   margin-left -(@width / 2)"
|> printf "%s"
namespace System
namespace System.IO
namespace System.Diagnostics
val RunNodeJS : nodePath:string -> initial:string -> script:string -> string

Full name: Script.RunNodeJS
val nodePath : string
val initial : string
val script : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val info : ProcessStartInfo
Multiple items
type ProcessStartInfo =
  new : unit -> ProcessStartInfo + 2 overloads
  member Arguments : string with get, set
  member CreateNoWindow : bool with get, set
  member Domain : string with get, set
  member EnvironmentVariables : StringDictionary
  member ErrorDialog : bool with get, set
  member ErrorDialogParentHandle : nativeint with get, set
  member FileName : string with get, set
  member LoadUserProfile : bool with get, set
  member Password : SecureString with get, set
  ...

Full name: System.Diagnostics.ProcessStartInfo

--------------------
ProcessStartInfo() : unit
ProcessStartInfo(fileName: string) : unit
ProcessStartInfo(fileName: string, arguments: string) : unit
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val proc : Process
Multiple items
type Process =
  inherit Component
  new : unit -> Process
  member BasePriority : int
  member BeginErrorReadLine : unit -> unit
  member BeginOutputReadLine : unit -> unit
  member CancelErrorRead : unit -> unit
  member CancelOutputRead : unit -> unit
  member Close : unit -> unit
  member CloseMainWindow : unit -> bool
  member EnableRaisingEvents : bool with get, set
  member ExitCode : int
  ...

Full name: System.Diagnostics.Process

--------------------
Process() : unit
Process.Start() : bool
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
property Process.StandardInput: StreamWriter
TextWriter.Write(value: obj) : unit
   (+0 other overloads)
TextWriter.Write(value: decimal) : unit
   (+0 other overloads)
TextWriter.Write(value: float) : unit
   (+0 other overloads)
TextWriter.Write(value: float32) : unit
   (+0 other overloads)
TextWriter.Write(value: uint64) : unit
   (+0 other overloads)
TextWriter.Write(value: int64) : unit
   (+0 other overloads)
TextWriter.Write(value: uint32) : unit
   (+0 other overloads)
TextWriter.Write(value: int) : unit
   (+0 other overloads)
TextWriter.Write(value: bool) : unit
   (+0 other overloads)
StreamWriter.Write(value: string) : unit
   (+0 other overloads)
StreamWriter.Flush() : unit
StreamWriter.Close() : unit
val output : string
property Process.StandardOutput: StreamReader
StreamReader.ReadToEnd() : string
Process.WaitForExit() : unit
Process.WaitForExit(milliseconds: int) : bool
property Process.ExitCode: int
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
property Process.StandardError: StreamReader
val RunStylus : (string -> string)

Full name: Script.RunStylus
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf

More information

Link:http://fssnip.net/l2
Posted:11 years ago
Author:Sergey Kaspar
Tags: web , nodejs