Skip to content

Commit

Permalink
feat(npm): update npm distribution to have getPath export instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 1, 2022
1 parent 1e14c8b commit 97cf284
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
/target
**/*.rs.bk
sourcemap
deployment/npm/buffer.generated.js
deployment/npm/plugin.wasm
deployment/npm/node_modules
6 changes: 4 additions & 2 deletions deployment/npm/README.md
Expand Up @@ -8,9 +8,11 @@ Use this with [@dprint/formatter](https://github.com/dprint/js-formatter) or jus

```ts
import { createFromBuffer } from "@dprint/formatter";
import { getBuffer } from "@dprint/typescript";
import { getPath } from "@dprint/typescript";
import * as fs from "fs";

const formatter = createFromBuffer(getBuffer());
const buffer = fs.readFileSync(getPath());
const formatter = createFromBuffer(buffer);

console.log(formatter.formatText("test.ts", "const t = 5;"));
```
4 changes: 2 additions & 2 deletions deployment/npm/index.d.ts
@@ -1,2 +1,2 @@
/** Gets a buffer representing the WASM module. */
export function getBuffer(): ArrayBuffer;
/** Gets an absolute path to the WASM module. */
export function getPath(): string;
33 changes: 5 additions & 28 deletions deployment/npm/index.js
@@ -1,34 +1,11 @@
/**
* Gets a buffer representing the WASM module.
* @returns {ArrayBuffer}
* Gets the path to the Wasm module.
* @returns {string}
*/
function getBuffer() {
const encodedBuffer = require("./buffer.generated").encodedBuffer;
return decodeEncodedBuffer(encodedBuffer);
}

/**
* @param {string} encodedBuffer
* @returns {ArrayBuffer}
*/
function decodeEncodedBuffer(encodedBuffer) {
// https://stackoverflow.com/a/51473757/188246
const binaryString = toBinaryString();
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;

function toBinaryString() {
if (typeof atob === "function") {
return atob(encodedBuffer);
} else {
return Buffer.from(encodedBuffer, "base64").toString("binary");
}
}
function getPath() {
return require("path").join(__dirname, "plugin.wasm");
}

module.exports = {
getBuffer,
getPath,
};
5 changes: 3 additions & 2 deletions deployment/npm/index.test.js
@@ -1,9 +1,10 @@
// @ts-check
const assert = require("assert");
const createFromBuffer = require("@dprint/formatter").createFromBuffer;
const getBuffer = require("./index").getBuffer;
const getPath = require("./index").getPath;

const formatter = createFromBuffer(getBuffer());
const buffer = require("fs").readFileSync(getPath());
const formatter = createFromBuffer(buffer);
const result = formatter.formatText("file.ts", "const t = 5");

assert.strictEqual(result, "const t = 5;\n");
18 changes: 17 additions & 1 deletion deployment/npm/package-lock.json

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

2 changes: 1 addition & 1 deletion deployment/npm/package.json
@@ -1,7 +1,7 @@
{
"name": "@dprint/typescript",
"version": "0.0.0",
"description": "Wasm buffer for dprint-plugin-typescript.",
"description": "Wasm module for dprint-plugin-typescript.",
"main": "./index.js",
"types": "./index.d.ts",
"repository": {
Expand Down
8 changes: 1 addition & 7 deletions deployment/npm/setup.js
Expand Up @@ -3,13 +3,7 @@ const fs = require("fs");
const path = require("path");
const args = process.argv.slice(2);
const wasmPath = path.join(__dirname, "../../target/wasm32-unknown-unknown/release/dprint_plugin_typescript.wasm");
const wasmBytes = fs.readFileSync(wasmPath);

let output = "module.exports.encodedBuffer = \"";
output += wasmBytes.toString("base64");
output += "\";\n";

fs.writeFileSync(path.join(__dirname, "buffer.generated.js"), output);
fs.copyFileSync(wasmPath, path.join(__dirname, "plugin.wasm"));

if (args.length > 0) {
// update the version based on the first argument
Expand Down

0 comments on commit 97cf284

Please sign in to comment.