Skip to content

Commit

Permalink
feat: wasm v4 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jun 30, 2024
1 parent f274692 commit 627d2d7
Show file tree
Hide file tree
Showing 12 changed files with 1,101 additions and 392 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ on: [push, pull_request]
jobs:
CI:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Test
run: deno test --allow-net
run: deno test --allow-net --allow-read=.
- name: Publish on tag
run: deno run -A jsr:@david/publish-on-tag@0.1.4
- name: Get tag version
if: startsWith(github.ref, 'refs/tags/')
id: get_tag_version
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.vscode
npm
deno.lock
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

[![CI](https://github.com/dprint/js-formatter/workflows/CI/badge.svg)](https://github.com/dprint/js-formatter/actions?query=workflow%3ACI)
[![npm version](https://badge.fury.io/js/%40dprint%2Fformatter.svg)](https://badge.fury.io/js/%40dprint%2Fformatter)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/dprint/mod.ts)
[![JSR](https://jsr.io/badges/@dprint/formatter)](https://jsr.io/@dprint/formatter)

JS formatter for dprint Wasm plugins.

## Deno
## Setup

Deno:

```sh
deno add @dprint/formatter
```

Node.js:

```sh
npm i @dprint/formatter
```

## Use

```ts
import {
createStreaming,
GlobalConfiguration,
} from "https://deno.land/x/dprint/mod.ts";
import { createStreaming, GlobalConfiguration } from "@dprint/formatter";

const globalConfig: GlobalConfiguration = {
indentWidth: 2,
Expand All @@ -31,29 +42,29 @@ tsFormatter.setConfig(globalConfig, {
console.log(tsFormatter.formatText("file.ts", "const t = 5;"));
```

## Node.js

Use the following:
Using with plugins on npm (ex. [@dprint/json](https://www.npmjs.com/package/@dprint/json)):

```ts
import { createFromBuffer } from "@dprint/formatter";
// You may have to use `getBuffer` on plugins that haven't updated yet.
// See the plugins README.md for details.
import { getPath } from "@dprint/json";
import * as fs from "fs";
import * as fs from "node:fs";

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

console.log(formatter.formatText("test.json", "{test: 5}"));
```

Unfortunately Node.js doesn't have any way to cache compiles at the moment and so it will have a slower than ideal startup time.

### Plugin NPM Packages

Note: In the future I will ensure plugins are published to JSR as well.

- [@dprint/json](https://www.npmjs.com/package/@dprint/json)
- [@dprint/typescript](https://www.npmjs.com/package/@dprint/typescript)
- [@dprint/markdown](https://www.npmjs.com/package/@dprint/markdown)
- [@dprint/toml](https://www.npmjs.com/package/@dprint/toml)
- [@dprint/dockerfile](https://www.npmjs.com/package/@dprint/dockerfile)
- [@dprint/biome](https://www.npmjs.com/package/@dprint/biome)
- [@dprint/ruff](https://www.npmjs.com/package/@dprint/ruff)
88 changes: 88 additions & 0 deletions common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export interface FormatRequest {
/** The file path to format. */
filePath: string;
/** File text to format. */
fileText: string;
/** Byte range to format. Note this is BYTE range and NOT character range. */
bytesRange?: readonly [number, number];
/** Configuration to set for a single format. */
overrideConfig?: Record<string, unknown>;
}

export interface Host {
setInstance(wasmInstance: WebAssembly.Instance): void;
setHostFormatter(formatWithHost: ((request: FormatRequest) => string) | undefined): void;
createImportObject(): WebAssembly.Imports;
}

/** Formats code. */
export interface Formatter {
/**
* Sets the configuration.
* @param globalConfig - Global configuration for use across plugins.
* @param pluginConfig - Plugin specific configuration.
*/
setConfig(
globalConfig: GlobalConfiguration,
pluginConfig: Record<string, unknown>,
): void;
/**
* Gets the configuration diagnostics.
*/
getConfigDiagnostics(): ConfigurationDiagnostic[];
/**
* Gets the resolved configuration.
* @returns An object containing the resolved configuration.
*/
getResolvedConfig(): Record<string, unknown>;
/**
* Gets the plugin info.
*/
getPluginInfo(): PluginInfo;
/**
* Gets what files the plugin matches based on the current configuration.
*/
getFileMatchingInfo(): FileMatchingInfo;
/**
* Gets the license text of the plugin.
*/
getLicenseText(): string;
/**
* Formats the specified file text.
* @param request - Data to format.
* @param formatWithHost - Host formatter.
* @returns The formatted text.
* @throws If there is an error formatting.
*/
formatText(request: FormatRequest, formatWithHost?: (request: FormatRequest) => string): string;
}

/** Configuration specified for use across plugins. */
export interface GlobalConfiguration {
lineWidth?: number;
indentWidth?: number;
useTabs?: boolean;
newLineKind?: "auto" | "lf" | "crlf" | "system";
}

/** A diagnostic indicating a problem with the specified configuration. */
export interface ConfigurationDiagnostic {
propertyName: string;
message: string;
}

/** Information about a plugin. */
export interface PluginInfo {
name: string;
version: string;
configKey: string;
helpUrl: string;
configSchemaUrl: string;
updateUrl?: string;
}

/** Information about how the current config matches files. */
export interface FileMatchingInfo {
fileExtensions: string[] | undefined;
fileNames: string[];
}
14 changes: 14 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
{
"name": "@dprint/formatter",
"version": "0.0.0",
"tasks": {
"build:npm": "deno run -A ./scripts/build_npm.ts"
},
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.2",
"@std/assert": "jsr:@std/assert@^0.226.0"
},
"publish": {
"exclude": [
".github",
"scripts",
"test"
]
},
"exports": "./mod.ts",
"exclude": [
"npm"
]
Expand Down
Loading

0 comments on commit 627d2d7

Please sign in to comment.