Skip to content

Commit

Permalink
fix npm build
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 30, 2024
1 parent 423ab47 commit 0d5861c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
5 changes: 5 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals } from "@std/assert";
import * as fs from "node:fs";
import { createFromBuffer, createStreaming, type Formatter, type GlobalConfiguration } from "./mod.ts";

Deno.test("it should create streaming", async () => {
Expand Down Expand Up @@ -107,7 +108,7 @@ function runGeneralJsonFormatterTests(formatter: Formatter) {
Deno.test("should support v4", () => {
// this plugin file's code is here: https://github.com/dprint/dprint/blob/main/crates/test-plugin/src/lib.rs
const formatter = createFromBuffer(
Deno.readFileSync(new URL("./test/test_plugin_v4.wasm", import.meta.url)),
fs.readFileSync(new URL("./test/test_plugin_v4.wasm", import.meta.url)),
);

formatter.setConfig({}, { "ending": "formatted_wasm" });
Expand Down
19 changes: 18 additions & 1 deletion scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { build } from "@deno/dnt";
import { build, emptyDir } from "@deno/dnt";

const wasmFileLocations = [
"npm/script/test",
"npm/esm/test",
];

await emptyDir("npm");

await build({
entryPoints: ["mod.ts"],
Expand All @@ -20,6 +27,12 @@ await build({
compilerOptions: {
lib: ["ES2021", "DOM"],
},
postBuild: () => {
for (const location of wasmFileLocations) {
Deno.mkdirSync(location, { recursive: true });
Deno.copyFileSync("test/test_plugin_v4.wasm", location + "/test_plugin_v4.wasm");
}
},
package: {
name: "@dprint/formatter",
version: Deno.args[0],
Expand All @@ -44,3 +57,7 @@ await build({

Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");

for (const location of wasmFileLocations) {
Deno.removeSync(location + "/test_plugin_v4.wasm");
}
25 changes: 20 additions & 5 deletions v4.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FormatRequest, Formatter, GlobalConfiguration, Host, PluginInfo } from "./common.ts";
import { FileMatchingInfo } from "./mod.ts";
import type { FileMatchingInfo } from "./mod.ts";

const decoder = new TextDecoder();
const encoder = new TextEncoder();
Expand All @@ -8,6 +8,22 @@ const encoder = new TextEncoder();
* Creates host for host formatting.
*/
export function createHost(): Host {
function writeStderr(buf: Uint8Array) {
try {
// deno-lint-ignore no-explicit-any
const global = globalThis as any;
if (global.Deno) {
global.Deno.stderr.writeSync(buf);
} else if (global.process) {
global.process.stderr.writeSync(buf);
} else {
// ignore
}
} catch {
// ignore
}
}

let instance: WebAssembly.Instance;
let hostFormatter: ((request: FormatRequest) => string) | undefined = undefined;
let formattedText = "";
Expand Down Expand Up @@ -43,10 +59,9 @@ export function createHost(): Host {

const buf = new Uint8Array(wasmMemoryBuffer, iovecBufPtr, iovecBufLen);

if (fd === 1) {
Deno.stdout.writeSync(buf);
} else if (fd === 2) {
Deno.stderr.writeSync(buf);
if (fd === 1 || fd === 2) {
// just write both stdout and stderr to stderr
writeStderr(buf);
} else {
return 1; // not supported fd
}
Expand Down

0 comments on commit 0d5861c

Please sign in to comment.