Skip to content

Commit 722c623

Browse files
committed
suppress logs in tests
1 parent 037586b commit 722c623

File tree

3 files changed

+60
-47
lines changed

3 files changed

+60
-47
lines changed

src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function build(context: CommandContext = makeCommandContext()) {
7171
continue;
7272
}
7373
if (verbose) process.stdout.write(`generate ${loader.path} → `);
74-
sourcePath = join(sourceRoot, await loader.load());
74+
sourcePath = join(sourceRoot, await loader.load({verbose}));
7575
if (verbose) console.log(sourcePath);
7676
}
7777
if (verbose) console.log("copy", sourcePath, "→", outputPath);

src/dataloader.ts

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {spawn} from "node:child_process";
2-
import {existsSync} from "node:fs";
2+
import {existsSync, statSync} from "node:fs";
33
import {mkdir, open, rename, unlink} from "node:fs/promises";
44
import {dirname, join} from "node:path";
55
import {maybeStat, prepareOutput} from "./files.js";
@@ -71,58 +71,71 @@ export class Loader {
7171
* to the source root; this is within the .observablehq/cache folder within
7272
* the source root.
7373
*/
74-
async load(): Promise<string> {
74+
async load({verbose = true}: {verbose?: boolean} = {}): Promise<string> {
7575
let command = runningCommands.get(this.path);
76-
if (command) return command;
77-
command = (async () => {
76+
if (!command) {
77+
command = (async () => {
78+
const outputPath = join(".observablehq", "cache", this.targetPath);
79+
const cachePath = join(this.sourceRoot, outputPath);
80+
const loaderStat = await maybeStat(this.path);
81+
const cacheStat = await maybeStat(cachePath);
82+
if (cacheStat && cacheStat.mtimeMs > loaderStat!.mtimeMs) return outputPath;
83+
const tempPath = join(this.sourceRoot, ".observablehq", "cache", `${this.targetPath}.${process.pid}`);
84+
await prepareOutput(tempPath);
85+
const tempFd = await open(tempPath, "w");
86+
const tempFileStream = tempFd.createWriteStream({highWaterMark: 1024 * 1024});
87+
const subprocess = spawn(this.command, this.args, {windowsHide: true, stdio: ["ignore", "pipe", "inherit"]});
88+
subprocess.stdout.pipe(tempFileStream);
89+
const code = await new Promise((resolve, reject) => {
90+
subprocess.on("error", reject);
91+
subprocess.on("close", resolve);
92+
});
93+
await tempFd.close();
94+
if (code === 0) {
95+
await mkdir(dirname(cachePath), {recursive: true});
96+
await rename(tempPath, cachePath);
97+
} else {
98+
await unlink(tempPath);
99+
throw new Error(`loader exited with code ${code}`);
100+
}
101+
return outputPath;
102+
})();
103+
command.finally(() => runningCommands.delete(this.path)).catch(() => {});
104+
runningCommands.set(this.path, command);
105+
}
106+
if (verbose) {
78107
console.info(`${this.path} start`);
79-
const time = performance.now();
80-
const outputPath = join(".observablehq", "cache", this.targetPath);
81-
const cachePath = join(this.sourceRoot, outputPath);
82-
const loaderStat = await maybeStat(this.path);
83-
const cacheStat = await maybeStat(cachePath);
84-
if (cacheStat && cacheStat.mtimeMs > loaderStat!.mtimeMs) return outputPath;
85-
const tempPath = join(this.sourceRoot, ".observablehq", "cache", `${this.targetPath}.${process.pid}`);
86-
await prepareOutput(tempPath);
87-
const tempFd = await open(tempPath, "w");
88-
const tempFileStream = tempFd.createWriteStream({highWaterMark: 1024 * 1024});
89-
const subprocess = spawn(this.command, this.args, {windowsHide: true, stdio: ["ignore", "pipe", "inherit"]});
90-
subprocess.stdout.pipe(tempFileStream);
91-
const code = await new Promise((resolve, reject) => {
92-
subprocess.on("error", reject);
93-
subprocess.on("close", resolve);
94-
});
95-
await tempFd.close();
96-
console.info(
97-
`${this.path} ${`${code === 0 ? success("success") : error("error")} ${outputBytes(
98-
(await maybeStat(tempPath))?.size
99-
)} in ${Math.floor(performance.now() - time)}ms`}`
108+
const start = performance.now();
109+
command.then(
110+
(path) => {
111+
console.info(
112+
`${this.path} ${green("success")} ${formatSize(statSync(path).size)} in ${formatElapsed(start)}`
113+
);
114+
},
115+
(error) => {
116+
console.info(`${this.path} ${red("error")} after ${formatElapsed(start)}: ${error.message}`);
117+
}
100118
);
101-
if (code === 0) {
102-
await mkdir(dirname(cachePath), {recursive: true});
103-
await rename(tempPath, cachePath);
104-
} else {
105-
await unlink(tempPath);
106-
throw new Error(`loader exited with code ${code}`);
107-
}
108-
return outputPath;
109-
})();
110-
command.finally(() => runningCommands.delete(this.path)).catch(() => {});
111-
runningCommands.set(this.path, command);
119+
}
112120
return command;
113121
}
114122
}
115123

116-
const error = color(31);
117-
const success = color(32);
118-
const warning = color(33);
124+
const red = color(31);
125+
const green = color(32);
126+
const yellow = color(33);
119127

120128
function color(code) {
121129
return process.stdout.isTTY ? (text) => `\x1b[${code}m${text}\x1b[0m` : String;
122130
}
123131

124-
function outputBytes(size) {
125-
if (!size) return warning("empty output");
132+
function formatSize(size) {
133+
if (!size) return yellow("empty output");
126134
const e = Math.floor(Math.log(size) / Math.log(1024));
127135
return `output ${+(size / 1024 ** e).toFixed(2)} ${["bytes", "KiB", "MiB", "GiB", "TiB"][e]}`;
128136
}
137+
138+
function formatElapsed(start) {
139+
const elapsed = performance.now() - start;
140+
return `${Math.floor(elapsed)}ms`;
141+
}

test/dataloaders-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import {Loader} from "../src/dataloader.js";
55
describe("data loaders are called with the appropriate command", () => {
66
it("a .js data loader is called with node", async () => {
77
const loader = Loader.find("test", "dataloaders/data1.txt")!;
8-
const out = await loader.load();
8+
const out = await loader.load({verbose: false});
99
assert.strictEqual(await readFile("test/" + out, "utf-8"), "node\n");
1010
});
1111
it("a .ts data loader is called with tsx", async () => {
12-
const loader = Loader.find("test", "dataloaders/data2.txt");
13-
const out = await loader!.load();
12+
const loader = Loader.find("test", "dataloaders/data2.txt")!;
13+
const out = await loader.load({verbose: false});
1414
assert.strictEqual(await readFile("test/" + out, "utf-8"), "tsx\n");
1515
});
1616
it("a .sh data loader is called as a shell script", async () => {
17-
const loader = Loader.find("test", "dataloaders/data3.txt");
18-
const out = await loader!.load();
17+
const loader = Loader.find("test", "dataloaders/data3.txt")!;
18+
const out = await loader.load({verbose: false});
1919
assert.strictEqual(await readFile("test/" + out, "utf-8"), "shell\n");
2020
});
2121
});

0 commit comments

Comments
 (0)