|
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'; |
5 | 2 |
|
6 | | -export interface FormatOptions { |
| 3 | +interface FormatOptions { |
7 | 4 | indent: number; |
8 | 5 | trailingNewline: boolean; |
9 | 6 | } |
10 | 7 |
|
| 8 | +const isNode: boolean = typeof process !== "undefined" && process.versions != null && process.versions.node != null; |
| 9 | + |
11 | 10 | const formatDockerfile = async (fileName: string, options: FormatOptions) => { |
| 11 | + if (!isNode) { |
| 12 | + throw new Error('formatDockerfile is only supported in Node.js'); |
| 13 | + } |
| 14 | + const fs = require('node:fs/promises'); |
| 15 | + |
12 | 16 | // This would only work in Node.js, so we don't add a wasmDownload function |
13 | 17 | const fileBuffer = await fs.readFile(fileName); |
14 | 18 | const fileContents = fileBuffer.toString(); |
15 | 19 | return formatDockerfileContents(fileContents, options); |
16 | 20 | } |
17 | 21 |
|
18 | | -const getWasmModule = () => fs.readFile(path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'format.wasm')) |
| 22 | +const getWasmModule = () => { |
| 23 | + if (isNode) { |
| 24 | + const path = require('node:path'); |
| 25 | + const url = require('node:url'); |
| 26 | + const fs = require('node:fs/promises'); |
| 27 | + return fs.readFile(path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), 'format.wasm')); |
| 28 | + } |
| 29 | + // In the browser, we need to fetch the wasm module |
| 30 | + throw new Error('WASM module not found. Please provide a function to fetch the WASM module.'); |
| 31 | +} |
19 | 32 |
|
20 | 33 | const formatDockerfileContents = async (fileContents: string, options: FormatOptions, getWasm: () => Promise<Buffer> = getWasmModule) => { |
21 | 34 | const go = new Go() // Defined in wasm_exec.js |
@@ -64,4 +77,4 @@ const formatDockerfileContents = async (fileContents: string, options: FormatOpt |
64 | 77 | return result |
65 | 78 | } |
66 | 79 |
|
67 | | -export { formatDockerfile, formatDockerfileContents } |
| 80 | +export { formatDockerfile, formatDockerfileContents, FormatOptions } |
0 commit comments