AScript.Lang.JavaScript.fs 0.2.0

dotnet add package AScript.Lang.JavaScript.fs --version 0.2.0
                    
NuGet\Install-Package AScript.Lang.JavaScript.fs -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AScript.Lang.JavaScript.fs" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AScript.Lang.JavaScript.fs" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="AScript.Lang.JavaScript.fs" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AScript.Lang.JavaScript.fs --version 0.2.0
                    
#r "nuget: AScript.Lang.JavaScript.fs, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AScript.Lang.JavaScript.fs@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AScript.Lang.JavaScript.fs&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=AScript.Lang.JavaScript.fs&version=0.2.0
                    
Install as a Cake Tool

AScript.Lang.JavaScript.fs

介绍

AScript.Lang.JavaScript脚本语言文件模块。

安装

install-package AScript.Lang.JavaScript.js

使用说明

  • 命名空间:using AScript.Lang.JavaScript.js;

注:服务端项目请谨慎添加文件模块,避免脚本中恶意删除、修改文件。

添加fs模块
JavaScriptLang.Instance.AddModule("fs", new JavaScriptFileSystemModule());
同步读文件
File.WriteAllText("test.txt", "hello world", System.Text.Encoding.UTF8);
string s = @"
var fs = require('fs');
fs.readFileSync('test.txt', 'utf-8')
";
var script = new Script();
script.Context.Langs = new[] { "js" };
var result = script.Eval(s);
Assert.AreEqual("hello world", result);
异步读文件
File.WriteAllText("test.txt", "async content", System.Text.Encoding.UTF8);
string s = @"
var fs = require('fs');
await fs.readFile('test.txt', 'utf-8')
";
var script = new Script();
script.Context.Langs = new[] { "js" };
var result = await script.EvalAsync<string>(s);
Assert.AreEqual("async content", result);
同步写文件
string s = @"
var fs = require('fs');
fs.writeFileSync('test.txt', 'hello world', 'utf-8');
true
";
var script = new Script();
script.Context.Langs = new[] { "js" };
var result = script.Eval(s);
Assert.AreEqual(true, result);
Assert.AreEqual("hello world", File.ReadAllText("test.txt", System.Text.Encoding.UTF8));
异步写文件
string s = @"
var fs = require('fs');
await fs.writeFile('test.txt', 'async write', 'utf-8');
true
";
var script = new Script();
script.Context.Langs = new[] { "js" };
var result = await script.EvalAsync<bool>(s);
Assert.AreEqual(true, result);
Assert.AreEqual("async write", File.ReadAllText("test.txt", System.Text.Encoding.UTF8));
其他方法
var fs = require('fs');
fs.appendFileSync('test.txt', 'append content', 'utf-8'); // 同步追加文件
fs.appendFile('test.txt', 'append context', 'utf-8'); // 异步追加文件
fs.copyFileSync('test.txt', 'test_copy.txt'); // 同步拷贝文件
fs.copyFile('test.txt', 'test_copy.txt'); // 异步拷贝文件
fs.unlinkSync('test.txt'); // 同步删除文件
fs.unlink('test.txt'); // 异步删除文件
var reader = fs.createReadStream('test.txt'); // 读文件流
var writer = fs.createWriteStream('test2.txt'); // 写文件流
reader.pipe(writer); // 读取文件流并写入目标文件流
// 读文件流事件:open/data/error/end/close
reader.on('data', function(chunk) {
    console.log(chunk);
});
reader.on('end', function() {
    console.log('end');
});
// 写文件流事件:open/error/finish/close
writer.on('open', ()=>console.log('open'));
writer.on('close', ()=>console.log('close'));
writer.write('hello');
writer.end();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 107 8/26/2026
0.1.0 110 8/18/2026
0.0.2 107 8/12/2026
0.0.1 125 7/14/2026

AScript1.7.0