diff --git a/examples/format-file.js b/examples/format-file.js new file mode 100644 index 0000000..afdb7db --- /dev/null +++ b/examples/format-file.js @@ -0,0 +1,10 @@ +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); +}); diff --git a/examples/run-format-file.js b/examples/run-format-file.js new file mode 100644 index 0000000..4e54e12 --- /dev/null +++ b/examples/run-format-file.js @@ -0,0 +1,9 @@ +import * as url from "node:url"; +import formatFile from './format-file.js' + +console.log( + formatFile( + url.fileURLToPath(new URL('../readme.md', import.meta.url)) + +) +) diff --git a/index.cjs b/index.cjs index 722ac6c..ec2fe2f 100644 --- a/index.cjs +++ b/index.cjs @@ -1,9 +1,9 @@ "use strict"; -const { makeSynchronizedModule } = require("make-synchronized"); +const { makeModuleSynchronized } = require("make-synchronized"); function createSynchronizedPrettier({ prettierEntry }) { - return makeSynchronizedModule(prettierEntry); + return makeModuleSynchronized(prettierEntry); } module.exports = createSynchronizedPrettier({ prettierEntry: "prettier" }); diff --git a/package.json b/package.json index 0e611eb..f2c1820 100644 --- a/package.json +++ b/package.json @@ -44,13 +44,13 @@ ] }, "peerDependencies": { - "prettier": "^3.0.0" + "prettier": "*" }, "devDependencies": { - "@types/node": "20.4.1", - "c8": "8.0.0", + "@types/node": "20.11.5", + "c8": "9.1.0", "npm-run-all": "4.1.5", - "prettier": "3.0.0" + "prettier": "3.2.4" }, "packageManager": "yarn@3.2.2", "publishConfig": { @@ -58,6 +58,6 @@ "registry": "https://registry.npmjs.org/" }, "dependencies": { - "make-synchronized": "^0.0.3" + "make-synchronized": "^0.2.1" } } diff --git a/readme.md b/readme.md index 7006d45..e93a596 100644 --- a/readme.md +++ b/readme.md @@ -36,24 +36,19 @@ This package is a simple wrapper of [`make-synchronized`](https://github.com/fis For more complex use cases, it more reasonable to extract into a separate file, and run with [`make-synchronized`](https://github.com/fisker/make-synchronized), example ```js -// runs-in-worker.js import * as fs from "node:fs/promises"; import * as prettier from "prettier"; +import makeSynchronized from "make-synchronized"; -export async function formatFile(file) { - const config = await prettier.resolveConfig(filepath); +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); + const formatted = await prettier.format(content, { + ...config, + filepath: file, + }); await fs.writeFile(file, formatted); -} -``` - -```js -import makeSynchronized from "make-synchronized"; - -const { - formatFile, // Synchronize version of `formatFile` in `runs-in-worker.js` -} = makeSynchronized(new URL("./runs-in-worker.js", import.meta.url)); +}); ``` ### `createSynchronizedPrettier(options)` diff --git a/tsconfig.json b/tsconfig.json index b7fee61..5d8a362 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "checkJs": true, "noEmit": true, "target": "esnext", - "module": "NodeNext" + "module": "NodeNext", }, - "files": ["index.cjs"] + "files": ["index.cjs"], }