Skip to content

Commit

Permalink
fix: update dnt and make changes to get it working on node (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 8, 2022
1 parent d1433f8 commit 29ad3fe
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
- uses: actions/setup-node@v2
with:
node-version: '14.x'
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- name: npm build
run: deno run -A --no-check ./scripts/build_npm.ts ${{steps.get_tag_version.outputs.TAG_VERSION}}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm
npm
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2021 David Sherret
Copyright (c) 2020-2022 David Sherret

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ console.log(tsFormatter.formatText("file.ts", "const t = 5;"));

```ts
import { createFromBuffer } from "@dprint/formatter";
import { getBuffer } from "@dprint/json";

const formatter = createFromBuffer(fs.readFileSync("./json.wasm"));
// or provide something like fs.readFileSync("./json.wasm")
const formatter = createFromBuffer(getBuffer());

console.log(formatter.formatText("test.json", "{test: 5}"));
```
Expand Down
6 changes: 3 additions & 3 deletions bvm.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"binaries": [
{
"path": "https://bvm.land/deno/1.14.3.json",
"checksum": "270150c4a72748bcb630da76d4402a0b4c99702f034cba62724f05e792116a9e",
"version": "1.14.3"
"path": "https://bvm.land/deno/1.17.2.json",
"checksum": "ca0aff221bb79cf83ac67a1abd568ed1390211589db251e148b560c7b0ea3fbe",
"version": "1.17.2"
}
]
}
6 changes: 3 additions & 3 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.57.0.wasm",
"https://plugins.dprint.dev/json-0.13.0.wasm",
"https://plugins.dprint.dev/markdown-0.10.0.wasm"
"https://plugins.dprint.dev/typescript-0.61.0.wasm",
"https://plugins.dprint.dev/json-0.14.0.wasm",
"https://plugins.dprint.dev/markdown-0.12.0.wasm"
]
}
35 changes: 27 additions & 8 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright 2020-2021 by David Sherret. All rights reserved.
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

/** Formats code. */
export interface Formatter {
/**
Expand Down Expand Up @@ -89,17 +85,40 @@ export function createImportObject(): WebAssembly.Imports {
};
}

export interface ResponseLike {
arrayBuffer(): Promise<BufferSource>;
}

/**
* Creates a formatter from the specified streaming source.
* @remarks This is the most efficient way to create a formatter.
* @param response - The streaming source to create the formatter from.
*/
export function createStreaming(
response: Promise<Response>,
response: Promise<ResponseLike>,
): Promise<Formatter> {
return WebAssembly
.instantiateStreaming(response,createImportObject())
.then((obj) => createFromInstance(obj.instance));
if (typeof WebAssembly.instantiateStreaming === "function") {
return WebAssembly
// deno-lint-ignore no-explicit-any
.instantiateStreaming(response as any, createImportObject())
.then((obj) => createFromInstance(obj.instance));
} else {
// fallback for node.js
return getArrayBuffer()
.then((buffer) => createFromBuffer(buffer));
}

function getArrayBuffer() {
if (isResponse(response)) {
return response.arrayBuffer();
} else {
return response.then((response) => response.arrayBuffer());
}

function isResponse(response: unknown): response is ResponseLike {
return (response as Response).arrayBuffer != null;
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.113.0/testing/asserts.ts";
import { createFromBuffer, createStreaming, Formatter, GlobalConfiguration } from "./mod.ts";

Deno.test("it should create streaming", async () => {
Expand Down
74 changes: 43 additions & 31 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
import { build } from "https://deno.land/x/[email protected]/mod.ts";

await build({
entryPoints: ["mod.ts"],
typeCheck: true,
test: false,
outDir: "./npm",
package: {
name: "@dprint/formatter",
version: Deno.args[0],
description: "Wasm formatter for dprint plugins.",
repository: {
type: "git",
url: "git+https://github.com/dprint/js-formatter.git",
},
keywords: [
"dprint",
"formatter",
"wasm",
],
author: "David Sherret",
license: "MIT",
bugs: {
url: "https://github.com/dprint/js-formatter/issues",
},
homepage: "https://github.com/dprint/js-formatter#readme",
},
});

Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");
import { build } from "https://deno.land/x/[email protected]/mod.ts";

await build({
entryPoints: ["mod.ts"],
typeCheck: true,
test: true,
outDir: "./npm",
shims: {
deno: {
test: "dev",
},
customDev: [{
globalNames: ["fetch"],
package: {
name: "undici",
version: "^4.12.1",
},
}],
},
package: {
name: "@dprint/formatter",
version: Deno.args[0],
description: "Wasm formatter for dprint plugins.",
repository: {
type: "git",
url: "git+https://github.com/dprint/js-formatter.git",
},
keywords: [
"dprint",
"formatter",
"wasm",
],
author: "David Sherret",
license: "MIT",
bugs: {
url: "https://github.com/dprint/js-formatter/issues",
},
homepage: "https://github.com/dprint/js-formatter#readme",
},
});

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

0 comments on commit 29ad3fe

Please sign in to comment.