@reteps/dockerfmt 0.2.1 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `@reteps/dockerfmt`
2
2
 
3
- Bindings around the Golang `dockerfmt` tooling.
3
+ Bindings around the Golang `dockerfmt` tooling. It uses [tinygo](https://github.com/tinygo-org/tinygo) to compile the Go code to WebAssembly, which is then used in the JS bindings.
4
4
 
5
5
 
6
6
  ```js
package/dist/format.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import './wasm_exec.cjs';
1
+ import './wasm_exec.js';
2
2
  interface FormatOptions {
3
3
  indent: number;
4
4
  trailingNewline: boolean;
5
5
  }
6
6
  declare const formatDockerfile: (fileName: string, options: FormatOptions) => Promise<string>;
7
- declare const formatDockerfileContents: (fileContents: string, options: FormatOptions) => Promise<string>;
8
- export { formatDockerfile, formatDockerfileContents };
7
+ declare const formatDockerfileContents: (fileContents: string, options: FormatOptions, getWasm?: () => Promise<Buffer>) => Promise<string>;
8
+ export { formatDockerfile, formatDockerfileContents, FormatOptions };
package/dist/format.js CHANGED
@@ -1,18 +1,31 @@
1
- import fs from 'node:fs/promises';
2
- import './wasm_exec.cjs';
3
- import path from 'node:path';
4
- import { fileURLToPath } from 'url';
1
+ import './wasm_exec.js';
2
+ const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
5
3
  const formatDockerfile = async (fileName, options) => {
4
+ if (!isNode) {
5
+ throw new Error('formatDockerfile is only supported in Node.js');
6
+ }
7
+ const fs = require('node:fs/promises');
8
+ // This would only work in Node.js, so we don't add a wasmDownload function
6
9
  const fileBuffer = await fs.readFile(fileName);
7
10
  const fileContents = fileBuffer.toString();
8
11
  return formatDockerfileContents(fileContents, options);
9
12
  };
10
- const formatDockerfileContents = async (fileContents, options) => {
13
+ const getWasmModule = () => {
14
+ if (isNode) {
15
+ const path = require('node:path');
16
+ const url = require('node:url');
17
+ const fs = require('node:fs/promises');
18
+ return fs.readFile(path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), 'format.wasm'));
19
+ }
20
+ // In the browser, we need to fetch the wasm module
21
+ throw new Error('WASM module not found. Please provide a function to fetch the WASM module.');
22
+ };
23
+ const formatDockerfileContents = async (fileContents, options, getWasm = getWasmModule) => {
11
24
  const go = new Go(); // Defined in wasm_exec.js
12
25
  const encoder = new TextEncoder();
13
26
  const decoder = new TextDecoder();
14
27
  // get current working directory
15
- const wasmBuffer = await fs.readFile(path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'format.wasm'));
28
+ const wasmBuffer = await getWasm();
16
29
  const wasm = await WebAssembly.instantiate(wasmBuffer, go.importObject);
17
30
  /**
18
31
  * Do not await this promise, because it only resolves once the go main()
package/dist/format.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@reteps/dockerfmt",
3
- "version": "0.2.1",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
7
7
  "//": "Requires tinygo 0.38.0 or later",
8
8
  "build-go": "tinygo build -o format.wasm -target wasm --no-debug",
9
- "build-js": "tsc && cp format.wasm wasm_exec.cjs dist",
9
+ "build-js": "tsc && cp format.wasm wasm_exec.js dist",
10
10
  "build": "npm run build-go && npm run build-js"
11
11
  },
12
12
  "files": [
File without changes