Skip to content

Commit 4366213

Browse files
committed
R and Python loaders
1 parent 4c6e961 commit 4366213

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

src/dataloader.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import {maybeStat, prepareOutput} from "./files.js";
66

77
const runningCommands = new Map<string, Promise<string>>();
88

9+
const languages = {
10+
".js": "node",
11+
".ts": "tsx",
12+
".py": "python3",
13+
".r": "RScript",
14+
".R": "RScript",
15+
".sh": "sh",
16+
".exe": null
17+
};
18+
919
export class Loader {
1020
/**
1121
* The command to run, such as "node" for a JavaScript loader, "tsx" for
@@ -52,13 +62,13 @@ export class Loader {
5262
* source root, if it exists. If there is no such loader, returns undefined.
5363
*/
5464
static find(sourceRoot: string, targetPath: string): Loader | undefined {
55-
for (const ext of [".js", ".ts", ".sh"]) {
65+
for (const ext in languages) {
5666
const sourcePath = targetPath + ext;
5767
const path = join(sourceRoot, sourcePath);
5868
if (!existsSync(path)) continue;
5969
return new Loader({
60-
command: ext === ".js" ? "node" : ext === ".ts" ? "tsx" : "sh",
61-
args: [path],
70+
command: languages[ext] ?? path,
71+
args: languages[ext] == null ? [] : [path],
6272
path,
6373
sourceRoot,
6474
targetPath

test/dataloaders-test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ describe("data loaders are called with the appropriate command", () => {
1313
const out = await loader.load({verbose: false});
1414
assert.strictEqual(await readFile("test/" + out, "utf-8"), "tsx\n");
1515
});
16-
it("a .sh data loader is called as a shell script", async () => {
16+
it("a .sh data loader is called with sh", async () => {
1717
const loader = Loader.find("test", "dataloaders/data3.txt")!;
1818
const out = await loader.load({verbose: false});
1919
assert.strictEqual(await readFile("test/" + out, "utf-8"), "shell\n");
2020
});
21+
it("a .exe data loader is invoked directly", async () => {
22+
const loader = Loader.find("test", "dataloaders/data4.txt")!;
23+
const out = await loader.load({verbose: false});
24+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "python3\n");
25+
});
26+
it("a .py data loader is called with python3", async () => {
27+
const loader = Loader.find("test", "dataloaders/data5.txt")!;
28+
const out = await loader.load({verbose: false});
29+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "python3\n");
30+
});
31+
it("a .R data loader is called with Rscript", async () => {
32+
const loader = Loader.find("test", "dataloaders/data6.txt")!;
33+
const out = await loader.load({verbose: false});
34+
assert.strictEqual(await readFile("test/" + out, "utf-8"), "Rscript\n");
35+
});
2136
});

test/dataloaders/data3.txt.sh

100755100644
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
#! /usr/bin/env bash
2-
31
echo "shell"

test/dataloaders/data4.txt.exe

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
3+
print("python3")

test/dataloaders/data5.txt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("python3")

test/dataloaders/data6.txt.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cat("Rscript\n")

0 commit comments

Comments
 (0)