@prettier/sync
Synchronous version of Prettier
Last updated 10 months ago by fisker .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @prettier/sync 
SYNC missed versions from official npm registry.

@prettier/sync

Build Status Coverage Npm Version MIT License

Synchronous version of Prettier

Installation

yarn add prettier @prettier/sync

Usage

import synchronizedPrettier from "@prettier/sync";

synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'

This package is a simple wrapper of make-synchronized.

For more complex use cases, it's better to use make-synchronized directly:

Example 1:

// utilities.js
import * as fs from "node:fs/promises";
import * as prettier from "prettier";

export async function formatFile(file) {
  const config = await prettier.resolveConfig(file);
  const content = await fs.readFile(file, "utf8");
  const formatted = await prettier.format(content, {
    ...config,
    filepath: file,
  });
  await fs.writeFile(file, formatted);
}

// index.js
import makeSynchronized from "make-synchronized";

const utilities = makeSynchronized(new URL("./utilities.js", import.meta.url));

utilities.formatFile("/path/to/file.js");

Example 2:

import * as fs from "node:fs/promises";
import * as prettier from "prettier";
import makeSynchronized from "make-synchronized";

export default makeSynchronized(import.meta, async function formatFile(file) {
  const config = await prettier.resolveConfig(file);
  const content = await fs.readFile(file, "utf8");
  const formatted = await prettier.format(content, {
    ...config,
    filepath: file,
  });
  await fs.writeFile(file, formatted);
});

Alternatively you can also use synckit or make-synchronous.

createSynchronizedPrettier(options)

options

Type: object

prettierEntry

Type: string | URL

Path or URL to prettier entry.

import { createSynchronizedPrettier } from "@prettier/sync";

const synchronizedPrettier = createSynchronizedPrettier({
  prettierEntry: "/path/to/prettier/index.js",
});

synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'

Current Tags

  • 0.6.1                                ...           latest (10 months ago)

14 Versions

  • 0.6.1                                ...           10 months ago
  • 0.6.0                                ...           10 months ago
  • 0.5.5                                ...           a year ago
  • 0.5.4                                ...           a year ago
  • 0.5.3                                ...           a year ago
  • 0.5.2                                ...           2 years ago
  • 0.5.1                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.4                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (1)
Dev Dependencies (5)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |