Node
The Node filter provides a number of convenience methods and variables which make writing Node scripts feel more like traditional Ruby.
List of Transformations
`command`child_process.execSync("command", {encoding: "utf8"})ARGVprocess.argv.slice(2)Dir.chdirprocess.chdirDir.entriesfs.readdirSyncDir.exist?fs.existsSyncDir.globfs.globSync(Node 22+)Dir.homeos.homedir()Dir.mkdirfs.mkdirSyncDir.mktmpdirfs.mkdtempSyncDir.pwdprocess.cwdDir.rmdirfs.rmdirSyncDir.tmpdiros.tmpdir()ENVprocess.envFile.absolute_pathpath.resolveFile.absolute_path?path.isAbsoluteFile.basenamepath.basenameFile.chmodfs.chmodSyncFile.chownfs.chownSyncFile.directory?fs.existsSync(p) && fs.statSync(p).isDirectory()File.dirnamepath.dirnameFile.exist?fs.existsSyncFile.expand_pathpath.resolveFile.extnamepath.extnameFile.file?fs.existsSync(p) && fs.statSync(p).isFile()File.joinpath.joinFile.lchmodfs.lchmodSyncFile.linkfs.linkSyncFile.lstatfs.lstatSyncFile::PATH_SEPARATORpath.delimiterFile.readfs.readFileSyncFile.readlinkfs.readlinkSyncFile.realpathfs.realpathSyncFile.renamefs.renameSyncFile::SEPARATORpath.sepFile.statfs.statSyncFile.symlinkfs.symlinkSyncFile.truncatefs.truncateSyncFile.unlinkfs.unlinkSyncFileUtils.cdprocess.chdirFileUtils.cpfs.copyFileSyncFileUtils.lnfs.linkSyncFileUtils.ln_sfs.symlinkSyncFileUtils.mkdirfs.mkdirSyncFileUtils.mkdir_pfs.mkdirSync(path, {recursive: true})FileUtils.mvfs.renameSyncFileUtils.pwdprocess.cwdFileUtils.rmfs.unlinkSyncFileUtils.rm_rffs.rmSync(path, {recursive: true, force: true})FileUtils.rmdirfs.rmdirSyncIO.readfs.readFileSyncIO.writefs.writeFileSyncPathname#relative_path_frompath.relative(from, to)systemchild_process.execSync(..., {stdio: "inherit"})
Async Mode
By default, the Node filter generates synchronous file operations (e.g., fs.readFileSync). For async/await-based code, pass the async: true option:
Ruby2JS.convert(source, filters: [Ruby2JS::Filter::Node], async: true)
In async mode:
- File operations use
fs/promisesand are wrapped withawait File.read("foo")await fs.readFile("foo", "utf8")IO.write("foo", "bar")await fs.writeFile("foo", "bar")FileUtils.mkdir_p("foo")await fs.mkdir("foo", {recursive: true})FileUtils.rm_rf("foo")await fs.rm("foo", {recursive: true, force: true})FileUtils.cp("src", "dest")await fs.copyFile("src", "dest")Dir.entries("foo")await fs.readdir("foo")Dir.glob("**/*.rb")await Array.fromAsync(fs.glob("**/*.rb"))(Node 22+)
Note: File.exist? and Dir.exist? always use fs.existsSync (imported as fsSync in async mode) because fs/promises has no equivalent.